@@ -12,16 +12,20 @@ public sealed class SkGraphicEngine : IGraphicEngine<SKBitmap>
12
12
private readonly SKCanvas canvas ;
13
13
private readonly SKColor defaultColor ;
14
14
private readonly SKPaint textPaint ;
15
+ private readonly SKTypeface ? typeface ;
16
+ private readonly SKFont font ;
15
17
private readonly WordCloudInput wordCloud ;
16
18
private bool bitmapExtracted ;
17
19
18
20
private SkGraphicEngine ( ISizer sizer , WordCloudInput wordCloud ,
19
- SKPaint textPaint )
21
+ SKPaint textPaint , SKTypeface ? typeface = null )
20
22
{
21
23
Sizer = sizer ;
22
24
this . wordCloud = wordCloud ;
23
25
defaultColor = textPaint . Color ;
24
26
this . textPaint = textPaint ;
27
+ this . typeface = typeface ;
28
+ font = typeface is null ? new SKFont ( ) : new SKFont ( typeface ) ;
25
29
Bitmap = new SKBitmap ( wordCloud . Width , wordCloud . Height ) ;
26
30
canvas = new SKCanvas ( Bitmap ) ;
27
31
}
@@ -36,9 +40,10 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
36
40
textPaint = new SKPaint
37
41
{
38
42
Color = defaultColor ,
39
- Typeface = font ,
40
43
IsAntialias = antialias
41
44
} ;
45
+ typeface = font ;
46
+ this . font = font is null ? new SKFont ( ) : new SKFont ( font ) ;
42
47
this . wordCloud = wordCloud ;
43
48
}
44
49
@@ -48,9 +53,8 @@ public SkGraphicEngine(ISizer sizer, WordCloudInput wordCloud,
48
53
49
54
public RectangleD Measure ( string text , int count )
50
55
{
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 ) ;
54
58
var m = wordCloud . ItemMargin ;
55
59
return new RectangleD ( rect . Left + m , rect . Top + m , rect . Width + 2 * m , rect . Height + 2 * m ) ;
56
60
}
@@ -59,7 +63,7 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s
59
63
{
60
64
// For computation explanation, see
61
65
// 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 ) ;
63
67
if ( colorHex != null )
64
68
{
65
69
textPaint . Color = SKColor . Parse ( colorHex ) ;
@@ -69,14 +73,14 @@ public void Draw(PointD location, RectangleD measured, string text, int count, s
69
73
textPaint . Color = defaultColor ;
70
74
}
71
75
canvas . DrawText ( text , ( float ) ( location . X - measured . Left ) ,
72
- ( float ) ( location . Y - measured . Top ) , textPaint ) ;
76
+ ( float ) ( location . Y - measured . Top ) , font , textPaint ) ;
73
77
}
74
78
75
79
public IGraphicEngine < SKBitmap > Clone ( )
76
80
{
77
81
var clonedTextPaint = textPaint . Clone ( ) ;
78
82
clonedTextPaint . Color = defaultColor ;
79
- return new SkGraphicEngine ( Sizer , wordCloud , clonedTextPaint ) ;
83
+ return new SkGraphicEngine ( Sizer , wordCloud , clonedTextPaint , typeface ) ;
80
84
}
81
85
82
86
public SKBitmap ExtractBitmap ( )
@@ -87,6 +91,7 @@ public SKBitmap ExtractBitmap()
87
91
88
92
public void Dispose ( )
89
93
{
94
+ font . Dispose ( ) ;
90
95
textPaint . Dispose ( ) ;
91
96
canvas . Dispose ( ) ;
92
97
if ( ! bitmapExtracted )
0 commit comments