-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebFontSequenceCoverage.cs
More file actions
154 lines (134 loc) · 4.65 KB
/
Copy pathWebFontSequenceCoverage.cs
File metadata and controls
154 lines (134 loc) · 4.65 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
using System.Globalization;
using System.Text;
namespace OdfKit.WebFonts.OpenType;
internal static class WebFontSequenceCoverage
{
internal static IReadOnlyList<WebFontTextSequence> Filter(
IReadOnlyList<WebFontTextSequence> sequences,
Func<int, bool> containsScalar,
Func<UnicodeVariationSequence, bool> containsVariation,
CancellationToken cancellationToken)
{
var supported = new List<WebFontTextSequence>();
var run = new StringBuilder();
foreach (WebFontTextSequence sequence in sequences)
{
foreach (string element in EnumerateClusters(sequence.Text))
{
cancellationToken.ThrowIfCancellationRequested();
WebFontTextSequence cluster = WebFontTextSequence.Create(element);
if (IsSupportedCluster(cluster, containsScalar, containsVariation))
{
run.Append(element);
}
else
{
Flush(run, supported);
}
}
Flush(run, supported);
}
return supported;
}
internal static bool RequiresGlyph(int scalar)
=> WebFontUnicodePolicy.RequiresStandaloneGlyph(scalar);
private static bool IsSupportedCluster(
WebFontTextSequence cluster,
Func<int, bool> containsScalar,
Func<UnicodeVariationSequence, bool> containsVariation)
{
bool hasGlyph = false;
for (int index = 0; index < cluster.UnicodeScalars.Count; index++)
{
int scalar = cluster.UnicodeScalars[index];
if (IsVariationSelector(scalar))
{
if (index == 0
|| !containsVariation(new UnicodeVariationSequence(
cluster.UnicodeScalars[index - 1],
scalar)))
{
return false;
}
continue;
}
bool requiresGlyph = RequiresGlyph(scalar);
if (requiresGlyph && !containsScalar(scalar))
{
return false;
}
hasGlyph |= requiresGlyph;
}
return hasGlyph;
}
private static bool IsVariationSelector(int scalar)
=> scalar is >= 0xFE00 and <= 0xFE0F or >= 0xE0100 and <= 0xE01EF;
private static IEnumerable<string> EnumerateClusters(string text)
{
TextElementEnumerator elements = StringInfo.GetTextElementEnumerator(text);
string? current = null;
while (elements.MoveNext())
{
string next = elements.GetTextElement();
if (current is null)
{
current = next;
continue;
}
WebFontTextSequence currentSequence = WebFontTextSequence.Create(current);
WebFontTextSequence nextSequence = WebFontTextSequence.Create(next);
if (ShouldCoalesce(currentSequence, nextSequence, next))
{
current = string.Concat(current, next);
continue;
}
yield return current;
current = next;
}
if (current is not null)
{
yield return current;
}
}
private static bool ShouldCoalesce(
WebFontTextSequence current,
WebFontTextSequence next,
string nextText)
{
int first = next.UnicodeScalars[0];
int last = current.UnicodeScalars[current.UnicodeScalars.Count - 1];
UnicodeCategory category = CharUnicodeInfo.GetUnicodeCategory(nextText, 0);
if (IsVariationSelector(first)
|| first == 0x200D
|| last == 0x200D
|| first is >= 0x1F3FB and <= 0x1F3FF
|| first is >= 0xE0020 and <= 0xE007F
|| category is UnicodeCategory.NonSpacingMark
or UnicodeCategory.SpacingCombiningMark
or UnicodeCategory.EnclosingMark)
{
return true;
}
if (first is not (>= 0x1F1E6 and <= 0x1F1FF))
{
return false;
}
int trailingRegionalCount = 0;
for (int index = current.UnicodeScalars.Count - 1;
index >= 0 && current.UnicodeScalars[index] is >= 0x1F1E6 and <= 0x1F1FF;
index--)
{
trailingRegionalCount++;
}
return trailingRegionalCount % 2 == 1;
}
private static void Flush(StringBuilder run, ICollection<WebFontTextSequence> supported)
{
if (run.Length == 0)
{
return;
}
supported.Add(WebFontTextSequence.Create(run.ToString()));
run.Clear();
}
}