OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 | |
9 #include "SkAdvancedTypefaceMetrics.h" | |
10 #include "SkTypes.h" | |
11 | |
12 SkAdvancedTypefaceMetrics::~SkAdvancedTypefaceMetrics() {} | |
13 | |
14 const int16_t kInvalidAdvance = SK_MinS16; | |
15 const int16_t kDontCareAdvance = SK_MinS16 + 1; | |
16 | |
17 static void stripUninterestingTrailingAdvancesFromRange( | |
18 SkAdvancedTypefaceMetrics::WidthRange* range) { | |
19 SkASSERT(range); | |
20 | |
21 int expectedAdvanceCount = range->fEndId - range->fStartId + 1; | |
22 if (range->fAdvance.count() < expectedAdvanceCount) { | |
23 return; | |
24 } | |
25 | |
26 for (int i = expectedAdvanceCount - 1; i >= 0; --i) { | |
27 if (range->fAdvance[i] != kDontCareAdvance && | |
28 range->fAdvance[i] != kInvalidAdvance && | |
29 range->fAdvance[i] != 0) { | |
30 range->fEndId = range->fStartId + i; | |
31 break; | |
32 } | |
33 } | |
34 } | |
35 | |
36 static void zeroWildcardsInRange(SkAdvancedTypefaceMetrics::WidthRange* range) { | |
37 SkASSERT(range); | |
38 if (range->fType != SkAdvancedTypefaceMetrics::WidthRange::kRange) { | |
39 return; | |
40 } | |
41 SkASSERT(range->fAdvance.count() == range->fEndId - range->fStartId + 1); | |
42 | |
43 // Zero out wildcards. | |
44 for (int i = 0; i < range->fAdvance.count(); ++i) { | |
45 if (range->fAdvance[i] == kDontCareAdvance) { | |
46 range->fAdvance[i] = 0; | |
47 } | |
48 } | |
49 } | |
50 | |
51 void SkAdvancedTypefaceMetrics::FinishRange( | |
52 SkAdvancedTypefaceMetrics::WidthRange* range, | |
53 int endId, | |
54 SkAdvancedTypefaceMetrics::WidthRange::MetricType type) { | |
55 range->fEndId = endId; | |
56 range->fType = type; | |
57 stripUninterestingTrailingAdvancesFromRange(range); | |
58 int newLength; | |
59 if (type == SkAdvancedTypefaceMetrics::WidthRange::kRange) { | |
60 newLength = range->fEndId - range->fStartId + 1; | |
61 } else { | |
62 if (range->fEndId == range->fStartId) { | |
63 range->fType = SkAdvancedTypefaceMetrics::WidthRange::kRange; | |
64 } | |
65 newLength = 1; | |
66 } | |
67 SkASSERT(range->fAdvance.count() >= newLength); | |
68 range->fAdvance.setCount(newLength); | |
69 zeroWildcardsInRange(range); | |
70 } | |
71 | |
72 void SkAdvancedTypefaceMetrics::setGlyphWidths( | |
73 int num_glyphs, | |
74 const uint32_t* subsetGlyphIDs, | |
75 uint32_t subsetGlyphIDsLength, | |
76 SkAdvancedTypefaceMetrics::GetAdvance getAdvance) { | |
77 // Assuming that on average, the ASCII representation of an advance plus | |
78 // a space is 8 characters and the ASCII representation of a glyph id is 3 | |
79 // characters, then the following cut offs for using different range types | |
80 // apply: | |
81 // The cost of stopping and starting the range is 7 characers | |
82 // a. Removing 4 0's or don't care's is a win | |
83 // The cost of stopping and starting the range plus a run is 22 | |
84 // characters | |
85 // b. Removing 3 repeating advances is a win | |
86 // c. Removing 2 repeating advances and 3 don't cares is a win | |
87 // When not currently in a range the cost of a run over a range is 16 | |
88 // characaters, so: | |
89 // d. Removing a leading 0/don't cares is a win because it is omitted | |
90 // e. Removing 2 repeating advances is a win | |
91 | |
92 WidthRange* prevRange = nullptr; | |
93 int16_t lastAdvance = kInvalidAdvance; | |
94 int repeatedAdvances = 0; | |
95 int wildCardsInRun = 0; | |
96 int trailingWildCards = 0; | |
97 uint32_t subsetIndex = 0; | |
98 | |
99 // Limit the loop count to glyph id ranges provided. | |
100 int firstIndex = 0; | |
101 int lastIndex = num_glyphs; | |
102 if (subsetGlyphIDs) { | |
103 firstIndex = static_cast<int>(subsetGlyphIDs[0]); | |
104 lastIndex = | |
105 static_cast<int>(subsetGlyphIDs[subsetGlyphIDsLength - 1]) + 1; | |
106 } | |
107 WidthRange curRange(firstIndex); | |
108 | |
109 for (int gId = firstIndex; gId <= lastIndex; gId++) { | |
110 int16_t advance = kInvalidAdvance; | |
111 if (gId < lastIndex) { | |
112 // Get glyph id only when subset is nullptr, or the id is in subset. | |
113 SkASSERT(!subsetGlyphIDs || (subsetIndex < subsetGlyphIDsLength && | |
114 static_cast<uint32_t>(gId) <= subsetGlyphIDs[subsetIndex])); | |
115 if (!subsetGlyphIDs || | |
116 (subsetIndex < subsetGlyphIDsLength && | |
117 static_cast<uint32_t>(gId) == subsetGlyphIDs[subsetIndex])) { | |
118 SkAssertResult(getAdvance(gId, &advance)); | |
119 ++subsetIndex; | |
120 } else { | |
121 advance = kDontCareAdvance; | |
122 } | |
123 } | |
124 if (advance == lastAdvance) { | |
125 repeatedAdvances++; | |
126 trailingWildCards = 0; | |
127 } else if (advance == kDontCareAdvance) { | |
128 wildCardsInRun++; | |
129 trailingWildCards++; | |
130 } else if (curRange.fAdvance.count() == | |
131 repeatedAdvances + 1 + wildCardsInRun) { // All in run. | |
132 if (lastAdvance == 0) { | |
133 curRange.fStartId = gId; // reset | |
134 curRange.fAdvance.setCount(0); | |
135 trailingWildCards = 0; | |
136 } else if (repeatedAdvances + 1 >= 2 || trailingWildCards >= 4) { | |
137 FinishRange(&curRange, gId - 1, WidthRange::kRun); | |
138 prevRange = fGlyphWidths.emplace_back(std::move(curRange)); | |
139 curRange = WidthRange(gId); | |
140 trailingWildCards = 0; | |
141 } | |
142 repeatedAdvances = 0; | |
143 wildCardsInRun = trailingWildCards; | |
144 trailingWildCards = 0; | |
145 } else { | |
146 if (lastAdvance == 0 && | |
147 repeatedAdvances + 1 + wildCardsInRun >= 4) { | |
148 FinishRange(&curRange, | |
149 gId - repeatedAdvances - wildCardsInRun - 2, | |
150 WidthRange::kRange); | |
151 prevRange = fGlyphWidths.emplace_back(std::move(curRange)); | |
152 curRange = WidthRange(gId); | |
153 trailingWildCards = 0; | |
154 } else if (trailingWildCards >= 4 && repeatedAdvances + 1 < 2) { | |
155 FinishRange(&curRange, gId - trailingWildCards - 1, | |
156 WidthRange::kRange); | |
157 prevRange = fGlyphWidths.emplace_back(std::move(curRange)); | |
158 curRange = WidthRange(gId); | |
159 trailingWildCards = 0; | |
160 } else if (lastAdvance != 0 && | |
161 (repeatedAdvances + 1 >= 3 || | |
162 (repeatedAdvances + 1 >= 2 && wildCardsInRun >= 3))) { | |
163 FinishRange(&curRange, | |
164 gId - repeatedAdvances - wildCardsInRun - 2, | |
165 WidthRange::kRange); | |
166 (void)fGlyphWidths.emplace_back(std::move(curRange)); | |
167 curRange = | |
168 WidthRange(gId - repeatedAdvances - wildCardsInRun - 1); | |
169 curRange.fAdvance.append(1, &lastAdvance); | |
170 FinishRange(&curRange, gId - 1, WidthRange::kRun); | |
171 prevRange = fGlyphWidths.emplace_back(std::move(curRange)); | |
172 curRange = WidthRange(gId); | |
173 trailingWildCards = 0; | |
174 } | |
175 repeatedAdvances = 0; | |
176 wildCardsInRun = trailingWildCards; | |
177 trailingWildCards = 0; | |
178 } | |
179 curRange.fAdvance.append(1, &advance); | |
180 if (advance != kDontCareAdvance) { | |
181 lastAdvance = advance; | |
182 } | |
183 } | |
184 if (curRange.fStartId == lastIndex) { | |
185 if (!prevRange) { | |
186 fGlyphWidths.reset(); | |
187 return; // https://crbug.com/567031 | |
188 } | |
189 } else { | |
190 FinishRange(&curRange, lastIndex - 1, WidthRange::kRange); | |
191 fGlyphWidths.emplace_back(std::move(curRange)); | |
192 } | |
193 } | |
OLD | NEW |