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 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 63 | SkTextBlob(int runCount, 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 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 81 | const int fRunCount; |
| 82 | const SkRect fBounds; |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 83 | const uint32_t fUniqueID; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 84 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 85 | SkDEBUGCODE(size_t fStorageSize;) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 86 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 87 | // The actual payload resides in externally-managed storage, following the object. |
| 88 | // (see the .cpp for more details) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 89 | |
| 90 | typedef SkRefCnt INHERITED; |
| 91 | }; |
| 92 | |
| 93 | /** \class SkTextBlobBuilder |
| 94 | |
| 95 | Helper class for constructing SkTextBlobs. |
| 96 | */ |
jbroman | 3053dfa | 2014-08-25 06:22:12 -0700 | [diff] [blame] | 97 | class SK_API SkTextBlobBuilder { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 98 | public: |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 99 | SkTextBlobBuilder(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 100 | |
| 101 | ~SkTextBlobBuilder(); |
| 102 | |
| 103 | /** |
| 104 | * Returns an immutable SkTextBlob for the current runs/glyphs. The builder is reset and |
| 105 | * can be reused. |
| 106 | */ |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 107 | sk_sp<SkTextBlob> make(); |
| 108 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Glyph and position buffers associated with a run. |
| 111 | * |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 112 | * A run is a sequence of glyphs sharing the same font metrics |
| 113 | * and positioning mode. |
| 114 | * |
| 115 | * If textByteCount is 0, utf8text and clusters will be NULL (no |
| 116 | * character information will be associated with the glyphs). |
| 117 | * |
| 118 | * utf8text will point to a buffer of size textByteCount bytes. |
| 119 | * |
| 120 | * clusters (if not NULL) will point to an array of size count. |
| 121 | * For each glyph, give the byte-offset into the text for the |
| 122 | * first byte in the first character in that glyph's cluster. |
| 123 | * Each value in the array should be an integer less than |
| 124 | * textByteCount. Values in the array should either be |
| 125 | * monotonically increasing (left-to-right text) or monotonically |
| 126 | * decreasing (right-to-left text). This definiton is conviently |
| 127 | * the same as used by Harfbuzz's hb_glyph_info_t::cluster field, |
| 128 | * except that Harfbuzz interleaves glyphs and clusters. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 129 | */ |
| 130 | struct RunBuffer { |
halcanary | d0e95a5 | 2016-07-25 07:18:12 -0700 | [diff] [blame] | 131 | SkGlyphID* glyphs; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 132 | SkScalar* pos; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 133 | char* utf8text; |
| 134 | uint32_t* clusters; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * Allocates a new default-positioned run and returns its writable glyph buffer |
| 139 | * for direct manipulation. |
| 140 | * |
| 141 | * @param font The font to be used for this run. |
| 142 | * @param count Number of glyphs. |
| 143 | * @param x,y Position within the blob. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 144 | * @param textByteCount length of the original UTF-8 text that |
| 145 | * corresponds to this sequence of glyphs. If 0, |
| 146 | * text will not be included in the textblob. |
| 147 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 148 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 149 | * be used when computing the blob bounds, to avoid re-measuring. |
| 150 | * |
| 151 | * @return A writable glyph buffer, valid until the next allocRun() or |
| 152 | * build() call. The buffer is guaranteed to hold @count@ glyphs. |
| 153 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 154 | const RunBuffer& allocRunText(const SkPaint& font, |
| 155 | int count, |
| 156 | SkScalar x, |
| 157 | SkScalar y, |
| 158 | int textByteCount, |
| 159 | SkString lang, |
| 160 | const SkRect* bounds = NULL); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 161 | const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 162 | const SkRect* bounds = NULL) { |
| 163 | return this->allocRunText(font, count, x, y, 0, SkString(), bounds); |
| 164 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Allocates a new horizontally-positioned run and returns its writable glyph and position |
| 168 | * buffers for direct manipulation. |
| 169 | * |
| 170 | * @param font The font to be used for this run. |
| 171 | * @param count Number of glyphs. |
| 172 | * @param y Vertical offset within the blob. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 173 | * @param textByteCount length of the original UTF-8 text that |
| 174 | * corresponds to this sequence of glyphs. If 0, |
| 175 | * text will not be included in the textblob. |
| 176 | * @param lang Language code, currently unimplemented. |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 177 | * @param bounds Optional run bounding box. If known in advance (!= NULL), it will |
| 178 | * be used when computing the blob bounds, to avoid re-measuring. |
| 179 | * |
| 180 | * @return Writable glyph and position buffers, valid until the next allocRun() |
| 181 | * or build() call. The buffers are guaranteed to hold @count@ elements. |
| 182 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 183 | const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y, |
| 184 | int textByteCount, SkString lang, |
| 185 | const SkRect* bounds = NULL); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 186 | const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 187 | const SkRect* bounds = NULL) { |
| 188 | return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds); |
| 189 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Allocates a new fully-positioned run and returns its writable glyph and position |
| 193 | * buffers for direct manipulation. |
| 194 | * |
| 195 | * @param font The font to be used for this run. |
| 196 | * @param count Number of glyphs. |
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 glyph buffer and position buffer are |
| 206 | * guaranteed to hold @count@ and 2 * @count@ elements, respectively. |
| 207 | */ |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 208 | const RunBuffer& allocRunTextPos(const SkPaint& font, int count, |
| 209 | int textByteCount, SkString lang, |
| 210 | const SkRect* bounds = NULL); |
| 211 | const RunBuffer& allocRunPos(const SkPaint& font, int count, |
| 212 | const SkRect* bounds = NULL) { |
| 213 | return this->allocRunTextPos(font, count, 0, SkString(), bounds); |
| 214 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 215 | |
| 216 | private: |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 217 | void reserve(size_t size); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 218 | void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 219 | int count, int textBytes, SkPoint offset, const SkRect* bounds); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 220 | bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, |
| 221 | int count, SkPoint offset); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 222 | void updateDeferredBounds(); |
| 223 | |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 224 | static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&); |
| 225 | static SkRect TightRunBounds(const SkTextBlob::RunRecord&); |
| 226 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 227 | SkAutoTMalloc<uint8_t> fStorage; |
| 228 | size_t fStorageSize; |
| 229 | size_t fStorageUsed; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 230 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 231 | SkRect fBounds; |
| 232 | int fRunCount; |
| 233 | bool fDeferredBounds; |
| 234 | size_t fLastRun; // index into fStorage |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 235 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 236 | RunBuffer fCurrentRunBuffer; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | #endif // SkTextBlob_DEFINED |