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