/src/skia/src/core/SkGlyph.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef SkGlyph_DEFINED |
9 | | #define SkGlyph_DEFINED |
10 | | |
11 | | #include "include/core/SkPath.h" |
12 | | #include "include/core/SkTypes.h" |
13 | | #include "include/private/SkChecksum.h" |
14 | | #include "include/private/SkFixed.h" |
15 | | #include "include/private/SkTo.h" |
16 | | #include "include/private/SkVx.h" |
17 | | #include "src/core/SkMask.h" |
18 | | #include "src/core/SkMathPriv.h" |
19 | | |
20 | | class SkArenaAlloc; |
21 | | class SkScalerContext; |
22 | | |
23 | | // A combination of SkGlyphID and sub-pixel position information. |
24 | | struct SkPackedGlyphID { |
25 | | static constexpr uint32_t kImpossibleID = ~0u; |
26 | | enum { |
27 | | // Lengths |
28 | | kGlyphIDLen = 16u, |
29 | | kSubPixelPosLen = 2u, |
30 | | |
31 | | // Bit positions |
32 | | kSubPixelX = 0u, |
33 | | kGlyphID = kSubPixelPosLen, |
34 | | kSubPixelY = kGlyphIDLen + kSubPixelPosLen, |
35 | | kEndData = kGlyphIDLen + 2 * kSubPixelPosLen, |
36 | | |
37 | | // Masks |
38 | | kGlyphIDMask = (1u << kGlyphIDLen) - 1, |
39 | | kSubPixelPosMask = (1u << kSubPixelPosLen) - 1, |
40 | | kMaskAll = (1u << kEndData) - 1, |
41 | | |
42 | | // Location of sub pixel info in a fixed pointer number. |
43 | | kFixedPointBinaryPointPos = 16u, |
44 | | kFixedPointSubPixelPosBits = kFixedPointBinaryPointPos - kSubPixelPosLen, |
45 | | }; |
46 | | |
47 | | static constexpr SkScalar kSubpixelRound = 1.f / (1u << (SkPackedGlyphID::kSubPixelPosLen + 1)); |
48 | | |
49 | | static constexpr SkIPoint kXYFieldMask{kSubPixelPosMask << kSubPixelX, |
50 | | kSubPixelPosMask << kSubPixelY}; |
51 | | |
52 | | constexpr explicit SkPackedGlyphID(SkGlyphID glyphID) |
53 | 23.6M | : fID{(uint32_t)glyphID << kGlyphID} { } |
54 | | |
55 | | constexpr SkPackedGlyphID(SkGlyphID glyphID, SkFixed x, SkFixed y) |
56 | 0 | : fID {PackIDXY(glyphID, x, y)} { } |
57 | | |
58 | | constexpr SkPackedGlyphID(SkGlyphID glyphID, uint32_t x, uint32_t y) |
59 | 0 | : fID {PackIDSubXSubY(glyphID, x, y)} { } |
60 | | |
61 | | SkPackedGlyphID(SkGlyphID glyphID, SkPoint pt, SkIPoint mask) |
62 | 220k | : fID{PackIDSkPoint(glyphID, pt, mask)} { } |
63 | | |
64 | 0 | constexpr explicit SkPackedGlyphID(uint32_t v) : fID{v & kMaskAll} { } |
65 | 418k | constexpr SkPackedGlyphID() : fID{kImpossibleID} {} |
66 | | |
67 | 23.7M | bool operator==(const SkPackedGlyphID& that) const { |
68 | 23.7M | return fID == that.fID; |
69 | 23.7M | } |
70 | 0 | bool operator!=(const SkPackedGlyphID& that) const { |
71 | 0 | return !(*this == that); |
72 | 0 | } |
73 | 0 | bool operator<(SkPackedGlyphID that) const { |
74 | 0 | return this->fID < that.fID; |
75 | 0 | } |
76 | | |
77 | 280k | SkGlyphID glyphID() const { |
78 | 280k | return (fID >> kGlyphID) & kGlyphIDMask; |
79 | 280k | } |
80 | | |
81 | 0 | uint32_t value() const { |
82 | 0 | return fID; |
83 | 0 | } |
84 | | |
85 | 74.7k | SkFixed getSubXFixed() const { |
86 | 74.7k | return this->subToFixed(kSubPixelX); |
87 | 74.7k | } |
88 | | |
89 | 74.7k | SkFixed getSubYFixed() const { |
90 | 74.7k | return this->subToFixed(kSubPixelY); |
91 | 74.7k | } |
92 | | |
93 | 7.53k | uint32_t hash() const { |
94 | 7.53k | return SkChecksum::CheapMix(fID); |
95 | 7.53k | } |
96 | | |
97 | 0 | SkString dump() const { |
98 | 0 | SkString str; |
99 | 0 | str.appendf("glyphID: %d, x: %d, y:%d", glyphID(), getSubXFixed(), getSubYFixed()); |
100 | 0 | return str; |
101 | 0 | } |
102 | | |
103 | | private: |
104 | 0 | static constexpr uint32_t PackIDSubXSubY(SkGlyphID glyphID, uint32_t x, uint32_t y) { |
105 | 0 | SkASSERT(x < (1u << kSubPixelPosLen)); |
106 | 0 | SkASSERT(y < (1u << kSubPixelPosLen)); |
107 | 0 |
|
108 | 0 | return (x << kSubPixelX) | (y << kSubPixelY) | (glyphID << kGlyphID); |
109 | 0 | } |
110 | | |
111 | | // Assumptions: pt is properly rounded. mask is set for the x or y fields. |
112 | | // |
113 | | // A sub-pixel field is a number on the interval [2^kSubPixel, 2^(kSubPixel + kSubPixelPosLen)). |
114 | | // Where kSubPixel is either kSubPixelX or kSubPixelY. Given a number x on [0, 1) we can |
115 | | // generate a sub-pixel field using: |
116 | | // sub-pixel-field = x * 2^(kSubPixel + kSubPixelPosLen) |
117 | | // |
118 | | // We can generate the integer sub-pixel field by &-ing the integer part of sub-filed with the |
119 | | // sub-pixel field mask. |
120 | | // int-sub-pixel-field = int(sub-pixel-field) & (kSubPixelPosMask << kSubPixel) |
121 | | // |
122 | | // The last trick is to extend the range from [0, 1) to [0, 2). The extend range is |
123 | | // necessary because the modulo 1 calculation (pt - floor(pt)) generates numbers on [-1, 1). |
124 | | // This does not round (floor) properly when converting to integer. Adding one to the range |
125 | | // causes truncation and floor to be the same. Coincidentally, masking to produce the field also |
126 | | // removes the +1. |
127 | 220k | static uint32_t PackIDSkPoint(SkGlyphID glyphID, SkPoint pt, SkIPoint mask) { |
128 | | #if 0 |
129 | | // TODO: why does this code not work on GCC 8.3 x86 Debug builds? |
130 | | using namespace skvx; |
131 | | using XY = Vec<2, float>; |
132 | | using SubXY = Vec<2, int>; |
133 | | |
134 | | const XY magic = {1.f * (1u << (kSubPixelPosLen + kSubPixelX)), |
135 | | 1.f * (1u << (kSubPixelPosLen + kSubPixelY))}; |
136 | | XY pos{pt.x(), pt.y()}; |
137 | | XY subPos = (pos - floor(pos)) + 1.0f; |
138 | | SubXY sub = cast<int>(subPos * magic) & SubXY{mask.x(), mask.y()}; |
139 | | #else |
140 | 220k | const float magicX = 1.f * (1u << (kSubPixelPosLen + kSubPixelX)), |
141 | 220k | magicY = 1.f * (1u << (kSubPixelPosLen + kSubPixelY)); |
142 | | |
143 | 220k | float x = pt.x(), |
144 | 220k | y = pt.y(); |
145 | 220k | x = (x - floorf(x)) + 1.0f; |
146 | 220k | y = (y - floorf(y)) + 1.0f; |
147 | 220k | int sub[] = { |
148 | 220k | (int)(x * magicX) & mask.x(), |
149 | 220k | (int)(y * magicY) & mask.y(), |
150 | 220k | }; |
151 | 220k | #endif |
152 | | |
153 | 220k | SkASSERT(sub[0] / (1u << kSubPixelX) < (1u << kSubPixelPosLen)); |
154 | 220k | SkASSERT(sub[1] / (1u << kSubPixelY) < (1u << kSubPixelPosLen)); |
155 | 220k | return (glyphID << kGlyphID) | sub[0] | sub[1]; |
156 | 220k | } |
157 | | |
158 | 0 | static constexpr uint32_t PackIDXY(SkGlyphID glyphID, SkFixed x, SkFixed y) { |
159 | 0 | return PackIDSubXSubY(glyphID, FixedToSub(x), FixedToSub(y)); |
160 | 0 | } |
161 | | |
162 | 0 | static constexpr uint32_t FixedToSub(SkFixed n) { |
163 | 0 | return ((uint32_t)n >> kFixedPointSubPixelPosBits) & kSubPixelPosMask; |
164 | 0 | } |
165 | | |
166 | 149k | constexpr SkFixed subToFixed(uint32_t subPixelPosBit) const { |
167 | 149k | uint32_t subPixelPosition = (fID >> subPixelPosBit) & kSubPixelPosMask; |
168 | 149k | return subPixelPosition << kFixedPointSubPixelPosBits; |
169 | 149k | } |
170 | | |
171 | | uint32_t fID; |
172 | | }; |
173 | | |
174 | | class SkGlyphRect; |
175 | | namespace skglyph { |
176 | | SkGlyphRect rect_union(SkGlyphRect, SkGlyphRect); |
177 | | SkGlyphRect rect_intersection(SkGlyphRect, SkGlyphRect); |
178 | | } // namespace skglyph |
179 | | |
180 | | // SkGlyphRect encodes rectangles with coordinates on [-32767, 32767]. It is specialized for |
181 | | // rectangle union and intersection operations. |
182 | | class SkGlyphRect { |
183 | | public: |
184 | | SkGlyphRect(int16_t left, int16_t top, int16_t right, int16_t bottom) |
185 | 6.55k | : fRect{left, top, (int16_t)-right, (int16_t)-bottom} { |
186 | 6.55k | SkDEBUGCODE(const int32_t min = std::numeric_limits<int16_t>::min()); |
187 | 6.55k | SkASSERT(left != min && top != min && right != min && bottom != min); |
188 | 6.55k | } |
189 | 0 | bool empty() const { |
190 | 0 | return fRect[0] >= -fRect[2] || fRect[1] >= -fRect[3]; |
191 | 0 | } |
192 | 1 | SkRect rect() const { |
193 | 1 | return SkRect::MakeLTRB(fRect[0], fRect[1], -fRect[2], -fRect[3]); |
194 | 1 | } |
195 | 508 | SkIRect iRect() const { |
196 | 508 | return SkIRect::MakeLTRB(fRect[0], fRect[1], -fRect[2], -fRect[3]); |
197 | 508 | } |
198 | 5.87k | SkGlyphRect offset(int16_t x, int16_t y) const { |
199 | 5.87k | return SkGlyphRect{fRect + Storage{x, y, SkTo<int16_t>(-x), SkTo<int16_t>(-y)}}; |
200 | 5.87k | } |
201 | 5.87k | skvx::Vec<2, int16_t> topLeft() const { return {fRect[0], fRect[1]}; } |
202 | | friend SkGlyphRect skglyph::rect_union(SkGlyphRect, SkGlyphRect); |
203 | | friend SkGlyphRect skglyph::rect_intersection(SkGlyphRect, SkGlyphRect); |
204 | | |
205 | | private: |
206 | | using Storage = skvx::Vec<4, int16_t>; |
207 | 11.7k | SkGlyphRect(Storage rect) : fRect{rect} { } |
208 | | Storage fRect; |
209 | | }; |
210 | | |
211 | | namespace skglyph { |
212 | 680 | inline SkGlyphRect empty_rect() { |
213 | 680 | constexpr int16_t max = std::numeric_limits<int16_t>::max(); |
214 | 680 | return {max, max, -max, -max}; |
215 | 680 | } |
216 | 0 | inline SkGlyphRect full_rect() { |
217 | 0 | constexpr int16_t max = std::numeric_limits<int16_t>::max(); |
218 | 0 | return {-max, -max, max, max}; |
219 | 0 | } |
220 | 5.87k | inline SkGlyphRect rect_union(SkGlyphRect a, SkGlyphRect b) { |
221 | 5.87k | return skvx::min(a.fRect, b.fRect); |
222 | 5.87k | } |
223 | 0 | inline SkGlyphRect rect_intersection(SkGlyphRect a, SkGlyphRect b) { |
224 | 0 | return skvx::max(a.fRect, b.fRect); |
225 | 0 | } |
226 | | } // namespace skglyph |
227 | | |
228 | | struct SkGlyphPrototype; |
229 | | |
230 | | class SkGlyph { |
231 | | public: |
232 | | // SkGlyph() is used for testing. |
233 | 23.1k | constexpr SkGlyph() : SkGlyph{SkPackedGlyphID()} { } |
234 | 141k | constexpr explicit SkGlyph(SkPackedGlyphID id) : fID{id} { } |
235 | | |
236 | 483k | SkVector advanceVector() const { return SkVector{fAdvanceX, fAdvanceY}; } |
237 | 17.3M | SkScalar advanceX() const { return fAdvanceX; } |
238 | 0 | SkScalar advanceY() const { return fAdvanceY; } |
239 | | |
240 | 117k | SkGlyphID getGlyphID() const { return fID.glyphID(); } |
241 | 288k | SkPackedGlyphID getPackedID() const { return fID; } |
242 | 946 | SkFixed getSubXFixed() const { return fID.getSubXFixed(); } |
243 | 946 | SkFixed getSubYFixed() const { return fID.getSubYFixed(); } |
244 | | |
245 | | size_t rowBytes() const; |
246 | | size_t rowBytesUsingFormat(SkMask::Format format) const; |
247 | | |
248 | | // Call this to set all of the metrics fields to 0 (e.g. if the scaler |
249 | | // encounters an error measuring a glyph). Note: this does not alter the |
250 | | // fImage, fPath, fID, fMaskFormat fields. |
251 | | void zeroMetrics(); |
252 | | |
253 | | SkMask mask() const; |
254 | | |
255 | | SkMask mask(SkPoint position) const; |
256 | | |
257 | | // Image |
258 | | // If we haven't already tried to associate an image with this glyph |
259 | | // (i.e. setImageHasBeenCalled() returns false), then use the |
260 | | // SkScalerContext or const void* argument to set the image. |
261 | | bool setImage(SkArenaAlloc* alloc, SkScalerContext* scalerContext); |
262 | | bool setImage(SkArenaAlloc* alloc, const void* image); |
263 | | |
264 | | // Merge the from glyph into this glyph using alloc to allocate image data. Return the number |
265 | | // of bytes allocated. Copy the width, height, top, left, format, and image into this glyph |
266 | | // making a copy of the image using the alloc. |
267 | | size_t setMetricsAndImage(SkArenaAlloc* alloc, const SkGlyph& from); |
268 | | |
269 | | // Returns true if the image has been set. |
270 | 41.7k | bool setImageHasBeenCalled() const { |
271 | 41.7k | return fImage != nullptr || this->isEmpty() || this->imageTooLarge(); |
272 | 41.7k | } |
273 | | |
274 | | // Return a pointer to the path if the image exists, otherwise return nullptr. |
275 | 43.2k | const void* image() const { SkASSERT(this->setImageHasBeenCalled()); return fImage; } |
276 | | |
277 | | // Return the size of the image. |
278 | | size_t imageSize() const; |
279 | | |
280 | | // Path |
281 | | // If we haven't already tried to associate a path to this glyph |
282 | | // (i.e. setPathHasBeenCalled() returns false), then use the |
283 | | // SkScalerContext or SkPath argument to try to do so. N.B. this |
284 | | // may still result in no path being associated with this glyph, |
285 | | // e.g. if you pass a null SkPath or the typeface is bitmap-only. |
286 | | // |
287 | | // This setPath() call is sticky... once you call it, the glyph |
288 | | // stays in its state permanently, ignoring any future calls. |
289 | | // |
290 | | // Returns true if this is the first time you called setPath() |
291 | | // and there actually is a path; call path() to get it. |
292 | | bool setPath(SkArenaAlloc* alloc, SkScalerContext* scalerContext); |
293 | | bool setPath(SkArenaAlloc* alloc, const SkPath* path); |
294 | | |
295 | | // Returns true if that path has been set. |
296 | 406k | bool setPathHasBeenCalled() const { return fPathData != nullptr; } |
297 | | |
298 | | // Return a pointer to the path if it exists, otherwise return nullptr. Only works if the |
299 | | // path was previously set. |
300 | | const SkPath* path() const; |
301 | | |
302 | | // Format |
303 | 117k | bool isColor() const { return fMaskFormat == SkMask::kARGB32_Format; } |
304 | 141k | SkMask::Format maskFormat() const { return fMaskFormat; } |
305 | | size_t formatAlignment() const; |
306 | | |
307 | | // Bounds |
308 | 235k | int maxDimension() const { return std::max(fWidth, fHeight); } |
309 | 800 | SkIRect iRect() const { return SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); } |
310 | 13.9M | SkRect rect() const { return SkRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); } |
311 | 5.87k | SkGlyphRect glyphRect() const { |
312 | 5.87k | return {fLeft, fTop, |
313 | 5.87k | SkTo<int16_t>(fLeft + fWidth), SkTo<int16_t>(fTop + fHeight)}; |
314 | 5.87k | } |
315 | 1.64k | int left() const { return fLeft; } |
316 | 1.64k | int top() const { return fTop; } |
317 | 3.07k | int width() const { return fWidth; } |
318 | 3.07k | int height() const { return fHeight; } |
319 | 188k | bool isEmpty() const { |
320 | | // fHeight == 0 -> fWidth == 0; |
321 | 188k | SkASSERT(fHeight != 0 || fWidth == 0); |
322 | 188k | return fWidth == 0; |
323 | 188k | } |
324 | 70.8k | bool imageTooLarge() const { return fWidth >= kMaxGlyphWidth; } |
325 | | |
326 | | // Make sure that the intercept information is on the glyph and return it, or return it if it |
327 | | // already exists. |
328 | | // * bounds - either end of the gap for the character. |
329 | | // * scale, xPos - information about how wide the gap is. |
330 | | // * array - accumulated gaps for many characters if not null. |
331 | | // * count - the number of gaps. |
332 | | void ensureIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos, |
333 | | SkScalar* array, int* count, SkArenaAlloc* alloc); |
334 | | |
335 | | private: |
336 | | // There are two sides to an SkGlyph, the scaler side (things that create glyph data) have |
337 | | // access to all the fields. Scalers are assumed to maintain all the SkGlyph invariants. The |
338 | | // consumer side has a tighter interface. |
339 | | friend class RandomScalerContext; |
340 | | friend class RemoteStrike; |
341 | | friend class SkScalerContext; |
342 | | friend class SkScalerContextProxy; |
343 | | friend class SkScalerContext_Empty; |
344 | | friend class SkScalerContext_FreeType; |
345 | | friend class SkScalerContext_FreeType_Base; |
346 | | friend class SkScalerContext_DW; |
347 | | friend class SkScalerContext_GDI; |
348 | | friend class SkScalerContext_Mac; |
349 | | friend class SkStrikeClientImpl; |
350 | | friend class SkTestScalerContext; |
351 | | friend class SkTestSVGScalerContext; |
352 | | friend class SkUserScalerContext; |
353 | | friend class TestSVGTypeface; |
354 | | friend class TestTypeface; |
355 | | |
356 | | static constexpr uint16_t kMaxGlyphWidth = 1u << 13u; |
357 | | |
358 | | // Support horizontal and vertical skipping strike-through / underlines. |
359 | | // The caller walks the linked list looking for a match. For a horizontal underline, |
360 | | // the fBounds contains the top and bottom of the underline. The fInterval pair contains the |
361 | | // beginning and end of of the intersection of the bounds and the glyph's path. |
362 | | // If interval[0] >= interval[1], no intersection was found. |
363 | | struct Intercept { |
364 | | Intercept* fNext; |
365 | | SkScalar fBounds[2]; // for horz underlines, the boundaries in Y |
366 | | SkScalar fInterval[2]; // the outside intersections of the axis and the glyph |
367 | | }; |
368 | | |
369 | | struct PathData { |
370 | | Intercept* fIntercept{nullptr}; |
371 | | SkPath fPath; |
372 | | bool fHasPath{false}; |
373 | | }; |
374 | | |
375 | | size_t allocImage(SkArenaAlloc* alloc); |
376 | | |
377 | | // path == nullptr indicates that there is no path. |
378 | | void installPath(SkArenaAlloc* alloc, const SkPath* path); |
379 | | |
380 | | // The width and height of the glyph mask. |
381 | | uint16_t fWidth = 0, |
382 | | fHeight = 0; |
383 | | |
384 | | // The offset from the glyphs origin on the baseline to the top left of the glyph mask. |
385 | | int16_t fTop = 0, |
386 | | fLeft = 0; |
387 | | |
388 | | // fImage must remain null if the glyph is empty or if width > kMaxGlyphWidth. |
389 | | void* fImage = nullptr; |
390 | | |
391 | | // Path data has tricky state. If the glyph isEmpty, then fPathData should always be nullptr, |
392 | | // else if fPathData is not null, then a path has been requested. The fPath field of fPathData |
393 | | // may still be null after the request meaning that there is no path for this glyph. |
394 | | PathData* fPathData = nullptr; |
395 | | |
396 | | // The advance for this glyph. |
397 | | float fAdvanceX = 0, |
398 | | fAdvanceY = 0; |
399 | | |
400 | | SkMask::Format fMaskFormat{SkMask::kBW_Format}; |
401 | | |
402 | | // Used by the DirectWrite scaler to track state. |
403 | | int8_t fForceBW = 0; |
404 | | |
405 | | SkPackedGlyphID fID; |
406 | | }; |
407 | | |
408 | | #endif |