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