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 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 8 | #include "SkTextBlobRunIterator.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 9 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 10 | #include "SkSafeMath.h" |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 11 | #include "SkTypeface.h" |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 12 | #include "SkValidatingReadBuffer.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 13 | #include "SkWriteBuffer.h" |
| 14 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 15 | #include <limits> |
| 16 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
| 18 | #include "text/GrTextBlobCache.h" |
| 19 | #endif |
| 20 | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // TODO(fmalita): replace with SkFont. |
| 24 | class RunFont : SkNoncopyable { |
| 25 | public: |
| 26 | RunFont(const SkPaint& paint) |
| 27 | : fSize(paint.getTextSize()) |
| 28 | , fScaleX(paint.getTextScaleX()) |
| 29 | , fTypeface(SkSafeRef(paint.getTypeface())) |
| 30 | , fSkewX(paint.getTextSkewX()) |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 31 | , fAlign(paint.getTextAlign()) |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 32 | , fHinting(paint.getHinting()) |
| 33 | , fFlags(paint.getFlags() & kFlagsMask) { } |
| 34 | |
| 35 | void applyToPaint(SkPaint* paint) const { |
| 36 | paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 37 | paint->setTypeface(fTypeface); |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 38 | paint->setTextSize(fSize); |
| 39 | paint->setTextScaleX(fScaleX); |
| 40 | paint->setTextSkewX(fSkewX); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 41 | paint->setTextAlign(static_cast<SkPaint::Align>(fAlign)); |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 42 | paint->setHinting(static_cast<SkPaint::Hinting>(fHinting)); |
| 43 | |
| 44 | paint->setFlags((paint->getFlags() & ~kFlagsMask) | fFlags); |
| 45 | } |
| 46 | |
| 47 | bool operator==(const RunFont& other) const { |
| 48 | return fTypeface == other.fTypeface |
| 49 | && fSize == other.fSize |
| 50 | && fScaleX == other.fScaleX |
| 51 | && fSkewX == other.fSkewX |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 52 | && fAlign == other.fAlign |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 53 | && fHinting == other.fHinting |
| 54 | && fFlags == other.fFlags; |
| 55 | } |
| 56 | |
| 57 | bool operator!=(const RunFont& other) const { |
| 58 | return !(*this == other); |
| 59 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 60 | |
| 61 | uint32_t flags() const { return fFlags; } |
| 62 | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 63 | private: |
| 64 | const static uint32_t kFlagsMask = |
| 65 | SkPaint::kAntiAlias_Flag | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 66 | SkPaint::kFakeBoldText_Flag | |
| 67 | SkPaint::kLinearText_Flag | |
| 68 | SkPaint::kSubpixelText_Flag | |
| 69 | SkPaint::kDevKernText_Flag | |
| 70 | SkPaint::kLCDRenderText_Flag | |
| 71 | SkPaint::kEmbeddedBitmapText_Flag | |
| 72 | SkPaint::kAutoHinting_Flag | |
| 73 | SkPaint::kVerticalText_Flag | |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 74 | SkPaint::kGenA8FromLCD_Flag; |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 75 | |
| 76 | SkScalar fSize; |
| 77 | SkScalar fScaleX; |
| 78 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 79 | // Keep this sk_sp off the first position, to avoid interfering with SkNoncopyable |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 80 | // empty baseclass optimization (http://code.google.com/p/skia/issues/detail?id=3694). |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 81 | sk_sp<SkTypeface> fTypeface; |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 82 | SkScalar fSkewX; |
| 83 | |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 84 | static_assert(SkPaint::kAlignCount < 4, "insufficient_align_bits"); |
| 85 | uint32_t fAlign : 2; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 86 | static_assert(SkPaint::kFull_Hinting < 4, "insufficient_hinting_bits"); |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 87 | uint32_t fHinting : 2; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 88 | static_assert((kFlagsMask & 0xffff) == kFlagsMask, "insufficient_flags_bits"); |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 89 | uint32_t fFlags : 16; |
| 90 | |
| 91 | typedef SkNoncopyable INHERITED; |
| 92 | }; |
| 93 | |
| 94 | struct RunFontStorageEquivalent { |
| 95 | SkScalar fSize, fScaleX; |
| 96 | void* fTypeface; |
| 97 | SkScalar fSkewX; |
| 98 | uint32_t fFlags; |
| 99 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 100 | static_assert(sizeof(RunFont) == sizeof(RunFontStorageEquivalent), "runfont_should_stay_packed"); |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 101 | |
| 102 | } // anonymous namespace |
| 103 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 104 | // |
| 105 | // Textblob data is laid out into externally-managed storage as follows: |
| 106 | // |
| 107 | // ----------------------------------------------------------------------------- |
| 108 | // | SkTextBlob | RunRecord | Glyphs[] | Pos[] | RunRecord | Glyphs[] | Pos[] | ... |
| 109 | // ----------------------------------------------------------------------------- |
| 110 | // |
| 111 | // Each run record describes a text blob run, and can be used to determine the (implicit) |
| 112 | // location of the following record. |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 113 | // |
| 114 | // Extended Textblob runs have more data after the Pos[] array: |
| 115 | // |
| 116 | // ------------------------------------------------------------------------- |
| 117 | // ... | RunRecord | Glyphs[] | Pos[] | TextSize | Clusters[] | Text[] | ... |
| 118 | // ------------------------------------------------------------------------- |
| 119 | // |
| 120 | // To determine the length of the extended run data, the TextSize must be read. |
| 121 | // |
| 122 | // Extended Textblob runs may be mixed with non-extended runs. |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 123 | |
| 124 | SkDEBUGCODE(static const unsigned kRunRecordMagic = 0xb10bcafe;) |
| 125 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 126 | namespace { |
| 127 | struct RunRecordStorageEquivalent { |
| 128 | RunFont fFont; |
| 129 | SkPoint fOffset; |
| 130 | uint32_t fCount; |
| 131 | uint32_t fFlags; |
| 132 | SkDEBUGCODE(unsigned fMagic;) |
| 133 | }; |
| 134 | } |
| 135 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 136 | class SkTextBlob::RunRecord { |
| 137 | public: |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 138 | RunRecord(uint32_t count, uint32_t textSize, const SkPoint& offset, const SkPaint& font, GlyphPositioning pos) |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 139 | : fFont(font) |
| 140 | , fCount(count) |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 141 | , fOffset(offset) |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 142 | , fFlags(pos) { |
| 143 | SkASSERT(static_cast<unsigned>(pos) <= Flags::kPositioning_Mask); |
| 144 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 145 | SkDEBUGCODE(fMagic = kRunRecordMagic); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 146 | if (textSize > 0) { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 147 | fFlags |= kExtended_Flag; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 148 | *this->textSizePtr() = textSize; |
| 149 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | uint32_t glyphCount() const { |
| 153 | return fCount; |
| 154 | } |
| 155 | |
| 156 | const SkPoint& offset() const { |
| 157 | return fOffset; |
| 158 | } |
| 159 | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 160 | const RunFont& font() const { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 161 | return fFont; |
| 162 | } |
| 163 | |
| 164 | GlyphPositioning positioning() const { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 165 | return static_cast<GlyphPositioning>(fFlags & kPositioning_Mask); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | uint16_t* glyphBuffer() const { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 169 | static_assert(SkIsAlignPtr(sizeof(RunRecord)), ""); |
| 170 | // Glyphs are stored immediately following the record. |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 171 | return reinterpret_cast<uint16_t*>(const_cast<RunRecord*>(this) + 1); |
| 172 | } |
| 173 | |
| 174 | SkScalar* posBuffer() const { |
| 175 | // Position scalars follow the (aligned) glyph buffer. |
| 176 | return reinterpret_cast<SkScalar*>(reinterpret_cast<uint8_t*>(this->glyphBuffer()) + |
| 177 | SkAlign4(fCount * sizeof(uint16_t))); |
| 178 | } |
| 179 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 180 | uint32_t textSize() const { return isExtended() ? *this->textSizePtr() : 0; } |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 181 | |
| 182 | uint32_t* clusterBuffer() const { |
| 183 | // clusters follow the textSize. |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 184 | return isExtended() ? 1 + this->textSizePtr() : nullptr; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | char* textBuffer() const { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 188 | return isExtended() |
| 189 | ? reinterpret_cast<char*>(this->clusterBuffer() + fCount) |
| 190 | : nullptr; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 193 | static size_t StorageSize(uint32_t glyphCount, uint32_t textSize, |
| 194 | SkTextBlob::GlyphPositioning positioning, |
| 195 | SkSafeMath* safe) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 196 | static_assert(SkIsAlign4(sizeof(SkScalar)), "SkScalar size alignment"); |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 197 | |
| 198 | auto glyphSize = safe->mul(glyphCount, sizeof(uint16_t)), |
| 199 | posSize = safe->mul(PosCount(glyphCount, positioning, safe), sizeof(SkScalar)); |
| 200 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 201 | // RunRecord object + (aligned) glyph buffer + position buffer |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 202 | auto size = sizeof(SkTextBlob::RunRecord); |
| 203 | size = safe->add(size, safe->alignUp(glyphSize, 4)); |
| 204 | size = safe->add(size, posSize); |
| 205 | |
| 206 | if (textSize) { // Extended run. |
| 207 | size = safe->add(size, sizeof(uint32_t)); |
| 208 | size = safe->add(size, safe->mul(glyphCount, sizeof(uint32_t))); |
| 209 | size = safe->add(size, textSize); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 210 | } |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 211 | |
| 212 | return safe->alignUp(size, sizeof(void*)); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static const RunRecord* First(const SkTextBlob* blob) { |
| 216 | // The first record (if present) is stored following the blob object. |
| 217 | return reinterpret_cast<const RunRecord*>(blob + 1); |
| 218 | } |
| 219 | |
| 220 | static const RunRecord* Next(const RunRecord* run) { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 221 | return SkToBool(run->fFlags & kLast_Flag) ? nullptr : NextUnchecked(run); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 222 | } |
| 223 | |
fmalita | 92d976c | 2015-10-05 11:09:57 -0700 | [diff] [blame] | 224 | void validate(const uint8_t* storageTop) const { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 225 | SkASSERT(kRunRecordMagic == fMagic); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 226 | SkASSERT((uint8_t*)NextUnchecked(this) <= storageTop); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 227 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 228 | SkASSERT(glyphBuffer() + fCount <= (uint16_t*)posBuffer()); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 229 | SkASSERT(posBuffer() + fCount * ScalarsPerGlyph(positioning()) |
| 230 | <= (SkScalar*)NextUnchecked(this)); |
| 231 | if (isExtended()) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 232 | SkASSERT(textSize() > 0); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 233 | SkASSERT(textSizePtr() < (uint32_t*)NextUnchecked(this)); |
| 234 | SkASSERT(clusterBuffer() < (uint32_t*)NextUnchecked(this)); |
| 235 | SkASSERT(textBuffer() + textSize() <= (char*)NextUnchecked(this)); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 236 | } |
| 237 | static_assert(sizeof(SkTextBlob::RunRecord) == sizeof(RunRecordStorageEquivalent), |
| 238 | "runrecord_should_stay_packed"); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | private: |
| 242 | friend class SkTextBlobBuilder; |
| 243 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 244 | enum Flags { |
| 245 | kPositioning_Mask = 0x03, // bits 0-1 reserved for positioning |
| 246 | kLast_Flag = 0x04, // set for the last blob run |
| 247 | kExtended_Flag = 0x08, // set for runs with text/cluster info |
| 248 | }; |
| 249 | |
| 250 | static const RunRecord* NextUnchecked(const RunRecord* run) { |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 251 | SkSafeMath safe; |
| 252 | auto res = reinterpret_cast<const RunRecord*>( |
| 253 | reinterpret_cast<const uint8_t*>(run) |
| 254 | + StorageSize(run->glyphCount(), run->textSize(), run->positioning(), &safe)); |
| 255 | SkASSERT(safe); |
| 256 | return res; |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 259 | static size_t PosCount(uint32_t glyphCount, |
| 260 | SkTextBlob::GlyphPositioning positioning, |
| 261 | SkSafeMath* safe) { |
| 262 | return safe->mul(glyphCount, ScalarsPerGlyph(positioning)); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 263 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 264 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 265 | uint32_t* textSizePtr() const { |
| 266 | // textSize follows the position buffer. |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 267 | SkASSERT(isExtended()); |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 268 | SkSafeMath safe; |
| 269 | auto res = (uint32_t*)(&this->posBuffer()[PosCount(fCount, positioning(), &safe)]); |
| 270 | SkASSERT(safe); |
| 271 | return res; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 272 | } |
| 273 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 274 | void grow(uint32_t count) { |
| 275 | SkScalar* initialPosBuffer = posBuffer(); |
| 276 | uint32_t initialCount = fCount; |
| 277 | fCount += count; |
| 278 | |
| 279 | // Move the initial pos scalars to their new location. |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 280 | size_t copySize = initialCount * sizeof(SkScalar) * ScalarsPerGlyph(positioning()); |
| 281 | SkASSERT((uint8_t*)posBuffer() + copySize <= (uint8_t*)NextUnchecked(this)); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 282 | |
| 283 | // memmove, as the buffers may overlap |
| 284 | memmove(posBuffer(), initialPosBuffer, copySize); |
| 285 | } |
| 286 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 287 | bool isExtended() const { |
| 288 | return fFlags & kExtended_Flag; |
| 289 | } |
| 290 | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 291 | RunFont fFont; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 292 | uint32_t fCount; |
| 293 | SkPoint fOffset; |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 294 | uint32_t fFlags; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 295 | |
| 296 | SkDEBUGCODE(unsigned fMagic;) |
| 297 | }; |
| 298 | |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 299 | static int32_t gNextID = 1; |
| 300 | static int32_t next_id() { |
| 301 | int32_t id; |
| 302 | do { |
| 303 | id = sk_atomic_inc(&gNextID); |
| 304 | } while (id == SK_InvalidGenID); |
| 305 | return id; |
| 306 | } |
| 307 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 308 | SkTextBlob::SkTextBlob(const SkRect& bounds) |
| 309 | : fBounds(bounds) |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 310 | , fUniqueID(next_id()) |
| 311 | , fAddedToCache(false) {} |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 312 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 313 | SkTextBlob::~SkTextBlob() { |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 314 | #if SK_SUPPORT_GPU |
| 315 | if (fAddedToCache.load()) { |
| 316 | GrTextBlobCache::PostPurgeBlobMessage(fUniqueID); |
| 317 | } |
| 318 | #endif |
| 319 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 320 | const auto* run = RunRecord::First(this); |
| 321 | do { |
| 322 | const auto* nextRun = RunRecord::Next(run); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 323 | SkDEBUGCODE(run->validate((uint8_t*)this + fStorageSize);) |
| 324 | run->~RunRecord(); |
| 325 | run = nextRun; |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 326 | } while (run); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 327 | } |
| 328 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 329 | namespace { |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 330 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 331 | union PositioningAndExtended { |
| 332 | int32_t intValue; |
| 333 | struct { |
| 334 | SkTextBlob::GlyphPositioning positioning; |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 335 | uint8_t extended; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 336 | uint16_t padding; |
| 337 | }; |
| 338 | }; |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 339 | |
| 340 | static_assert(sizeof(PositioningAndExtended) == sizeof(int32_t), ""); |
| 341 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 342 | } // namespace |
| 343 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 344 | unsigned SkTextBlob::ScalarsPerGlyph(GlyphPositioning pos) { |
| 345 | // GlyphPositioning values are directly mapped to scalars-per-glyph. |
| 346 | SkASSERT(pos <= 2); |
| 347 | return pos; |
| 348 | } |
| 349 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 350 | SkTextBlobRunIterator::SkTextBlobRunIterator(const SkTextBlob* blob) |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 351 | : fCurrentRun(SkTextBlob::RunRecord::First(blob)) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 352 | SkDEBUGCODE(fStorageTop = (uint8_t*)blob + blob->fStorageSize;) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 353 | } |
| 354 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 355 | bool SkTextBlobRunIterator::done() const { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 356 | return !fCurrentRun; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 357 | } |
| 358 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 359 | void SkTextBlobRunIterator::next() { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 360 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 361 | |
| 362 | if (!this->done()) { |
| 363 | SkDEBUGCODE(fCurrentRun->validate(fStorageTop);) |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 364 | fCurrentRun = SkTextBlob::RunRecord::Next(fCurrentRun); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 365 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 366 | } |
| 367 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 368 | uint32_t SkTextBlobRunIterator::glyphCount() const { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 369 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 370 | return fCurrentRun->glyphCount(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 371 | } |
| 372 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 373 | const uint16_t* SkTextBlobRunIterator::glyphs() const { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 374 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 375 | return fCurrentRun->glyphBuffer(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 376 | } |
| 377 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 378 | const SkScalar* SkTextBlobRunIterator::pos() const { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 379 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 380 | return fCurrentRun->posBuffer(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 381 | } |
| 382 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 383 | const SkPoint& SkTextBlobRunIterator::offset() const { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 384 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 385 | return fCurrentRun->offset(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 386 | } |
| 387 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 388 | SkTextBlob::GlyphPositioning SkTextBlobRunIterator::positioning() const { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 389 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 390 | return fCurrentRun->positioning(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 391 | } |
| 392 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 393 | void SkTextBlobRunIterator::applyFontToPaint(SkPaint* paint) const { |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 394 | SkASSERT(!this->done()); |
| 395 | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 396 | fCurrentRun->font().applyToPaint(paint); |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 397 | } |
| 398 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 399 | uint32_t* SkTextBlobRunIterator::clusters() const { |
| 400 | SkASSERT(!this->done()); |
| 401 | return fCurrentRun->clusterBuffer(); |
| 402 | } |
| 403 | uint32_t SkTextBlobRunIterator::textSize() const { |
| 404 | SkASSERT(!this->done()); |
| 405 | return fCurrentRun->textSize(); |
| 406 | } |
| 407 | char* SkTextBlobRunIterator::text() const { |
| 408 | SkASSERT(!this->done()); |
| 409 | return fCurrentRun->textBuffer(); |
| 410 | } |
| 411 | |
| 412 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 413 | bool SkTextBlobRunIterator::isLCD() const { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 414 | return SkToBool(fCurrentRun->font().flags() & SkPaint::kLCDRenderText_Flag); |
| 415 | } |
| 416 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 417 | SkTextBlobBuilder::SkTextBlobBuilder() |
| 418 | : fStorageSize(0) |
| 419 | , fStorageUsed(0) |
| 420 | , fRunCount(0) |
| 421 | , fDeferredBounds(false) |
| 422 | , fLastRun(0) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 423 | fBounds.setEmpty(); |
| 424 | } |
| 425 | |
| 426 | SkTextBlobBuilder::~SkTextBlobBuilder() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 427 | if (nullptr != fStorage.get()) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 428 | // We are abandoning runs and must destruct the associated font data. |
| 429 | // The easiest way to accomplish that is to use the blob destructor. |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 430 | this->make(); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 431 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 432 | } |
| 433 | |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 434 | SkRect SkTextBlobBuilder::TightRunBounds(const SkTextBlob::RunRecord& run) { |
| 435 | SkRect bounds; |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 436 | SkPaint paint; |
| 437 | run.font().applyToPaint(&paint); |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 438 | |
| 439 | if (SkTextBlob::kDefault_Positioning == run.positioning()) { |
| 440 | paint.measureText(run.glyphBuffer(), run.glyphCount() * sizeof(uint16_t), &bounds); |
| 441 | return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 442 | } |
| 443 | |
| 444 | SkAutoSTArray<16, SkRect> glyphBounds(run.glyphCount()); |
| 445 | paint.getTextWidths(run.glyphBuffer(), |
| 446 | run.glyphCount() * sizeof(uint16_t), |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 447 | nullptr, |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 448 | glyphBounds.get()); |
| 449 | |
| 450 | SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || |
| 451 | SkTextBlob::kHorizontal_Positioning == run.positioning()); |
| 452 | // kFull_Positioning => [ x, y, x, y... ] |
| 453 | // kHorizontal_Positioning => [ x, x, x... ] |
| 454 | // (const y applied by runBounds.offset(run->offset()) later) |
| 455 | const SkScalar horizontalConstY = 0; |
| 456 | const SkScalar* glyphPosX = run.posBuffer(); |
| 457 | const SkScalar* glyphPosY = (run.positioning() == SkTextBlob::kFull_Positioning) ? |
| 458 | glyphPosX + 1 : &horizontalConstY; |
| 459 | const unsigned posXInc = SkTextBlob::ScalarsPerGlyph(run.positioning()); |
| 460 | const unsigned posYInc = (run.positioning() == SkTextBlob::kFull_Positioning) ? |
| 461 | posXInc : 0; |
| 462 | |
| 463 | bounds.setEmpty(); |
| 464 | for (unsigned i = 0; i < run.glyphCount(); ++i) { |
| 465 | bounds.join(glyphBounds[i].makeOffset(*glyphPosX, *glyphPosY)); |
| 466 | glyphPosX += posXInc; |
| 467 | glyphPosY += posYInc; |
| 468 | } |
| 469 | |
| 470 | SkASSERT((void*)glyphPosX <= SkTextBlob::RunRecord::Next(&run)); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 471 | |
| 472 | return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 473 | } |
| 474 | |
| 475 | SkRect SkTextBlobBuilder::ConservativeRunBounds(const SkTextBlob::RunRecord& run) { |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 476 | SkASSERT(run.glyphCount() > 0); |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 477 | SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || |
| 478 | SkTextBlob::kHorizontal_Positioning == run.positioning()); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 479 | |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 480 | SkPaint paint; |
| 481 | run.font().applyToPaint(&paint); |
| 482 | const SkRect fontBounds = paint.getFontBounds(); |
| 483 | if (fontBounds.isEmpty()) { |
| 484 | // Empty font bounds are likely a font bug. TightBounds has a better chance of |
| 485 | // producing useful results in this case. |
| 486 | return TightRunBounds(run); |
| 487 | } |
| 488 | |
| 489 | // Compute the glyph position bbox. |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 490 | SkRect bounds; |
| 491 | switch (run.positioning()) { |
| 492 | case SkTextBlob::kHorizontal_Positioning: { |
| 493 | const SkScalar* glyphPos = run.posBuffer(); |
| 494 | SkASSERT((void*)(glyphPos + run.glyphCount()) <= SkTextBlob::RunRecord::Next(&run)); |
| 495 | |
| 496 | SkScalar minX = *glyphPos; |
| 497 | SkScalar maxX = *glyphPos; |
| 498 | for (unsigned i = 1; i < run.glyphCount(); ++i) { |
| 499 | SkScalar x = glyphPos[i]; |
| 500 | minX = SkMinScalar(x, minX); |
| 501 | maxX = SkMaxScalar(x, maxX); |
| 502 | } |
| 503 | |
| 504 | bounds.setLTRB(minX, 0, maxX, 0); |
| 505 | } break; |
| 506 | case SkTextBlob::kFull_Positioning: { |
| 507 | const SkPoint* glyphPosPts = reinterpret_cast<const SkPoint*>(run.posBuffer()); |
| 508 | SkASSERT((void*)(glyphPosPts + run.glyphCount()) <= SkTextBlob::RunRecord::Next(&run)); |
| 509 | |
| 510 | bounds.setBounds(glyphPosPts, run.glyphCount()); |
| 511 | } break; |
| 512 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 513 | SK_ABORT("unsupported positioning mode"); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Expand by typeface glyph bounds. |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 517 | bounds.fLeft += fontBounds.left(); |
| 518 | bounds.fTop += fontBounds.top(); |
| 519 | bounds.fRight += fontBounds.right(); |
| 520 | bounds.fBottom += fontBounds.bottom(); |
| 521 | |
| 522 | // Offset by run position. |
| 523 | return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 524 | } |
| 525 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 526 | void SkTextBlobBuilder::updateDeferredBounds() { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 527 | SkASSERT(!fDeferredBounds || fRunCount > 0); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 528 | |
| 529 | if (!fDeferredBounds) { |
| 530 | return; |
| 531 | } |
| 532 | |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 533 | SkASSERT(fLastRun >= sizeof(SkTextBlob)); |
| 534 | SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + |
| 535 | fLastRun); |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 536 | |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 537 | // FIXME: we should also use conservative bounds for kDefault_Positioning. |
| 538 | SkRect runBounds = SkTextBlob::kDefault_Positioning == run->positioning() ? |
| 539 | TightRunBounds(*run) : ConservativeRunBounds(*run); |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 540 | fBounds.join(runBounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 541 | fDeferredBounds = false; |
| 542 | } |
| 543 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 544 | void SkTextBlobBuilder::reserve(size_t size) { |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 545 | SkSafeMath safe; |
| 546 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 547 | // We don't currently pre-allocate, but maybe someday... |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 548 | if (safe.add(fStorageUsed, size) <= fStorageSize && safe) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 551 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 552 | if (0 == fRunCount) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 553 | SkASSERT(nullptr == fStorage.get()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 554 | SkASSERT(0 == fStorageSize); |
| 555 | SkASSERT(0 == fStorageUsed); |
| 556 | |
| 557 | // the first allocation also includes blob storage |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 558 | fStorageUsed = sizeof(SkTextBlob); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 561 | fStorageSize = safe.add(fStorageUsed, size); |
| 562 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 563 | // FYI: This relies on everything we store being relocatable, particularly SkPaint. |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 564 | // Also, this is counting on the underlying realloc to throw when passed max(). |
| 565 | fStorage.realloc(safe ? fStorageSize : std::numeric_limits<size_t>::max()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | bool SkTextBlobBuilder::mergeRun(const SkPaint &font, SkTextBlob::GlyphPositioning positioning, |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 569 | uint32_t count, SkPoint offset) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 570 | if (0 == fLastRun) { |
| 571 | SkASSERT(0 == fRunCount); |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | SkASSERT(fLastRun >= sizeof(SkTextBlob)); |
| 576 | SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + |
| 577 | fLastRun); |
| 578 | SkASSERT(run->glyphCount() > 0); |
| 579 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 580 | if (run->textSize() != 0) { |
| 581 | return false; |
| 582 | } |
| 583 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 584 | if (run->positioning() != positioning |
| 585 | || run->font() != font |
| 586 | || (run->glyphCount() + count < run->glyphCount())) { |
| 587 | return false; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // we can merge same-font/same-positioning runs in the following cases: |
| 591 | // * fully positioned run following another fully positioned run |
| 592 | // * horizontally postioned run following another horizontally positioned run with the same |
| 593 | // y-offset |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 594 | if (SkTextBlob::kFull_Positioning != positioning |
| 595 | && (SkTextBlob::kHorizontal_Positioning != positioning |
| 596 | || run->offset().y() != offset.y())) { |
| 597 | return false; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 600 | SkSafeMath safe; |
| 601 | size_t sizeDelta = |
| 602 | SkTextBlob::RunRecord::StorageSize(run->glyphCount() + count, 0, positioning, &safe) - |
| 603 | SkTextBlob::RunRecord::StorageSize(run->glyphCount() , 0, positioning, &safe); |
| 604 | if (!safe) { |
| 605 | return false; |
| 606 | } |
| 607 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 608 | this->reserve(sizeDelta); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 609 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 610 | // reserve may have realloced |
| 611 | run = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + fLastRun); |
| 612 | uint32_t preMergeCount = run->glyphCount(); |
| 613 | run->grow(count); |
| 614 | |
| 615 | // Callers expect the buffers to point at the newly added slice, ant not at the beginning. |
| 616 | fCurrentRunBuffer.glyphs = run->glyphBuffer() + preMergeCount; |
| 617 | fCurrentRunBuffer.pos = run->posBuffer() |
| 618 | + preMergeCount * SkTextBlob::ScalarsPerGlyph(positioning); |
| 619 | |
| 620 | fStorageUsed += sizeDelta; |
| 621 | |
| 622 | SkASSERT(fStorageUsed <= fStorageSize); |
| 623 | run->validate(fStorage.get() + fStorageUsed); |
| 624 | |
| 625 | return true; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void SkTextBlobBuilder::allocInternal(const SkPaint &font, |
| 629 | SkTextBlob::GlyphPositioning positioning, |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 630 | int count, int textSize, SkPoint offset, |
| 631 | const SkRect* bounds) { |
| 632 | if (count <= 0 || textSize < 0 || font.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { |
| 633 | fCurrentRunBuffer = { nullptr, nullptr, nullptr, nullptr }; |
| 634 | return; |
| 635 | } |
| 636 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 637 | if (textSize != 0 || !this->mergeRun(font, positioning, count, offset)) { |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 638 | this->updateDeferredBounds(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 639 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 640 | SkSafeMath safe; |
| 641 | size_t runSize = SkTextBlob::RunRecord::StorageSize(count, textSize, positioning, &safe); |
| 642 | if (!safe) { |
| 643 | fCurrentRunBuffer = { nullptr, nullptr, nullptr, nullptr }; |
| 644 | return; |
| 645 | } |
| 646 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 647 | this->reserve(runSize); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 648 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 649 | SkASSERT(fStorageUsed >= sizeof(SkTextBlob)); |
| 650 | SkASSERT(fStorageUsed + runSize <= fStorageSize); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 651 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 652 | SkTextBlob::RunRecord* run = new (fStorage.get() + fStorageUsed) |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 653 | SkTextBlob::RunRecord(count, textSize, offset, font, positioning); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 654 | fCurrentRunBuffer.glyphs = run->glyphBuffer(); |
| 655 | fCurrentRunBuffer.pos = run->posBuffer(); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 656 | fCurrentRunBuffer.utf8text = run->textBuffer(); |
| 657 | fCurrentRunBuffer.clusters = run->clusterBuffer(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 658 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 659 | fLastRun = fStorageUsed; |
| 660 | fStorageUsed += runSize; |
| 661 | fRunCount++; |
| 662 | |
| 663 | SkASSERT(fStorageUsed <= fStorageSize); |
| 664 | run->validate(fStorage.get() + fStorageUsed); |
| 665 | } |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 666 | SkASSERT(textSize > 0 || nullptr == fCurrentRunBuffer.utf8text); |
| 667 | SkASSERT(textSize > 0 || nullptr == fCurrentRunBuffer.clusters); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 668 | if (!fDeferredBounds) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 669 | if (bounds) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 670 | fBounds.join(*bounds); |
| 671 | } else { |
| 672 | fDeferredBounds = true; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 677 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunText(const SkPaint& font, int count, |
| 678 | SkScalar x, SkScalar y, |
| 679 | int textByteCount, |
| 680 | SkString lang, |
| 681 | const SkRect* bounds) { |
| 682 | this->allocInternal(font, SkTextBlob::kDefault_Positioning, count, textByteCount, SkPoint::Make(x, y), bounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 683 | return fCurrentRunBuffer; |
| 684 | } |
| 685 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 686 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPosH(const SkPaint& font, int count, |
| 687 | SkScalar y, |
| 688 | int textByteCount, |
| 689 | SkString lang, |
| 690 | const SkRect* bounds) { |
| 691 | this->allocInternal(font, SkTextBlob::kHorizontal_Positioning, count, textByteCount, SkPoint::Make(0, y), |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 692 | bounds); |
| 693 | |
| 694 | return fCurrentRunBuffer; |
| 695 | } |
| 696 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 697 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPos(const SkPaint& font, int count, |
| 698 | int textByteCount, |
| 699 | SkString lang, |
| 700 | const SkRect *bounds) { |
Ben Wagner | dff47af | 2017-08-30 16:24:22 -0400 | [diff] [blame] | 701 | this->allocInternal(font, SkTextBlob::kFull_Positioning, count, textByteCount, SkPoint::Make(0, 0), bounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 702 | |
| 703 | return fCurrentRunBuffer; |
| 704 | } |
| 705 | |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 706 | sk_sp<SkTextBlob> SkTextBlobBuilder::make() { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 707 | if (!fRunCount) { |
| 708 | // We don't instantiate empty blobs. |
| 709 | SkASSERT(!fStorage.get()); |
| 710 | SkASSERT(fStorageUsed == 0); |
| 711 | SkASSERT(fStorageSize == 0); |
| 712 | SkASSERT(fLastRun == 0); |
| 713 | SkASSERT(fBounds.isEmpty()); |
| 714 | return nullptr; |
| 715 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 716 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 717 | this->updateDeferredBounds(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 718 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 719 | // Tag the last run as such. |
| 720 | auto* lastRun = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + fLastRun); |
| 721 | lastRun->fFlags |= SkTextBlob::RunRecord::kLast_Flag; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 722 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 723 | SkTextBlob* blob = new (fStorage.release()) SkTextBlob(fBounds); |
fmalita | 92d976c | 2015-10-05 11:09:57 -0700 | [diff] [blame] | 724 | SkDEBUGCODE(const_cast<SkTextBlob*>(blob)->fStorageSize = fStorageSize;) |
| 725 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 726 | SkDEBUGCODE( |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 727 | SkSafeMath safe; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 728 | size_t validateSize = sizeof(SkTextBlob); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 729 | for (const auto* run = SkTextBlob::RunRecord::First(blob); run; |
| 730 | run = SkTextBlob::RunRecord::Next(run)) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 731 | validateSize += SkTextBlob::RunRecord::StorageSize( |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 732 | run->fCount, run->textSize(), run->positioning(), &safe); |
fmalita | 92d976c | 2015-10-05 11:09:57 -0700 | [diff] [blame] | 733 | run->validate(reinterpret_cast<const uint8_t*>(blob) + fStorageUsed); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 734 | fRunCount--; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 735 | } |
| 736 | SkASSERT(validateSize == fStorageUsed); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 737 | SkASSERT(fRunCount == 0); |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame^] | 738 | SkASSERT(safe); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 739 | ) |
| 740 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 741 | fStorageUsed = 0; |
| 742 | fStorageSize = 0; |
| 743 | fRunCount = 0; |
| 744 | fLastRun = 0; |
| 745 | fBounds.setEmpty(); |
| 746 | |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 747 | return sk_sp<SkTextBlob>(blob); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 748 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 749 | |
| 750 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 751 | |
| 752 | void SkTextBlob::flatten(SkWriteBuffer& buffer) const { |
| 753 | buffer.writeRect(fBounds); |
| 754 | |
| 755 | SkPaint runPaint; |
| 756 | SkTextBlobRunIterator it(this); |
| 757 | while (!it.done()) { |
| 758 | SkASSERT(it.glyphCount() > 0); |
| 759 | |
| 760 | buffer.write32(it.glyphCount()); |
| 761 | PositioningAndExtended pe; |
| 762 | pe.intValue = 0; |
| 763 | pe.positioning = it.positioning(); |
| 764 | SkASSERT((int32_t)it.positioning() == pe.intValue); // backwards compat. |
| 765 | |
| 766 | uint32_t textSize = it.textSize(); |
| 767 | pe.extended = textSize > 0; |
| 768 | buffer.write32(pe.intValue); |
| 769 | if (pe.extended) { |
| 770 | buffer.write32(textSize); |
| 771 | } |
| 772 | buffer.writePoint(it.offset()); |
| 773 | // This should go away when switching to SkFont |
| 774 | it.applyFontToPaint(&runPaint); |
| 775 | buffer.writePaint(runPaint); |
| 776 | |
| 777 | buffer.writeByteArray(it.glyphs(), it.glyphCount() * sizeof(uint16_t)); |
| 778 | buffer.writeByteArray(it.pos(), |
| 779 | it.glyphCount() * sizeof(SkScalar) * ScalarsPerGlyph(it.positioning())); |
| 780 | if (pe.extended) { |
| 781 | buffer.writeByteArray(it.clusters(), sizeof(uint32_t) * it.glyphCount()); |
| 782 | buffer.writeByteArray(it.text(), it.textSize()); |
| 783 | } |
| 784 | |
| 785 | it.next(); |
| 786 | } |
| 787 | |
| 788 | // Marker for the last run (0 is not a valid glyph count). |
| 789 | buffer.write32(0); |
| 790 | } |
| 791 | |
| 792 | sk_sp<SkTextBlob> SkTextBlob::MakeFromBuffer(SkReadBuffer& reader) { |
| 793 | const int runCount = reader.isVersionLT(SkReadBuffer::kTextBlobImplicitRunCount_Version) |
| 794 | ? reader.read32() : std::numeric_limits<int>::max(); |
| 795 | if (runCount < 0) { |
| 796 | return nullptr; |
| 797 | } |
| 798 | |
| 799 | SkRect bounds; |
| 800 | reader.readRect(&bounds); |
| 801 | |
| 802 | SkTextBlobBuilder blobBuilder; |
| 803 | for (int i = 0; i < runCount; ++i) { |
| 804 | int glyphCount = reader.read32(); |
| 805 | if (glyphCount == 0 && |
| 806 | !reader.isVersionLT(SkReadBuffer::kTextBlobImplicitRunCount_Version)) { |
| 807 | // End-of-runs marker. |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | PositioningAndExtended pe; |
| 812 | pe.intValue = reader.read32(); |
| 813 | GlyphPositioning pos = pe.positioning; |
| 814 | if (glyphCount <= 0 || pos > kFull_Positioning) { |
| 815 | return nullptr; |
| 816 | } |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 817 | int textSize = pe.extended ? reader.read32() : 0; |
| 818 | if (textSize < 0) { |
| 819 | return nullptr; |
| 820 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 821 | |
| 822 | SkPoint offset; |
| 823 | reader.readPoint(&offset); |
| 824 | SkPaint font; |
| 825 | reader.readPaint(&font); |
| 826 | |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 827 | if (!reader.isValid()) { |
| 828 | return nullptr; |
| 829 | } |
| 830 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 831 | const SkTextBlobBuilder::RunBuffer* buf = nullptr; |
| 832 | switch (pos) { |
| 833 | case kDefault_Positioning: |
| 834 | buf = &blobBuilder.allocRunText(font, glyphCount, offset.x(), offset.y(), |
| 835 | textSize, SkString(), &bounds); |
| 836 | break; |
| 837 | case kHorizontal_Positioning: |
| 838 | buf = &blobBuilder.allocRunTextPosH(font, glyphCount, offset.y(), |
| 839 | textSize, SkString(), &bounds); |
| 840 | break; |
| 841 | case kFull_Positioning: |
| 842 | buf = &blobBuilder.allocRunTextPos(font, glyphCount, textSize, SkString(), &bounds); |
| 843 | break; |
| 844 | default: |
| 845 | return nullptr; |
| 846 | } |
| 847 | |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 848 | if (!buf->glyphs || |
| 849 | !buf->pos || |
| 850 | (pe.extended && (!buf->clusters || !buf->utf8text))) { |
| 851 | return nullptr; |
| 852 | } |
| 853 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 854 | if (!reader.readByteArray(buf->glyphs, glyphCount * sizeof(uint16_t)) || |
| 855 | !reader.readByteArray(buf->pos, |
| 856 | glyphCount * sizeof(SkScalar) * ScalarsPerGlyph(pos))) { |
| 857 | return nullptr; |
| 858 | } |
| 859 | |
| 860 | if (pe.extended) { |
| 861 | if (!reader.readByteArray(buf->clusters, glyphCount * sizeof(uint32_t)) || |
| 862 | !reader.readByteArray(buf->utf8text, textSize)) { |
| 863 | return nullptr; |
| 864 | } |
| 865 | } |
| 866 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 867 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 868 | return blobBuilder.make(); |
| 869 | } |
| 870 | |
| 871 | class SkTypefaceCatalogerWriteBuffer : public SkBinaryWriteBuffer { |
| 872 | public: |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 873 | SkTypefaceCatalogerWriteBuffer(SkTypefaceCatalogerProc proc, void* ctx) |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 874 | : SkBinaryWriteBuffer(SkBinaryWriteBuffer::kCrossProcess_Flag) |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 875 | , fCatalogerProc(proc) |
| 876 | , fCatalogerCtx(ctx) |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 877 | {} |
| 878 | |
| 879 | void writeTypeface(SkTypeface* typeface) override { |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 880 | fCatalogerProc(typeface, fCatalogerCtx); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 881 | this->write32(typeface ? typeface->uniqueID() : 0); |
| 882 | } |
| 883 | |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 884 | SkTypefaceCatalogerProc fCatalogerProc; |
| 885 | void* fCatalogerCtx; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 886 | }; |
| 887 | |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 888 | sk_sp<SkData> SkTextBlob::serialize(SkTypefaceCatalogerProc proc, void* ctx) const { |
| 889 | SkTypefaceCatalogerWriteBuffer buffer(proc, ctx); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 890 | this->flatten(buffer); |
| 891 | |
| 892 | size_t total = buffer.bytesWritten(); |
| 893 | sk_sp<SkData> data = SkData::MakeUninitialized(total); |
| 894 | buffer.writeToMemory(data->writable_data()); |
| 895 | return data; |
| 896 | } |
| 897 | |
Mike Reed | e84b482 | 2017-11-15 12:11:01 -0500 | [diff] [blame] | 898 | class SkTypefaceResolverReadBuffer : public SkValidatingReadBuffer { |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 899 | public: |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 900 | SkTypefaceResolverReadBuffer(const void* data, size_t size, SkTypefaceResolverProc proc, |
| 901 | void* ctx) |
Mike Reed | e84b482 | 2017-11-15 12:11:01 -0500 | [diff] [blame] | 902 | : SkValidatingReadBuffer(data, size) |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 903 | , fResolverProc(proc) |
| 904 | , fResolverCtx(ctx) |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 905 | {} |
| 906 | |
| 907 | sk_sp<SkTypeface> readTypeface() override { |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 908 | auto id = this->readUInt(); |
| 909 | return this->isValid() ? fResolverProc(id, fResolverCtx) : nullptr; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 910 | } |
| 911 | |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 912 | SkTypefaceResolverProc fResolverProc; |
| 913 | void* fResolverCtx; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 914 | }; |
| 915 | |
| 916 | sk_sp<SkTextBlob> SkTextBlob::Deserialize(const void* data, size_t length, |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 917 | SkTypefaceResolverProc proc, void* ctx) { |
| 918 | SkTypefaceResolverReadBuffer buffer(data, length, proc, ctx); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 919 | return SkTextBlob::MakeFromBuffer(buffer); |
| 920 | } |