public byte[] CreateWatermark(byte[] byteArray, string[] content) { var _skImage = SKBitmap.Decode(byteArray); var w = _skImage.Width; var h = _skImage.Height; using (SKCanvas canvas = new SKCanvas(_skImage)) { using (SKPaint textPaint = new SKPaint { TextSize = 48 }) { SKFontMetrics fontMetrics; textPaint.GetFontMetrics(out fontMetrics); var lineHeight = fontMetrics.Bottom - fontMetrics.Top; textPaint.Style = SKPaintStyle.Stroke; textPaint.Color = new SKColor(128, 0, 0); textPaint.StrokeWidth = 2; h -= 10; w -= 10; for (var x = content.Length - 1; x > -1; x--) { var txt = content[x]; SKRect bounds = new SKRect(); textPaint.MeasureText(txt, ref bounds); canvas.DrawText(txt, (w - bounds.Width), h, textPaint); h -= ((int)lineHeight + 5); } } } return(_skImage.Bytes); }
public StaticLayout(string source, SKPaint paint, int width, SKTextAlign alignment) { this.source = source; this.paint = paint; this.width = width; this.alignment = alignment; paint.MeasureText(source, ref this.textRect); paint.GetFontMetrics(out fontMetrics); }
/// <summary>Calculates the needed font size for the X labels</summary> /// <returns></returns> internal LabelMetrics CalculateXLabels(float labelHeight, float startFontSize = 100.0F) { float minScale = 1000.0F; float maxHeight = 0.0F; float fontSize = startFontSize; float desc = 0.0F; // scale only based on height using (SKPaint textPaint = new SKPaint() { TextSize = fontSize, IsAntialias = true, IsStroke = false }) { textPaint.Typeface = SKTypeface.FromFamilyName(this.LabelFontFamily); SKFontMetrics metrics; textPaint.GetFontMetrics(out metrics); // we only care about the height this time which we get from Descent - Ascent float scaley = labelHeight / (metrics.Descent - metrics.Ascent); if (scaley < minScale) { minScale = scaley; } fontSize = textPaint.TextSize * minScale * (this.XLabelFontScale / 100.0F); } // use the new font size using (SKPaint textPaint = new SKPaint() { TextSize = fontSize, IsAntialias = true, IsStroke = false }) { textPaint.Typeface = SKTypeface.FromFamilyName(this.LabelFontFamily); SKFontMetrics metrics; textPaint.GetFontMetrics(out metrics); if (metrics.Descent > desc) { desc = metrics.Descent; } // sets the distance from Ascent to Descent if (metrics.Descent - metrics.Ascent > maxHeight) { maxHeight = metrics.Descent - metrics.Ascent; } } return(new LabelMetrics(fontSize, maxHeight, desc)); }
public static void ProcessImg(object obj) { var id = int.Parse(obj.ToString()); var str = dic[id]; var skFontPaint = new SKPaint { TextSize = 100 - 10, Color = SKColors.White, TextAlign = SKTextAlign.Center }; foreach (var font in Fonts) { skFontPaint.Typeface = SKTypeface.FromFile("sarasa-gothic-sc-regular.ttf"); var count = 0; SKFontMetrics sKFontMetrics; skFontPaint.GetFontMetrics(out sKFontMetrics); var skSurface = SKSurface.Create(new SKImageInfo(100, 100)); var skCanvas = skSurface.Canvas; skCanvas.DrawText(str, 50, 100 - 10 - (int)sKFontMetrics.UnderlinePosition, skFontPaint); var image = skSurface.Snapshot(); skCanvas.Dispose(); skSurface.Dispose(); for (float degree = -90; degree <= 90; degree++) { var img = ApplyRotate(image, degree); Images.Add(new ImageFile { Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = img }); count++; var imge = ApplyErode(img, 3); Images.Add(new ImageFile { Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = imge }); count++; Images.Add(new ImageFile { Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = ApplyErode(img, 3) }); count++; Images.Add(new ImageFile { Filename = id.ToString() + "-" + str + "-" + count.ToString() + ".png", Data = ApplyErode(imge, 3) }); count++; } } ThCount.Take(); }
private SKPaint getTextBlock(double fontSize) { if (!this.textBlocks.ContainsKey(fontSize)) { SKPaint textBlock = new SKPaint(); Label label = new Label(); textBlock.TextSize = (float)fontSize; textBlock.Typeface = SKTypeface.FromFamilyName(label.FontFamily); // leave enough height for the characters for the highest ("M") and lowest ("g") characters SKFontMetrics metrics = new SKFontMetrics(); textBlock.GetFontMetrics(out metrics); this.fontLineHeight = metrics.Descent - metrics.Ascent; SKRect bounds = new SKRect(); textBlock.MeasureText("M", ref bounds); this.leftMargin = bounds.Left; this.textBlocks[fontSize] = textBlock; } return(this.textBlocks[fontSize]); }
/// <summary>Determines the maximum font size that will fit in the width of the rect</summary> /// <param name="brush"></param> /// <param name="rect"></param> /// <param name="text"></param> /// <returns></returns> public static LabelMetrics CalcSizeForWidth(SKPaint brush, float rectWidth, string text) { // this is about the largest font size to fit '888' in a iPhone 11 max pro float fontSize = 500.0F; // set the brush to the max font size brush.TextSize = fontSize; // measure the text SKRect textBounds = new SKRect(); brush.MeasureText(text, ref textBounds); // scale the font size fontSize = brush.TextSize * (rectWidth / textBounds.Width); SKFontMetrics metrics; brush.GetFontMetrics(out metrics); return(new LabelMetrics(fontSize, metrics.Descent - metrics.Ascent, metrics.Descent)); }
public byte[] CreateWatermark(byte[] byteArray, string[] content, SKColor color, int fontSize) { var _skImage = SKBitmap.Decode(byteArray); var w = _skImage.Width; var h = _skImage.Height; using (SKCanvas canvas = new SKCanvas(_skImage)) { using (SKPaint textPaint = new SKPaint { TextSize = fontSize }) { SKFontMetrics fontMetrics; textPaint.GetFontMetrics(out fontMetrics); var lineHeight = fontMetrics.Bottom - fontMetrics.Top; textPaint.Style = SKPaintStyle.Stroke; textPaint.Color = color; textPaint.StrokeWidth = 2; h -= 10; w -= 10; for (var x = content.Length - 1; x > -1; x--) { var txt = content[x]; SKRect bounds = new SKRect(); textPaint.MeasureText(txt, ref bounds); canvas.DrawText(txt, (w - bounds.Width), h, textPaint); h -= ((int)lineHeight + 5); } } } var bitmap = _skImage.ToBitmap(); var stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); return(stream.ToArray()); }
/// <summary>Calculates the needed font size for the Y labels</summary> /// <returns></returns> internal LabelMetrics CalculateYLabels(float labelWidth, float startFontSize = 100.0F) { float minScale = 1000.0F; float maxHeight = 0.0F; float fontSize = startFontSize; float desc = 0.0F; // no data if (this.YLabels == null || this.YLabels.Count < 0) { return(new LabelMetrics(fontSize, maxHeight, desc)); } // scale only based on width using (SKPaint textPaint = new SKPaint() { TextSize = fontSize, IsAntialias = true, IsStroke = false }) { textPaint.Typeface = SKTypeface.FromFamilyName(this.LabelFontFamily); foreach (LineLabel entry in this.YLabels) { if (String.IsNullOrWhiteSpace(entry.Text)) { continue; } SKRect textBounds = new SKRect(); textPaint.MeasureText(entry.Text, ref textBounds); float scale = labelWidth / textBounds.Width; if (scale < minScale) { minScale = scale; } } // adjust the font fontSize = textPaint.TextSize * minScale * (this.YLabelFontScale / 100.0F); } // use the new font size using (SKPaint textPaint = new SKPaint() { TextSize = fontSize, IsAntialias = true, IsStroke = false }) { textPaint.Typeface = SKTypeface.FromFamilyName(this.LabelFontFamily); SKFontMetrics metrics; textPaint.GetFontMetrics(out metrics); if (metrics.Descent > desc) { desc = metrics.Descent; } // sets the distance from Ascent to Descent if (metrics.Descent - metrics.Ascent > maxHeight) { maxHeight = metrics.Descent - metrics.Ascent; } } return(new LabelMetrics(fontSize, maxHeight, desc)); }
/// <summary>Calculates the needed font size for the X labels</summary> /// <returns></returns> /// <remarks>Right now only calculates the prime label text box</remarks> internal LabelMetrics CalculateLabels(string label, FontAttributes attr, float fontScale = 100.0F) { // if the label is empty if (String.IsNullOrEmpty(label)) { return(LabelMetrics.Default()); } if (fontScale < 1) { fontScale = 1.0F; } float maxHeight = 0.0F; float fontSize = 250.0F; float desc = 0.0F; // set the max label width to some default // use the inscribed square width //this.MaxPrimeLabelWidth = this.OuterRect.Width - (this.ArcLineWidth * BorderFactor * 2.0F); this.MaxPrimeLabelWidth = this.InscribedRect.Width; // determine the ideal font using (SKPaint textPaint = new SKPaint()) { // init the font with all its properties this.SetFont(textPaint, fontSize, attr); // find the min font size for the width LabelMetrics widthMet = QuickCalc.CalcSizeForWidth(textPaint, this.MaxPrimeLabelWidth, label); textPaint.TextSize = widthMet.FontSize; // get the NEW font metrics which includes max height SKFontMetrics metrics; textPaint.GetFontMetrics(out metrics); // set the calculated values thus far desc = metrics.Descent; maxHeight = metrics.Descent - metrics.Ascent; // for fun calculate the width of the circle at the top of the text //float chord = Convert.ToSingle( QuickCalc.CalcChord( this.OuterRect.Width / 2.0F, maxHeight / 2.0F ) ); //this.MaxPrimeLabelWidth = chord - ( this.ArcLineWidth * BorderFactor * 2.0F ); // now check its not too tall if (maxHeight > this.InscribedRect.Height) { // scale the font further based on height float scale = this.InscribedRect.Height / maxHeight; textPaint.TextSize = textPaint.TextSize * scale; } // now scale by the relative user set scale if (fontScale < 100.0F) { textPaint.TextSize = textPaint.TextSize * (fontScale / 100.0F); } // remeasure after user set scaling textPaint.GetFontMetrics(out metrics); fontSize = textPaint.TextSize; desc = metrics.Descent; maxHeight = metrics.Descent - metrics.Ascent; } return(new LabelMetrics(fontSize, maxHeight, desc)); }
/// <summary>Minimum font that will fit in the label width area</summary> /// <returns></returns> internal LabelMetrics CalculateXLabels(float labelWidth, float startFontSize = 100.0F) { float minScale = 1000.0F; float maxHeight = 0.0F; float fontSize = startFontSize; float desc = 0.0F; using (SKPaint textPaint = new SKPaint() { TextSize = fontSize, IsAntialias = true, IsStroke = false }) { textPaint.Typeface = SKTypeface.FromFamilyName(this.XLabelFontFamily); foreach (GraphEntry entry in this.Entries) { if (entry == null || String.IsNullOrWhiteSpace(entry.Label)) { continue; } SKRect textBounds = new SKRect(); textPaint.MeasureText(entry.Label, ref textBounds); float scale = labelWidth / textBounds.Width; if (scale < minScale) { minScale = scale; } } // reduce the font size using optional scaling fontSize = textPaint.TextSize * minScale * (this.XLabelFontScale / 100.0F); textPaint.TextSize = fontSize; // calculate the font metrics for each label to figure out the max height needed foreach (GraphEntry entry in this.Entries) { if (entry == null || String.IsNullOrWhiteSpace(entry.Label)) { continue; } SKFontMetrics metrics; textPaint.GetFontMetrics(out metrics); if (metrics.Descent > desc) { desc = metrics.Descent; } // sets the distance from Ascent to Descent if (metrics.Descent - metrics.Ascent > maxHeight) { maxHeight = metrics.Descent - metrics.Ascent; } } } return(new LabelMetrics(fontSize, maxHeight, desc)); }
public TextStyle(SKPaint paint) { this.Paint = paint; Shaper = new MyShaper(paint.Typeface); paint.GetFontMetrics(out FontMetrics); }