Skip to content

Commit

Permalink
Use SKFont instead of deprecated SKPaint.Font
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Dec 3, 2024
1 parent c8f8e7b commit 939b4a8
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ public sealed class SkGraphicEngine : IGraphicEngine<SKBitmap>
private readonly SKCanvas canvas;
private readonly SKColor defaultColor;
private readonly SKPaint textPaint;
private readonly SKTypeface? typeface;
private readonly SKFont font;
private readonly WordCloudInput wordCloud;
private bool bitmapExtracted;

private SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
SKPaint textPaint)
SKPaint textPaint, SKTypeface? typeface = null)
{
Sizer = sizer;
this.wordCloud = wordCloud;
defaultColor = textPaint.Color;
this.textPaint = textPaint;
this.typeface = typeface;
font = typeface is null ? new SKFont() : new SKFont(typeface);
Bitmap = new SKBitmap(wordCloud.Width, wordCloud.Height);
canvas = new SKCanvas(Bitmap);
}
Expand All @@ -36,9 +40,10 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
textPaint = new SKPaint
{
Color = defaultColor,
Typeface = font,
IsAntialias = antialias
};
typeface = font;
this.font = font is null ? new SKFont() : new SKFont(font);
this.wordCloud = wordCloud;
}

Expand All @@ -48,9 +53,8 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,

public RectangleD Measure(string text, int count)
{
textPaint.TextSize = (float)Sizer.GetFontSize(count);
SKRect rect = new SKRect();
textPaint.MeasureText(text, ref rect);
font.Size = (float)Sizer.GetFontSize(count);
font.MeasureText(text, out SKRect rect);
var m = wordCloud.ItemMargin;
return new RectangleD(rect.Left + m, rect.Top + m, rect.Width + 2 * m, rect.Height + 2 * m);
}
Expand All @@ -59,7 +63,7 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s
{
// For computation explanation, see
// https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/basics/text.
textPaint.TextSize = (float)Sizer.GetFontSize(count);
font.Size = (float)Sizer.GetFontSize(count);
if (colorHex != null)
{
textPaint.Color = SKColor.Parse(colorHex);
Expand All @@ -69,14 +73,14 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s
textPaint.Color = defaultColor;
}
canvas.DrawText(text, (float)(location.X - measured.Left),
(float)(location.Y - measured.Top), textPaint);
(float)(location.Y - measured.Top), font, textPaint);
}

public IGraphicEngine<SKBitmap> Clone()
{
var clonedTextPaint = textPaint.Clone();
clonedTextPaint.Color = defaultColor;
return new SkGraphicEngine(Sizer, wordCloud, clonedTextPaint);
return new SkGraphicEngine(Sizer, wordCloud, clonedTextPaint, typeface);
}

public SKBitmap ExtractBitmap()
Expand All @@ -87,6 +91,7 @@ public SKBitmap ExtractBitmap()

public void Dispose()
{
font.Dispose();
textPaint.Dispose();
canvas.Dispose();
if (!bitmapExtracted)
Expand Down

0 comments on commit 939b4a8

Please sign in to comment.