SkFont.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #ifndef SkFont_DEFINED
  8. #define SkFont_DEFINED
  9. #include "SkFontTypes.h"
  10. #include "SkScalar.h"
  11. #include "SkTypeface.h"
  12. class SkMatrix;
  13. class SkPaint;
  14. class SkPath;
  15. struct SkFontMetrics;
  16. /** \class SkFont
  17. SkFont controls options applied when drawing and measuring text.
  18. */
  19. class SK_API SkFont {
  20. public:
  21. /** Whether edge pixels draw opaque or with partial transparency.
  22. */
  23. enum class Edging {
  24. kAlias, //!< no transparent pixels on glyph edges
  25. kAntiAlias, //!< may have transparent pixels on glyph edges
  26. kSubpixelAntiAlias, //!< glyph positioned in pixel using transparency
  27. };
  28. #ifdef SK_SUPPORT_LEGACY_NESTED_HINTINGENUM
  29. /** Amount of font hinting applied to glyph outlines.
  30. */
  31. enum Hinting : uint8_t {
  32. kNo_Hinting = 0, //!< glyph outlines unchanged
  33. kSlight_Hinting = 1, //!< minimal modification to improve constrast
  34. kNormal_Hinting = 2, //!< glyph outlines modified to improve constrast
  35. kFull_Hinting = 3, //!< modifies glyph outlines for maximum constrast
  36. };
  37. #endif
  38. /** Constructs SkFont with default values.
  39. @return default initialized SkFont
  40. */
  41. SkFont();
  42. /** Constructs SkFont with default values with SkTypeface and size in points.
  43. @param typeface font and style used to draw and measure text
  44. @param size typographic height of text
  45. @return initialized SkFont
  46. */
  47. SkFont(sk_sp<SkTypeface> typeface, SkScalar size);
  48. /** Constructs SkFont with default values with SkTypeface and size in points,
  49. horizontal scale, and horizontal skew. Horizontal scale emulates condensed
  50. and expanded fonts. Horizontal skew emulates oblique fonts.
  51. @param typeface font and style used to draw and measure text
  52. @param size typographic height of text
  53. @param scaleX text horizontal scale
  54. @param skewX additional shear on x-axis relative to y-axis
  55. @return initialized SkFont
  56. */
  57. SkFont(sk_sp<SkTypeface> typeface, SkScalar size, SkScalar scaleX, SkScalar skewX);
  58. /** Compares SkFont and font, and returns true if they are equivalent.
  59. May return false if SkTypeface has identical contents but different pointers.
  60. @param font SkPaint to compare
  61. @return true if SkFont pair are equivalent
  62. */
  63. bool operator==(const SkFont& font) const;
  64. /** If true, instructs the font manager to always hint glyphs.
  65. Returned value is only meaningful if platform uses FreeType as the font manager.
  66. @return true if all glyphs are hinted
  67. */
  68. bool isForceAutoHinting() const { return SkToBool(fFlags & kForceAutoHinting_PrivFlag); }
  69. /** Returns true if font engine may return glyphs from font bitmaps instead of from outlines.
  70. @return true if glyphs may be font bitmaps
  71. */
  72. bool isEmbeddedBitmaps() const { return SkToBool(fFlags & kEmbeddedBitmaps_PrivFlag); }
  73. /** Returns true if glyphs at different sub-pixel positions may differ on pixel edge coverage.
  74. @return true if glyph positioned in pixel using transparency
  75. */
  76. bool isSubpixel() const { return SkToBool(fFlags & kSubpixel_PrivFlag); }
  77. /** Returns true if text is converted to SkPath before drawing and measuring.
  78. @return true glyph hints are never applied
  79. */
  80. bool isLinearMetrics() const { return SkToBool(fFlags & kLinearMetrics_PrivFlag); }
  81. /** Returns true if bold is approximated by increasing the stroke width when creating glyph
  82. bitmaps from outlines.
  83. @return bold is approximated through stroke width
  84. */
  85. bool isEmbolden() const { return SkToBool(fFlags & kEmbolden_PrivFlag); }
  86. /** Sets whether to always hint glyphs.
  87. If forceAutoHinting is set, instructs the font manager to always hint glyphs.
  88. Only affects platforms that use FreeType as the font manager.
  89. @param forceAutoHinting setting to always hint glyphs
  90. */
  91. void setForceAutoHinting(bool forceAutoHinting);
  92. /** Requests, but does not require, to use bitmaps in fonts instead of outlines.
  93. @param embeddedBitmaps setting to use bitmaps in fonts
  94. */
  95. void setEmbeddedBitmaps(bool embeddedBitmaps);
  96. /** Requests, but does not require, that glyphs respect sub-pixel positioning.
  97. @param subpixel setting for sub-pixel positioning
  98. */
  99. void setSubpixel(bool subpixel);
  100. /** Requests, but does not require, that glyphs are converted to SkPath
  101. before drawing and measuring.
  102. @param linearMetrics setting for converting glyphs to paths
  103. */
  104. void setLinearMetrics(bool linearMetrics);
  105. /** Increases stroke width when creating glyph bitmaps to approximate a bold typeface.
  106. @param embolden setting for bold approximation
  107. */
  108. void setEmbolden(bool embolden);
  109. /** Whether edge pixels draw opaque or with partial transparency.
  110. @return one of: Edging::kAlias, Edging::kAntiAlias, Edging::kSubpixelAntiAlias
  111. */
  112. Edging getEdging() const { return (Edging)fEdging; }
  113. /** Requests, but does not require, that edge pixels draw opaque or with
  114. partial transparency.
  115. @param edging one of: Edging::kAlias, Edging::kAntiAlias, Edging::kSubpixelAntiAlias
  116. */
  117. void setEdging(Edging edging);
  118. /** Sets level of glyph outline adjustment.
  119. Does not check for valid values of hintingLevel.
  120. @param hintingLevel one of: SkFontHinting::kNone, SkFontHinting::kSlight,
  121. SkFontHinting::kNormal, SkFontHinting::kFull
  122. */
  123. void setHinting(SkFontHinting hintingLevel);
  124. /** Returns level of glyph outline adjustment.
  125. @return one of: SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal,
  126. SkFontHinting::kFull
  127. */
  128. SkFontHinting getHinting() const { return (SkFontHinting)fHinting; }
  129. /** Returns a font with the same attributes of this font, but with the specified size.
  130. Returns nullptr if size is less than zero, infinite, or NaN.
  131. @param size typographic height of text
  132. @return initialized SkFont
  133. */
  134. SkFont makeWithSize(SkScalar size) const;
  135. /** Returns SkTypeface if set, or nullptr.
  136. Does not alter SkTypeface SkRefCnt.
  137. @return SkTypeface if previously set, nullptr otherwise
  138. */
  139. SkTypeface* getTypeface() const { return fTypeface.get(); }
  140. /** Returns text size in points.
  141. @return typographic height of text
  142. */
  143. SkScalar getSize() const { return fSize; }
  144. /** Returns text scale on x-axis.
  145. Default value is 1.
  146. @return text horizontal scale
  147. */
  148. SkScalar getScaleX() const { return fScaleX; }
  149. /** Returns text skew on x-axis.
  150. Default value is zero.
  151. @return additional shear on x-axis relative to y-axis
  152. */
  153. SkScalar getSkewX() const { return fSkewX; }
  154. /** Increases SkTypeface SkRefCnt by one.
  155. @return SkTypeface if previously set, nullptr otherwise
  156. */
  157. sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
  158. /** Sets SkTypeface to typeface, decreasing SkRefCnt of the previous SkTypeface.
  159. Pass nullptr to clear SkTypeface and use the default typeface. Increments
  160. tf SkRefCnt by one.
  161. @param tf font and style used to draw text
  162. */
  163. void setTypeface(sk_sp<SkTypeface> tf) { fTypeface = tf; }
  164. /** Sets text size in points.
  165. Has no effect if textSize is not greater than or equal to zero.
  166. @param textSize typographic height of text
  167. */
  168. void setSize(SkScalar textSize);
  169. /** Sets text scale on x-axis.
  170. Default value is 1.
  171. @param scaleX text horizontal scale
  172. */
  173. void setScaleX(SkScalar scaleX);
  174. /** Sets text skew on x-axis.
  175. Default value is zero.
  176. @param skewX additional shear on x-axis relative to y-axis
  177. */
  178. void setSkewX(SkScalar skewX);
  179. /** Converts text into glyph indices.
  180. Returns the number of glyph indices represented by text.
  181. SkTextEncoding specifies how text represents characters or glyphs.
  182. glyphs may be nullptr, to compute the glyph count.
  183. Does not check text for valid character codes or valid glyph indices.
  184. If byteLength equals zero, returns zero.
  185. If byteLength includes a partial character, the partial character is ignored.
  186. If encoding is kUTF8_SkTextEncoding and text contains an invalid UTF-8 sequence,
  187. zero is returned.
  188. If maxGlyphCount is not sufficient to store all the glyphs, no glyphs are copied.
  189. The total glyph count is returned for subsequent buffer reallocation.
  190. @param text character storage encoded with SkPaint::TextEncoding
  191. @param byteLength length of character storage in bytes
  192. @param encoding one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  193. kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  194. @param glyphs storage for glyph indices; may be nullptr
  195. @param maxGlyphCount storage capacity
  196. @return number of glyphs represented by text of length byteLength
  197. */
  198. int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
  199. SkGlyphID glyphs[], int maxGlyphCount) const;
  200. /** Returns glyph index for Unicode character.
  201. @param uni Unicode character
  202. @return glyph index
  203. */
  204. uint16_t unicharToGlyph(SkUnichar uni) const {
  205. return fTypeface->unicharToGlyph(uni);
  206. }
  207. /** Returns number of glyphs represented by text.
  208. @param text character storage encoded with SkPaint::TextEncoding
  209. @param byteLength length of character storage in bytes
  210. @param encoding one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  211. kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  212. @return number of glyphs represented by text of length byteLength
  213. */
  214. int countText(const void* text, size_t byteLength, SkTextEncoding encoding) const {
  215. return this->textToGlyphs(text, byteLength, encoding, nullptr, 0);
  216. }
  217. /** Returns true if all text corresponds to a non-zero glyph index.
  218. Returns false if any characters in text are not supported in
  219. SkTypeface.
  220. If SkTextEncoding is kGlyphID_SkTextEncoding,
  221. returns true if all glyph indices in text are non-zero;
  222. does not check to see if text contains valid glyph indices for SkTypeface.
  223. Returns true if byteLength is zero.
  224. @param text array of characters or glyphs
  225. @param byteLength number of bytes in text array
  226. @param encoding text encoding
  227. @return true if all text corresponds to a non-zero glyph index
  228. */
  229. bool containsText(const void* text, size_t byteLength, SkTextEncoding encoding) const;
  230. /** Returns the advance width of text.
  231. The advance is the normal distance to move before drawing additional text.
  232. Returns the bounding box of text if bounds is not nullptr.
  233. @param text character storage encoded with SkPaint::TextEncoding
  234. @param byteLength length of character storage in bytes
  235. @param encoding one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  236. kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  237. @param bounds returns bounding box relative to (0, 0) if not nullptr
  238. @return number of glyphs represented by text of length byteLength
  239. */
  240. SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding encoding,
  241. SkRect* bounds = nullptr) const;
  242. /** DEPRECATED
  243. Retrieves the advance and bounds for each glyph in glyphs.
  244. Both widths and bounds may be nullptr.
  245. If widths is not nullptr, widths must be an array of count entries.
  246. if bounds is not nullptr, bounds must be an array of count entries.
  247. @param glyphs array of glyph indices to be measured
  248. @param count number of glyphs
  249. @param widths returns text advances for each glyph; may be nullptr
  250. @param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
  251. */
  252. void getWidths(const uint16_t glyphs[], int count, SkScalar widths[], SkRect bounds[]) const {
  253. this->getWidthsBounds(glyphs, count, widths, bounds, nullptr);
  254. }
  255. // DEPRECATED
  256. void getWidths(const uint16_t glyphs[], int count, SkScalar widths[], std::nullptr_t) const {
  257. this->getWidths(glyphs, count, widths);
  258. }
  259. /** Experimental
  260. Retrieves the advance and bounds for each glyph in glyphs.
  261. Both widths and bounds may be nullptr.
  262. If widths is not nullptr, widths must be an array of count entries.
  263. if bounds is not nullptr, bounds must be an array of count entries.
  264. @param glyphs array of glyph indices to be measured
  265. @param count number of glyphs
  266. @param widths returns text advances for each glyph
  267. */
  268. void getWidths(const uint16_t glyphs[], int count, SkScalar widths[]) const {
  269. this->getWidthsBounds(glyphs, count, widths, nullptr, nullptr);
  270. }
  271. /** Experimental.
  272. Retrieves the advance and bounds for each glyph in glyphs.
  273. Both widths and bounds may be nullptr.
  274. If widths is not nullptr, widths must be an array of count entries.
  275. if bounds is not nullptr, bounds must be an array of count entries.
  276. @param glyphs array of glyph indices to be measured
  277. @param count number of glyphs
  278. @param widths returns text advances for each glyph; may be nullptr
  279. @param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
  280. @param paint optional, specifies stroking, patheffect and maskfilter
  281. */
  282. void getWidthsBounds(const uint16_t glyphs[], int count, SkScalar widths[], SkRect bounds[],
  283. const SkPaint* paint) const;
  284. /** Experimental.
  285. Retrieves the bounds for each glyph in glyphs.
  286. bounds must be an array of count entries.
  287. If paint is not nullptr, its stroking, patheffect and maskfilter fields will be respected.
  288. @param glyphs array of glyph indices to be measured
  289. @param count number of glyphs
  290. @param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
  291. @param paint optional, specifies stroking, patheffect and maskfilter
  292. */
  293. void getBounds(const uint16_t glyphs[], int count, SkRect bounds[], const SkPaint* paint) const {
  294. this->getWidthsBounds(glyphs, count, nullptr, bounds, paint);
  295. }
  296. /** Experimental
  297. Retrieves the positions for each glyph, beginning at the specified origin. The caller
  298. must allocated at least count number of elements in the pos[] array.
  299. @param glyphs array of glyph indices to be positioned
  300. @param count number of glyphs
  301. @param pos returns glyphs positions
  302. @param origin location of the first glyph. Defaults to {0, 0}.
  303. */
  304. void getPos(const uint16_t glyphs[], int count, SkPoint pos[], SkPoint origin = {0, 0}) const;
  305. /** Experimental
  306. Retrieves the x-positions for each glyph, beginning at the specified origin. The caller
  307. must allocated at least count number of elements in the xpos[] array.
  308. @param glyphs array of glyph indices to be positioned
  309. @param count number of glyphs
  310. @param xpos returns glyphs x-positions
  311. @param origin x-position of the first glyph. Defaults to 0.
  312. */
  313. void getXPos(const uint16_t glyphs[], int count, SkScalar xpos[], SkScalar origin = 0) const;
  314. /** Returns path corresponding to glyph outline.
  315. If glyph has an outline, copies outline to path and returns true.
  316. path returned may be empty.
  317. If glyph is described by a bitmap, returns false and ignores path parameter.
  318. @param glyphID index of glyph
  319. @param path pointer to existing SkPath
  320. @return true if glyphID is described by path
  321. */
  322. bool getPath(uint16_t glyphID, SkPath* path) const;
  323. /** Returns path corresponding to glyph array.
  324. @param glyphIDs array of glyph indices
  325. @param count number of glyphs
  326. @param glyphPathProc function returning one glyph description as path
  327. @param ctx function context
  328. */
  329. void getPaths(const uint16_t glyphIDs[], int count,
  330. void (*glyphPathProc)(const SkPath* pathOrNull, const SkMatrix& mx, void* ctx),
  331. void* ctx) const;
  332. /** Returns SkFontMetrics associated with SkTypeface.
  333. The return value is the recommended spacing between lines: the sum of metrics
  334. descent, ascent, and leading.
  335. If metrics is not nullptr, SkFontMetrics is copied to metrics.
  336. Results are scaled by text size but does not take into account
  337. dimensions required by text scale, text skew, fake bold,
  338. style stroke, and SkPathEffect.
  339. @param metrics storage for SkFontMetrics; may be nullptr
  340. @return recommended spacing between lines
  341. */
  342. SkScalar getMetrics(SkFontMetrics* metrics) const;
  343. /** Returns the recommended spacing between lines: the sum of metrics
  344. descent, ascent, and leading.
  345. Result is scaled by text size but does not take into account
  346. dimensions required by stroking and SkPathEffect.
  347. Returns the same result as getMetrics().
  348. @return recommended spacing between lines
  349. */
  350. SkScalar getSpacing() const { return this->getMetrics(nullptr); }
  351. /** Deprecated.
  352. */
  353. void LEGACY_applyToPaint(SkPaint* paint) const;
  354. /** Deprecated.
  355. */
  356. void LEGACY_applyPaintFlags(uint32_t paintFlags);
  357. /** Deprecated.
  358. */
  359. static SkFont LEGACY_ExtractFromPaint(const SkPaint& paint);
  360. private:
  361. enum PrivFlags {
  362. kForceAutoHinting_PrivFlag = 1 << 0,
  363. kEmbeddedBitmaps_PrivFlag = 1 << 1,
  364. kSubpixel_PrivFlag = 1 << 2,
  365. kLinearMetrics_PrivFlag = 1 << 3,
  366. kEmbolden_PrivFlag = 1 << 4,
  367. };
  368. static constexpr unsigned kAllFlags = 0x07F;
  369. sk_sp<SkTypeface> fTypeface;
  370. SkScalar fSize;
  371. SkScalar fScaleX;
  372. SkScalar fSkewX;
  373. uint8_t fFlags;
  374. uint8_t fEdging;
  375. uint8_t fHinting;
  376. SkScalar setupForAsPaths(SkPaint*);
  377. friend class SkCanonicalizeFont;
  378. };
  379. #endif