• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 #include "src/core/SkGlyph.h"
9 
10 #include "include/core/SkDrawable.h"
11 #include "src/core/SkArenaAlloc.h"
12 #include "src/core/SkScalerContext.h"
13 #include "src/pathops/SkPathOpsCubic.h"
14 #include "src/pathops/SkPathOpsQuad.h"
15 
16 SkGlyph::SkGlyph(const SkGlyph&) = default;
17 SkGlyph& SkGlyph::operator=(const SkGlyph&) = default;
18 SkGlyph::SkGlyph(SkGlyph&&) = default;
19 SkGlyph& SkGlyph::operator=(SkGlyph&&) = default;
20 SkGlyph::~SkGlyph() = default;
21 
mask() const22 SkMask SkGlyph::mask() const {
23     SkMask mask;
24     mask.fImage = (uint8_t*)fImage;
25     mask.fBounds.setXYWH(fLeft, fTop, fWidth, fHeight);
26     mask.fRowBytes = this->rowBytes();
27     mask.fFormat = fMaskFormat;
28     return mask;
29 }
30 
mask(SkPoint position) const31 SkMask SkGlyph::mask(SkPoint position) const {
32     SkMask answer = this->mask();
33     answer.fBounds.offset(SkScalarFloorToInt(position.x()), SkScalarFloorToInt(position.y()));
34     return answer;
35 }
36 
zeroMetrics()37 void SkGlyph::zeroMetrics() {
38     fAdvanceX = 0;
39     fAdvanceY = 0;
40     fWidth    = 0;
41     fHeight   = 0;
42     fTop      = 0;
43     fLeft     = 0;
44 }
45 
bits_to_bytes(size_t bits)46 static size_t bits_to_bytes(size_t bits) {
47     return (bits + 7) >> 3;
48 }
49 
format_alignment(SkMask::Format format)50 static size_t format_alignment(SkMask::Format format) {
51     switch (format) {
52         case SkMask::kBW_Format:
53         case SkMask::kA8_Format:
54         case SkMask::k3D_Format:
55         case SkMask::kSDF_Format:
56             return alignof(uint8_t);
57         case SkMask::kARGB32_Format:
58             return alignof(uint32_t);
59         case SkMask::kLCD16_Format:
60             return alignof(uint16_t);
61         default:
62             SK_ABORT("Unknown mask format.");
63             break;
64     }
65     return 0;
66 }
67 
format_rowbytes(int width,SkMask::Format format)68 static size_t format_rowbytes(int width, SkMask::Format format) {
69     return format == SkMask::kBW_Format ? bits_to_bytes(width)
70                                         : width * format_alignment(format);
71 }
72 
formatAlignment() const73 size_t SkGlyph::formatAlignment() const {
74     return format_alignment(this->maskFormat());
75 }
76 
allocImage(SkArenaAlloc * alloc)77 size_t SkGlyph::allocImage(SkArenaAlloc* alloc) {
78     SkASSERT(!this->isEmpty());
79     auto size = this->imageSize();
80     fImage = alloc->makeBytesAlignedTo(size, this->formatAlignment());
81 
82     return size;
83 }
84 
setImage(SkArenaAlloc * alloc,SkScalerContext * scalerContext)85 bool SkGlyph::setImage(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
86     if (!this->setImageHasBeenCalled()) {
87         // It used to be that getImage() could change the fMaskFormat. Extra checking to make
88         // sure there are no regressions.
89         SkDEBUGCODE(SkMask::Format oldFormat = this->maskFormat());
90         this->allocImage(alloc);
91         scalerContext->getImage(*this);
92         SkASSERT(oldFormat == this->maskFormat());
93         return true;
94     }
95     return false;
96 }
97 
setImage(SkArenaAlloc * alloc,const void * image)98 bool SkGlyph::setImage(SkArenaAlloc* alloc, const void* image) {
99     if (!this->setImageHasBeenCalled()) {
100         this->allocImage(alloc);
101         memcpy(fImage, image, this->imageSize());
102         return true;
103     }
104     return false;
105 }
106 
setMetricsAndImage(SkArenaAlloc * alloc,const SkGlyph & from)107 size_t SkGlyph::setMetricsAndImage(SkArenaAlloc* alloc, const SkGlyph& from) {
108     // Since the code no longer tries to find replacement glyphs, the image should always be
109     // nullptr.
110     SkASSERT(fImage == nullptr || from.fImage == nullptr);
111 
112     // TODO(herb): remove "if" when we are sure there are no colliding glyphs.
113     if (fImage == nullptr) {
114         fAdvanceX = from.fAdvanceX;
115         fAdvanceY = from.fAdvanceY;
116         fWidth = from.fWidth;
117         fHeight = from.fHeight;
118         fTop = from.fTop;
119         fLeft = from.fLeft;
120         fForceBW = from.fForceBW;
121         fMaskFormat = from.fMaskFormat;
122 
123         // From glyph may not have an image because the glyph is too large.
124         if (from.fImage != nullptr && this->setImage(alloc, from.image())) {
125             return this->imageSize();
126         }
127 
128         SkDEBUGCODE(fAdvancesBoundsFormatAndInitialPathDone = from.fAdvancesBoundsFormatAndInitialPathDone;)
129     }
130     return 0;
131 }
132 
rowBytes() const133 size_t SkGlyph::rowBytes() const {
134     return format_rowbytes(fWidth, fMaskFormat);
135 }
136 
rowBytesUsingFormat(SkMask::Format format) const137 size_t SkGlyph::rowBytesUsingFormat(SkMask::Format format) const {
138     return format_rowbytes(fWidth, format);
139 }
140 
imageSize() const141 size_t SkGlyph::imageSize() const {
142     if (this->isEmpty() || this->imageTooLarge()) { return 0; }
143 
144     size_t size = this->rowBytes() * fHeight;
145 
146     if (fMaskFormat == SkMask::k3D_Format) {
147         size *= 3;
148     }
149 
150     return size;
151 }
152 
installPath(SkArenaAlloc * alloc,const SkPath * path,bool hairline)153 void SkGlyph::installPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline) {
154     SkASSERT(fPathData == nullptr);
155     SkASSERT(!this->setPathHasBeenCalled());
156     fPathData = alloc->make<SkGlyph::PathData>();
157     if (path != nullptr) {
158         fPathData->fPath = *path;
159         fPathData->fPath.updateBoundsCache();
160         fPathData->fPath.getGenerationID();
161         fPathData->fHasPath = true;
162         fPathData->fHairline = hairline;
163     }
164 }
165 
setPath(SkArenaAlloc * alloc,SkScalerContext * scalerContext)166 bool SkGlyph::setPath(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
167     if (!this->setPathHasBeenCalled()) {
168         scalerContext->getPath(*this, alloc);
169         SkASSERT(this->setPathHasBeenCalled());
170         return this->path() != nullptr;
171     }
172 
173     return false;
174 }
175 
setPath(SkArenaAlloc * alloc,const SkPath * path,bool hairline)176 bool SkGlyph::setPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline) {
177     if (!this->setPathHasBeenCalled()) {
178         this->installPath(alloc, path, hairline);
179         return this->path() != nullptr;
180     }
181     return false;
182 }
183 
path() const184 const SkPath* SkGlyph::path() const {
185     // setPath must have been called previously.
186     SkASSERT(this->setPathHasBeenCalled());
187     if (fPathData->fHasPath) {
188         return &fPathData->fPath;
189     }
190     return nullptr;
191 }
192 
pathIsHairline() const193 bool SkGlyph::pathIsHairline() const {
194     // setPath must have been called previously.
195     SkASSERT(this->setPathHasBeenCalled());
196     return fPathData->fHairline;
197 }
198 
installDrawable(SkArenaAlloc * alloc,sk_sp<SkDrawable> drawable)199 void SkGlyph::installDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) {
200     SkASSERT(fDrawableData == nullptr);
201     SkASSERT(!this->setDrawableHasBeenCalled());
202     fDrawableData = alloc->make<SkGlyph::DrawableData>();
203     if (drawable != nullptr) {
204         fDrawableData->fDrawable = std::move(drawable);
205         fDrawableData->fDrawable->getGenerationID();
206         fDrawableData->fHasDrawable = true;
207     }
208 }
209 
setDrawable(SkArenaAlloc * alloc,SkScalerContext * scalerContext)210 bool SkGlyph::setDrawable(SkArenaAlloc* alloc, SkScalerContext* scalerContext) {
211     if (!this->setDrawableHasBeenCalled()) {
212         sk_sp<SkDrawable> drawable = scalerContext->getDrawable(*this);
213         this->installDrawable(alloc, std::move(drawable));
214         return this->drawable() != nullptr;
215     }
216     return false;
217 }
218 
setDrawable(SkArenaAlloc * alloc,sk_sp<SkDrawable> drawable)219 bool SkGlyph::setDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) {
220     if (!this->setDrawableHasBeenCalled()) {
221         this->installDrawable(alloc, std::move(drawable));
222         return this->drawable() != nullptr;
223     }
224     return false;
225 }
226 
drawable() const227 SkDrawable* SkGlyph::drawable() const {
228     // setDrawable must have been called previously.
229     SkASSERT(this->setDrawableHasBeenCalled());
230     if (fDrawableData->fHasDrawable) {
231         return fDrawableData->fDrawable.get();
232     }
233     return nullptr;
234 }
235 
calculate_path_gap(SkScalar topOffset,SkScalar bottomOffset,const SkPath & path)236 static std::tuple<SkScalar, SkScalar> calculate_path_gap(
237         SkScalar topOffset, SkScalar bottomOffset, const SkPath& path) {
238 
239     // Left and Right of an ever expanding gap around the path.
240     SkScalar left  = SK_ScalarMax,
241              right = SK_ScalarMin;
242     auto expandGap = [&left, &right](SkScalar v) {
243         left  = std::min(left, v);
244         right = std::max(right, v);
245     };
246 
247     // Handle all the different verbs for the path.
248     SkPoint pts[4];
249     auto addLine = [&expandGap, &pts](SkScalar offset) {
250         SkScalar t = sk_ieee_float_divide(offset - pts[0].fY, pts[1].fY - pts[0].fY);
251         if (0 <= t && t < 1) {   // this handles divide by zero above
252             expandGap(pts[0].fX + t * (pts[1].fX - pts[0].fX));
253         }
254     };
255 
256     auto addQuad = [&expandGap, &pts](SkScalar offset) {
257         SkDQuad quad;
258         quad.set(pts);
259         double roots[2];
260         int count = quad.horizontalIntersect(offset, roots);
261         while (--count >= 0) {
262             expandGap(quad.ptAtT(roots[count]).asSkPoint().fX);
263         }
264     };
265 
266     auto addCubic = [&expandGap, &pts](SkScalar offset) {
267         SkDCubic cubic;
268         cubic.set(pts);
269         double roots[3];
270         int count = cubic.horizontalIntersect(offset, roots);
271         while (--count >= 0) {
272             expandGap(cubic.ptAtT(roots[count]).asSkPoint().fX);
273         }
274     };
275 
276     // Handle when a verb's points are in the gap between top and bottom.
277     auto addPts = [&expandGap, &pts, topOffset, bottomOffset](int ptCount) {
278         for (int i = 0; i < ptCount; ++i) {
279             if (topOffset < pts[i].fY && pts[i].fY < bottomOffset) {
280                 expandGap(pts[i].fX);
281             }
282         }
283     };
284 
285     SkPath::Iter iter(path, false);
286     SkPath::Verb verb;
287     while (SkPath::kDone_Verb != (verb = iter.next(pts))) {
288         switch (verb) {
289             case SkPath::kMove_Verb: {
290                 break;
291             }
292             case SkPath::kLine_Verb: {
293                 addLine(topOffset);
294                 addLine(bottomOffset);
295                 addPts(2);
296                 break;
297             }
298             case SkPath::kQuad_Verb: {
299                 SkScalar quadTop = std::min(std::min(pts[0].fY, pts[1].fY), pts[2].fY);
300                 if (bottomOffset < quadTop) { break; }
301                 SkScalar quadBottom = std::max(std::max(pts[0].fY, pts[1].fY), pts[2].fY);
302                 if (topOffset > quadBottom) { break; }
303                 addQuad(topOffset);
304                 addQuad(bottomOffset);
305                 addPts(3);
306                 break;
307             }
308             case SkPath::kConic_Verb: {
309                 SkASSERT(0);  // no support for text composed of conics
310                 break;
311             }
312             case SkPath::kCubic_Verb: {
313                 SkScalar quadTop =
314                         std::min(std::min(std::min(pts[0].fY, pts[1].fY), pts[2].fY), pts[3].fY);
315                 if (bottomOffset < quadTop) { break; }
316                 SkScalar quadBottom =
317                         std::max(std::max(std::max(pts[0].fY, pts[1].fY), pts[2].fY), pts[3].fY);
318                 if (topOffset > quadBottom) { break; }
319                 addCubic(topOffset);
320                 addCubic(bottomOffset);
321                 addPts(4);
322                 break;
323             }
324             case SkPath::kClose_Verb: {
325                 break;
326             }
327             default: {
328                 SkASSERT(0);
329                 break;
330             }
331         }
332     }
333 
334     return std::tie(left, right);
335 }
336 
ensureIntercepts(const SkScalar * bounds,SkScalar scale,SkScalar xPos,SkScalar * array,int * count,SkArenaAlloc * alloc)337 void SkGlyph::ensureIntercepts(const SkScalar* bounds, SkScalar scale, SkScalar xPos,
338                                SkScalar* array, int* count, SkArenaAlloc* alloc) {
339 
340     auto offsetResults = [scale, xPos](
341             const SkGlyph::Intercept* intercept,SkScalar* array, int* count) {
342         if (array) {
343             array += *count;
344             for (int index = 0; index < 2; index++) {
345                 *array++ = intercept->fInterval[index] * scale + xPos;
346             }
347         }
348         *count += 2;
349     };
350 
351     const SkGlyph::Intercept* match =
352             [this](const SkScalar bounds[2]) -> const SkGlyph::Intercept* {
353                 if (!fPathData) {
354                     return nullptr;
355                 }
356                 const SkGlyph::Intercept* intercept = fPathData->fIntercept;
357                 while (intercept) {
358                     if (bounds[0] == intercept->fBounds[0] && bounds[1] == intercept->fBounds[1]) {
359                         return intercept;
360                     }
361                     intercept = intercept->fNext;
362                 }
363                 return nullptr;
364             }(bounds);
365 
366     if (match) {
367         if (match->fInterval[0] < match->fInterval[1]) {
368             offsetResults(match, array, count);
369         }
370         return;
371     }
372 
373     SkGlyph::Intercept* intercept = alloc->make<SkGlyph::Intercept>();
374     intercept->fNext = fPathData->fIntercept;
375     intercept->fBounds[0] = bounds[0];
376     intercept->fBounds[1] = bounds[1];
377     intercept->fInterval[0] = SK_ScalarMax;
378     intercept->fInterval[1] = SK_ScalarMin;
379     fPathData->fIntercept = intercept;
380     const SkPath* path = &(fPathData->fPath);
381     const SkRect& pathBounds = path->getBounds();
382     if (pathBounds.fBottom < bounds[0] || bounds[1] < pathBounds.fTop) {
383         return;
384     }
385 
386     std::tie(intercept->fInterval[0], intercept->fInterval[1])
387             = calculate_path_gap(bounds[0], bounds[1], *path);
388 
389     if (intercept->fInterval[0] >= intercept->fInterval[1]) {
390         intercept->fInterval[0] = SK_ScalarMax;
391         intercept->fInterval[1] = SK_ScalarMin;
392         return;
393     }
394     offsetResults(intercept, array, count);
395 }
396 
SkGlyphDigest(size_t index,const SkGlyph & glyph)397 SkGlyphDigest::SkGlyphDigest(size_t index, const SkGlyph& glyph)
398         : fPackedGlyphID{glyph.getPackedID().value()}
399         , fIndex{SkTo<uint32_t>(index)}
400         , fIsEmpty(glyph.isEmpty())
401         , fIsColor(glyph.isColor())
402         , fCanDrawAsMask{SkStrikeForGPU::CanDrawAsMask(glyph)}
403         , fCanDrawAsSDFT{SkStrikeForGPU::CanDrawAsSDFT(glyph)}
404         , fMaxDimension{(uint16_t)glyph.maxDimension()} {}
405