1 /*
2 * Copyright 2006 The Android Open Source Project
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 #include "SkStrike.h"
9
10 #include "SkGraphics.h"
11 #include "SkMakeUnique.h"
12 #include "SkMutex.h"
13 #include "SkOnce.h"
14 #include "SkPath.h"
15 #include "SkTemplates.h"
16 #include "SkTypeface.h"
17 #include <cctype>
18
19 namespace {
compute_path_size(const SkPath & path)20 size_t compute_path_size(const SkPath& path) {
21 return sizeof(SkPath) + path.countPoints() * sizeof(SkPoint);
22 }
23 } // namespace
24
SkStrike(const SkDescriptor & desc,std::unique_ptr<SkScalerContext> scaler,const SkFontMetrics & fontMetrics)25 SkStrike::SkStrike(
26 const SkDescriptor& desc,
27 std::unique_ptr<SkScalerContext> scaler,
28 const SkFontMetrics& fontMetrics)
29 : fDesc{desc}
30 , fScalerContext{std::move(scaler)}
31 , fFontMetrics{fontMetrics}
32 , fIsSubpixel{fScalerContext->isSubpixel()}
33 , fAxisAlignment{fScalerContext->computeAxisAlignmentForHText()}
34 {
35 SkASSERT(fScalerContext != nullptr);
36 fMemoryUsed = sizeof(*this);
37 }
38
getDescriptor() const39 const SkDescriptor& SkStrike::getDescriptor() const {
40 return *fDesc.getDesc();
41 }
42
43 #ifdef SK_DEBUG
44 #define VALIDATE() AutoValidate av(this)
45 #else
46 #define VALIDATE()
47 #endif
48
getGlyphCount() const49 unsigned SkStrike::getGlyphCount() const {
50 return fScalerContext->getGlyphCount();
51 }
52
countCachedGlyphs() const53 int SkStrike::countCachedGlyphs() const {
54 return fGlyphMap.count();
55 }
56
isGlyphCached(SkGlyphID glyphID,SkFixed x,SkFixed y) const57 bool SkStrike::isGlyphCached(SkGlyphID glyphID, SkFixed x, SkFixed y) const {
58 SkPackedGlyphID packedGlyphID{glyphID, x, y};
59 return fGlyphMap.find(packedGlyphID) != nullptr;
60 }
61
getRawGlyphByID(SkPackedGlyphID id)62 SkGlyph* SkStrike::getRawGlyphByID(SkPackedGlyphID id) {
63 return lookupByPackedGlyphID(id, kNothing_MetricsType);
64 }
65
getGlyphIDAdvance(uint16_t glyphID)66 const SkGlyph& SkStrike::getGlyphIDAdvance(uint16_t glyphID) {
67 VALIDATE();
68 SkPackedGlyphID packedGlyphID(glyphID);
69 return *this->lookupByPackedGlyphID(packedGlyphID, kJustAdvance_MetricsType);
70 }
71
getGlyphIDMetrics(uint16_t glyphID)72 const SkGlyph& SkStrike::getGlyphIDMetrics(uint16_t glyphID) {
73 VALIDATE();
74 SkPackedGlyphID packedGlyphID(glyphID);
75 return *this->lookupByPackedGlyphID(packedGlyphID, kFull_MetricsType);
76 }
77
getGlyphIDMetrics(uint16_t glyphID,SkFixed x,SkFixed y)78 const SkGlyph& SkStrike::getGlyphIDMetrics(uint16_t glyphID, SkFixed x, SkFixed y) {
79 VALIDATE();
80 SkPackedGlyphID packedGlyphID(glyphID, x, y);
81 return *this->lookupByPackedGlyphID(packedGlyphID, kFull_MetricsType);
82 }
83
getAdvances(SkSpan<const SkGlyphID> glyphIDs,SkPoint advances[])84 void SkStrike::getAdvances(SkSpan<const SkGlyphID> glyphIDs, SkPoint advances[]) {
85 for (auto glyphID : glyphIDs) {
86 auto glyph = this->getGlyphIDAdvance(glyphID);
87 *advances++ = SkPoint::Make(glyph.fAdvanceX, glyph.fAdvanceY);
88 }
89 }
90
lookupByPackedGlyphID(SkPackedGlyphID packedGlyphID,MetricsType type)91 SkGlyph* SkStrike::lookupByPackedGlyphID(SkPackedGlyphID packedGlyphID, MetricsType type) {
92 SkGlyph* glyphPtr = fGlyphMap.findOrNull(packedGlyphID);
93
94 if (nullptr == glyphPtr) {
95 glyphPtr = this->allocateNewGlyph(packedGlyphID, type);
96 fGlyphMap.set(glyphPtr);
97 } else {
98 if (type == kFull_MetricsType && glyphPtr->isJustAdvance()) {
99 fScalerContext->getMetrics(glyphPtr);
100 }
101 }
102 return glyphPtr;
103 }
104
allocateNewGlyph(SkPackedGlyphID packedGlyphID,MetricsType mtype)105 SkGlyph* SkStrike::allocateNewGlyph(SkPackedGlyphID packedGlyphID, MetricsType mtype) {
106 fMemoryUsed += sizeof(SkGlyph);
107
108 SkGlyph* glyphPtr = fAlloc.make<SkGlyph>(packedGlyphID);
109 fGlyphMap.set(glyphPtr);
110
111 if (kNothing_MetricsType == mtype) {
112 return glyphPtr;
113 } else if (kJustAdvance_MetricsType == mtype) {
114 fScalerContext->getAdvance(glyphPtr);
115 } else {
116 SkASSERT(kFull_MetricsType == mtype);
117 fScalerContext->getMetrics(glyphPtr);
118 }
119
120 SkASSERT(glyphPtr->fID != SkPackedGlyphID());
121 return glyphPtr;
122 }
123
findImage(const SkGlyph & glyph)124 const void* SkStrike::findImage(const SkGlyph& glyph) {
125 if (glyph.fWidth > 0 && glyph.fWidth < kMaxGlyphWidth) {
126 if (nullptr == glyph.fImage) {
127 SkDEBUGCODE(SkMask::Format oldFormat = (SkMask::Format)glyph.fMaskFormat);
128 size_t size = const_cast<SkGlyph&>(glyph).allocImage(&fAlloc);
129 // check that alloc() actually succeeded
130 if (glyph.fImage) {
131 fScalerContext->getImage(glyph);
132 // TODO: the scaler may have changed the maskformat during
133 // getImage (e.g. from AA or LCD to BW) which means we may have
134 // overallocated the buffer. Check if the new computedImageSize
135 // is smaller, and if so, strink the alloc size in fImageAlloc.
136 fMemoryUsed += size;
137 }
138 SkASSERT(oldFormat == glyph.fMaskFormat);
139 }
140 }
141 return glyph.fImage;
142 }
143
initializeImage(const volatile void * data,size_t size,SkGlyph * glyph)144 void SkStrike::initializeImage(const volatile void* data, size_t size, SkGlyph* glyph) {
145 // Don't overwrite the image if we already have one. We could have used a fallback if the
146 // glyph was missing earlier.
147 if (glyph->fImage) return;
148
149 if (glyph->fWidth > 0 && glyph->fWidth < kMaxGlyphWidth) {
150 size_t allocSize = glyph->allocImage(&fAlloc);
151 // check that alloc() actually succeeded
152 if (glyph->fImage) {
153 SkASSERT(size == allocSize);
154 memcpy(glyph->fImage, const_cast<const void*>(data), allocSize);
155 fMemoryUsed += size;
156 }
157 }
158 }
159
findPath(const SkGlyph & glyph)160 const SkPath* SkStrike::findPath(const SkGlyph& glyph) {
161
162 if (!glyph.isEmpty()) {
163 // If the path already exists, return it.
164 if (glyph.fPathData != nullptr) {
165 if (glyph.fPathData->fHasPath) {
166 return &glyph.fPathData->fPath;
167 }
168 return nullptr;
169 }
170
171 const_cast<SkGlyph&>(glyph).addPath(fScalerContext.get(), &fAlloc);
172 if (glyph.fPathData != nullptr) {
173 fMemoryUsed += compute_path_size(glyph.fPathData->fPath);
174 }
175
176 return glyph.path();
177 }
178
179 return nullptr;
180 }
181
initializePath(SkGlyph * glyph,const volatile void * data,size_t size)182 bool SkStrike::initializePath(SkGlyph* glyph, const volatile void* data, size_t size) {
183 // Don't overwrite the path if we already have one. We could have used a fallback if the
184 // glyph was missing earlier.
185 if (glyph->fPathData) return true;
186
187 if (glyph->fWidth) {
188 SkGlyph::PathData* pathData = fAlloc.make<SkGlyph::PathData>();
189 glyph->fPathData = pathData;
190 auto path = skstd::make_unique<SkPath>();
191 if (!pathData->fPath.readFromMemory(const_cast<const void*>(data), size)) {
192 return false;
193 }
194 fMemoryUsed += compute_path_size(glyph->fPathData->fPath);
195 pathData->fHasPath = true;
196 }
197
198 return true;
199 }
200
belongsToCache(const SkGlyph * glyph) const201 bool SkStrike::belongsToCache(const SkGlyph* glyph) const {
202 return glyph && fGlyphMap.findOrNull(glyph->getPackedID()) == glyph;
203 }
204
getCachedGlyphAnySubPix(SkGlyphID glyphID,SkPackedGlyphID vetoID) const205 const SkGlyph* SkStrike::getCachedGlyphAnySubPix(SkGlyphID glyphID,
206 SkPackedGlyphID vetoID) const {
207 for (SkFixed subY = 0; subY < SK_Fixed1; subY += SK_FixedQuarter) {
208 for (SkFixed subX = 0; subX < SK_Fixed1; subX += SK_FixedQuarter) {
209 SkPackedGlyphID packedGlyphID{glyphID, subX, subY};
210 if (packedGlyphID == vetoID) continue;
211 if (SkGlyph* glyphPtr = fGlyphMap.findOrNull(packedGlyphID)) {
212 return glyphPtr;
213 }
214 }
215 }
216
217 return nullptr;
218 }
219
initializeGlyphFromFallback(SkGlyph * glyph,const SkGlyph & fallback)220 void SkStrike::initializeGlyphFromFallback(SkGlyph* glyph, const SkGlyph& fallback) {
221 fMemoryUsed += glyph->copyImageData(fallback, &fAlloc);
222 }
223
rounding() const224 SkVector SkStrike::rounding() const {
225 return SkStrikeCommon::PixelRounding(fIsSubpixel, fAxisAlignment);
226 }
227
getGlyphMetrics(SkGlyphID glyphID,SkPoint position)228 const SkGlyph& SkStrike::getGlyphMetrics(SkGlyphID glyphID, SkPoint position) {
229 if (!fIsSubpixel) {
230 return this->getGlyphIDMetrics(glyphID);
231 } else {
232 SkIPoint lookupPosition = SkStrikeCommon::SubpixelLookup(fAxisAlignment, position);
233
234 return this->getGlyphIDMetrics(glyphID, lookupPosition.x(), lookupPosition.y());
235 }
236 }
237
238 #include "../pathops/SkPathOpsCubic.h"
239 #include "../pathops/SkPathOpsQuad.h"
240
quad_in_bounds(const SkScalar * pts,const SkScalar bounds[2])241 static bool quad_in_bounds(const SkScalar* pts, const SkScalar bounds[2]) {
242 SkScalar min = SkTMin(SkTMin(pts[0], pts[2]), pts[4]);
243 if (bounds[1] < min) {
244 return false;
245 }
246 SkScalar max = SkTMax(SkTMax(pts[0], pts[2]), pts[4]);
247 return bounds[0] < max;
248 }
249
cubic_in_bounds(const SkScalar * pts,const SkScalar bounds[2])250 static bool cubic_in_bounds(const SkScalar* pts, const SkScalar bounds[2]) {
251 SkScalar min = SkTMin(SkTMin(SkTMin(pts[0], pts[2]), pts[4]), pts[6]);
252 if (bounds[1] < min) {
253 return false;
254 }
255 SkScalar max = SkTMax(SkTMax(SkTMax(pts[0], pts[2]), pts[4]), pts[6]);
256 return bounds[0] < max;
257 }
258
OffsetResults(const SkGlyph::Intercept * intercept,SkScalar scale,SkScalar xPos,SkScalar * array,int * count)259 void SkStrike::OffsetResults(const SkGlyph::Intercept* intercept, SkScalar scale,
260 SkScalar xPos, SkScalar* array, int* count) {
261 if (array) {
262 array += *count;
263 for (int index = 0; index < 2; index++) {
264 *array++ = intercept->fInterval[index] * scale + xPos;
265 }
266 }
267 *count += 2;
268 }
269
AddInterval(SkScalar val,SkGlyph::Intercept * intercept)270 void SkStrike::AddInterval(SkScalar val, SkGlyph::Intercept* intercept) {
271 intercept->fInterval[0] = SkTMin(intercept->fInterval[0], val);
272 intercept->fInterval[1] = SkTMax(intercept->fInterval[1], val);
273 }
274
AddPoints(const SkPoint * pts,int ptCount,const SkScalar bounds[2],bool yAxis,SkGlyph::Intercept * intercept)275 void SkStrike::AddPoints(const SkPoint* pts, int ptCount, const SkScalar bounds[2],
276 bool yAxis, SkGlyph::Intercept* intercept) {
277 for (int i = 0; i < ptCount; ++i) {
278 SkScalar val = *(&pts[i].fY - yAxis);
279 if (bounds[0] < val && val < bounds[1]) {
280 AddInterval(*(&pts[i].fX + yAxis), intercept);
281 }
282 }
283 }
284
AddLine(const SkPoint pts[2],SkScalar axis,bool yAxis,SkGlyph::Intercept * intercept)285 void SkStrike::AddLine(const SkPoint pts[2], SkScalar axis, bool yAxis,
286 SkGlyph::Intercept* intercept) {
287 SkScalar t = yAxis ? sk_ieee_float_divide(axis - pts[0].fX, pts[1].fX - pts[0].fX)
288 : sk_ieee_float_divide(axis - pts[0].fY, pts[1].fY - pts[0].fY);
289 if (0 <= t && t < 1) { // this handles divide by zero above
290 AddInterval(yAxis ? pts[0].fY + t * (pts[1].fY - pts[0].fY)
291 : pts[0].fX + t * (pts[1].fX - pts[0].fX), intercept);
292 }
293 }
294
AddQuad(const SkPoint pts[3],SkScalar axis,bool yAxis,SkGlyph::Intercept * intercept)295 void SkStrike::AddQuad(const SkPoint pts[3], SkScalar axis, bool yAxis,
296 SkGlyph::Intercept* intercept) {
297 SkDQuad quad;
298 quad.set(pts);
299 double roots[2];
300 int count = yAxis ? quad.verticalIntersect(axis, roots)
301 : quad.horizontalIntersect(axis, roots);
302 while (--count >= 0) {
303 SkPoint pt = quad.ptAtT(roots[count]).asSkPoint();
304 AddInterval(*(&pt.fX + yAxis), intercept);
305 }
306 }
307
AddCubic(const SkPoint pts[4],SkScalar axis,bool yAxis,SkGlyph::Intercept * intercept)308 void SkStrike::AddCubic(const SkPoint pts[4], SkScalar axis, bool yAxis,
309 SkGlyph::Intercept* intercept) {
310 SkDCubic cubic;
311 cubic.set(pts);
312 double roots[3];
313 int count = yAxis ? cubic.verticalIntersect(axis, roots)
314 : cubic.horizontalIntersect(axis, roots);
315 while (--count >= 0) {
316 SkPoint pt = cubic.ptAtT(roots[count]).asSkPoint();
317 AddInterval(*(&pt.fX + yAxis), intercept);
318 }
319 }
320
MatchBounds(const SkGlyph * glyph,const SkScalar bounds[2])321 const SkGlyph::Intercept* SkStrike::MatchBounds(const SkGlyph* glyph,
322 const SkScalar bounds[2]) {
323 if (!glyph->fPathData) {
324 return nullptr;
325 }
326 const SkGlyph::Intercept* intercept = glyph->fPathData->fIntercept;
327 while (intercept) {
328 if (bounds[0] == intercept->fBounds[0] && bounds[1] == intercept->fBounds[1]) {
329 return intercept;
330 }
331 intercept = intercept->fNext;
332 }
333 return nullptr;
334 }
335
findIntercepts(const SkScalar bounds[2],SkScalar scale,SkScalar xPos,bool yAxis,SkGlyph * glyph,SkScalar * array,int * count)336 void SkStrike::findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos,
337 bool yAxis, SkGlyph* glyph, SkScalar* array, int* count) {
338 const SkGlyph::Intercept* match = MatchBounds(glyph, bounds);
339
340 if (match) {
341 if (match->fInterval[0] < match->fInterval[1]) {
342 OffsetResults(match, scale, xPos, array, count);
343 }
344 return;
345 }
346
347 SkGlyph::Intercept* intercept = fAlloc.make<SkGlyph::Intercept>();
348 intercept->fNext = glyph->fPathData->fIntercept;
349 intercept->fBounds[0] = bounds[0];
350 intercept->fBounds[1] = bounds[1];
351 intercept->fInterval[0] = SK_ScalarMax;
352 intercept->fInterval[1] = SK_ScalarMin;
353 glyph->fPathData->fIntercept = intercept;
354 const SkPath* path = &(glyph->fPathData->fPath);
355 const SkRect& pathBounds = path->getBounds();
356 if (*(&pathBounds.fBottom - yAxis) < bounds[0] || bounds[1] < *(&pathBounds.fTop - yAxis)) {
357 return;
358 }
359 SkPath::Iter iter(*path, false);
360 SkPoint pts[4];
361 SkPath::Verb verb;
362 while (SkPath::kDone_Verb != (verb = iter.next(pts))) {
363 switch (verb) {
364 case SkPath::kMove_Verb:
365 break;
366 case SkPath::kLine_Verb:
367 AddLine(pts, bounds[0], yAxis, intercept);
368 AddLine(pts, bounds[1], yAxis, intercept);
369 AddPoints(pts, 2, bounds, yAxis, intercept);
370 break;
371 case SkPath::kQuad_Verb:
372 if (!quad_in_bounds(&pts[0].fY - yAxis, bounds)) {
373 break;
374 }
375 AddQuad(pts, bounds[0], yAxis, intercept);
376 AddQuad(pts, bounds[1], yAxis, intercept);
377 AddPoints(pts, 3, bounds, yAxis, intercept);
378 break;
379 case SkPath::kConic_Verb:
380 SkASSERT(0); // no support for text composed of conics
381 break;
382 case SkPath::kCubic_Verb:
383 if (!cubic_in_bounds(&pts[0].fY - yAxis, bounds)) {
384 break;
385 }
386 AddCubic(pts, bounds[0], yAxis, intercept);
387 AddCubic(pts, bounds[1], yAxis, intercept);
388 AddPoints(pts, 4, bounds, yAxis, intercept);
389 break;
390 case SkPath::kClose_Verb:
391 break;
392 default:
393 SkASSERT(0);
394 break;
395 }
396 }
397 if (intercept->fInterval[0] >= intercept->fInterval[1]) {
398 intercept->fInterval[0] = SK_ScalarMax;
399 intercept->fInterval[1] = SK_ScalarMin;
400 return;
401 }
402 OffsetResults(intercept, scale, xPos, array, count);
403 }
404
dump() const405 void SkStrike::dump() const {
406 const SkTypeface* face = fScalerContext->getTypeface();
407 const SkScalerContextRec& rec = fScalerContext->getRec();
408 SkMatrix matrix;
409 rec.getSingleMatrix(&matrix);
410 matrix.preScale(SkScalarInvert(rec.fTextSize), SkScalarInvert(rec.fTextSize));
411 SkString name;
412 face->getFamilyName(&name);
413
414 SkString msg;
415 SkFontStyle style = face->fontStyle();
416 msg.printf("cache typeface:%x %25s:(%d,%d,%d)\n %s glyphs:%3d",
417 face->uniqueID(), name.c_str(), style.weight(), style.width(), style.slant(),
418 rec.dump().c_str(), fGlyphMap.count());
419 SkDebugf("%s\n", msg.c_str());
420 }
421
hasImage(const SkGlyph & glyph)422 bool SkStrike::hasImage(const SkGlyph& glyph) {
423 return !glyph.isEmpty() && this->findImage(glyph) != nullptr;
424 }
425
hasPath(const SkGlyph & glyph)426 bool SkStrike::hasPath(const SkGlyph& glyph) {
427 return !glyph.isEmpty() && this->findPath(glyph) != nullptr;
428 }
429
430 #ifdef SK_DEBUG
forceValidate() const431 void SkStrike::forceValidate() const {
432 size_t memoryUsed = sizeof(*this);
433 fGlyphMap.foreach ([&memoryUsed](const SkGlyph* glyphPtr) {
434 memoryUsed += sizeof(SkGlyph);
435 if (glyphPtr->fImage) {
436 memoryUsed += glyphPtr->computeImageSize();
437 }
438 if (glyphPtr->fPathData) {
439 memoryUsed += compute_path_size(glyphPtr->fPathData->fPath);
440 }
441 });
442 SkASSERT(fMemoryUsed == memoryUsed);
443 }
444
validate() const445 void SkStrike::validate() const {
446 #ifdef SK_DEBUG_GLYPH_CACHE
447 forceValidate();
448 #endif
449 }
450 #endif
451
452
453