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