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