SkGlyph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #ifndef SkGlyph_DEFINED
  8. #define SkGlyph_DEFINED
  9. #include "include/core/SkPath.h"
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkChecksum.h"
  12. #include "include/private/SkFixed.h"
  13. #include "include/private/SkTo.h"
  14. #include "src/core/SkMask.h"
  15. class SkArenaAlloc;
  16. class SkStrike;
  17. class SkScalerContext;
  18. // needs to be != to any valid SkMask::Format
  19. #define MASK_FORMAT_UNKNOWN (0xFF)
  20. #define MASK_FORMAT_JUST_ADVANCE MASK_FORMAT_UNKNOWN
  21. /** SkGlyphID + subpixel-pos */
  22. struct SkPackedGlyphID {
  23. static constexpr uint32_t kImpossibleID = ~0u;
  24. enum {
  25. kSubBits = 2u,
  26. kSubMask = ((1u << kSubBits) - 1),
  27. kSubShift = 24u, // must be large enough for glyphs and unichars
  28. kCodeMask = ((1u << kSubShift) - 1),
  29. // relative offsets for X and Y subpixel bits
  30. kSubShiftX = kSubBits,
  31. kSubShiftY = 0
  32. };
  33. constexpr explicit SkPackedGlyphID(SkGlyphID glyphID)
  34. : fID{glyphID} { }
  35. constexpr SkPackedGlyphID(SkGlyphID glyphID, SkFixed x, SkFixed y)
  36. : fID {PackIDXY(glyphID, x, y)} {
  37. SkASSERT(fID != kImpossibleID);
  38. }
  39. constexpr SkPackedGlyphID(SkGlyphID code, SkIPoint pt)
  40. : SkPackedGlyphID(code, pt.fX, pt.fY) { }
  41. constexpr SkPackedGlyphID() : fID{kImpossibleID} {}
  42. bool operator==(const SkPackedGlyphID& that) const {
  43. return fID == that.fID;
  44. }
  45. bool operator!=(const SkPackedGlyphID& that) const {
  46. return !(*this == that);
  47. }
  48. bool operator<(SkPackedGlyphID that) const {
  49. return this->fID < that.fID;
  50. }
  51. uint32_t code() const {
  52. return fID & kCodeMask;
  53. }
  54. uint32_t value() const {
  55. return fID;
  56. }
  57. SkFixed getSubXFixed() const {
  58. return SubToFixed(ID2SubX(fID));
  59. }
  60. SkFixed getSubYFixed() const {
  61. return SubToFixed(ID2SubY(fID));
  62. }
  63. uint32_t hash() const {
  64. return SkChecksum::CheapMix(fID);
  65. }
  66. SkString dump() const {
  67. SkString str;
  68. str.appendf("code: %d, x: %d, y:%d", code(), getSubXFixed(), getSubYFixed());
  69. return str;
  70. }
  71. private:
  72. static constexpr uint32_t PackIDXY(SkGlyphID glyphID, SkFixed x, SkFixed y) {
  73. return (FixedToSub(x) << (kSubShift + kSubShiftX))
  74. | (FixedToSub(y) << (kSubShift + kSubShiftY))
  75. | glyphID;
  76. }
  77. static constexpr unsigned ID2SubX(uint32_t id) {
  78. return id >> (kSubShift + kSubShiftX);
  79. }
  80. static constexpr unsigned ID2SubY(uint32_t id) {
  81. return (id >> (kSubShift + kSubShiftY)) & kSubMask;
  82. }
  83. static constexpr unsigned FixedToSub(SkFixed n) {
  84. return (n >> (16 - kSubBits)) & kSubMask;
  85. }
  86. static constexpr SkFixed SubToFixed(uint32_t sub) {
  87. SkASSERT(sub <= kSubMask);
  88. return sub << (16u - kSubBits);
  89. }
  90. uint32_t fID;
  91. };
  92. struct SkGlyphPrototype;
  93. class SkGlyph {
  94. public:
  95. static constexpr SkFixed kSubpixelRound = SK_FixedHalf >> SkPackedGlyphID::kSubBits;
  96. constexpr explicit SkGlyph(SkPackedGlyphID id) : fID{id} { }
  97. explicit SkGlyph(const SkGlyphPrototype& p);
  98. SkVector advanceVector() const { return SkVector{fAdvanceX, fAdvanceY}; }
  99. SkScalar advanceX() const { return fAdvanceX; }
  100. SkScalar advanceY() const { return fAdvanceY; }
  101. SkGlyphID getGlyphID() const { return fID.code(); }
  102. SkPackedGlyphID getPackedID() const { return fID; }
  103. SkFixed getSubXFixed() const { return fID.getSubXFixed(); }
  104. SkFixed getSubYFixed() const { return fID.getSubYFixed(); }
  105. size_t rowBytes() const;
  106. size_t rowBytesUsingFormat(SkMask::Format format) const;
  107. // Call this to set all of the metrics fields to 0 (e.g. if the scaler
  108. // encounters an error measuring a glyph). Note: this does not alter the
  109. // fImage, fPath, fID, fMaskFormat fields.
  110. void zeroMetrics();
  111. SkMask mask() const;
  112. SkMask mask(SkPoint position) const;
  113. // Image
  114. // If we haven't already tried to associate an image with this glyph
  115. // (i.e. setImageHasBeenCalled() returns false), then use the
  116. // SkScalerContext or const void* argument to set the image.
  117. bool setImage(SkArenaAlloc* alloc, SkScalerContext* scalerContext);
  118. bool setImage(SkArenaAlloc* alloc, const void* image);
  119. // Merge the from glyph into this glyph using alloc to allocate image data. Return true if
  120. // image data was allocated. If the image for this glyph has not been initialized, then copy
  121. // the width, height, top, left, format, and image into this glyph making a copy of the image
  122. // using the alloc.
  123. bool setMetricsAndImage(SkArenaAlloc* alloc, const SkGlyph& from);
  124. // Returns true if the image has been set.
  125. bool setImageHasBeenCalled() const {
  126. return fImage != nullptr || this->isEmpty() || this->imageTooLarge();
  127. }
  128. // Return a pointer to the path if the image exists, otherwise return nullptr.
  129. const void* image() const { SkASSERT(this->setImageHasBeenCalled()); return fImage; }
  130. // Return the size of the image.
  131. size_t imageSize() const;
  132. // Path
  133. // If we haven't already tried to associate a path to this glyph
  134. // (i.e. setPathHasBeenCalled() returns false), then use the
  135. // SkScalerContext or SkPath argument to try to do so. N.B. this
  136. // may still result in no path being associated with this glyph,
  137. // e.g. if you pass a null SkPath or the typeface is bitmap-only.
  138. //
  139. // This setPath() call is sticky... once you call it, the glyph
  140. // stays in its state permanently, ignoring any future calls.
  141. //
  142. // Returns true if this is the first time you called setPath()
  143. // and there actually is a path; call path() to get it.
  144. bool setPath(SkArenaAlloc* alloc, SkScalerContext* scalerContext);
  145. bool setPath(SkArenaAlloc* alloc, const SkPath* path);
  146. // Returns true if that path has been set.
  147. bool setPathHasBeenCalled() const { return fPathData != nullptr; }
  148. // Return a pointer to the path if it exists, otherwise return nullptr. Only works if the
  149. // path was previously set.
  150. const SkPath* path() const;
  151. // Format
  152. bool isColor() const { return fMaskFormat == SkMask::kARGB32_Format; }
  153. SkMask::Format maskFormat() const { return static_cast<SkMask::Format>(fMaskFormat); }
  154. size_t formatAlignment() const;
  155. // Bounds
  156. int maxDimension() const { return std::max(fWidth, fHeight); }
  157. SkIRect iRect() const { return SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); }
  158. SkRect rect() const { return SkRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); }
  159. int left() const { return fLeft; }
  160. int top() const { return fTop; }
  161. int width() const { return fWidth; }
  162. int height() const { return fHeight; }
  163. bool isEmpty() const {
  164. // fHeight == 0 -> fWidth == 0;
  165. SkASSERT(fHeight != 0 || fWidth == 0);
  166. return fWidth == 0;
  167. }
  168. bool imageTooLarge() const { return fWidth >= kMaxGlyphWidth; }
  169. // Make sure that the intercept information is on the glyph and return it, or return it if it
  170. // already exists.
  171. // * bounds - either end of the gap for the character.
  172. // * scale, xPos - information about how wide the gap is.
  173. // * array - accumulated gaps for many characters if not null.
  174. // * count - the number of gaps.
  175. void ensureIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos,
  176. SkScalar* array, int* count, SkArenaAlloc* alloc);
  177. private:
  178. // There are two sides to an SkGlyph, the scaler side (things that create glyph data) have
  179. // access to all the fields. Scalers are assumed to maintain all the SkGlyph invariants. The
  180. // consumer side has a tighter interface.
  181. friend class RandomScalerContext;
  182. friend class SkScalerContext;
  183. friend class SkScalerContextProxy;
  184. friend class SkScalerContext_Empty;
  185. friend class SkScalerContext_FreeType;
  186. friend class SkScalerContext_FreeType_Base;
  187. friend class SkScalerContext_DW;
  188. friend class SkScalerContext_GDI;
  189. friend class SkScalerContext_Mac;
  190. friend class SkStrikeClient;
  191. friend class SkStrikeServer;
  192. friend class SkTestScalerContext;
  193. friend class SkTestSVGScalerContext;
  194. friend class TestSVGTypeface;
  195. friend class TestTypeface;
  196. static constexpr uint16_t kMaxGlyphWidth = 1u << 13u;
  197. // Support horizontal and vertical skipping strike-through / underlines.
  198. // The caller walks the linked list looking for a match. For a horizontal underline,
  199. // the fBounds contains the top and bottom of the underline. The fInterval pair contains the
  200. // beginning and end of of the intersection of the bounds and the glyph's path.
  201. // If interval[0] >= interval[1], no intersection was found.
  202. struct Intercept {
  203. Intercept* fNext;
  204. SkScalar fBounds[2]; // for horz underlines, the boundaries in Y
  205. SkScalar fInterval[2]; // the outside intersections of the axis and the glyph
  206. };
  207. struct PathData {
  208. Intercept* fIntercept{nullptr};
  209. SkPath fPath;
  210. bool fHasPath{false};
  211. };
  212. size_t allocImage(SkArenaAlloc* alloc);
  213. // path == nullptr indicates that there is no path.
  214. void installPath(SkArenaAlloc* alloc, const SkPath* path);
  215. // The width and height of the glyph mask.
  216. uint16_t fWidth = 0,
  217. fHeight = 0;
  218. // The offset from the glyphs origin on the baseline to the top left of the glyph mask.
  219. int16_t fTop = 0,
  220. fLeft = 0;
  221. // fImage must remain null if the glyph is empty or if width > kMaxGlyphWidth.
  222. void* fImage = nullptr;
  223. // Path data has tricky state. If the glyph isEmpty, then fPathData should always be nullptr,
  224. // else if fPathData is not null, then a path has been requested. The fPath field of fPathData
  225. // may still be null after the request meaning that there is no path for this glyph.
  226. PathData* fPathData = nullptr;
  227. // The advance for this glyph.
  228. float fAdvanceX = 0,
  229. fAdvanceY = 0;
  230. // This is a combination of SkMask::Format and SkGlyph state. The SkGlyph can be in one of two
  231. // states, just the advances have been calculated, and all the metrics are available. The
  232. // illegal mask format is used to signal that only the advances are available.
  233. uint8_t fMaskFormat = MASK_FORMAT_UNKNOWN;
  234. // Used by the DirectWrite scaler to track state.
  235. int8_t fForceBW = 0;
  236. const SkPackedGlyphID fID;
  237. };
  238. struct SkGlyphPrototype {
  239. SkPackedGlyphID id;
  240. float advanceX = 0,
  241. advanceY = 0;
  242. // The width and height of the glyph mask.
  243. uint16_t width = 0,
  244. height = 0;
  245. // The offset from the glyphs origin on the baseline to the top left of the glyph mask.
  246. int16_t left = 0,
  247. top = 0;
  248. SkMask::Format maskFormat = SkMask::kBW_Format;
  249. bool forceBW = false;
  250. };
  251. #endif