fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 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 SkTextBlob_DEFINED |
| 9 | #define SkTextBlob_DEFINED |
| 10 | |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 11 | #include "../private/SkTemplates.h" |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 12 | #include "../private/SkAtomics.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 13 | #include "SkPaint.h" |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 14 | #include "SkString.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 15 | #include "SkRefCnt.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 16 | |
Herb Derby | 8a6348e | 2018-07-12 15:30:35 -0400 | [diff] [blame^] | 17 | class SkGlyphRunList; |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 18 | class SkReadBuffer; |
| 19 | class SkWriteBuffer; |
| 20 | |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 21 | struct SkSerialProcs; |
| 22 | struct SkDeserialProcs; |
| 23 | |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 24 | typedef void (*SkTypefaceCatalogerProc)(SkTypeface*, void* ctx); |
| 25 | typedef sk_sp<SkTypeface> (*SkTypefaceResolverProc)(uint32_t id, void* ctx); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 26 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 27 | /** \class SkTextBlob |
| 28 | |
| 29 | SkTextBlob combines multiple text runs into an immutable, ref-counted structure. |
| 30 | */ |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 31 | class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 32 | public: |
| 33 | /** |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 34 | * Returns a conservative blob bounding box. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 35 | */ |
| 36 | const SkRect& bounds() const { return fBounds; } |
| 37 | |
| 38 | /** |
| 39 | * Return a non-zero, unique value representing the text blob. |
| 40 | */ |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 41 | uint32_t uniqueID() const { return fUniqueID; } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 42 | |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Serialize to a buffer. |
| 45 | */ |
| 46 | void flatten(SkWriteBuffer&) const; |
| 47 | |
| 48 | /** |
| 49 | * Recreate an SkTextBlob that was serialized into a buffer. |
| 50 | * |
| 51 | * @param SkReadBuffer Serialized blob data. |
| 52 | * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is |
| 53 | * invalid. |
| 54 | */ |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 55 | static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&); |
| 56 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 57 | enum GlyphPositioning : uint8_t { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 58 | kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph. |
| 59 | kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph. |
| 60 | kFull_Positioning = 2 // Point positioning -- two scalars per glyph. |
| 61 | }; |
| 62 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 63 | /** |
| 64 | * Serialize the typeface into a data blob, storing type uniqueID of each referenced typeface. |
| 65 | * During this process, each time a typeface is encountered, it is passed to the catalog, |
| 66 | * allowing the caller to what typeface IDs will need to be resolved in Deserialize(). |
| 67 | */ |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 68 | sk_sp<SkData> serialize(SkTypefaceCatalogerProc, void* ctx) const; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 69 | |
| 70 | /** |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 71 | * Similar to serialize above, but writes directly into |memory|. Returns bytes written or 0u |
| 72 | * if serialization failed due to insufficient size. |
| 73 | */ |
| 74 | size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const; |
| 75 | |
| 76 | /** |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 77 | * Re-create a text blob previously serialized. Since the serialized form records the uniqueIDs |
| 78 | * of its typefaces, deserialization requires that the caller provide the corresponding |
| 79 | * SkTypefaces for those IDs. |
| 80 | */ |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 81 | static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, |
| 82 | SkTypefaceResolverProc, void* ctx); |
| 83 | |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 84 | sk_sp<SkData> serialize(const SkSerialProcs&) const; |
Mike Reed | f6eb1f9 | 2017-12-19 17:12:13 -0500 | [diff] [blame] | 85 | sk_sp<SkData> serialize() const; |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 86 | static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs&); |
Mike Reed | f6eb1f9 | 2017-12-19 17:12:13 -0500 | [diff] [blame] | 87 | static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size); |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 88 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 89 | private: |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 90 | friend class SkNVRefCnt<SkTextBlob>; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 91 | class RunRecord; |
| 92 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 93 | explicit SkTextBlob(const SkRect& bounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 94 | |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 95 | ~SkTextBlob(); |
bsalomon | 0728031 | 2014-11-20 08:02:46 -0800 | [diff] [blame] | 96 | |
| 97 | // Memory for objects of this class is created with sk_malloc rather than operator new and must |
| 98 | // be freed with sk_free. |
Yong-Hwan Baek | 688a8e5 | 2018-07-09 14:14:26 +0900 | [diff] [blame] | 99 | void operator delete(void* p); |
| 100 | void* operator new(size_t); |
| 101 | void* operator new(size_t, void* p); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 102 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 103 | static unsigned ScalarsPerGlyph(GlyphPositioning pos); |
| 104 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 105 | // Call when this blob is part of the key to a cache entry. This allows the cache |
| 106 | // to know automatically those entries can be purged when this SkTextBlob is deleted. |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 107 | void notifyAddedToCache(uint32_t cacheID) const { |
| 108 | fCacheID.store(cacheID); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Herb Derby | 8a6348e | 2018-07-12 15:30:35 -0400 | [diff] [blame^] | 111 | friend class SkGlyphRunList; |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 112 | friend class GrTextBlobCache; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 113 | friend class SkTextBlobBuilder; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 114 | friend class SkTextBlobRunIterator; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 115 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 116 | const SkRect fBounds; |
| 117 | const uint32_t fUniqueID; |
| 118 | mutable SkAtomic<uint32_t> fCacheID; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 119 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 120 | SkDEBUGCODE(size_t fStorageSize;) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 121 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 122 | // The actual payload resides in externally-managed storage, following the object. |
| 123 | // (see the .cpp for more details) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 124 | |
| 125 | typedef SkRefCnt INHERITED; |
| 126 | }; |
| 127 | |
| 128 | /** \class SkTextBlobBuilder |
| 129 | |
| 130 | Helper class for constructing SkTextBlobs. |
| 131 | */ |
jbroman | 3053dfa | 2014-08-25 06:22:12 -0700 | [diff] [blame] | 132 | class SK_API SkTextBlobBuilder { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 133 | public: |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 134 | SkTextBlobBuilder(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 135 | |
| 136 | ~SkTextBlobBuilder(); |
| 137 | |
| 138 | /** |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 139 | * Returns an immutable SkTextBlob for the current runs/glyphs, |
| 140 | * or nullptr if no runs were allocated. |
| 141 | * |
| 142 | * The builder is reset and can be reused. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 143 | */ |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 144 | sk_sp<SkTextBlob> make(); |
| 145 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Glyph and position buffers associated with a run. |
| 148 | * |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 149 | * A run is a sequence of glyphs sharing the same font metrics |
| 150 | * and positioning mode. |
| 151 | * |
| 152 | * If textByteCount is 0, utf8text and clusters will be NULL (no |
| 153 | * character information will be associated with the glyphs). |
| 154 | * |
| 155 | * utf8text will point to a buffer of size textByteCount bytes. |
| 156 | * |
| 157 | * clusters (if not NULL) will point to an array of size count. |
| 158 | * For each glyph, give the byte-offset into the text for the |
| 159 | * first byte in the first character in that glyph's cluster. |
| 160 | * Each value in the array should be an integer less than |
| 161 | * textByteCount. Values in the array should either be |
| 162 | * monotonically increasing (left-to-right text) or monotonically |
| 163 | * decreasing (right-to-left text). This definiton is conviently |
| 164 | * the same as used by Harfbuzz's hb_glyph_info_t::cluster field, |
| 165 | * except that Harfbuzz interleaves glyphs and clusters. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 166 | */ |
| 167 | struct RunBuffer { |
halcanary | d0e95a5 | 2016-07-25 07:18:12 -0700 | [diff] [blame] | 168 | SkGlyphID* glyphs; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 169 | SkScalar* pos; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 170 | char* utf8text; |
| 171 | uint32_t* clusters; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /** |
| 175 | * Allocates a new default-positioned run and returns its writable glyph buffer |
| 176 | * for direct manipulation. |
| 177 | * |
| 178 | * @param font The font to be used for this run. |
| 179 | * @param count Number of glyphs. |
| 180 | * @param x,y Position within the blob. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 181 | * @param textByteCount length of the original UTF-8 text that |
| 182 | * corresponds to this sequence of glyphs. If 0, |
| 183 | * text will not be included in the textblob. |
| 184 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 185 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 186 | * be used when computing the blob bounds, to avoid re-measuring. |
| 187 | * |
| 188 | * @return A writable glyph buffer, valid until the next allocRun() or |
| 189 | * build() call. The buffer is guaranteed to hold @count@ glyphs. |
| 190 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 191 | const RunBuffer& allocRunText(const SkPaint& font, |
| 192 | int count, |
| 193 | SkScalar x, |
| 194 | SkScalar y, |
| 195 | int textByteCount, |
| 196 | SkString lang, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 197 | const SkRect* bounds = nullptr); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 198 | const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 199 | const SkRect* bounds = nullptr) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 200 | return this->allocRunText(font, count, x, y, 0, SkString(), bounds); |
| 201 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Allocates a new horizontally-positioned run and returns its writable glyph and position |
| 205 | * buffers for direct manipulation. |
| 206 | * |
| 207 | * @param font The font to be used for this run. |
| 208 | * @param count Number of glyphs. |
| 209 | * @param y Vertical offset within the blob. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 210 | * @param textByteCount length of the original UTF-8 text that |
| 211 | * corresponds to this sequence of glyphs. If 0, |
| 212 | * text will not be included in the textblob. |
| 213 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 214 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 215 | * be used when computing the blob bounds, to avoid re-measuring. |
| 216 | * |
| 217 | * @return Writable glyph and position buffers, valid until the next allocRun() |
| 218 | * or build() call. The buffers are guaranteed to hold @count@ elements. |
| 219 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 220 | const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y, |
| 221 | int textByteCount, SkString lang, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 222 | const SkRect* bounds = nullptr); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 223 | const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 224 | const SkRect* bounds = nullptr) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 225 | return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds); |
| 226 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * Allocates a new fully-positioned run and returns its writable glyph and position |
| 230 | * buffers for direct manipulation. |
| 231 | * |
| 232 | * @param font The font to be used for this run. |
| 233 | * @param count Number of glyphs. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 234 | * @param textByteCount length of the original UTF-8 text that |
| 235 | * corresponds to this sequence of glyphs. If 0, |
| 236 | * text will not be included in the textblob. |
| 237 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 238 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 239 | * be used when computing the blob bounds, to avoid re-measuring. |
| 240 | * |
| 241 | * @return Writable glyph and position buffers, valid until the next allocRun() |
| 242 | * or build() call. The glyph buffer and position buffer are |
| 243 | * guaranteed to hold @count@ and 2 * @count@ elements, respectively. |
| 244 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 245 | const RunBuffer& allocRunTextPos(const SkPaint& font, int count, |
| 246 | int textByteCount, SkString lang, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 247 | const SkRect* bounds = nullptr); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 248 | const RunBuffer& allocRunPos(const SkPaint& font, int count, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 249 | const SkRect* bounds = nullptr) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 250 | return this->allocRunTextPos(font, count, 0, SkString(), bounds); |
| 251 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 252 | |
| 253 | private: |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 254 | void reserve(size_t size); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 255 | void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 256 | int count, int textBytes, SkPoint offset, const SkRect* bounds); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 257 | bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 258 | uint32_t count, SkPoint offset); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 259 | void updateDeferredBounds(); |
| 260 | |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 261 | static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&); |
| 262 | static SkRect TightRunBounds(const SkTextBlob::RunRecord&); |
| 263 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 264 | SkAutoTMalloc<uint8_t> fStorage; |
| 265 | size_t fStorageSize; |
| 266 | size_t fStorageUsed; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 267 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 268 | SkRect fBounds; |
| 269 | int fRunCount; |
| 270 | bool fDeferredBounds; |
| 271 | size_t fLastRun; // index into fStorage |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 272 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 273 | RunBuffer fCurrentRunBuffer; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | #endif // SkTextBlob_DEFINED |