Skia
2D Graphics Library
SkFont.h
Go to the documentation of this file.
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/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkTo.h"
17 #include "include/private/base/SkTypeTraits.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <type_traits>
22 #include <utility>
23 #include <vector>
24 
25 class SkMatrix;
26 class SkPaint;
27 class SkPath;
28 enum class SkFontHinting;
29 enum class SkTextEncoding;
30 struct SkFontMetrics;
31 struct SkPoint;
32 
36 class SK_API SkFont {
37 public:
40  enum class Edging {
41  kAlias,
42  kAntiAlias,
43  kSubpixelAntiAlias,
44  };
45 
50  SkFont();
51 
59 
65  explicit SkFont(sk_sp<SkTypeface> typeface);
66 
67 
78  SkFont(sk_sp<SkTypeface> typeface, SkScalar size, SkScalar scaleX, SkScalar skewX);
79 
80 
87  bool operator==(const SkFont& font) const;
88 
95  bool operator!=(const SkFont& font) const { return !(*this == font); }
96 
102  bool isForceAutoHinting() const { return SkToBool(fFlags & kForceAutoHinting_PrivFlag); }
103 
108  bool isEmbeddedBitmaps() const { return SkToBool(fFlags & kEmbeddedBitmaps_PrivFlag); }
109 
114  bool isSubpixel() const { return SkToBool(fFlags & kSubpixel_PrivFlag); }
115 
120  bool isLinearMetrics() const { return SkToBool(fFlags & kLinearMetrics_PrivFlag); }
121 
127  bool isEmbolden() const { return SkToBool(fFlags & kEmbolden_PrivFlag); }
128 
134  bool isBaselineSnap() const { return SkToBool(fFlags & kBaselineSnap_PrivFlag); }
135 
143  void setForceAutoHinting(bool forceAutoHinting);
144 
149  void setEmbeddedBitmaps(bool embeddedBitmaps);
150 
155  void setSubpixel(bool subpixel);
156 
164  void setLinearMetrics(bool linearMetrics);
165 
170  void setEmbolden(bool embolden);
171 
177  void setBaselineSnap(bool baselineSnap);
178 
181  Edging getEdging() const { return (Edging)fEdging; }
182 
186  void setEdging(Edging edging);
187 
191  void setHinting(SkFontHinting hintingLevel);
192 
195  SkFontHinting getHinting() const { return (SkFontHinting)fHinting; }
196 
204 
210  SkTypeface* getTypeface() const {return fTypeface.get(); }
211 
216  SkScalar getSize() const { return fSize; }
217 
223  SkScalar getScaleX() const { return fScaleX; }
224 
230  SkScalar getSkewX() const { return fSkewX; }
231 
236  sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
237 
244  void setTypeface(sk_sp<SkTypeface> tf) { fTypeface = std::move(tf); }
245 
251  void setSize(SkScalar textSize);
252 
258  void setScaleX(SkScalar scaleX);
259 
265  void setSkewX(SkScalar skewX);
266 
295  int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
296  SkGlyphID glyphs[], int maxGlyphCount) const;
297 
306 
307  void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const;
308 
319  int countText(const void* text, size_t byteLength, SkTextEncoding encoding) const {
320  return this->textToGlyphs(text, byteLength, encoding, nullptr, 0);
321  }
322 
332  SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
333  SkRect* bounds = nullptr) const {
334  return this->measureText(text, byteLength, encoding, bounds, nullptr);
335  }
336 
348  SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
349  SkRect* bounds, const SkPaint* paint) const;
350 
362  void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[]) const {
363  this->getWidthsBounds(glyphs, count, widths, bounds, nullptr);
364  }
365 
366  // DEPRECATED
367  void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], std::nullptr_t) const {
368  this->getWidths(glyphs, count, widths);
369  }
370 
380  void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[]) const {
381  this->getWidthsBounds(glyphs, count, widths, nullptr, nullptr);
382  }
383 
395  void getWidthsBounds(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[],
396  const SkPaint* paint) const;
397 
398 
408  void getBounds(const SkGlyphID glyphs[], int count, SkRect bounds[],
409  const SkPaint* paint) const {
410  this->getWidthsBounds(glyphs, count, nullptr, bounds, paint);
411  }
412 
421  void getPos(const SkGlyphID glyphs[], int count, SkPoint pos[], SkPoint origin = {0, 0}) const;
422 
431  void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin = 0) const;
432 
443  std::vector<SkScalar> getIntercepts(const SkGlyphID glyphs[], int count, const SkPoint pos[],
444  SkScalar top, SkScalar bottom,
445  const SkPaint* = nullptr) const;
446 
456  bool getPath(SkGlyphID glyphID, SkPath* path) const;
457 
465  void getPaths(const SkGlyphID glyphIDs[], int count,
466  void (*glyphPathProc)(const SkPath* pathOrNull, const SkMatrix& mx, void* ctx),
467  void* ctx) const;
468 
481 
490  SkScalar getSpacing() const { return this->getMetrics(nullptr); }
491 
495  void dump() const;
496 
497  using sk_is_trivially_relocatable = std::true_type;
498 
499 private:
500  enum PrivFlags {
501  kForceAutoHinting_PrivFlag = 1 << 0,
502  kEmbeddedBitmaps_PrivFlag = 1 << 1,
503  kSubpixel_PrivFlag = 1 << 2,
504  kLinearMetrics_PrivFlag = 1 << 3,
505  kEmbolden_PrivFlag = 1 << 4,
506  kBaselineSnap_PrivFlag = 1 << 5,
507  };
508 
509  static constexpr unsigned kAllFlags = kForceAutoHinting_PrivFlag
510  | kEmbeddedBitmaps_PrivFlag
511  | kSubpixel_PrivFlag
512  | kLinearMetrics_PrivFlag
513  | kEmbolden_PrivFlag
514  | kBaselineSnap_PrivFlag;
515 
516  sk_sp<SkTypeface> fTypeface;
517  SkScalar fSize;
518  SkScalar fScaleX;
519  SkScalar fSkewX;
520  uint8_t fFlags;
521  uint8_t fEdging;
522  uint8_t fHinting;
523 
524  static_assert(::sk_is_trivially_relocatable<decltype(fTypeface)>::value);
525 
526  SkScalar setupForAsPaths(SkPaint*);
527  bool hasSomeAntiAliasing() const;
528 
529  friend class SkFontPriv;
530  friend class SkGlyphRunListPainterCPU;
531  friend class SkStrikeSpec;
532  friend class SkRemoteGlyphCacheTest;
533 
534  sk_sp<SkTypeface> refTypefaceOrDefault() const;
535  SkTypeface* getTypefaceOrDefault() const;
536 };
537 
538 #endif
SkFontHinting
Definition: SkFontTypes.h:18
SkTextEncoding
Definition: SkFontTypes.h:11
float SkScalar
Definition: SkScalar.h:14
int32_t SkUnichar
32 bit integer to hold a unicode value
Definition: SkTypes.h:167
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:171
SkFont controls options applied when drawing and measuring text.
Definition: SkFont.h:36
bool isBaselineSnap() const
Returns true if baselines will be snapped to pixel positions when the current transformation matrix i...
Definition: SkFont.h:134
void getBounds(const SkGlyphID glyphs[], int count, SkRect bounds[], const SkPaint *paint) const
Retrieves the bounds for each glyph in glyphs.
Definition: SkFont.h:408
void setSubpixel(bool subpixel)
Requests, but does not require, that glyphs respect sub-pixel positioning.
bool isEmbolden() const
Returns true if bold is approximated by increasing the stroke width when creating glyph bitmaps from ...
Definition: SkFont.h:127
void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin=0) const
Retrieves the x-positions for each glyph, beginning at the specified origin.
bool getPath(SkGlyphID glyphID, SkPath *path) const
Modifies path to be the outline of the glyph.
bool operator!=(const SkFont &font) const
Compares SkFont and font, and returns true if they are not equivalent.
Definition: SkFont.h:95
void setScaleX(SkScalar scaleX)
Sets text scale on x-axis.
bool operator==(const SkFont &font) const
Compares SkFont and font, and returns true if they are equivalent.
void setLinearMetrics(bool linearMetrics)
Requests, but does not require, linearly scalable font and glyph metrics.
Edging getEdging() const
Whether edge pixels draw opaque or with partial transparency.
Definition: SkFont.h:181
void setTypeface(sk_sp< SkTypeface > tf)
Sets SkTypeface to typeface, decreasing SkRefCnt of the previous SkTypeface.
Definition: SkFont.h:244
void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[]) const
DEPRECATED Retrieves the advance and bounds for each glyph in glyphs.
Definition: SkFont.h:362
SkScalar getSize() const
Returns text size in points.
Definition: SkFont.h:216
SkScalar measureText(const void *text, size_t byteLength, SkTextEncoding encoding, SkRect *bounds=nullptr) const
Returns the advance width of text.
Definition: SkFont.h:332
void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], std::nullptr_t) const
Definition: SkFont.h:367
SkFont(sk_sp< SkTypeface > typeface, SkScalar size, SkScalar scaleX, SkScalar skewX)
Constructs SkFont with default values with SkTypeface and size in points, horizontal scale,...
SkTypeface * getTypeface() const
Returns SkTypeface if set, or nullptr.
Definition: SkFont.h:210
void setBaselineSnap(bool baselineSnap)
Requests that baselines be snapped to pixels when the current transformation matrix is axis aligned.
void getPos(const SkGlyphID glyphs[], int count, SkPoint pos[], SkPoint origin={0, 0}) const
Retrieves the positions for each glyph, beginning at the specified origin.
void setEdging(Edging edging)
Requests, but does not require, that edge pixels draw opaque or with partial transparency.
SkFont(sk_sp< SkTypeface > typeface)
Constructs SkFont with default values with SkTypeface.
SkFontHinting getHinting() const
Returns level of glyph outline adjustment.
Definition: SkFont.h:195
void setSize(SkScalar textSize)
Sets text size in points.
bool isForceAutoHinting() const
If true, instructs the font manager to always hint glyphs.
Definition: SkFont.h:102
bool isLinearMetrics() const
Returns true if font and glyph metrics are requested to be linearly scalable.
Definition: SkFont.h:120
void dump() const
Dumps fields of the font to SkDebugf.
bool isEmbeddedBitmaps() const
Returns true if font engine may return glyphs from font bitmaps instead of from outlines.
Definition: SkFont.h:108
void getPaths(const SkGlyphID glyphIDs[], int count, void(*glyphPathProc)(const SkPath *pathOrNull, const SkMatrix &mx, void *ctx), void *ctx) const
Returns path corresponding to glyph array.
std::vector< SkScalar > getIntercepts(const SkGlyphID glyphs[], int count, const SkPoint pos[], SkScalar top, SkScalar bottom, const SkPaint *=nullptr) const
Returns intervals [start, end] describing lines parallel to the advance that intersect with the glyph...
SkFont(sk_sp< SkTypeface > typeface, SkScalar size)
Constructs SkFont with default values with SkTypeface and size in points.
sk_sp< SkTypeface > refTypeface() const
Increases SkTypeface SkRefCnt by one.
Definition: SkFont.h:236
int countText(const void *text, size_t byteLength, SkTextEncoding encoding) const
Returns number of glyphs represented by text.
Definition: SkFont.h:319
SkScalar getScaleX() const
Returns text scale on x-axis.
Definition: SkFont.h:223
SkScalar getSkewX() const
Returns text skew on x-axis.
Definition: SkFont.h:230
void setSkewX(SkScalar skewX)
Sets text skew on x-axis.
bool isSubpixel() const
Returns true if glyphs may be drawn at sub-pixel offsets.
Definition: SkFont.h:114
void setForceAutoHinting(bool forceAutoHinting)
Sets whether to always hint glyphs.
SkGlyphID unicharToGlyph(SkUnichar uni) const
Returns glyph index for Unicode character.
SkScalar getMetrics(SkFontMetrics *metrics) const
Returns SkFontMetrics associated with SkTypeface.
SkScalar measureText(const void *text, size_t byteLength, SkTextEncoding encoding, SkRect *bounds, const SkPaint *paint) const
Returns the advance width of text.
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
Converts text into glyph indices.
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
SkFont makeWithSize(SkScalar size) const
Returns a font with the same attributes of this font, but with the specified size.
void setHinting(SkFontHinting hintingLevel)
Sets level of glyph outline adjustment.
Edging
Whether edge pixels draw opaque or with partial transparency.
Definition: SkFont.h:40
SkScalar getSpacing() const
Returns the recommended spacing between lines: the sum of metrics descent, ascent,...
Definition: SkFont.h:490
void setEmbolden(bool embolden)
Increases stroke width when creating glyph bitmaps to approximate a bold typeface.
std::true_type sk_is_trivially_relocatable
Definition: SkFont.h:497
SkFont()
Constructs SkFont with default values.
void setEmbeddedBitmaps(bool embeddedBitmaps)
Requests, but does not require, to use bitmaps in fonts instead of outlines.
void getWidthsBounds(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[], const SkPaint *paint) const
Retrieves the advance and bounds for each glyph in glyphs.
void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[]) const
Retrieves the advance and bounds for each glyph in glyphs.
Definition: SkFont.h:380
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
SkPath contain geometry.
Definition: SkPath.h:58
The SkTypeface class specifies the typeface and intrinsic style of a font.
Definition: SkTypeface.h:52
The metrics of an SkFont.
Definition: SkFontMetrics.h:18
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582