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" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 12 | #include "SkPaint.h" |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 13 | #include "SkString.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 15 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 16 | class SkReadBuffer; |
| 17 | class SkWriteBuffer; |
| 18 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 19 | /** \class SkTextBlob |
| 20 | |
| 21 | SkTextBlob combines multiple text runs into an immutable, ref-counted structure. |
| 22 | */ |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 23 | class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 24 | public: |
| 25 | /** |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 26 | * Returns a conservative blob bounding box. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 27 | */ |
| 28 | const SkRect& bounds() const { return fBounds; } |
| 29 | |
| 30 | /** |
| 31 | * Return a non-zero, unique value representing the text blob. |
| 32 | */ |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 33 | uint32_t uniqueID() const { return fUniqueID; } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 34 | |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Serialize to a buffer. |
| 37 | */ |
| 38 | void flatten(SkWriteBuffer&) const; |
| 39 | |
| 40 | /** |
| 41 | * Recreate an SkTextBlob that was serialized into a buffer. |
| 42 | * |
| 43 | * @param SkReadBuffer Serialized blob data. |
| 44 | * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is |
| 45 | * invalid. |
| 46 | */ |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 47 | static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&); |
| 48 | |
| 49 | static const SkTextBlob* CreateFromBuffer(SkReadBuffer& buffer) { |
| 50 | return MakeFromBuffer(buffer).release(); |
| 51 | } |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 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 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 59 | private: |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 60 | friend class SkNVRefCnt<SkTextBlob>; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 61 | class RunRecord; |
| 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. |
| 69 | void operator delete(void* p) { sk_free(p); } |
| 70 | void* operator new(size_t) { |
| 71 | SkFAIL("All blobs are created by placement new."); |
| 72 | return sk_malloc_throw(0); |
| 73 | } |
| 74 | void* operator new(size_t, void* p) { return p; } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 75 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 76 | static unsigned ScalarsPerGlyph(GlyphPositioning pos); |
| 77 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 78 | friend class SkTextBlobBuilder; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 79 | friend class SkTextBlobRunIterator; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 80 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame^] | 81 | const SkRect fBounds; |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 82 | const uint32_t fUniqueID; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 83 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 84 | SkDEBUGCODE(size_t fStorageSize;) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 85 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 86 | // The actual payload resides in externally-managed storage, following the object. |
| 87 | // (see the .cpp for more details) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 88 | |
| 89 | typedef SkRefCnt INHERITED; |
| 90 | }; |
| 91 | |
| 92 | /** \class SkTextBlobBuilder |
| 93 | |
| 94 | Helper class for constructing SkTextBlobs. |
| 95 | */ |
jbroman | 3053dfa | 2014-08-25 06:22:12 -0700 | [diff] [blame] | 96 | class SK_API SkTextBlobBuilder { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 97 | public: |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 98 | SkTextBlobBuilder(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 99 | |
| 100 | ~SkTextBlobBuilder(); |
| 101 | |
| 102 | /** |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame^] | 103 | * Returns an immutable SkTextBlob for the current runs/glyphs, |
| 104 | * or nullptr if no runs were allocated. |
| 105 | * |
| 106 | * The builder is reset and can be reused. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 107 | */ |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 108 | sk_sp<SkTextBlob> make(); |
| 109 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Glyph and position buffers associated with a run. |
| 112 | * |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 113 | * A run is a sequence of glyphs sharing the same font metrics |
| 114 | * and positioning mode. |
| 115 | * |
| 116 | * If textByteCount is 0, utf8text and clusters will be NULL (no |
| 117 | * character information will be associated with the glyphs). |
| 118 | * |
| 119 | * utf8text will point to a buffer of size textByteCount bytes. |
| 120 | * |
| 121 | * clusters (if not NULL) will point to an array of size count. |
| 122 | * For each glyph, give the byte-offset into the text for the |
| 123 | * first byte in the first character in that glyph's cluster. |
| 124 | * Each value in the array should be an integer less than |
| 125 | * textByteCount. Values in the array should either be |
| 126 | * monotonically increasing (left-to-right text) or monotonically |
| 127 | * decreasing (right-to-left text). This definiton is conviently |
| 128 | * the same as used by Harfbuzz's hb_glyph_info_t::cluster field, |
| 129 | * except that Harfbuzz interleaves glyphs and clusters. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 130 | */ |
| 131 | struct RunBuffer { |
halcanary | d0e95a5 | 2016-07-25 07:18:12 -0700 | [diff] [blame] | 132 | SkGlyphID* glyphs; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 133 | SkScalar* pos; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 134 | char* utf8text; |
| 135 | uint32_t* clusters; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * Allocates a new default-positioned run and returns its writable glyph buffer |
| 140 | * for direct manipulation. |
| 141 | * |
| 142 | * @param font The font to be used for this run. |
| 143 | * @param count Number of glyphs. |
| 144 | * @param x,y Position within the blob. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 145 | * @param textByteCount length of the original UTF-8 text that |
| 146 | * corresponds to this sequence of glyphs. If 0, |
| 147 | * text will not be included in the textblob. |
| 148 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 149 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 150 | * be used when computing the blob bounds, to avoid re-measuring. |
| 151 | * |
| 152 | * @return A writable glyph buffer, valid until the next allocRun() or |
| 153 | * build() call. The buffer is guaranteed to hold @count@ glyphs. |
| 154 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 155 | const RunBuffer& allocRunText(const SkPaint& font, |
| 156 | int count, |
| 157 | SkScalar x, |
| 158 | SkScalar y, |
| 159 | int textByteCount, |
| 160 | SkString lang, |
| 161 | const SkRect* bounds = NULL); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 162 | const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 163 | const SkRect* bounds = NULL) { |
| 164 | return this->allocRunText(font, count, x, y, 0, SkString(), bounds); |
| 165 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Allocates a new horizontally-positioned run and returns its writable glyph and position |
| 169 | * buffers for direct manipulation. |
| 170 | * |
| 171 | * @param font The font to be used for this run. |
| 172 | * @param count Number of glyphs. |
| 173 | * @param y Vertical offset 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 Writable glyph and position buffers, valid until the next allocRun() |
| 182 | * or build() call. The buffers are guaranteed to hold @count@ elements. |
| 183 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 184 | const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y, |
| 185 | int textByteCount, SkString lang, |
| 186 | const SkRect* bounds = NULL); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 187 | const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 188 | const SkRect* bounds = NULL) { |
| 189 | return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds); |
| 190 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Allocates a new fully-positioned run and returns its writable glyph and position |
| 194 | * buffers for direct manipulation. |
| 195 | * |
| 196 | * @param font The font to be used for this run. |
| 197 | * @param count Number of glyphs. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 198 | * @param textByteCount length of the original UTF-8 text that |
| 199 | * corresponds to this sequence of glyphs. If 0, |
| 200 | * text will not be included in the textblob. |
| 201 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 202 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 203 | * be used when computing the blob bounds, to avoid re-measuring. |
| 204 | * |
| 205 | * @return Writable glyph and position buffers, valid until the next allocRun() |
| 206 | * or build() call. The glyph buffer and position buffer are |
| 207 | * guaranteed to hold @count@ and 2 * @count@ elements, respectively. |
| 208 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 209 | const RunBuffer& allocRunTextPos(const SkPaint& font, int count, |
| 210 | int textByteCount, SkString lang, |
| 211 | const SkRect* bounds = NULL); |
| 212 | const RunBuffer& allocRunPos(const SkPaint& font, int count, |
| 213 | const SkRect* bounds = NULL) { |
| 214 | return this->allocRunTextPos(font, count, 0, SkString(), bounds); |
| 215 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 216 | |
| 217 | private: |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 218 | void reserve(size_t size); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 219 | void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 220 | int count, int textBytes, SkPoint offset, const SkRect* bounds); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 221 | bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, |
| 222 | int count, SkPoint offset); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 223 | void updateDeferredBounds(); |
| 224 | |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 225 | static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&); |
| 226 | static SkRect TightRunBounds(const SkTextBlob::RunRecord&); |
| 227 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 228 | SkAutoTMalloc<uint8_t> fStorage; |
| 229 | size_t fStorageSize; |
| 230 | size_t fStorageUsed; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 231 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 232 | SkRect fBounds; |
| 233 | int fRunCount; |
| 234 | bool fDeferredBounds; |
| 235 | size_t fLastRun; // index into fStorage |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 236 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 237 | RunBuffer fCurrentRunBuffer; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | #endif // SkTextBlob_DEFINED |