@@ -12,16 +12,20 @@ public sealed class SkGraphicEngine : IGraphicEngine<SKBitmap>
1212 private readonly SKCanvas canvas ;
1313 private readonly SKColor defaultColor ;
1414 private readonly SKPaint textPaint ;
15+ private readonly SKTypeface ? typeface ;
16+ private readonly SKFont font ;
1517 private readonly WordCloudInput wordCloud ;
1618 private bool bitmapExtracted ;
1719
1820 private SkGraphicEngine ( ISizer sizer , WordCloudInput wordCloud ,
19- SKPaint textPaint )
21+ SKPaint textPaint , SKTypeface ? typeface = null )
2022 {
2123 Sizer = sizer ;
2224 this . wordCloud = wordCloud ;
2325 defaultColor = textPaint . Color ;
2426 this . textPaint = textPaint ;
27+ this . typeface = typeface ;
28+ font = typeface is null ? new SKFont ( ) : new SKFont ( typeface ) ;
2529 Bitmap = new SKBitmap ( wordCloud . Width , wordCloud . Height ) ;
2630 canvas = new SKCanvas ( Bitmap ) ;
2731 }
@@ -36,9 +40,10 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
3640 textPaint = new SKPaint
3741 {
3842 Color = defaultColor ,
39- Typeface = font ,
4043 IsAntialias = antialias
4144 } ;
45+ typeface = font ;
46+ this . font = font is null ? new SKFont ( ) : new SKFont ( font ) ;
4247 this . wordCloud = wordCloud ;
4348 }
4449
@@ -48,9 +53,8 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
4853
4954 public RectangleD Measure ( string text , int count )
5055 {
51- textPaint . TextSize = ( float ) Sizer . GetFontSize ( count ) ;
52- SKRect rect = new SKRect ( ) ;
53- textPaint . MeasureText ( text , ref rect ) ;
56+ font . Size = ( float ) Sizer . GetFontSize ( count ) ;
57+ font . MeasureText ( text , out SKRect rect ) ;
5458 var m = wordCloud . ItemMargin ;
5559 return new RectangleD ( rect . Left + m , rect . Top + m , rect . Width + 2 * m , rect . Height + 2 * m ) ;
5660 }
@@ -59,7 +63,7 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s
5963 {
6064 // For computation explanation, see
6165 // https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/basics/text.
62- textPaint . TextSize = ( float ) Sizer . GetFontSize ( count ) ;
66+ font . Size = ( float ) Sizer . GetFontSize ( count ) ;
6367 if ( colorHex != null )
6468 {
6569 textPaint . Color = SKColor . Parse ( colorHex ) ;
@@ -69,14 +73,14 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s
6973 textPaint . Color = defaultColor ;
7074 }
7175 canvas . DrawText ( text , ( float ) ( location . X - measured . Left ) ,
72- ( float ) ( location . Y - measured . Top ) , textPaint ) ;
76+ ( float ) ( location . Y - measured . Top ) , font , textPaint ) ;
7377 }
7478
7579 public IGraphicEngine < SKBitmap > Clone ( )
7680 {
7781 var clonedTextPaint = textPaint . Clone ( ) ;
7882 clonedTextPaint . Color = defaultColor ;
79- return new SkGraphicEngine ( Sizer , wordCloud , clonedTextPaint ) ;
83+ return new SkGraphicEngine ( Sizer , wordCloud , clonedTextPaint , typeface ) ;
8084 }
8185
8286 public SKBitmap ExtractBitmap ( )
@@ -87,6 +91,7 @@ public SKBitmap ExtractBitmap()
8791
8892 public void Dispose ( )
8993 {
94+ font . Dispose ( ) ;
9095 textPaint . Dispose ( ) ;
9196 canvas . Dispose ( ) ;
9297 if ( ! bitmapExtracted )
0 commit comments