-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCanvasExtensions.cs
More file actions
162 lines (129 loc) · 4.44 KB
/
CanvasExtensions.cs
File metadata and controls
162 lines (129 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using SkiaSharp;
using SkiaSharp.HarfBuzz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace SkiaTextRendering.Extensions;
public static class CanvasExtensions
{
public static void DrawShapedText(this SKCanvas canvas, string text, float x, float y, SKFont font, SKPaint paint) =>
DrawShapedText(canvas, text, x, y, SKTextAlign.Left, font, paint);
public static void DrawShapedText(this SKCanvas canvas, string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
{
if (string.IsNullOrEmpty(text))
return;
var shaper = GetShaper(font.Typeface);
DrawShapedText(canvas, shaper, text, x, y, textAlign, font, paint);
}
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKFont font, SKPaint paint) =>
DrawShapedText(canvas, shaper, text, x, y, SKTextAlign.Left, font, paint);
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
{
if (string.IsNullOrEmpty(text))
return;
if (canvas == null)
throw new ArgumentNullException(nameof(canvas));
if (shaper == null)
throw new ArgumentNullException(nameof(shaper));
if (paint == null)
throw new ArgumentNullException(nameof(paint));
font.Typeface = shaper.Typeface;
// shape the text
var result = GetShapeResult(shaper, text, font);
// create the text blob
using var builder = new SKTextBlobBuilder();
var run = builder.AllocateRawPositionedRun(font, result.Codepoints.Length, null);
// copy the glyphs
var g = run.Glyphs;
var p = run.Positions;
for (var i = 0; i < result.Codepoints.Length; i++)
{
g[i] = (ushort)result.Codepoints[i];
p[i] = result.Points[i];
}
// build
using var textBlob = builder.Build();
// adjust alignment
var xOffset = 0f;
if (textAlign != SKTextAlign.Left)
{
var width = result.Width;
if (textAlign == SKTextAlign.Center)
width *= 0.5f;
xOffset -= width;
}
// draw the text
canvas.DrawText(textBlob, x + xOffset, y, paint);
if (clearCacheTimer is null && cacheDuration > 0)
clearCacheTimer = new Timer(_ => ClearCache(), null, 0, cacheDuration);
}
private static uint cacheDuration = 0;
public static void SetShaperCacheDuration(this SKCanvas? canvas, uint milliseconds) => SetShaperCacheDuration(milliseconds);
public static void SetShaperCacheDuration(uint milliseconds)
{
cacheDuration = milliseconds;
clearCacheTimer?.Change(0, cacheDuration);
}
private static readonly Dictionary<int, (SKShaper shaper, DateTime cachedAt)> shaperCache = new();
private static readonly Dictionary<int, (SKShaper.Result shapeResult, DateTime cachedAt)> shapeResultCache = new();
private static SKShaper GetShaper(SKTypeface typeface)
{
if (cacheDuration == 0)
return new SKShaper(typeface);
var key = HashCode.Combine(typeface.FamilyName, typeface.IsBold, typeface.IsItalic);
SKShaper shaper;
lock (shaperCache)
{
shaper = shaperCache.TryGetValue(key, out var value)
? value.shaper
: new SKShaper(typeface);
shaperCache[key] = (shaper, DateTime.Now); // update timestamp
}
return shaper;
}
private static SKShaper.Result GetShapeResult(SKShaper shaper, string text, SKFont font)
{
if (cacheDuration == 0)
return shaper.Shape(text, 0, 0, font);
var key = HashCode.Combine(font.Typeface.FamilyName, font.Size, font.Typeface.IsBold, font.Typeface.IsItalic, text);
SKShaper.Result result;
lock (shapeResultCache)
{
result = shapeResultCache.TryGetValue(key, out var value)
? value.shapeResult
: shaper.Shape(text, 0, 0, font);
shapeResultCache[key] = (result, DateTime.Now); // update timestamp
}
return result;
}
private static Timer? clearCacheTimer = null;
private static void ClearCache()
{
var outdated = DateTime.Now - TimeSpan.FromMilliseconds(cacheDuration);
lock (shaperCache)
{
foreach (var kv in shaperCache.AsEnumerable())
{
if (kv.Value.cachedAt < outdated)
{
if (shaperCache.Remove(kv.Key, out var entry))
entry.shaper.Dispose();
}
}
}
lock (shapeResultCache)
{
foreach (var kv in shapeResultCache.AsEnumerable())
{
if (kv.Value.cachedAt < outdated)
shapeResultCache.Remove(kv.Key, out var _);
}
}
if ((shaperCache.Count == 0 && shapeResultCache.Count == 0) || cacheDuration == 0)
{
clearCacheTimer?.Dispose();
clearCacheTimer = null;
}
}
}