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
SkPackedGlyphIDSkPackedGlyphID52 constexpr explicit SkPackedGlyphID(SkGlyphID glyphID)
53 : fID{(uint32_t)glyphID << kGlyphID} { }
54
SkPackedGlyphIDSkPackedGlyphID55 constexpr SkPackedGlyphID(SkGlyphID glyphID, SkFixed x, SkFixed y)
56 : fID {PackIDXY(glyphID, x, y)} { }
57
SkPackedGlyphIDSkPackedGlyphID58 constexpr SkPackedGlyphID(SkGlyphID glyphID, uint32_t x, uint32_t y)
59 : fID {PackIDSubXSubY(glyphID, x, y)} { }
60
SkPackedGlyphIDSkPackedGlyphID61 SkPackedGlyphID(SkGlyphID glyphID, SkPoint pt, SkIPoint mask)
62 : fID{PackIDSkPoint(glyphID, pt, mask)} { }
63
SkPackedGlyphIDSkPackedGlyphID64 constexpr explicit SkPackedGlyphID(uint32_t v) : fID{v & kMaskAll} { }
SkPackedGlyphIDSkPackedGlyphID65 constexpr SkPackedGlyphID() : fID{kImpossibleID} {}
66
67 bool operator==(const SkPackedGlyphID& that) const {
68 return fID == that.fID;
69 }
70 bool operator!=(const SkPackedGlyphID& that) const {
71 return !(*this == that);
72 }
73 bool operator<(SkPackedGlyphID that) const {
74 return this->fID < that.fID;
75 }
76
glyphIDSkPackedGlyphID77 SkGlyphID glyphID() const {
78 return (fID >> kGlyphID) & kGlyphIDMask;
79 }
80
valueSkPackedGlyphID81 uint32_t value() const {
82 return fID;
83 }
84
getSubXFixedSkPackedGlyphID85 SkFixed getSubXFixed() const {
86 return this->subToFixed(kSubPixelX);
87 }
88
getSubYFixedSkPackedGlyphID89 SkFixed getSubYFixed() const {
90 return this->subToFixed(kSubPixelY);
91 }
92
hashSkPackedGlyphID93 uint32_t hash() const {
94 return SkChecksum::CheapMix(fID);
95 }
96
dumpSkPackedGlyphID97 SkString dump() const {
98 SkString str;
99 str.appendf("glyphID: %d, x: %d, y:%d", glyphID(), getSubXFixed(), getSubYFixed());
100 return str;
101 }
102
103 private:
PackIDSubXSubYSkPackedGlyphID104 static constexpr uint32_t PackIDSubXSubY(SkGlyphID glyphID, uint32_t x, uint32_t y) {
105 SkASSERT(x < (1u << kSubPixelPosLen));
106 SkASSERT(y < (1u << kSubPixelPosLen));
107
108 return (x << kSubPixelX) | (y << kSubPixelY) | (glyphID << kGlyphID);
109 }
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.
PackIDSkPointSkPackedGlyphID127 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 const float magicX = 1.f * (1u << (kSubPixelPosLen + kSubPixelX)),
141 magicY = 1.f * (1u << (kSubPixelPosLen + kSubPixelY));
142
143 float x = pt.x(),
144 y = pt.y();
145 x = (x - floorf(x)) + 1.0f;
146 y = (y - floorf(y)) + 1.0f;
147 int sub[] = {
148 (int)(x * magicX) & mask.x(),
149 (int)(y * magicY) & mask.y(),
150 };
151 #endif
152
153 SkASSERT(sub[0] / (1u << kSubPixelX) < (1u << kSubPixelPosLen));
154 SkASSERT(sub[1] / (1u << kSubPixelY) < (1u << kSubPixelPosLen));
155 return (glyphID << kGlyphID) | sub[0] | sub[1];
156 }
157
PackIDXYSkPackedGlyphID158 static constexpr uint32_t PackIDXY(SkGlyphID glyphID, SkFixed x, SkFixed y) {
159 return PackIDSubXSubY(glyphID, FixedToSub(x), FixedToSub(y));
160 }
161
FixedToSubSkPackedGlyphID162 static constexpr uint32_t FixedToSub(SkFixed n) {
163 return ((uint32_t)n >> kFixedPointSubPixelPosBits) & kSubPixelPosMask;
164 }
165
subToFixedSkPackedGlyphID166 constexpr SkFixed subToFixed(uint32_t subPixelPosBit) const {
167 uint32_t subPixelPosition = (fID >> subPixelPosBit) & kSubPixelPosMask;
168 return subPixelPosition << kFixedPointSubPixelPosBits;
169 }
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:
SkGlyphRect(int16_t left,int16_t top,int16_t right,int16_t bottom)184 SkGlyphRect(int16_t left, int16_t top, int16_t right, int16_t bottom)
185 : fRect{left, top, (int16_t)-right, (int16_t)-bottom} {
186 SkDEBUGCODE(const int32_t min = std::numeric_limits<int16_t>::min());
187 SkASSERT(left != min && top != min && right != min && bottom != min);
188 }
empty()189 bool empty() const {
190 return fRect[0] >= -fRect[2] || fRect[1] >= -fRect[3];
191 }
rect()192 SkRect rect() const {
193 return SkRect::MakeLTRB(fRect[0], fRect[1], -fRect[2], -fRect[3]);
194 }
iRect()195 SkIRect iRect() const {
196 return SkIRect::MakeLTRB(fRect[0], fRect[1], -fRect[2], -fRect[3]);
197 }
offset(int16_t x,int16_t y)198 SkGlyphRect offset(int16_t x, int16_t y) const {
199 return SkGlyphRect{fRect + Storage{x, y, SkTo<int16_t>(-x), SkTo<int16_t>(-y)}};
200 }
topLeft()201 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>;
SkGlyphRect(Storage rect)207 SkGlyphRect(Storage rect) : fRect{rect} { }
208 Storage fRect;
209 };
210
211 namespace skglyph {
empty_rect()212 inline SkGlyphRect empty_rect() {
213 constexpr int16_t max = std::numeric_limits<int16_t>::max();
214 return {max, max, -max, -max};
215 }
full_rect()216 inline SkGlyphRect full_rect() {
217 constexpr int16_t max = std::numeric_limits<int16_t>::max();
218 return {-max, -max, max, max};
219 }
rect_union(SkGlyphRect a,SkGlyphRect b)220 inline SkGlyphRect rect_union(SkGlyphRect a, SkGlyphRect b) {
221 return skvx::min(a.fRect, b.fRect);
222 }
rect_intersection(SkGlyphRect a,SkGlyphRect b)223 inline SkGlyphRect rect_intersection(SkGlyphRect a, SkGlyphRect b) {
224 return skvx::max(a.fRect, b.fRect);
225 }
226 } // namespace skglyph
227
228 struct SkGlyphPrototype;
229
230 class SkGlyph {
231 public:
232 // SkGlyph() is used for testing.
SkGlyph()233 constexpr SkGlyph() : SkGlyph{SkPackedGlyphID()} { }
SkGlyph(SkPackedGlyphID id)234 constexpr explicit SkGlyph(SkPackedGlyphID id) : fID{id} { }
235
advanceVector()236 SkVector advanceVector() const { return SkVector{fAdvanceX, fAdvanceY}; }
advanceX()237 SkScalar advanceX() const { return fAdvanceX; }
advanceY()238 SkScalar advanceY() const { return fAdvanceY; }
239
getGlyphID()240 SkGlyphID getGlyphID() const { return fID.glyphID(); }
getPackedID()241 SkPackedGlyphID getPackedID() const { return fID; }
getSubXFixed()242 SkFixed getSubXFixed() const { return fID.getSubXFixed(); }
getSubYFixed()243 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.
setImageHasBeenCalled()270 bool setImageHasBeenCalled() const {
271 return fImage != nullptr || this->isEmpty() || this->imageTooLarge();
272 }
273
274 // Return a pointer to the path if the image exists, otherwise return nullptr.
image()275 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.
setPathHasBeenCalled()296 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
isColor()303 bool isColor() const { return fMaskFormat == SkMask::kARGB32_Format; }
maskFormat()304 SkMask::Format maskFormat() const { return fMaskFormat; }
305 size_t formatAlignment() const;
306
307 // Bounds
maxDimension()308 int maxDimension() const { return std::max(fWidth, fHeight); }
iRect()309 SkIRect iRect() const { return SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); }
rect()310 SkRect rect() const { return SkRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); }
glyphRect()311 SkGlyphRect glyphRect() const {
312 return {fLeft, fTop,
313 SkTo<int16_t>(fLeft + fWidth), SkTo<int16_t>(fTop + fHeight)};
314 }
left()315 int left() const { return fLeft; }
top()316 int top() const { return fTop; }
width()317 int width() const { return fWidth; }
height()318 int height() const { return fHeight; }
isEmpty()319 bool isEmpty() const {
320 // fHeight == 0 -> fWidth == 0;
321 SkASSERT(fHeight != 0 || fWidth == 0);
322 return fWidth == 0;
323 }
imageTooLarge()324 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
409