SkTextBlob.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /* Generated by tools/bookmaker from include/core/SkTextBlob.h and docs/SkTextBlob_Reference.bmh
  8. on 2018-08-10 12:59:44. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkTextBlob_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkTextBlob_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkTextBlob.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkTextBlob_DEFINED
  16. #define SkTextBlob_DEFINED
  17. #include "../private/SkTemplates.h"
  18. #include "SkFont.h"
  19. #include "SkPaint.h"
  20. #include "SkString.h"
  21. #include "SkRefCnt.h"
  22. #include <atomic>
  23. struct SkSerialProcs;
  24. struct SkDeserialProcs;
  25. /** \class SkTextBlob
  26. SkTextBlob combines multiple text runs into an immutable container. Each text
  27. run consists of glyphs, SkPaint, and position. Only parts of SkPaint related to
  28. fonts and text rendering are used by run.
  29. */
  30. class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
  31. public:
  32. /** Returns conservative bounding box. Uses SkPaint associated with each glyph to
  33. determine glyph bounds, and unions all bounds. Returned bounds may be
  34. larger than the bounds of all glyphs in runs.
  35. @return conservative bounding box
  36. */
  37. const SkRect& bounds() const { return fBounds; }
  38. /** Returns a non-zero value unique among all text blobs.
  39. @return identifier for SkTextBlob
  40. */
  41. uint32_t uniqueID() const { return fUniqueID; }
  42. /** Creates SkTextBlob with a single run.
  43. font contains attributes used to define the run text.
  44. @param text character code points or glyphs drawn
  45. @param byteLength byte length of text array
  46. @param font text size, typeface, text scale, and so on, used to draw
  47. @return SkTextBlob constructed from one run
  48. */
  49. static sk_sp<SkTextBlob> MakeFromText(const void* text, size_t byteLength, const SkFont& font,
  50. SkPaint::TextEncoding encoding = SkPaint::kUTF8_TextEncoding);
  51. /** Creates SkTextBlob with a single run. string meaning depends on SkPaint::TextEncoding;
  52. by default, string is encoded as UTF-8.
  53. font contains attributes used to define the run text.
  54. @param string character code points or glyphs drawn
  55. @param font text size, typeface, text scale, and so on, used to draw
  56. @return SkTextBlob constructed from one run
  57. */
  58. static sk_sp<SkTextBlob> MakeFromString(const char* string, const SkFont& font,
  59. SkPaint::TextEncoding encoding = SkPaint::kUTF8_TextEncoding) {
  60. if (!string) {
  61. return nullptr;
  62. }
  63. return MakeFromText(string, strlen(string), font, encoding);
  64. }
  65. /** Writes data to allow later reconstruction of SkTextBlob. memory points to storage
  66. to receive the encoded data, and memory_size describes the size of storage.
  67. Returns bytes used if provided storage is large enough to hold all data;
  68. otherwise, returns zero.
  69. procs.fTypefaceProc permits supplying a custom function to encode SkTypeface.
  70. If procs.fTypefaceProc is nullptr, default encoding is used. procs.fTypefaceCtx
  71. may be used to provide user context to procs.fTypefaceProc; procs.fTypefaceProc
  72. is called with a pointer to SkTypeface and user context.
  73. @param procs custom serial data encoders; may be nullptr
  74. @param memory storage for data
  75. @param memory_size size of storage
  76. @return bytes written, or zero if required storage is larger than memory_size
  77. */
  78. size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const;
  79. /** Returns storage containing SkData describing SkTextBlob, using optional custom
  80. encoders.
  81. procs.fTypefaceProc permits supplying a custom function to encode SkTypeface.
  82. If procs.fTypefaceProc is nullptr, default encoding is used. procs.fTypefaceCtx
  83. may be used to provide user context to procs.fTypefaceProc; procs.fTypefaceProc
  84. is called with a pointer to SkTypeface and user context.
  85. @param procs custom serial data encoders; may be nullptr
  86. @return storage containing serialized SkTextBlob
  87. */
  88. sk_sp<SkData> serialize(const SkSerialProcs& procs) const;
  89. /** Recreates SkTextBlob that was serialized into data. Returns constructed SkTextBlob
  90. if successful; otherwise, returns nullptr. Fails if size is smaller than
  91. required data length, or if data does not permit constructing valid SkTextBlob.
  92. procs.fTypefaceProc permits supplying a custom function to decode SkTypeface.
  93. If procs.fTypefaceProc is nullptr, default decoding is used. procs.fTypefaceCtx
  94. may be used to provide user context to procs.fTypefaceProc; procs.fTypefaceProc
  95. is called with a pointer to SkTypeface data, data byte length, and user context.
  96. @param data pointer for serial data
  97. @param size size of data
  98. @param procs custom serial data decoders; may be nullptr
  99. @return SkTextBlob constructed from data in memory
  100. */
  101. static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
  102. const SkDeserialProcs& procs);
  103. private:
  104. friend class SkNVRefCnt<SkTextBlob>;
  105. class RunRecord;
  106. enum GlyphPositioning : uint8_t;
  107. explicit SkTextBlob(const SkRect& bounds);
  108. ~SkTextBlob();
  109. // Memory for objects of this class is created with sk_malloc rather than operator new and must
  110. // be freed with sk_free.
  111. void operator delete(void* p);
  112. void* operator new(size_t);
  113. void* operator new(size_t, void* p);
  114. static unsigned ScalarsPerGlyph(GlyphPositioning pos);
  115. // Call when this blob is part of the key to a cache entry. This allows the cache
  116. // to know automatically those entries can be purged when this SkTextBlob is deleted.
  117. void notifyAddedToCache(uint32_t cacheID) const {
  118. fCacheID.store(cacheID);
  119. }
  120. friend class SkGlyphRunList;
  121. friend class GrTextBlobCache;
  122. friend class SkTextBlobBuilder;
  123. friend class SkTextBlobPriv;
  124. friend class SkTextBlobRunIterator;
  125. const SkRect fBounds;
  126. const uint32_t fUniqueID;
  127. mutable std::atomic<uint32_t> fCacheID;
  128. SkDEBUGCODE(size_t fStorageSize;)
  129. // The actual payload resides in externally-managed storage, following the object.
  130. // (see the .cpp for more details)
  131. typedef SkRefCnt INHERITED;
  132. };
  133. /** \class SkTextBlobBuilder
  134. Helper class for constructing SkTextBlob.
  135. */
  136. class SK_API SkTextBlobBuilder {
  137. public:
  138. /** Constructs empty SkTextBlobBuilder. By default, SkTextBlobBuilder has no runs.
  139. @return empty SkTextBlobBuilder
  140. */
  141. SkTextBlobBuilder();
  142. /** Deletes data allocated internally by SkTextBlobBuilder.
  143. */
  144. ~SkTextBlobBuilder();
  145. /** Returns SkTextBlob built from runs of glyphs added by builder. Returned
  146. SkTextBlob is immutable; it may be copied, but its contents may not be altered.
  147. Returns nullptr if no runs of glyphs were added by builder.
  148. Resets SkTextBlobBuilder to its initial empty state, allowing it to be
  149. reused to build a new set of runs.
  150. @return SkTextBlob or nullptr
  151. */
  152. sk_sp<SkTextBlob> make();
  153. /** \struct SkTextBlobBuilder::RunBuffer
  154. RunBuffer supplies storage for glyphs and positions within a run.
  155. A run is a sequence of glyphs sharing font metrics and positioning.
  156. Each run may position its glyphs in one of three ways:
  157. by specifying where the first glyph is drawn, and allowing font metrics to
  158. determine the advance to subsequent glyphs; by specifying a baseline, and
  159. the position on that baseline for each glyph in run; or by providing SkPoint
  160. array, one per glyph.
  161. */
  162. struct RunBuffer {
  163. SkGlyphID* glyphs; //!< storage for glyphs in run
  164. SkScalar* pos; //!< storage for positions in run
  165. char* utf8text; //!< reserved for future use
  166. uint32_t* clusters; //!< reserved for future use
  167. };
  168. /** Returns run with storage for glyphs. Caller must write count glyphs to
  169. RunBuffer::glyphs before next call to SkTextBlobBuilder.
  170. RunBuffer::utf8text, and RunBuffer::clusters should be ignored.
  171. Glyphs share metrics in font.
  172. Glyphs are positioned on a baseline at (x, y), using font metrics to
  173. determine their relative placement.
  174. bounds defines an optional bounding box, used to suppress drawing when SkTextBlob
  175. bounds does not intersect SkSurface bounds. If bounds is nullptr, SkTextBlob bounds
  176. is computed from (x, y) and RunBuffer::glyphs metrics.
  177. @param font SkFont used for this run
  178. @param count number of glyphs
  179. @param x horizontal offset within the blob
  180. @param y vertical offset within the blob
  181. @param bounds optional run bounding box
  182. @return writable glyph buffer
  183. */
  184. const RunBuffer& allocRun(const SkFont& font, int count, SkScalar x, SkScalar y,
  185. const SkRect* bounds = nullptr);
  186. /** Returns run with storage for glyphs and positions along baseline. Caller must
  187. write count glyphs to RunBuffer::glyphs, and count scalars to RunBuffer::pos;
  188. before next call to SkTextBlobBuilder.
  189. RunBuffer::utf8text, and RunBuffer::clusters should be ignored.
  190. Glyphs share metrics in font.
  191. Glyphs are positioned on a baseline at y, using x-axis positions written by
  192. caller to RunBuffer::pos.
  193. bounds defines an optional bounding box, used to suppress drawing when SkTextBlob
  194. bounds does not intersect SkSurface bounds. If bounds is nullptr, SkTextBlob bounds
  195. is computed from y, RunBuffer::pos, and RunBuffer::glyphs metrics.
  196. @param font SkFont used for this run
  197. @param count number of glyphs
  198. @param y vertical offset within the blob
  199. @param bounds optional run bounding box
  200. @return writable glyph buffer and x-axis position buffer
  201. */
  202. const RunBuffer& allocRunPosH(const SkFont& font, int count, SkScalar y,
  203. const SkRect* bounds = nullptr);
  204. /** Returns run with storage for glyphs and SkPoint positions. Caller must
  205. write count glyphs to RunBuffer::glyphs, and count SkPoint to RunBuffer::pos;
  206. before next call to SkTextBlobBuilder.
  207. RunBuffer::utf8text, and RunBuffer::clusters should be ignored.
  208. Glyphs share metrics in font.
  209. Glyphs are positioned using SkPoint written by caller to RunBuffer::pos, using
  210. two scalar values for each SkPoint.
  211. bounds defines an optional bounding box, used to suppress drawing when SkTextBlob
  212. bounds does not intersect SkSurface bounds. If bounds is nullptr, SkTextBlob bounds
  213. is computed from RunBuffer::pos, and RunBuffer::glyphs metrics.
  214. @param font SkFont used for this run
  215. @param count number of glyphs
  216. @param bounds optional run bounding box
  217. @return writable glyph buffer and SkPoint buffer
  218. */
  219. const RunBuffer& allocRunPos(const SkFont& font, int count,
  220. const SkRect* bounds = nullptr);
  221. #ifdef SK_SUPPORT_LEGACY_TEXTBLOBBUILD_WITH_PAINT
  222. /** Deprecated.
  223. */
  224. const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
  225. const SkRect* bounds = nullptr) {
  226. return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
  227. }
  228. /** Deprecated.
  229. */
  230. const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
  231. const SkRect* bounds = nullptr) {
  232. return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
  233. }
  234. /** Deprecated.
  235. */
  236. const RunBuffer& allocRunPos(const SkPaint& font, int count,
  237. const SkRect* bounds = nullptr) {
  238. return this->allocRunTextPos(font, count, 0, SkString(), bounds);
  239. }
  240. #endif
  241. private:
  242. const RunBuffer& allocRunText(const SkPaint& font,
  243. int count,
  244. SkScalar x,
  245. SkScalar y,
  246. int textByteCount,
  247. SkString lang,
  248. const SkRect* bounds = nullptr);
  249. const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
  250. int textByteCount, SkString lang,
  251. const SkRect* bounds = nullptr);
  252. const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
  253. int textByteCount, SkString lang,
  254. const SkRect* bounds = nullptr);
  255. void reserve(size_t size);
  256. void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
  257. int count, int textBytes, SkPoint offset, const SkRect* bounds);
  258. bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
  259. uint32_t count, SkPoint offset);
  260. void updateDeferredBounds();
  261. static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
  262. static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
  263. friend class SkTextBlobPriv;
  264. friend class SkTextBlobBuilderPriv;
  265. SkAutoTMalloc<uint8_t> fStorage;
  266. size_t fStorageSize;
  267. size_t fStorageUsed;
  268. SkRect fBounds;
  269. int fRunCount;
  270. bool fDeferredBounds;
  271. size_t fLastRun; // index into fStorage
  272. RunBuffer fCurrentRunBuffer;
  273. };
  274. #endif // SkTextBlob_DEFINED