Debian Codegraph @

include/core/SkFont.h

raw
1 /*
2 * Copyright 2014 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 #ifndef SkFont_DEFINED
9 #define SkFont_DEFINED
10
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPoint.h" // IWYU pragma: keep (for unspanned apis)
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSpan.h"
17 #include "include/core/SkTypeface.h"
18 #include "include/core/SkTypes.h"
19 #include "include/private/base/SkTo.h"
20 #include "include/private/base/SkTypeTraits.h"
21
22 #include <cstddef>
23 #include <cstdint>
24 #include <optional>
25 #include <vector>
26
27 class SkMatrix;
28 class SkPaint;
29 enum class SkFontHinting;
30 enum class SkTextEncoding;
31 struct SkFontMetrics;
32
33 namespace skcpu { class GlyphRunListPainter; }
34
35 /** \class SkFont
36 SkFont controls options applied when drawing and measuring text.
37 */
38 class SK_API SkFont {
39 public:
40 /** Whether edge pixels draw opaque or with partial transparency.
41 */
42 enum class Edging {
43 kAlias, //!< no transparent pixels on glyph edges
44 kAntiAlias, //!< may have transparent pixels on glyph edges
45 kSubpixelAntiAlias, //!< glyph positioned in pixel using transparency
46 };
47
48 /** Constructs SkFont with default values.
49
50 @return default initialized SkFont
51 */
52 SkFont();
53
54 /** Constructs SkFont with default values with SkTypeface and size.
55
56 @param typeface font and style used to draw and measure text
57 @param size EM size in local coordinate units
58 @return initialized SkFont
59 */
60 SkFont(sk_sp<SkTypeface> typeface, SkScalar size);
61
62 /** Constructs SkFont with default values with SkTypeface.
63
64 @param typeface font and style used to draw and measure text
65 @return initialized SkFont
66 */
67 explicit SkFont(sk_sp<SkTypeface> typeface);
68
69
70 /** Constructs SkFont with default values with SkTypeface and size in points,
71 horizontal scale, and horizontal skew. Horizontal scale emulates condensed
72 and expanded fonts. Horizontal skew emulates oblique fonts.
73
74 @param typeface font and style used to draw and measure text
75 @param size EM size in local coordinate units
76 @param scaleX text horizontal scale
77 @param skewX additional shear on x-axis relative to y-axis
78 @return initialized SkFont
79 */
80 SkFont(sk_sp<SkTypeface> typeface, SkScalar size, SkScalar scaleX, SkScalar skewX);
81
82
83 /** Compares SkFont and font, and returns true if they are equivalent.
84 May return false if SkTypeface has identical contents but different pointers.
85
86 @param font font to compare
87 @return true if SkFont pair are equivalent
88 */
89 bool operator==(const SkFont& font) const;
90
91 /** Compares SkFont and font, and returns true if they are not equivalent.
92 May return true if SkTypeface has identical contents but different pointers.
93
94 @param font font to compare
95 @return true if SkFont pair are not equivalent
96 */
97 bool operator!=(const SkFont& font) const { return !(*this == font); }
98
99 /** If true, instructs the font manager to always hint glyphs.
100 Returned value is only meaningful if platform uses FreeType as the font manager.
101
102 @return true if all glyphs are hinted
103 */
104 bool isForceAutoHinting() const { return SkToBool(fFlags & kForceAutoHinting_PrivFlag); }
105
106 /** Returns true if font engine may return glyphs from font bitmaps instead of from outlines.
107
108 @return true if glyphs may be font bitmaps
109 */
110 bool isEmbeddedBitmaps() const { return SkToBool(fFlags & kEmbeddedBitmaps_PrivFlag); }
111
112 /** Returns true if glyphs may be drawn at sub-pixel offsets.
113
114 @return true if glyphs may be drawn at sub-pixel offsets.
115 */
116 bool isSubpixel() const { return SkToBool(fFlags & kSubpixel_PrivFlag); }
117
118 /** Returns true if font and glyph metrics are requested to be linearly scalable.
119
120 @return true if font and glyph metrics are requested to be linearly scalable.
121 */
122 bool isLinearMetrics() const { return SkToBool(fFlags & kLinearMetrics_PrivFlag); }
123
124 /** Returns true if bold is approximated by increasing the stroke width when creating glyph
125 bitmaps from outlines.
126
127 @return bold is approximated through stroke width
128 */
129 bool isEmbolden() const { return SkToBool(fFlags & kEmbolden_PrivFlag); }
130
131 /** Returns true if baselines will be snapped to pixel positions when the current transformation
132 matrix is axis aligned.
133
134 @return baselines may be snapped to pixels
135 */
136 bool isBaselineSnap() const { return SkToBool(fFlags & kBaselineSnap_PrivFlag); }
137
138 /** Sets whether to always hint glyphs.
139 If forceAutoHinting is set, instructs the font manager to always hint glyphs.
140
141 Only affects platforms that use FreeType as the font manager.
142
143 @param forceAutoHinting setting to always hint glyphs
144 */
145 void setForceAutoHinting(bool forceAutoHinting);
146
147 /** Requests, but does not require, to use bitmaps in fonts instead of outlines.
148
149 @param embeddedBitmaps setting to use bitmaps in fonts
150 */
151 void setEmbeddedBitmaps(bool embeddedBitmaps);
152
153 /** Requests, but does not require, that glyphs respect sub-pixel positioning.
154
155 @param subpixel setting for sub-pixel positioning
156 */
157 void setSubpixel(bool subpixel);
158
159 /** Requests, but does not require, linearly scalable font and glyph metrics.
160
161 For outline fonts 'true' means font and glyph metrics should ignore hinting and rounding.
162 Note that some bitmap formats may not be able to scale linearly and will ignore this flag.
163
164 @param linearMetrics setting for linearly scalable font and glyph metrics.
165 */
166 void setLinearMetrics(bool linearMetrics);
167
168 /** Increases stroke width when creating glyph bitmaps to approximate a bold typeface.
169
170 @param embolden setting for bold approximation
171 */
172 void setEmbolden(bool embolden);
173
174 /** Requests that baselines be snapped to pixels when the current transformation matrix is axis
175 aligned.
176
177 @param baselineSnap setting for baseline snapping to pixels
178 */
179 void setBaselineSnap(bool baselineSnap);
180
181 /** Whether edge pixels draw opaque or with partial transparency.
182 */
183 Edging getEdging() const { return (Edging)fEdging; }
184
185 /** Requests, but does not require, that edge pixels draw opaque or with
186 partial transparency.
187 */
188 void setEdging(Edging edging);
189
190 /** Sets level of glyph outline adjustment.
191 Does not check for valid values of hintingLevel.
192 */
193 void setHinting(SkFontHinting hintingLevel);
194
195 /** Returns level of glyph outline adjustment.
196 */
197 SkFontHinting getHinting() const { return (SkFontHinting)fHinting; }
198
199 /** Returns a font with the same attributes of this font, but with the specified size.
200 Returns nullptr if size is less than zero, infinite, or NaN.
201
202 @param size EM size in local coordinate units
203 @return initialized SkFont
204 */
205 SkFont makeWithSize(SkScalar size) const;
206
207 /** Does not alter SkTypeface SkRefCnt.
208
209 @return non-null SkTypeface
210 */
211 SkTypeface* getTypeface() const {
212 SkASSERT(fTypeface);
213 return fTypeface.get();
214 }
215
216 /** Return EM size in local coordinate units.
217 See https://skia.org/docs/user/coordinates/#local-coordinates .
218
219 @return EM size in local coordinate units
220 */
221 SkScalar getSize() const { return fSize; }
222
223 /** Returns text scale on x-axis.
224 Default value is 1.
225
226 @return text horizontal scale
227 */
228 SkScalar getScaleX() const { return fScaleX; }
229
230 /** Returns text skew on x-axis.
231 Default value is zero.
232
233 @return additional shear on x-axis relative to y-axis
234 */
235 SkScalar getSkewX() const { return fSkewX; }
236
237 /** Increases SkTypeface SkRefCnt by one.
238
239 @return A non-null SkTypeface.
240 */
241 sk_sp<SkTypeface> refTypeface() const {
242 SkASSERT(fTypeface);
243 return fTypeface;
244 }
245
246 /** Sets SkTypeface to typeface, decreasing SkRefCnt of the previous SkTypeface.
247 Pass nullptr to clear SkTypeface and use an empty typeface (which draws nothing).
248 Increments tf SkRefCnt by one.
249
250 @param tf font and style used to draw text
251 */
252 void setTypeface(sk_sp<SkTypeface> tf);
253
254 /** Sets the EM size in local coordinate units.
255 See https://skia.org/docs/user/coordinates/#local-coordinates .
256 Has no effect if textSize is not greater than or equal to zero.
257
258 @param textSize EM size in local coordinate units
259 */
260 void setSize(SkScalar textSize);
261
262 /** Sets text scale on x-axis.
263 Default value is 1.
264
265 @param scaleX text horizontal scale
266 */
267 void setScaleX(SkScalar scaleX);
268
269 /** Sets text skew on x-axis.
270 Default value is zero.
271
272 @param skewX additional shear on x-axis relative to y-axis
273 */
274 void setSkewX(SkScalar skewX);
275
276 /** Converts text into glyph indices.
277 Returns the number of glyph indices represented by text.
278 SkTextEncoding specifies how text represents characters or glyphs.
279 glyphs may be empty, to compute the glyph count.
280
281 Does not check text for valid character codes or valid glyph indices.
282
283 If byteLength equals zero, returns zero.
284 If byteLength includes a partial character, the partial character is ignored.
285
286 If encoding is SkTextEncoding::kUTF8 and text contains an invalid UTF-8 sequence,
287 zero is returned.
288
289 When encoding is SkTextEncoding::kUTF8, SkTextEncoding::kUTF16, or
290 SkTextEncoding::kUTF32; then each Unicode codepoint is mapped to a
291 single glyph. This function uses the default character-to-glyph
292 mapping from the SkTypeface and maps characters not found in the
293 SkTypeface to zero.
294
295 If glyphs.size() is not sufficient to store all the glyphs, no glyphs are copied.
296 The total glyph count is returned for subsequent buffer reallocation.
297
298 @param text character storage encoded with SkTextEncoding
299 @param byteLength length of character storage in bytes
300 @param glyphs storage for glyph indices; may be empty
301 @return number of glyphs represented by text of length byteLength
302 */
303 size_t textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
304 SkSpan<SkGlyphID> glyphs) const;
305
306 /** Returns glyph index for Unicode character.
307
308 If the character is not supported by the SkTypeface, returns 0.
309
310 @param uni Unicode character
311 @return glyph index
312 */
313 SkGlyphID unicharToGlyph(SkUnichar uni) const;
314
315 void unicharsToGlyphs(SkSpan<const SkUnichar> src, SkSpan<SkGlyphID> dst) const;
316
317 /** Returns number of glyphs represented by text.
318
319 If encoding is SkTextEncoding::kUTF8, SkTextEncoding::kUTF16, or
320 SkTextEncoding::kUTF32; then each Unicode codepoint is mapped to a
321 single glyph.
322
323 @param text character storage encoded with SkTextEncoding
324 @param byteLength length of character storage in bytes
325 @return number of glyphs represented by text of length byteLength
326 */
327 size_t countText(const void* text, size_t byteLength, SkTextEncoding encoding) const {
328 return this->textToGlyphs(text, byteLength, encoding, {});
329 }
330
331 /** Returns the advance width of text.
332 The advance is the normal distance to move before drawing additional text.
333 Returns the bounding box of text if bounds is not nullptr.
334
335 @param text character storage encoded with SkTextEncoding
336 @param byteLength length of character storage in bytes
337 @param bounds returns bounding box relative to (0, 0) if not nullptr
338 @return the sum of the default advance widths
339 */
340 SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
341 SkRect* bounds = nullptr) const {
342 return this->measureText(text, byteLength, encoding, bounds, nullptr);
343 }
344
345 /** Returns the advance width of text.
346 The advance is the normal distance to move before drawing additional text.
347 Returns the bounding box of text if bounds is not nullptr. The paint
348 stroke settings, mask filter, or path effect may modify the bounds.
349
350 @param text character storage encoded with SkTextEncoding
351 @param byteLength length of character storage in bytes
352 @param bounds returns bounding box relative to (0, 0) if not nullptr
353 @param paint optional; may be nullptr
354 @return the sum of the default advance widths
355 */
356 SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
357 SkRect* bounds, const SkPaint* paint) const;
358
359 /** Retrieves the advance and bounds for each glyph in glyphs.
360 widths receives min(widths.size(), glyphs.size()) values.
361 bounds receives min(bounds.size(), glyphs.size()) values.
362
363 @param glyphs array of glyph indices to be measured
364 @param widths returns text advances for each glyph
365 @param bounds returns bounds for each glyph relative to (0, 0)
366 @param paint optional, specifies stroking, SkPathEffect and SkMaskFilter
367 */
368 void getWidthsBounds(SkSpan<const SkGlyphID> glyphs, SkSpan<SkScalar> widths, SkSpan<SkRect> bounds,
369 const SkPaint* paint) const;
370
371 /** Retrieves the advance and bounds for each glyph in glyphs.
372 widths receives min(widths.size(), glyphs.size()) values.
373
374 @param glyphs array of glyph indices to be measured
375 @param widths returns text advances for each glyph
376 */
377 void getWidths(SkSpan<const SkGlyphID> glyphs, SkSpan<SkScalar> widths) const {
378 this->getWidthsBounds(glyphs, widths, {}, nullptr);
379 }
380 SkScalar getWidth(SkGlyphID glyph) const {
381 SkScalar width;
382 this->getWidthsBounds({&glyph, 1}, {&width, 1}, {}, nullptr);
383 return width;
384 }
385
386 /** Retrieves the bounds for each glyph in glyphs.
387 bounds receives min(bounds.size(), glyphs.size()) values.
388 If paint is not nullptr, its stroking, SkPathEffect, and SkMaskFilter fields are respected.
389
390 @param glyphs array of glyph indices to be measured
391 @param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
392 @param paint optional, specifies stroking, SkPathEffect, and SkMaskFilter
393 */
394 void getBounds(SkSpan<const SkGlyphID> glyphs, SkSpan<SkRect> bounds,
395 const SkPaint* paint) const {
396 this->getWidthsBounds(glyphs, {}, bounds, paint);
397 }
398 SkRect getBounds(SkGlyphID glyph, const SkPaint* paint) const {
399 SkRect bounds;
400 this->getBounds({&glyph, 1}, {&bounds, 1}, paint);
401 return bounds;
402 }
403
404 /** Retrieves the positions for each glyph, beginning at the specified origin.
405 pos receives min(pos.size(), glyphs.size()) values.
406
407 @param glyphs array of glyph indices to be positioned
408 @param pos returns glyphs positions
409 @param origin location of the first glyph. Defaults to {0, 0}.
410 */
411 void getPos(SkSpan<const SkGlyphID> glyphs, SkSpan<SkPoint> pos, SkPoint origin = {0, 0}) const;
412
413 /** Retrieves the x-positions for each glyph, beginning at the specified origin.
414 xpos receives min(xpos.size(), glyphs.size()) values.
415
416 @param glyphs array of glyph indices to be positioned
417 @param xpos returns glyphs x-positions
418 @param origin x-position of the first glyph. Defaults to 0.
419 */
420 void getXPos(SkSpan<const SkGlyphID> glyphs, SkSpan<SkScalar> xpos, SkScalar origin = 0) const;
421
422 /** Returns intervals [start, end] describing lines parallel to the advance that intersect
423 * with the glyphs.
424 *
425 * @param glyphs the glyphs to intersect
426 * @param pos the position of each glyph
427 * @param top the top of the line intersecting
428 * @param bottom the bottom of the line intersecting
429 @return array of pairs of x values [start, end]. May be empty.
430 */
431 std::vector<SkScalar> getIntercepts(SkSpan<const SkGlyphID> glyphs,
432 SkSpan<const SkPoint> pos,
433 SkScalar top, SkScalar bottom,
434 const SkPaint* = nullptr) const;
435
436 /*
437 * If the specified glyph can be represented as a path, return its path.
438 * If it is not (e.g. it is represented with a bitmap) return {}.
439 *
440 * Note: an 'empty' glyph (e.g. what a space " " character might map to) can return
441 * a path, but that path may have zero contours.
442 */
443 std::optional<SkPath> getPath(SkGlyphID glyphID) const;
444
445 /** Returns path corresponding to glyph array.
446
447 @param glyphIDs array of glyph indices
448 @param glyphPathProc function returning one glyph description as path
449 @param ctx function context
450 */
451 void getPaths(SkSpan<const SkGlyphID> glyphIDs,
452 void (*glyphPathProc)(const SkPath* pathOrNull, const SkMatrix& mx, void* ctx),
453 void* ctx) const;
454
455 /** Returns SkFontMetrics associated with SkTypeface.
456 The return value is the recommended spacing between lines: the sum of metrics
457 descent, ascent, and leading.
458 If metrics is not nullptr, SkFontMetrics is copied to metrics.
459 Results are scaled by text size but does not take into account
460 dimensions required by text scale, text skew, fake bold,
461 style stroke, and SkPathEffect.
462
463 @param metrics storage for SkFontMetrics; may be nullptr
464 @return recommended spacing between lines
465 */
466 SkScalar getMetrics(SkFontMetrics* metrics) const;
467
468 /** Returns the recommended spacing between lines: the sum of metrics
469 descent, ascent, and leading.
470 Result is scaled by text size but does not take into account
471 dimensions required by stroking and SkPathEffect.
472 Returns the same result as getMetrics().
473
474 @return recommended spacing between lines
475 */
476 SkScalar getSpacing() const { return this->getMetrics(nullptr); }
477
478 /** Dumps fields of the font to SkDebugf. May change its output over time, so clients should
479 * not rely on this for anything specific. Used to aid in debugging.
480 */
481 void dump() const;
482
483 using sk_is_trivially_relocatable = std::true_type;
484
485 #ifdef SK_SUPPORT_UNSPANNED_APIS
486 int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
487 SkGlyphID glyphs[], int maxGlyphCount) const {
488 return (int)this->textToGlyphs(text, byteLength, encoding, {glyphs, maxGlyphCount});
489 }
490 void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
491 this->unicharsToGlyphs({uni, count}, {glyphs, count});
492 }
493
494 void getPos(const SkGlyphID glyphs[], int count, SkPoint pos[], SkPoint origin = {0, 0}) const {
495 this->getPos({glyphs, count}, {pos, count}, origin);
496 }
497 void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin = 0) const {
498 this->getXPos({glyphs, count}, {xpos, count}, origin);
499 }
500 void getPaths(const SkGlyphID glyphIDs[], int count,
501 void (*glyphPathProc)(const SkPath* pathOrNull, const SkMatrix& mx, void* ctx),
502 void* ctx) const {
503 this->getPaths({glyphIDs, count}, glyphPathProc, ctx);
504 }
505 void getWidthsBounds(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[],
506 const SkPaint* paint) const {
507 const auto nw = widths ? count : 0;
508 const auto nb = bounds ? count : 0;
509 this->getWidthsBounds({glyphs, count}, {widths, nw}, {bounds, nb}, paint);
510 }
511 void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[]) const {
512 const auto nw = widths ? count : 0;
513 const auto nb = bounds ? count : 0;
514 this->getWidthsBounds({glyphs, count}, {widths, nw}, {bounds, nb}, nullptr);
515 }
516 void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], std::nullptr_t) const {
517 this->getWidthsBounds({glyphs, count}, {widths, count}, {}, nullptr);
518 }
519 void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[]) const {
520 this->getWidthsBounds({glyphs, count}, {widths, count}, {}, nullptr);
521 }
522 void getBounds(const SkGlyphID glyphs[], int count, SkRect bounds[],
523 const SkPaint* paint) const {
524 this->getWidthsBounds({glyphs, count}, {}, {bounds, count}, paint);
525 }
526
527 std::vector<SkScalar> getIntercepts(const SkGlyphID glyphs[], int count, const SkPoint pos[],
528 SkScalar top, SkScalar bottom,
529 const SkPaint* paint = nullptr) const {
530 return this->getIntercepts({glyphs, count}, {pos, count}, top, bottom, paint);
531 }
532 #endif
533
534
535 private:
536 enum PrivFlags {
537 kForceAutoHinting_PrivFlag = 1 << 0,
538 kEmbeddedBitmaps_PrivFlag = 1 << 1,
539 kSubpixel_PrivFlag = 1 << 2,
540 kLinearMetrics_PrivFlag = 1 << 3,
541 kEmbolden_PrivFlag = 1 << 4,
542 kBaselineSnap_PrivFlag = 1 << 5,
543 };
544
545 static constexpr unsigned kAllFlags = kForceAutoHinting_PrivFlag
546 | kEmbeddedBitmaps_PrivFlag
547 | kSubpixel_PrivFlag
548 | kLinearMetrics_PrivFlag
549 | kEmbolden_PrivFlag
550 | kBaselineSnap_PrivFlag;
551
552 sk_sp<SkTypeface> fTypeface;
553 SkScalar fSize;
554 SkScalar fScaleX;
555 SkScalar fSkewX;
556 uint8_t fFlags;
557 uint8_t fEdging;
558 uint8_t fHinting;
559
560 static_assert(::sk_is_trivially_relocatable<decltype(fTypeface)>::value);
561
562 SkScalar setupForAsPaths(SkPaint*);
563 bool hasSomeAntiAliasing() const;
564
565 friend class SkFontPriv;
566 friend class skcpu::GlyphRunListPainter;
567 friend class SkStrikeSpec;
568 friend class SkRemoteGlyphCacheTest;
569 };
570
571 #endif