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 
12 #include "include/core/SkScalar.h"
14 
15 class SkMatrix;
16 class SkPaint;
17 class SkPath;
18 struct SkFontMetrics;
19 
23 class SK_API SkFont {
24 public:
27  enum class Edging {
28  kAlias,
29  kAntiAlias,
30  kSubpixelAntiAlias,
31  };
32 
37  SkFont();
38 
46 
52  explicit SkFont(sk_sp<SkTypeface> typeface);
53 
54 
65  SkFont(sk_sp<SkTypeface> typeface, SkScalar size, SkScalar scaleX, SkScalar skewX);
66 
67 
74  bool operator==(const SkFont& font) const;
75 
82  bool operator!=(const SkFont& font) const { return !(*this == font); }
83 
89  bool isForceAutoHinting() const { return SkToBool(fFlags & kForceAutoHinting_PrivFlag); }
90 
95  bool isEmbeddedBitmaps() const { return SkToBool(fFlags & kEmbeddedBitmaps_PrivFlag); }
96 
101  bool isSubpixel() const { return SkToBool(fFlags & kSubpixel_PrivFlag); }
102 
107  bool isLinearMetrics() const { return SkToBool(fFlags & kLinearMetrics_PrivFlag); }
108 
114  bool isEmbolden() const { return SkToBool(fFlags & kEmbolden_PrivFlag); }
115 
121  bool isBaselineSnap() const { return SkToBool(fFlags & kBaselineSnap_PrivFlag); }
122 
130  void setForceAutoHinting(bool forceAutoHinting);
131 
136  void setEmbeddedBitmaps(bool embeddedBitmaps);
137 
142  void setSubpixel(bool subpixel);
143 
151  void setLinearMetrics(bool linearMetrics);
152 
157  void setEmbolden(bool embolden);
158 
164  void setBaselineSnap(bool baselineSnap);
165 
168  Edging getEdging() const { return (Edging)fEdging; }
169 
173  void setEdging(Edging edging);
174 
178  void setHinting(SkFontHinting hintingLevel);
179 
182  SkFontHinting getHinting() const { return (SkFontHinting)fHinting; }
183 
191 
197  SkTypeface* getTypeface() const {return fTypeface.get(); }
198 
206 
211  SkScalar getSize() const { return fSize; }
212 
218  SkScalar getScaleX() const { return fScaleX; }
219 
225  SkScalar getSkewX() const { return fSkewX; }
226 
231  sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
232 
239 
246  void setTypeface(sk_sp<SkTypeface> tf) { fTypeface = tf; }
247 
253  void setSize(SkScalar textSize);
254 
260  void setScaleX(SkScalar scaleX);
261 
267  void setSkewX(SkScalar skewX);
268 
297  int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
298  SkGlyphID glyphs[], int maxGlyphCount) const;
299 
308 
309  void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const;
310 
321  int countText(const void* text, size_t byteLength, SkTextEncoding encoding) const {
322  return this->textToGlyphs(text, byteLength, encoding, nullptr, 0);
323  }
324 
334  SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
335  SkRect* bounds = nullptr) const {
336  return this->measureText(text, byteLength, encoding, bounds, nullptr);
337  }
338 
350  SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
351  SkRect* bounds, const SkPaint* paint) const;
352 
364  void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[]) const {
365  this->getWidthsBounds(glyphs, count, widths, bounds, nullptr);
366  }
367 
368  // DEPRECATED
369  void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], std::nullptr_t) const {
370  this->getWidths(glyphs, count, widths);
371  }
372 
382  void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[]) const {
383  this->getWidthsBounds(glyphs, count, widths, nullptr, nullptr);
384  }
385 
397  void getWidthsBounds(const SkGlyphID glyphs[], int count, SkScalar widths[], SkRect bounds[],
398  const SkPaint* paint) const;
399 
400 
410  void getBounds(const SkGlyphID glyphs[], int count, SkRect bounds[],
411  const SkPaint* paint) const {
412  this->getWidthsBounds(glyphs, count, nullptr, bounds, paint);
413  }
414 
423  void getPos(const SkGlyphID glyphs[], int count, SkPoint pos[], SkPoint origin = {0, 0}) const;
424 
433  void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin = 0) const;
434 
444  bool getPath(SkGlyphID glyphID, SkPath* path) const;
445 
453  void getPaths(const SkGlyphID glyphIDs[], int count,
454  void (*glyphPathProc)(const SkPath* pathOrNull, const SkMatrix& mx, void* ctx),
455  void* ctx) const;
456 
469 
478  SkScalar getSpacing() const { return this->getMetrics(nullptr); }
479 
483  void dump() const;
484 
485 private:
486  enum PrivFlags {
487  kForceAutoHinting_PrivFlag = 1 << 0,
488  kEmbeddedBitmaps_PrivFlag = 1 << 1,
489  kSubpixel_PrivFlag = 1 << 2,
490  kLinearMetrics_PrivFlag = 1 << 3,
491  kEmbolden_PrivFlag = 1 << 4,
492  kBaselineSnap_PrivFlag = 1 << 5,
493  };
494 
495  static constexpr unsigned kAllFlags = kForceAutoHinting_PrivFlag
496  | kEmbeddedBitmaps_PrivFlag
497  | kSubpixel_PrivFlag
498  | kLinearMetrics_PrivFlag
499  | kEmbolden_PrivFlag
500  | kBaselineSnap_PrivFlag;
501 
502  sk_sp<SkTypeface> fTypeface;
503  SkScalar fSize;
504  SkScalar fScaleX;
505  SkScalar fSkewX;
506  uint8_t fFlags;
507  uint8_t fEdging;
508  uint8_t fHinting;
509 
510  SkScalar setupForAsPaths(SkPaint*);
511  bool hasSomeAntiAliasing() const;
512 
513  friend class SkFontPriv;
514  friend class SkGlyphRunListPainter;
515  friend class SkTextBlobCacheDiffCanvas;
516  friend class SkStrikeSpec;
517 };
518 
519 #endif
SkFont::isLinearMetrics
bool isLinearMetrics() const
Returns true if font and glyph metrics are requested to be linearly scalable.
Definition: SkFont.h:107
SkFont::getBounds
void getBounds(const SkGlyphID glyphs[], int count, SkRect bounds[], const SkPaint *paint) const
Retrieves the bounds for each glyph in glyphs.
Definition: SkFont.h:410
SkGlyphID
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:556
SkFont::getSkewX
SkScalar getSkewX() const
Returns text skew on x-axis.
Definition: SkFont.h:225
SkFont::getPaths
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.
SkFont::getPos
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.
SkFont::unicharsToGlyphs
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
SkFont::textToGlyphs
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
Converts text into glyph indices.
SkPath
Definition: SkPath.h:44
SkFont::SkFont
SkFont(sk_sp< SkTypeface > typeface)
Constructs SkFont with default values with SkTypeface.
SkFont::setSize
void setSize(SkScalar textSize)
Sets text size in points.
SkFont::countText
int countText(const void *text, size_t byteLength, SkTextEncoding encoding) const
Returns number of glyphs represented by text.
Definition: SkFont.h:321
SkFont::isEmbolden
bool isEmbolden() const
Returns true if bold is approximated by increasing the stroke width when creating glyph bitmaps from ...
Definition: SkFont.h:114
SkFont::getSize
SkScalar getSize() const
Returns text size in points.
Definition: SkFont.h:211
SkFont::refTypeface
sk_sp< SkTypeface > refTypeface() const
Increases SkTypeface SkRefCnt by one.
Definition: SkFont.h:231
SkFont::isForceAutoHinting
bool isForceAutoHinting() const
If true, instructs the font manager to always hint glyphs.
Definition: SkFont.h:89
SkFont::setBaselineSnap
void setBaselineSnap(bool baselineSnap)
Requests that baselines be snapped to pixels when the current transformation matrix is axis aligned.
SkTypeface.h
SkTypeface
Definition: SkTypeface.h:45
SkFont::setSubpixel
void setSubpixel(bool subpixel)
Requests, but does not require, that glyphs respect sub-pixel positioning.
SkFont::getWidthsBounds
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.
SkFont::setSkewX
void setSkewX(SkScalar skewX)
Sets text skew on x-axis.
SkScalar.h
sk_sp< SkTypeface >
SkFont::SkFont
SkFont(sk_sp< SkTypeface > typeface, SkScalar size)
Constructs SkFont with default values with SkTypeface and size in points.
SkFontMetrics
Definition: SkFontMetrics.h:17
SkPoint
Definition: SkPoint.h:160
SkToBool
static constexpr bool SkToBool(const T &x)
Definition: SkTypes.h:497
SkFont::operator!=
bool operator!=(const SkFont &font) const
Compares SkFont and font, and returns true if they are not equivalent.
Definition: SkFont.h:82
SkFontTypes.h
SkFont::makeWithSize
SkFont makeWithSize(SkScalar size) const
Returns a font with the same attributes of this font, but with the specified size.
SkFont::isBaselineSnap
bool isBaselineSnap() const
Returns true if baselines will be snapped to pixel positions when the current transformation matrix i...
Definition: SkFont.h:121
SkPaint
Definition: SkPaint.h:39
SkFont::getWidths
void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[], std::nullptr_t) const
Definition: SkFont.h:369
SkFont::SkFont
SkFont(sk_sp< SkTypeface > typeface, SkScalar size, SkScalar scaleX, SkScalar skewX)
Constructs SkFont with default values with SkTypeface and size in points, horizontal scale,...
SK_API
#define SK_API
Definition: SkTypes.h:181
SkFont::getEdging
Edging getEdging() const
Whether edge pixels draw opaque or with partial transparency.
Definition: SkFont.h:168
SkFont::unicharToGlyph
SkGlyphID unicharToGlyph(SkUnichar uni) const
Returns glyph index for Unicode character.
SkFont::getWidths
void getWidths(const SkGlyphID glyphs[], int count, SkScalar widths[]) const
Retrieves the advance and bounds for each glyph in glyphs.
Definition: SkFont.h:382
SkFont::getPath
bool getPath(SkGlyphID glyphID, SkPath *path) const
Modifies path to be the outline of the glyph.
SkFont::getTypefaceOrDefault
SkTypeface * getTypefaceOrDefault() const
Returns SkTypeface if set, or the default typeface.
SkFont::getSpacing
SkScalar getSpacing() const
Returns the recommended spacing between lines: the sum of metrics descent, ascent,...
Definition: SkFont.h:478
SkFont::setEmbolden
void setEmbolden(bool embolden)
Increases stroke width when creating glyph bitmaps to approximate a bold typeface.
SkFont::getWidths
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:364
SkFont::getScaleX
SkScalar getScaleX() const
Returns text scale on x-axis.
Definition: SkFont.h:218
SkFont::measureText
SkScalar measureText(const void *text, size_t byteLength, SkTextEncoding encoding, SkRect *bounds, const SkPaint *paint) const
Returns the advance width of text.
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkUnichar
int32_t SkUnichar
32 bit integer to hold a unicode value
Definition: SkTypes.h:552
SkFont::Edging
Edging
Whether edge pixels draw opaque or with partial transparency.
Definition: SkFont.h:27
SkTextEncoding
SkTextEncoding
Definition: SkFontTypes.h:11
SkFont::getHinting
SkFontHinting getHinting() const
Returns level of glyph outline adjustment.
Definition: SkFont.h:182
SkFontHinting
SkFontHinting
Definition: SkFontTypes.h:18
SkFont::isEmbeddedBitmaps
bool isEmbeddedBitmaps() const
Returns true if font engine may return glyphs from font bitmaps instead of from outlines.
Definition: SkFont.h:95
SkFont::setEdging
void setEdging(Edging edging)
Requests, but does not require, that edge pixels draw opaque or with partial transparency.
SkFont::isSubpixel
bool isSubpixel() const
Returns true if glyphs may be drawn at sub-pixel offsets.
Definition: SkFont.h:101
SkMatrix
Definition: SkMatrix.h:48
SkFont::refTypefaceOrDefault
sk_sp< SkTypeface > refTypefaceOrDefault() const
Increases SkTypeface SkRefCnt by one.
SkFont::getMetrics
SkScalar getMetrics(SkFontMetrics *metrics) const
Returns SkFontMetrics associated with SkTypeface.
SkFont::operator==
bool operator==(const SkFont &font) const
Compares SkFont and font, and returns true if they are equivalent.
SkFont
Definition: SkFont.h:23
SkFont::setHinting
void setHinting(SkFontHinting hintingLevel)
Sets level of glyph outline adjustment.
SkFont::getXPos
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.
SkFont::getTypeface
SkTypeface * getTypeface() const
Returns SkTypeface if set, or nullptr.
Definition: SkFont.h:197
SkRect
Definition: SkRect.h:579
SkFont::setEmbeddedBitmaps
void setEmbeddedBitmaps(bool embeddedBitmaps)
Requests, but does not require, to use bitmaps in fonts instead of outlines.
SkFont::setTypeface
void setTypeface(sk_sp< SkTypeface > tf)
Sets SkTypeface to typeface, decreasing SkRefCnt of the previous SkTypeface.
Definition: SkFont.h:246
SkFont::setScaleX
void setScaleX(SkScalar scaleX)
Sets text scale on x-axis.
SkFont::measureText
SkScalar measureText(const void *text, size_t byteLength, SkTextEncoding encoding, SkRect *bounds=nullptr) const
Returns the advance width of text.
Definition: SkFont.h:334
SkFont::dump
void dump() const
Dumps fields of the font to SkDebugf.
SkFont::setForceAutoHinting
void setForceAutoHinting(bool forceAutoHinting)
Sets whether to always hint glyphs.
SkFont::setLinearMetrics
void setLinearMetrics(bool linearMetrics)
Requests, but does not require, linearly scalable font and glyph metrics.
SkFont::SkFont
SkFont()
Constructs SkFont with default values.