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