Debian Codegraph @

src/core/SkGlyph.cpp

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright 2018 Google LLC
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 "src/core/SkGlyph.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkDrawable.h"
13 #include "include/core/SkPicture.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSerialProcs.h"
16 #include "include/core/SkSpan.h"
17 #include "include/private/base/SkFloatingPoint.h"
18 #include "include/private/base/SkTFitsIn.h"
19 #include "include/private/base/SkTo.h"
20 #include "src/base/SkArenaAlloc.h"
21 #include "src/base/SkBezierCurves.h"
22 #include "src/core/SkPictureData.h"
23 #include "src/core/SkReadBuffer.h"
24 #include "src/core/SkScalerContext.h"
25 #include "src/core/SkWriteBuffer.h"
26 #include "src/text/StrikeForGPU.h"
27
28 #include <cstring>
29 #include <optional>
30 #include <tuple>
31 #include <utility>
32
33 using namespace skglyph;
34 using namespace sktext;
35
36 // -- SkPictureBackedGlyphDrawable -----------------------------------------------------------------
37 sk_sp<SkPictureBackedGlyphDrawable>
38 SkPictureBackedGlyphDrawable::MakeFromBuffer(SkReadBuffer& buffer) {
39 SkASSERT(buffer.isValid());
40
41 sk_sp<SkData> pictureData = buffer.readByteArrayAsData();
42
43 // Return nullptr if invalid or there an empty drawable, which is represented by nullptr.
44 if (!buffer.isValid() || pictureData->empty()) {
45 return nullptr;
46 }
47
48 // Propagate the outer buffer's allow-SkSL setting to the picture decoder, using the flag on
49 // the deserial procs.
50 SkDeserialProcs procs;
51 procs.fAllowSkSL = buffer.allowSkSL();
52 procs.fAllowTagsProc = [](SkFourByteTag tag, void*) -> bool {
53 return tag == SK_PICT_BUFFER_SIZE_TAG ||
54 tag == SK_PICT_FACTORY_TAG ||
55 tag == SK_PICT_PAINT_BUFFER_TAG ||
56 tag == SK_PICT_PATH_BUFFER_TAG ||
57 tag == SK_PICT_READER_TAG;
58 };
59 sk_sp<SkPicture> picture = SkPicture::MakeFromData(pictureData.get(), &procs);
60 if (!buffer.validate(picture != nullptr)) {
61 return nullptr;
62 }
63
64 return sk_make_sp<SkPictureBackedGlyphDrawable>(std::move(picture));
65 }
66
67 void SkPictureBackedGlyphDrawable::FlattenDrawable(SkWriteBuffer& buffer, SkDrawable* drawable) {
68 if (drawable == nullptr) {
69 buffer.writeByteArray(nullptr, 0);
70 return;
71 }
72
73 sk_sp<SkPicture> picture = drawable->makePictureSnapshot();
74 // These drawables should not have SkImages, SkTypefaces or SkPictures inside of them, so
75 // the default SkSerialProcs are sufficient.
76 sk_sp<SkData> data = picture->serialize();
77
78 // If the picture is too big, or there is no picture, then drop by sending an empty byte array.
79 if (!SkTFitsIn<uint32_t>(data->size()) || data->empty()) {
80 buffer.writeByteArray(nullptr, 0);
81 return;
82 }
83
84 buffer.writeByteArray(data->data(), data->size());
85 }
86
87 SkPictureBackedGlyphDrawable::SkPictureBackedGlyphDrawable(sk_sp<SkPicture> picture)
88 : fPicture(std::move(picture)) {}
89
90 SkRect SkPictureBackedGlyphDrawable::onGetBounds() {
91 return fPicture->cullRect();
92 }
93
94 size_t SkPictureBackedGlyphDrawable::onApproximateBytesUsed() {
95 return sizeof(SkPictureBackedGlyphDrawable) + fPicture->approximateBytesUsed();
96 }
97
98 void SkPictureBackedGlyphDrawable::onDraw(SkCanvas* canvas) {
99 canvas->drawPicture(fPicture);
100 }
101
102 //-- SkGlyph ---------------------------------------------------------------------------------------
103 std::optional<SkGlyph> SkGlyph::MakeFromBuffer(SkReadBuffer& buffer) {
104 SkASSERT(buffer.isValid());
105 const SkPackedGlyphID packedID{buffer.readUInt()};
106 const SkVector advance = buffer.readPoint();
107 const uint32_t dimensions = buffer.readUInt();
108 const uint32_t leftTop = buffer.readUInt();
109 const SkMask::Format format = SkTo<SkMask::Format>(buffer.readUInt());
110
111 if (!buffer.validate(SkMask::IsValidFormat(format))) {
112 return std::nullopt;
113 }
114
115 SkGlyph glyph{packedID};
116 glyph.fAdvanceX = advance.x();
117 glyph.fAdvanceY = advance.y();
118 glyph.fWidth = dimensions >> 16;
119 glyph.fHeight = dimensions & 0xffffu;
120 glyph.fLeft = leftTop >> 16;
121 glyph.fTop = leftTop & 0xffffu;
122 glyph.fMaskFormat = format;
123 SkDEBUGCODE(glyph.fAdvancesBoundsFormatAndInitialPathDone = true;)
124 return glyph;
125 }
126
127 SkMask SkGlyph::mask() const {
128 SkIRect bounds = SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight);
129 return SkMask(static_cast<const uint8_t*>(fImage), bounds, this->rowBytes(), fMaskFormat);
130 }
131
132 SkMask SkGlyph::mask(SkPoint position) const {
133 SkASSERT(SkScalarIsInt(position.x()) && SkScalarIsInt(position.y()));
134 SkIRect bounds = SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight);
135 bounds.offset(SkScalarFloorToInt(position.x()), SkScalarFloorToInt(position.y()));
136 return SkMask(static_cast<const uint8_t*>(fImage), bounds, this->rowBytes(), fMaskFormat);
137 }
138
139 void SkGlyph::zeroMetrics() {
140 fAdvanceX = 0;
141 fAdvanceY = 0;
142 fWidth = 0;
143 fHeight = 0;
144 fTop = 0;
145 fLeft = 0;
146 }
147
148 static size_t bits_to_bytes(size_t bits) {
149 return (bits + 7) >> 3;
150 }
151
152 static size_t format_alignment(SkMask::Format format) {
153 switch (format) {
154 case SkMask::kBW_Format: [[fallthrough]];
155 case SkMask::kA8_Format: [[fallthrough]];
156 case SkMask::k3D_Format: [[fallthrough]];
157 case SkMask::kSDF_Format:
158 return alignof(uint8_t);
159 case SkMask::kARGB32_Format:
160 return alignof(uint32_t);
161 case SkMask::kLCD16_Format:
162 return alignof(uint16_t);
163 }
164 SkUNREACHABLE;
165 }
166
167 static size_t format_rowbytes(int width, SkMask::Format format) {
168 return format == SkMask::kBW_Format ? bits_to_bytes(width)
169 : width * format_alignment(format);
170 }
171
172 size_t SkGlyph::formatAlignment() const {
173 return format_alignment(this->maskFormat());
174 }
175
176 size_t SkGlyph::allocImage(SkArenaAlloc* alloc) {
177 SkASSERT(!this->isEmpty());
178 auto size = this->imageSize();
179 fImage = alloc->makeBytesAlignedTo(size, this->formatAlignment());
180
181 return size;
182 }
183
184 bool SkGlyph::setImage(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
185 if (!this->setImageHasBeenCalled()) {
186 // It used to be that getImage() could change the fMaskFormat. Extra checking to make
187 // sure there are no regressions.
188 SkDEBUGCODE(SkMask::Format oldFormat = this->maskFormat());
189 this->allocImage(alloc);
190 scalerContext->getImage(*this);
191 SkASSERT(oldFormat == this->maskFormat());
192 return true;
193 }
194 return false;
195 }
196
197 bool SkGlyph::setImage(SkArenaAlloc* alloc, const void* image) {
198 if (!this->setImageHasBeenCalled()) {
199 this->allocImage(alloc);
200 memcpy(fImage, image, this->imageSize());
201 return true;
202 }
203 return false;
204 }
205
206 size_t SkGlyph::setMetricsAndImage(SkArenaAlloc* alloc, const SkGlyph& from) {
207 // Since the code no longer tries to find replacement glyphs, the image should always be
208 // nullptr.
209 SkASSERT(fImage == nullptr || from.fImage == nullptr);
210
211 // TODO(herb): remove "if" when we are sure there are no colliding glyphs.
212 if (fImage == nullptr) {
213 fAdvanceX = from.fAdvanceX;
214 fAdvanceY = from.fAdvanceY;
215 fWidth = from.fWidth;
216 fHeight = from.fHeight;
217 fTop = from.fTop;
218 fLeft = from.fLeft;
219 fScalerContextBits = from.fScalerContextBits;
220 fMaskFormat = from.fMaskFormat;
221
222 // From glyph may not have an image because the glyph is too large.
223 if (from.fImage != nullptr && this->setImage(alloc, from.image())) {
224 return this->imageSize();
225 }
226
227 SkDEBUGCODE(fAdvancesBoundsFormatAndInitialPathDone = from.fAdvancesBoundsFormatAndInitialPathDone;)
228 }
229 return 0;
230 }
231
232 size_t SkGlyph::rowBytes() const {
233 return format_rowbytes(fWidth, fMaskFormat);
234 }
235
236 size_t SkGlyph::rowBytesUsingFormat(SkMask::Format format) const {
237 return format_rowbytes(fWidth, format);
238 }
239
240 size_t SkGlyph::imageSize() const {
241 if (this->isEmpty() || this->imageTooLarge()) { return 0; }
242
243 size_t size = this->rowBytes() * fHeight;
244
245 if (fMaskFormat == SkMask::k3D_Format) {
246 size *= 3;
247 }
248
249 return size;
250 }
251
252 void SkGlyph::installPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline, bool modified) {
253 SkASSERT(fPathData == nullptr);
254 SkASSERT(!this->setPathHasBeenCalled());
255 fPathData = alloc->make<SkGlyph::PathData>();
256 if (path != nullptr) {
257 fPathData->fPath = *path;
258 fPathData->fPath.updateBoundsCache();
259 fPathData->fPath.getGenerationID();
260 fPathData->fHasPath = true;
261 fPathData->fHairline = hairline;
262 fPathData->fModified = modified;
263 }
264 }
265
266 bool SkGlyph::setPath(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
267 if (!this->setPathHasBeenCalled()) {
268 scalerContext->getPath(*this, alloc);
269 SkASSERT(this->setPathHasBeenCalled());
270 return this->path() != nullptr;
271 }
272
273 return false;
274 }
275
276 bool SkGlyph::setPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline, bool modified) {
277 if (!this->setPathHasBeenCalled()) {
278 this->installPath(alloc, path, hairline, modified);
279 return this->path() != nullptr;
280 }
281 return false;
282 }
283
284 const SkPath* SkGlyph::path() const {
285 // setPath must have been called previously.
286 SkASSERT(this->setPathHasBeenCalled());
287 if (fPathData->fHasPath) {
288 return &fPathData->fPath;
289 }
290 return nullptr;
291 }
292
293 bool SkGlyph::pathIsHairline() const {
294 // setPath must have been called previously.
295 SkASSERT(this->setPathHasBeenCalled());
296 return fPathData->fHairline;
297 }
298
299 bool SkGlyph::pathIsModified() const {
300 // setPath must have been called previously.
301 SkASSERT(this->setPathHasBeenCalled());
302 return fPathData->fModified;
303 }
304
305 void SkGlyph::installDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) {
306 SkASSERT(fDrawableData == nullptr);
307 SkASSERT(!this->setDrawableHasBeenCalled());
308 fDrawableData = alloc->make<SkGlyph::DrawableData>();
309 if (drawable != nullptr) {
310 fDrawableData->fDrawable = std::move(drawable);
311 fDrawableData->fDrawable->getGenerationID();
312 fDrawableData->fHasDrawable = true;
313 }
314 }
315
316 bool SkGlyph::setDrawable(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
317 if (!this->setDrawableHasBeenCalled()) {
318 sk_sp<SkDrawable> drawable = scalerContext->getDrawable(*this);
319 this->installDrawable(alloc, std::move(drawable));
320 return this->drawable() != nullptr;
321 }
322 return false;
323 }
324
325 bool SkGlyph::setDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) {
326 if (!this->setDrawableHasBeenCalled()) {
327 this->installDrawable(alloc, std::move(drawable));
328 return this->drawable() != nullptr;
329 }
330 return false;
331 }
332
333 SkDrawable* SkGlyph::drawable() const {
334 // setDrawable must have been called previously.
335 SkASSERT(this->setDrawableHasBeenCalled());
336 if (fDrawableData->fHasDrawable) {
337 return fDrawableData->fDrawable.get();
338 }
339 return nullptr;
340 }
341
342 void SkGlyph::flattenMetrics(SkWriteBuffer& buffer) const {
343 buffer.writeUInt(fID.value());
344 buffer.writePoint({fAdvanceX, fAdvanceY});
345 buffer.writeUInt(fWidth << 16 | fHeight);
346 // Note: << has undefined behavior for negative values, so convert everything to the bit
347 // values of uint16_t. Using the cast keeps the signed values fLeft and fTop from sign
348 // extending.
349 const uint32_t left = static_cast<uint16_t>(fLeft);
350 const uint32_t top = static_cast<uint16_t>(fTop);
351 buffer.writeUInt(left << 16 | top);
352 buffer.writeUInt(SkTo<uint32_t>(fMaskFormat));
353 }
354
355 void SkGlyph::flattenImage(SkWriteBuffer& buffer) const {
356 SkASSERT(this->setImageHasBeenCalled());
357
358 // If the glyph is empty or too big, then no image data is sent.
359 if (!this->isEmpty() && SkGlyphDigest::FitsInAtlas(*this)) {
360 buffer.writeByteArray(this->image(), this->imageSize());
361 }
362 }
363
364 size_t SkGlyph::addImageFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) {
365 SkASSERT(buffer.isValid());
366
367 // If the glyph is empty or too big, then no image data is received.
368 if (this->isEmpty() || !SkGlyphDigest::FitsInAtlas(*this)) {
369 return 0;
370 }
371
372 size_t memoryIncrease = 0;
373
374 void* imageData = alloc->makeBytesAlignedTo(this->imageSize(), this->formatAlignment());
375 buffer.readByteArray(imageData, this->imageSize());
376 if (buffer.isValid()) {
377 this->installImage(imageData);
378 memoryIncrease += this->imageSize();
379 }
380
381 return memoryIncrease;
382 }
383
384 void SkGlyph::flattenPath(SkWriteBuffer& buffer) const {
385 SkASSERT(this->setPathHasBeenCalled());
386
387 const bool hasPath = this->path() != nullptr;
388 buffer.writeBool(hasPath);
389 if (hasPath) {
390 buffer.writeBool(this->pathIsHairline());
391 buffer.writeBool(this->pathIsModified());
392 buffer.writePath(*this->path());
393 }
394 }
395
396 size_t SkGlyph::addPathFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) {
397 SkASSERT(buffer.isValid());
398
399 size_t memoryIncrease = 0;
400 const bool hasPath = buffer.readBool();
401 // Check if the buffer is invalid, so as to not make a logical decision on invalid data.
402 if (!buffer.isValid()) {
403 return 0;
404 }
405 if (hasPath) {
406 const bool pathIsHairline = buffer.readBool();
407 const bool pathIsModified = buffer.readBool();
408 if (auto path = buffer.readPath()) {
409 if (this->setPath(alloc, &path.value(), pathIsHairline, pathIsModified)) {
410 memoryIncrease += path->approximateBytesUsed();
411 }
412 }
413 } else {
414 this->setPath(alloc, nullptr, false, false);
415 }
416
417 return memoryIncrease;
418 }
419
420 void SkGlyph::flattenDrawable(SkWriteBuffer& buffer) const {
421 SkASSERT(this->setDrawableHasBeenCalled());
422
423 if (this->isEmpty() || this->drawable() == nullptr) {
424 SkPictureBackedGlyphDrawable::FlattenDrawable(buffer, nullptr);
425 return;
426 }
427
428 SkPictureBackedGlyphDrawable::FlattenDrawable(buffer, this->drawable());
429 }
430
431 size_t SkGlyph::addDrawableFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) {
432 SkASSERT(buffer.isValid());
433
434 sk_sp<SkDrawable> drawable = SkPictureBackedGlyphDrawable::MakeFromBuffer(buffer);
435 if (!buffer.isValid()) {
436 return 0;
437 }
438
439 if (this->setDrawable(alloc, std::move(drawable))) {
440 return this->drawable()->approximateBytesUsed();
441 }
442
443 return 0;
444 }
445
446 static std::tuple<SkScalar, SkScalar> calculate_path_gap(
447 SkScalar topOffset, SkScalar bottomOffset, const SkPath& path) {
448
449 // Left and Right of an ever expanding gap around the path.
450 SkScalar left = SK_ScalarMax,
451 right = SK_ScalarMin;
452
453 auto expandGap = [&left, &right](SkScalar v) {
454 left = std::min(left, v);
455 right = std::max(right, v);
456 };
457
458 // Handle all the different verbs for the path.
459 auto addLine = [&](SkSpan<const SkPoint> pts, SkScalar offset) {
460 SkScalar t = sk_ieee_float_divide(offset - pts[0].fY, pts[1].fY - pts[0].fY);
461 if (0 <= t && t < 1) { // this handles divide by zero above
462 expandGap(pts[0].fX + t * (pts[1].fX - pts[0].fX));
463 }
464 };
465
466 auto addQuad = [&](SkSpan<const SkPoint> pts, SkScalar offset) {
467 float intersectionStorage[2];
468 auto intersections = SkBezierQuad::IntersectWithHorizontalLine(
469 pts, offset, intersectionStorage);
470
471 for(float intersection : intersections) {
472 expandGap(intersection);
473 }
474 };
475
476 auto addCubic = [&](SkSpan<const SkPoint> pts, SkScalar offset) {
477 float intersectionStorage[3];
478 auto intersections = SkBezierCubic::IntersectWithHorizontalLine(
479 pts, offset, intersectionStorage);
480
481 for(float intersection : intersections) {
482 expandGap(intersection);
483 }
484 };
485
486 // Handle when a verb's points are in the gap between top and bottom.
487 auto addPts = [&expandGap, topOffset, bottomOffset](SkSpan<const SkPoint> pts) {
488 for (const SkPoint p : pts) {
489 if (topOffset < p.fY && p.fY < bottomOffset) {
490 expandGap(p.fX);
491 }
492 }
493 };
494
495 SkPath::Iter iter(path, false);
496 while (auto rec = iter.next()) {
497 const SkSpan<const SkPoint> pts = rec->fPoints;
498 switch (rec->fVerb) {
499 case SkPathVerb::kMove: {
500 break;
501 }
502 case SkPathVerb::kLine: {
503 auto [lineTop, lineBottom] = std::minmax({pts[0].fY, pts[1].fY});
504
505 // The y-coordinates of the points intersect the top and bottom offsets.
506 if (topOffset <= lineBottom && lineTop <= bottomOffset) {
507 addLine(pts, topOffset);
508 addLine(pts, bottomOffset);
509 addPts(pts);
510 }
511 break;
512 }
513 case SkPathVerb::kQuad: {
514 auto [quadTop, quadBottom] = std::minmax({pts[0].fY, pts[1].fY, pts[2].fY});
515
516 // The y-coordinates of the points intersect the top and bottom offsets.
517 if (topOffset <= quadBottom && quadTop <= bottomOffset) {
518 addQuad(pts, topOffset);
519 addQuad(pts, bottomOffset);
520 addPts(pts);
521 }
522 break;
523 }
524 case SkPathVerb::kConic: {
525 SkDEBUGFAIL("There should be no conic primitives in glyph outlines.");
526 break;
527 }
528 case SkPathVerb::kCubic: {
529 auto [cubicTop, cubicBottom] =
530 std::minmax({pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY});
531
532 // The y-coordinates of the points intersect the top and bottom offsets.
533 if (topOffset <= cubicBottom && cubicTop <= bottomOffset) {
534 addCubic(pts, topOffset);
535 addCubic(pts, bottomOffset);
536 addPts(pts);
537 }
538 break;
539 }
540 case SkPathVerb::kClose: {
541 break;
542 }
543 }
544 }
545
546 return std::tie(left, right);
547 }
548
549 void SkGlyph::ensureIntercepts(const SkScalar* bounds, SkScalar scale, SkScalar xPos,
550 SkScalar* array, int* count, SkArenaAlloc* alloc) {
551
552 auto offsetResults = [scale, xPos](
553 const SkGlyph::Intercept* intercept,SkScalar* array, int* count) {
554 if (array) {
555 array += *count;
556 for (int index = 0; index < 2; index++) {
557 *array++ = intercept->fInterval[index] * scale + xPos;
558 }
559 }
560 *count += 2;
561 };
562
563 const SkGlyph::Intercept* match =
564 [this](const SkScalar bounds[2]) -> const SkGlyph::Intercept* {
565 if (fPathData == nullptr) {
566 return nullptr;
567 }
568 const SkGlyph::Intercept* intercept = fPathData->fIntercept;
569 while (intercept != nullptr) {
570 if (bounds[0] == intercept->fBounds[0] && bounds[1] == intercept->fBounds[1]) {
571 return intercept;
572 }
573 intercept = intercept->fNext;
574 }
575 return nullptr;
576 }(bounds);
577
578 if (match != nullptr) {
579 if (match->fInterval[0] < match->fInterval[1]) {
580 offsetResults(match, array, count);
581 }
582 return;
583 }
584
585 SkGlyph::Intercept* intercept = alloc->make<SkGlyph::Intercept>();
586 intercept->fNext = fPathData->fIntercept;
587 intercept->fBounds[0] = bounds[0];
588 intercept->fBounds[1] = bounds[1];
589 intercept->fInterval[0] = SK_ScalarMax;
590 intercept->fInterval[1] = SK_ScalarMin;
591 fPathData->fIntercept = intercept;
592 const SkPath* path = &(fPathData->fPath);
593 const SkRect& pathBounds = path->getBounds();
594 if (pathBounds.fBottom < bounds[0] || bounds[1] < pathBounds.fTop) {
595 return;
596 }
597
598 std::tie(intercept->fInterval[0], intercept->fInterval[1])
599 = calculate_path_gap(bounds[0], bounds[1], *path);
600
601 if (intercept->fInterval[0] >= intercept->fInterval[1]) {
602 intercept->fInterval[0] = SK_ScalarMax;
603 intercept->fInterval[1] = SK_ScalarMin;
604 return;
605 }
606 offsetResults(intercept, array, count);
607 }
608
609 namespace {
610 uint32_t init_actions(const SkGlyph& glyph) {
611 constexpr uint32_t kAllUnset = 0;
612 constexpr uint32_t kDrop = SkTo<uint32_t>(GlyphAction::kDrop);
613 constexpr uint32_t kAllDrop =
614 kDrop << kDirectMask |
615 kDrop << kDirectMaskCPU |
616 kDrop << kMask |
617 kDrop << kSDFT |
618 kDrop << kPath |
619 kDrop << kDrawable;
620 return glyph.isEmpty() ? kAllDrop : kAllUnset;
621 }
622 } // namespace
623
624 // -- SkGlyphDigest --------------------------------------------------------------------------------
625 SkGlyphDigest::SkGlyphDigest(size_t index, const SkGlyph& glyph)
626 : fPackedID{SkTo<uint64_t>(glyph.getPackedID().value())}
627 , fIndex{SkTo<uint64_t>(index)}
628 , fIsEmpty(glyph.isEmpty())
629 , fFormat(glyph.maskFormat())
630 , fActions{init_actions(glyph)}
631 , fLeft{SkTo<int16_t>(glyph.left())}
632 , fTop{SkTo<int16_t>(glyph.top())}
633 , fWidth{SkTo<uint16_t>(glyph.width())}
634 , fHeight{SkTo<uint16_t>(glyph.height())} {}
635
636 void SkGlyphDigest::setActionFor(skglyph::ActionType actionType,
637 SkGlyph* glyph,
638 StrikeForGPU* strike) {
639 // We don't have to do any more if the glyph is marked as kDrop because it was isEmpty().
640 if (this->actionFor(actionType) == GlyphAction::kUnset) {
641 GlyphAction action = GlyphAction::kReject;
642 switch (actionType) {
643 case kDirectMask: {
644 if (this->fitsInAtlasDirect()) {
645 action = GlyphAction::kAccept;
646 }
647 break;
648 }
649 case kDirectMaskCPU: {
650 if (strike->prepareForImage(glyph)) {
651 SkASSERT(!glyph->isEmpty());
652 action = GlyphAction::kAccept;
653 }
654 break;
655 }
656 case kMask: {
657 if (this->fitsInAtlasInterpolated()) {
658 action = GlyphAction::kAccept;
659 }
660 break;
661 }
662 case kSDFT: {
663 if (this->fitsInAtlasDirect() &&
664 this->maskFormat() == SkMask::Format::kSDF_Format) {
665 action = GlyphAction::kAccept;
666 }
667 break;
668 }
669 case kPath: {
670 if (strike->prepareForPath(glyph)) {
671 action = GlyphAction::kAccept;
672 }
673 break;
674 }
675 case kDrawable: {
676 if (strike->prepareForDrawable(glyph)) {
677 action = GlyphAction::kAccept;
678 }
679 break;
680 }
681 }
682 this->setAction(actionType, action);
683 }
684 }
685
686 bool SkGlyphDigest::FitsInAtlas(const SkGlyph& glyph) {
687 return glyph.maxDimension() <= kSkSideTooBigForAtlas;
688 }
689
690 // -- SkGlyphPositionRoundingSpec ------------------------------------------------------------------
691 SkVector SkGlyphPositionRoundingSpec::HalfAxisSampleFreq(
692 bool isSubpixel, SkAxisAlignment axisAlignment) {
693 if (!isSubpixel) {
694 return {SK_ScalarHalf, SK_ScalarHalf};
695 } else {
696 switch (axisAlignment) {
697 case SkAxisAlignment::kX:
698 return {SkPackedGlyphID::kSubpixelRound, SK_ScalarHalf};
699 case SkAxisAlignment::kY:
700 return {SK_ScalarHalf, SkPackedGlyphID::kSubpixelRound};
701 case SkAxisAlignment::kNone:
702 return {SkPackedGlyphID::kSubpixelRound, SkPackedGlyphID::kSubpixelRound};
703 }
704 }
705
706 // Some compilers need this.
707 return {0, 0};
708 }
709
710 SkIPoint SkGlyphPositionRoundingSpec::IgnorePositionMask(
711 bool isSubpixel, SkAxisAlignment axisAlignment) {
712 return SkIPoint::Make((!isSubpixel || axisAlignment == SkAxisAlignment::kY) ? 0 : ~0,
713 (!isSubpixel || axisAlignment == SkAxisAlignment::kX) ? 0 : ~0);
714 }
715
716 SkIPoint SkGlyphPositionRoundingSpec::IgnorePositionFieldMask(bool isSubpixel,
717 SkAxisAlignment axisAlignment) {
718 SkIPoint ignoreMask = IgnorePositionMask(isSubpixel, axisAlignment);
719 SkIPoint answer{ignoreMask.x() & SkPackedGlyphID::kXYFieldMask.x(),
720 ignoreMask.y() & SkPackedGlyphID::kXYFieldMask.y()};
721 return answer;
722 }
723
724 SkGlyphPositionRoundingSpec::SkGlyphPositionRoundingSpec(
725 bool isSubpixel, SkAxisAlignment axisAlignment)
726 : halfAxisSampleFreq{HalfAxisSampleFreq(isSubpixel, axisAlignment)}
727 , ignorePositionMask{IgnorePositionMask(isSubpixel, axisAlignment)}
728 , ignorePositionFieldMask {IgnorePositionFieldMask(isSubpixel, axisAlignment)} {}