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 | |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 8 | #include "SkTextBlob.h" |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 9 | #include "SkFontPriv.h" |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 10 | #include "SkGlyphRun.h" |
Herb Derby | eb3f674 | 2018-03-05 14:36:45 -0500 | [diff] [blame] | 11 | #include "SkPaintPriv.h" |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 13 | #include "SkRSXform.h" |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 14 | #include "SkSafeMath.h" |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 15 | #include "SkTextBlobPriv.h" |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 16 | #include "SkTypeface.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 17 | #include "SkWriteBuffer.h" |
| 18 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 19 | #include <atomic> |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 20 | #include <limits> |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 21 | #include <new> |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 22 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 23 | #if SK_SUPPORT_GPU |
| 24 | #include "text/GrTextBlobCache.h" |
| 25 | #endif |
| 26 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 27 | namespace { |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 28 | struct RunFontStorageEquivalent { |
| 29 | SkScalar fSize, fScaleX; |
| 30 | void* fTypeface; |
| 31 | SkScalar fSkewX; |
| 32 | uint32_t fFlags; |
| 33 | }; |
Mike Reed | 7b7ab59 | 2018-12-05 21:32:05 -0500 | [diff] [blame] | 34 | static_assert(sizeof(SkFont) == sizeof(RunFontStorageEquivalent), "runfont_should_stay_packed"); |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 35 | } |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 36 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 37 | size_t SkTextBlob::RunRecord::StorageSize(uint32_t glyphCount, uint32_t textSize, |
| 38 | SkTextBlob::GlyphPositioning positioning, |
| 39 | SkSafeMath* safe) { |
| 40 | static_assert(SkIsAlign4(sizeof(SkScalar)), "SkScalar size alignment"); |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 41 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 42 | auto glyphSize = safe->mul(glyphCount, sizeof(uint16_t)), |
| 43 | posSize = safe->mul(PosCount(glyphCount, positioning, safe), sizeof(SkScalar)); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 44 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 45 | // RunRecord object + (aligned) glyph buffer + position buffer |
| 46 | auto size = sizeof(SkTextBlob::RunRecord); |
| 47 | size = safe->add(size, safe->alignUp(glyphSize, 4)); |
| 48 | size = safe->add(size, posSize); |
| 49 | |
| 50 | if (textSize) { // Extended run. |
| 51 | size = safe->add(size, sizeof(uint32_t)); |
| 52 | size = safe->add(size, safe->mul(glyphCount, sizeof(uint32_t))); |
| 53 | size = safe->add(size, textSize); |
| 54 | } |
| 55 | |
| 56 | return safe->alignUp(size, sizeof(void*)); |
| 57 | } |
| 58 | |
| 59 | const SkTextBlob::RunRecord* SkTextBlob::RunRecord::First(const SkTextBlob* blob) { |
| 60 | // The first record (if present) is stored following the blob object. |
| 61 | // (aligned up to make the RunRecord aligned too) |
| 62 | return reinterpret_cast<const RunRecord*>(SkAlignPtr((uintptr_t)(blob + 1))); |
| 63 | } |
| 64 | |
| 65 | const SkTextBlob::RunRecord* SkTextBlob::RunRecord::Next(const RunRecord* run) { |
| 66 | return SkToBool(run->fFlags & kLast_Flag) ? nullptr : NextUnchecked(run); |
| 67 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 68 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 69 | namespace { |
| 70 | struct RunRecordStorageEquivalent { |
Mike Reed | 7b7ab59 | 2018-12-05 21:32:05 -0500 | [diff] [blame] | 71 | SkFont fFont; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 72 | SkPoint fOffset; |
| 73 | uint32_t fCount; |
| 74 | uint32_t fFlags; |
| 75 | SkDEBUGCODE(unsigned fMagic;) |
| 76 | }; |
| 77 | } |
| 78 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 79 | void SkTextBlob::RunRecord::validate(const uint8_t* storageTop) const { |
| 80 | SkASSERT(kRunRecordMagic == fMagic); |
| 81 | SkASSERT((uint8_t*)NextUnchecked(this) <= storageTop); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 82 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 83 | SkASSERT(glyphBuffer() + fCount <= (uint16_t*)posBuffer()); |
| 84 | SkASSERT(posBuffer() + fCount * ScalarsPerGlyph(positioning()) |
| 85 | <= (SkScalar*)NextUnchecked(this)); |
| 86 | if (isExtended()) { |
| 87 | SkASSERT(textSize() > 0); |
| 88 | SkASSERT(textSizePtr() < (uint32_t*)NextUnchecked(this)); |
| 89 | SkASSERT(clusterBuffer() < (uint32_t*)NextUnchecked(this)); |
| 90 | SkASSERT(textBuffer() + textSize() <= (char*)NextUnchecked(this)); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 91 | } |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 92 | static_assert(sizeof(SkTextBlob::RunRecord) == sizeof(RunRecordStorageEquivalent), |
| 93 | "runrecord_should_stay_packed"); |
| 94 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 95 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 96 | const SkTextBlob::RunRecord* SkTextBlob::RunRecord::NextUnchecked(const RunRecord* run) { |
| 97 | SkSafeMath safe; |
| 98 | auto res = reinterpret_cast<const RunRecord*>( |
| 99 | reinterpret_cast<const uint8_t*>(run) |
| 100 | + StorageSize(run->glyphCount(), run->textSize(), run->positioning(), &safe)); |
| 101 | SkASSERT(safe); |
| 102 | return res; |
| 103 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 104 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 105 | size_t SkTextBlob::RunRecord::PosCount(uint32_t glyphCount, |
| 106 | SkTextBlob::GlyphPositioning positioning, |
| 107 | SkSafeMath* safe) { |
| 108 | return safe->mul(glyphCount, ScalarsPerGlyph(positioning)); |
| 109 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 110 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 111 | uint32_t* SkTextBlob::RunRecord::textSizePtr() const { |
| 112 | // textSize follows the position buffer. |
| 113 | SkASSERT(isExtended()); |
| 114 | SkSafeMath safe; |
| 115 | auto res = (uint32_t*)(&this->posBuffer()[PosCount(fCount, positioning(), &safe)]); |
| 116 | SkASSERT(safe); |
| 117 | return res; |
| 118 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 119 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 120 | void SkTextBlob::RunRecord::grow(uint32_t count) { |
| 121 | SkScalar* initialPosBuffer = posBuffer(); |
| 122 | uint32_t initialCount = fCount; |
| 123 | fCount += count; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 124 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 125 | // Move the initial pos scalars to their new location. |
| 126 | size_t copySize = initialCount * sizeof(SkScalar) * ScalarsPerGlyph(positioning()); |
| 127 | SkASSERT((uint8_t*)posBuffer() + copySize <= (uint8_t*)NextUnchecked(this)); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 128 | |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 129 | // memmove, as the buffers may overlap |
| 130 | memmove(posBuffer(), initialPosBuffer, copySize); |
| 131 | } |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 132 | |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 133 | static int32_t next_id() { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 134 | static std::atomic<int32_t> nextID{1}; |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 135 | int32_t id; |
| 136 | do { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 137 | id = nextID++; |
joshualitt | 2af8583 | 2015-03-25 13:40:13 -0700 | [diff] [blame] | 138 | } while (id == SK_InvalidGenID); |
| 139 | return id; |
| 140 | } |
| 141 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 142 | SkTextBlob::SkTextBlob(const SkRect& bounds) |
| 143 | : fBounds(bounds) |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 144 | , fUniqueID(next_id()) |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 145 | , fCacheID(SK_InvalidUniqueID) {} |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 146 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 147 | SkTextBlob::~SkTextBlob() { |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 148 | #if SK_SUPPORT_GPU |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 149 | if (SK_InvalidUniqueID != fCacheID.load()) { |
| 150 | GrTextBlobCache::PostPurgeBlobMessage(fUniqueID, fCacheID); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 151 | } |
| 152 | #endif |
| 153 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 154 | const auto* run = RunRecord::First(this); |
| 155 | do { |
| 156 | const auto* nextRun = RunRecord::Next(run); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 157 | SkDEBUGCODE(run->validate((uint8_t*)this + fStorageSize);) |
| 158 | run->~RunRecord(); |
| 159 | run = nextRun; |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 160 | } while (run); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 163 | namespace { |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 164 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 165 | union PositioningAndExtended { |
| 166 | int32_t intValue; |
| 167 | struct { |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 168 | uint8_t positioning; |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 169 | uint8_t extended; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 170 | uint16_t padding; |
| 171 | }; |
| 172 | }; |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 173 | |
| 174 | static_assert(sizeof(PositioningAndExtended) == sizeof(int32_t), ""); |
| 175 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 176 | } // namespace |
| 177 | |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 178 | enum SkTextBlob::GlyphPositioning : uint8_t { |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 179 | kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph. |
| 180 | kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph. |
| 181 | kFull_Positioning = 2, // Point positioning -- two scalars per glyph. |
| 182 | kRSXform_Positioning = 3, // RSXform positioning -- four scalars per glyph. |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 183 | }; |
| 184 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 185 | unsigned SkTextBlob::ScalarsPerGlyph(GlyphPositioning pos) { |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 186 | const uint8_t gScalarsPerPositioning[] = { |
| 187 | 0, // kDefault_Positioning |
| 188 | 1, // kHorizontal_Positioning |
| 189 | 2, // kFull_Positioning |
| 190 | 4, // kRSXform_Positioning |
| 191 | }; |
| 192 | SkASSERT((unsigned)pos <= 3); |
| 193 | return gScalarsPerPositioning[pos]; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Yong-Hwan Baek | 688a8e5 | 2018-07-09 14:14:26 +0900 | [diff] [blame] | 196 | void SkTextBlob::operator delete(void* p) { |
| 197 | sk_free(p); |
| 198 | } |
| 199 | |
| 200 | void* SkTextBlob::operator new(size_t) { |
| 201 | SK_ABORT("All blobs are created by placement new."); |
| 202 | return sk_malloc_throw(0); |
| 203 | } |
| 204 | |
| 205 | void* SkTextBlob::operator new(size_t, void* p) { |
| 206 | return p; |
| 207 | } |
| 208 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 209 | SkTextBlobRunIterator::SkTextBlobRunIterator(const SkTextBlob* blob) |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 210 | : fCurrentRun(SkTextBlob::RunRecord::First(blob)) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 211 | SkDEBUGCODE(fStorageTop = (uint8_t*)blob + blob->fStorageSize;) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 212 | } |
| 213 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 214 | void SkTextBlobRunIterator::next() { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 215 | SkASSERT(!this->done()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 216 | |
| 217 | if (!this->done()) { |
| 218 | SkDEBUGCODE(fCurrentRun->validate(fStorageTop);) |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 219 | fCurrentRun = SkTextBlob::RunRecord::Next(fCurrentRun); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 220 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 223 | SkTextBlobRunIterator::GlyphPositioning SkTextBlobRunIterator::positioning() const { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 224 | SkASSERT(!this->done()); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 225 | static_assert(static_cast<GlyphPositioning>(SkTextBlob::kDefault_Positioning) == |
| 226 | kDefault_Positioning, ""); |
| 227 | static_assert(static_cast<GlyphPositioning>(SkTextBlob::kHorizontal_Positioning) == |
| 228 | kHorizontal_Positioning, ""); |
| 229 | static_assert(static_cast<GlyphPositioning>(SkTextBlob::kFull_Positioning) == |
| 230 | kFull_Positioning, ""); |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 231 | static_assert(static_cast<GlyphPositioning>(SkTextBlob::kRSXform_Positioning) == |
| 232 | kRSXform_Positioning, ""); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 233 | |
| 234 | return SkTo<GlyphPositioning>(fCurrentRun->positioning()); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 235 | } |
| 236 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 237 | void SkTextBlobRunIterator::applyFontToPaint(SkPaint* paint) const { |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 238 | SkASSERT(!this->done()); |
| 239 | |
Mike Reed | 7b7ab59 | 2018-12-05 21:32:05 -0500 | [diff] [blame] | 240 | fCurrentRun->font().LEGACY_applyToPaint(paint); |
| 241 | paint->setTextEncoding(kGlyphID_SkTextEncoding); |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 242 | } |
| 243 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 244 | bool SkTextBlobRunIterator::isLCD() const { |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 245 | return fCurrentRun->font().getEdging() == SkFont::Edging::kSubpixelAntiAlias; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 246 | } |
| 247 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 248 | SkTextBlobBuilder::SkTextBlobBuilder() |
| 249 | : fStorageSize(0) |
| 250 | , fStorageUsed(0) |
| 251 | , fRunCount(0) |
| 252 | , fDeferredBounds(false) |
| 253 | , fLastRun(0) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 254 | fBounds.setEmpty(); |
| 255 | } |
| 256 | |
| 257 | SkTextBlobBuilder::~SkTextBlobBuilder() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | if (nullptr != fStorage.get()) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 259 | // We are abandoning runs and must destruct the associated font data. |
| 260 | // The easiest way to accomplish that is to use the blob destructor. |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 261 | this->make(); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 262 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 263 | } |
| 264 | |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 265 | SkRect SkTextBlobBuilder::TightRunBounds(const SkTextBlob::RunRecord& run) { |
Mike Reed | d1ca672 | 2018-12-10 14:53:16 -0500 | [diff] [blame] | 266 | const SkFont& font = run.font(); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 267 | SkRect bounds; |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 268 | |
| 269 | if (SkTextBlob::kDefault_Positioning == run.positioning()) { |
Mike Reed | d1ca672 | 2018-12-10 14:53:16 -0500 | [diff] [blame] | 270 | font.measureText(run.glyphBuffer(), run.glyphCount() * sizeof(uint16_t), |
| 271 | kGlyphID_SkTextEncoding, &bounds); |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 272 | return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 273 | } |
| 274 | |
| 275 | SkAutoSTArray<16, SkRect> glyphBounds(run.glyphCount()); |
Mike Reed | d1ca672 | 2018-12-10 14:53:16 -0500 | [diff] [blame] | 276 | font.getBounds(run.glyphBuffer(), run.glyphCount(), glyphBounds.get(), nullptr); |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 277 | |
| 278 | SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || |
| 279 | SkTextBlob::kHorizontal_Positioning == run.positioning()); |
| 280 | // kFull_Positioning => [ x, y, x, y... ] |
| 281 | // kHorizontal_Positioning => [ x, x, x... ] |
| 282 | // (const y applied by runBounds.offset(run->offset()) later) |
| 283 | const SkScalar horizontalConstY = 0; |
| 284 | const SkScalar* glyphPosX = run.posBuffer(); |
| 285 | const SkScalar* glyphPosY = (run.positioning() == SkTextBlob::kFull_Positioning) ? |
| 286 | glyphPosX + 1 : &horizontalConstY; |
| 287 | const unsigned posXInc = SkTextBlob::ScalarsPerGlyph(run.positioning()); |
| 288 | const unsigned posYInc = (run.positioning() == SkTextBlob::kFull_Positioning) ? |
| 289 | posXInc : 0; |
| 290 | |
| 291 | bounds.setEmpty(); |
| 292 | for (unsigned i = 0; i < run.glyphCount(); ++i) { |
| 293 | bounds.join(glyphBounds[i].makeOffset(*glyphPosX, *glyphPosY)); |
| 294 | glyphPosX += posXInc; |
| 295 | glyphPosY += posYInc; |
| 296 | } |
| 297 | |
| 298 | SkASSERT((void*)glyphPosX <= SkTextBlob::RunRecord::Next(&run)); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 299 | |
| 300 | return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 301 | } |
| 302 | |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 303 | static SkRect map_quad_to_rect(const SkRSXform& xform, const SkRect& rect) { |
| 304 | return SkMatrix().setRSXform(xform).mapRect(rect); |
| 305 | } |
| 306 | |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 307 | SkRect SkTextBlobBuilder::ConservativeRunBounds(const SkTextBlob::RunRecord& run) { |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 308 | SkASSERT(run.glyphCount() > 0); |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 309 | SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 310 | SkTextBlob::kHorizontal_Positioning == run.positioning() || |
| 311 | SkTextBlob::kRSXform_Positioning == run.positioning()); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 312 | |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 313 | const SkRect fontBounds = SkFontPriv::GetFontBounds(run.font()); |
fmalita | b0b45d3 | 2015-10-09 14:46:28 -0700 | [diff] [blame] | 314 | if (fontBounds.isEmpty()) { |
| 315 | // Empty font bounds are likely a font bug. TightBounds has a better chance of |
| 316 | // producing useful results in this case. |
| 317 | return TightRunBounds(run); |
| 318 | } |
| 319 | |
| 320 | // Compute the glyph position bbox. |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 321 | SkRect bounds; |
| 322 | switch (run.positioning()) { |
| 323 | case SkTextBlob::kHorizontal_Positioning: { |
| 324 | const SkScalar* glyphPos = run.posBuffer(); |
| 325 | SkASSERT((void*)(glyphPos + run.glyphCount()) <= SkTextBlob::RunRecord::Next(&run)); |
| 326 | |
| 327 | SkScalar minX = *glyphPos; |
| 328 | SkScalar maxX = *glyphPos; |
| 329 | for (unsigned i = 1; i < run.glyphCount(); ++i) { |
| 330 | SkScalar x = glyphPos[i]; |
| 331 | minX = SkMinScalar(x, minX); |
| 332 | maxX = SkMaxScalar(x, maxX); |
| 333 | } |
| 334 | |
| 335 | bounds.setLTRB(minX, 0, maxX, 0); |
| 336 | } break; |
| 337 | case SkTextBlob::kFull_Positioning: { |
| 338 | const SkPoint* glyphPosPts = reinterpret_cast<const SkPoint*>(run.posBuffer()); |
| 339 | SkASSERT((void*)(glyphPosPts + run.glyphCount()) <= SkTextBlob::RunRecord::Next(&run)); |
| 340 | |
| 341 | bounds.setBounds(glyphPosPts, run.glyphCount()); |
| 342 | } break; |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 343 | case SkTextBlob::kRSXform_Positioning: { |
| 344 | const SkRSXform* xform = reinterpret_cast<const SkRSXform*>(run.posBuffer()); |
| 345 | SkASSERT((void*)(xform + run.glyphCount()) <= SkTextBlob::RunRecord::Next(&run)); |
| 346 | bounds = map_quad_to_rect(xform[0], fontBounds); |
| 347 | for (unsigned i = 1; i < run.glyphCount(); ++i) { |
| 348 | bounds.join(map_quad_to_rect(xform[i], fontBounds)); |
| 349 | } |
| 350 | } break; |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 351 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 352 | SK_ABORT("unsupported positioning mode"); |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 355 | if (run.positioning() != SkTextBlob::kRSXform_Positioning) { |
| 356 | // Expand by typeface glyph bounds. |
| 357 | bounds.fLeft += fontBounds.left(); |
| 358 | bounds.fTop += fontBounds.top(); |
| 359 | bounds.fRight += fontBounds.right(); |
| 360 | bounds.fBottom += fontBounds.bottom(); |
| 361 | } |
fmalita | 3dc40ac | 2015-01-28 10:56:06 -0800 | [diff] [blame] | 362 | |
| 363 | // Offset by run position. |
| 364 | return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 365 | } |
| 366 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 367 | void SkTextBlobBuilder::updateDeferredBounds() { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 368 | SkASSERT(!fDeferredBounds || fRunCount > 0); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 369 | |
| 370 | if (!fDeferredBounds) { |
| 371 | return; |
| 372 | } |
| 373 | |
Mike Klein | 93ce79d | 2018-08-10 12:41:57 +0000 | [diff] [blame] | 374 | SkASSERT(fLastRun >= SkAlignPtr(sizeof(SkTextBlob))); |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 375 | SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + |
| 376 | fLastRun); |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 377 | |
fmalita | f9a4072 | 2015-01-29 12:24:24 -0800 | [diff] [blame] | 378 | // FIXME: we should also use conservative bounds for kDefault_Positioning. |
| 379 | SkRect runBounds = SkTextBlob::kDefault_Positioning == run->positioning() ? |
| 380 | TightRunBounds(*run) : ConservativeRunBounds(*run); |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 381 | fBounds.join(runBounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 382 | fDeferredBounds = false; |
| 383 | } |
| 384 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 385 | void SkTextBlobBuilder::reserve(size_t size) { |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 386 | SkSafeMath safe; |
| 387 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 388 | // We don't currently pre-allocate, but maybe someday... |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 389 | if (safe.add(fStorageUsed, size) <= fStorageSize && safe) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 392 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 393 | if (0 == fRunCount) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 394 | SkASSERT(nullptr == fStorage.get()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 395 | SkASSERT(0 == fStorageSize); |
| 396 | SkASSERT(0 == fStorageUsed); |
| 397 | |
| 398 | // the first allocation also includes blob storage |
Mike Klein | 93ce79d | 2018-08-10 12:41:57 +0000 | [diff] [blame] | 399 | // aligned up to a pointer alignment so SkTextBlob::RunRecords after it stay aligned. |
| 400 | fStorageUsed = SkAlignPtr(sizeof(SkTextBlob)); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 403 | fStorageSize = safe.add(fStorageUsed, size); |
| 404 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 405 | // FYI: This relies on everything we store being relocatable, particularly SkPaint. |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 406 | // Also, this is counting on the underlying realloc to throw when passed max(). |
| 407 | fStorage.realloc(safe ? fStorageSize : std::numeric_limits<size_t>::max()); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 410 | bool SkTextBlobBuilder::mergeRun(const SkFont& font, SkTextBlob::GlyphPositioning positioning, |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 411 | uint32_t count, SkPoint offset) { |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 412 | if (0 == fLastRun) { |
| 413 | SkASSERT(0 == fRunCount); |
| 414 | return false; |
| 415 | } |
| 416 | |
Mike Klein | 93ce79d | 2018-08-10 12:41:57 +0000 | [diff] [blame] | 417 | SkASSERT(fLastRun >= SkAlignPtr(sizeof(SkTextBlob))); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 418 | SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + |
| 419 | fLastRun); |
| 420 | SkASSERT(run->glyphCount() > 0); |
| 421 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 422 | if (run->textSize() != 0) { |
| 423 | return false; |
| 424 | } |
| 425 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 426 | if (run->positioning() != positioning |
| 427 | || run->font() != font |
| 428 | || (run->glyphCount() + count < run->glyphCount())) { |
| 429 | return false; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // we can merge same-font/same-positioning runs in the following cases: |
| 433 | // * fully positioned run following another fully positioned run |
| 434 | // * horizontally postioned run following another horizontally positioned run with the same |
| 435 | // y-offset |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 436 | if (SkTextBlob::kFull_Positioning != positioning |
| 437 | && (SkTextBlob::kHorizontal_Positioning != positioning |
| 438 | || run->offset().y() != offset.y())) { |
| 439 | return false; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 442 | SkSafeMath safe; |
| 443 | size_t sizeDelta = |
| 444 | SkTextBlob::RunRecord::StorageSize(run->glyphCount() + count, 0, positioning, &safe) - |
| 445 | SkTextBlob::RunRecord::StorageSize(run->glyphCount() , 0, positioning, &safe); |
| 446 | if (!safe) { |
| 447 | return false; |
| 448 | } |
| 449 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 450 | this->reserve(sizeDelta); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 451 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 452 | // reserve may have realloced |
| 453 | run = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + fLastRun); |
| 454 | uint32_t preMergeCount = run->glyphCount(); |
| 455 | run->grow(count); |
| 456 | |
| 457 | // Callers expect the buffers to point at the newly added slice, ant not at the beginning. |
| 458 | fCurrentRunBuffer.glyphs = run->glyphBuffer() + preMergeCount; |
| 459 | fCurrentRunBuffer.pos = run->posBuffer() |
| 460 | + preMergeCount * SkTextBlob::ScalarsPerGlyph(positioning); |
| 461 | |
| 462 | fStorageUsed += sizeDelta; |
| 463 | |
| 464 | SkASSERT(fStorageUsed <= fStorageSize); |
| 465 | run->validate(fStorage.get() + fStorageUsed); |
| 466 | |
| 467 | return true; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 470 | void SkTextBlobBuilder::allocInternal(const SkFont& font, |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 471 | SkTextBlob::GlyphPositioning positioning, |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 472 | int count, int textSize, SkPoint offset, |
| 473 | const SkRect* bounds) { |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 474 | if (count <= 0 || textSize < 0) { |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 475 | fCurrentRunBuffer = { nullptr, nullptr, nullptr, nullptr }; |
| 476 | return; |
| 477 | } |
| 478 | |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 479 | if (textSize != 0 || !this->mergeRun(font, positioning, count, offset)) { |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 480 | this->updateDeferredBounds(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 481 | |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 482 | SkSafeMath safe; |
| 483 | size_t runSize = SkTextBlob::RunRecord::StorageSize(count, textSize, positioning, &safe); |
| 484 | if (!safe) { |
| 485 | fCurrentRunBuffer = { nullptr, nullptr, nullptr, nullptr }; |
| 486 | return; |
| 487 | } |
| 488 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 489 | this->reserve(runSize); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 490 | |
Mike Klein | 93ce79d | 2018-08-10 12:41:57 +0000 | [diff] [blame] | 491 | SkASSERT(fStorageUsed >= SkAlignPtr(sizeof(SkTextBlob))); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 492 | SkASSERT(fStorageUsed + runSize <= fStorageSize); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 493 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 494 | SkTextBlob::RunRecord* run = new (fStorage.get() + fStorageUsed) |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 495 | SkTextBlob::RunRecord(count, textSize, offset, font, positioning); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 496 | fCurrentRunBuffer.glyphs = run->glyphBuffer(); |
| 497 | fCurrentRunBuffer.pos = run->posBuffer(); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 498 | fCurrentRunBuffer.utf8text = run->textBuffer(); |
| 499 | fCurrentRunBuffer.clusters = run->clusterBuffer(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 500 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 501 | fLastRun = fStorageUsed; |
| 502 | fStorageUsed += runSize; |
| 503 | fRunCount++; |
| 504 | |
| 505 | SkASSERT(fStorageUsed <= fStorageSize); |
| 506 | run->validate(fStorage.get() + fStorageUsed); |
| 507 | } |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 508 | SkASSERT(textSize > 0 || nullptr == fCurrentRunBuffer.utf8text); |
| 509 | SkASSERT(textSize > 0 || nullptr == fCurrentRunBuffer.clusters); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 510 | if (!fDeferredBounds) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 511 | if (bounds) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 512 | fBounds.join(*bounds); |
| 513 | } else { |
| 514 | fDeferredBounds = true; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 519 | // SkFont versions |
| 520 | |
| 521 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRun(const SkFont& font, int count, |
| 522 | SkScalar x, SkScalar y, |
| 523 | const SkRect* bounds) { |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 524 | this->allocInternal(font, SkTextBlob::kDefault_Positioning, count, 0, {x, y}, bounds); |
| 525 | return fCurrentRunBuffer; |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunPosH(const SkFont& font, int count, |
| 529 | SkScalar y, |
| 530 | const SkRect* bounds) { |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 531 | this->allocInternal(font, SkTextBlob::kHorizontal_Positioning, count, 0, {0, y}, bounds); |
| 532 | return fCurrentRunBuffer; |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunPos(const SkFont& font, int count, |
| 536 | const SkRect* bounds) { |
Mike Reed | b3f4aac | 2018-12-05 15:40:29 -0500 | [diff] [blame] | 537 | this->allocInternal(font, SkTextBlob::kFull_Positioning, count, 0, {0, 0}, bounds); |
| 538 | return fCurrentRunBuffer; |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 541 | const SkTextBlobBuilder::RunBuffer& |
| 542 | SkTextBlobBuilder::allocRunRSXform(const SkFont& font, int count) { |
| 543 | this->allocInternal(font, SkTextBlob::kRSXform_Positioning, count, 0, {0, 0}, nullptr); |
| 544 | return fCurrentRunBuffer; |
| 545 | } |
| 546 | |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 547 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunText(const SkFont& font, int count, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 548 | SkScalar x, SkScalar y, |
| 549 | int textByteCount, |
| 550 | SkString lang, |
| 551 | const SkRect* bounds) { |
| 552 | this->allocInternal(font, SkTextBlob::kDefault_Positioning, count, textByteCount, SkPoint::Make(x, y), bounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 553 | return fCurrentRunBuffer; |
| 554 | } |
| 555 | |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 556 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPosH(const SkFont& font, int count, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 557 | SkScalar y, |
| 558 | int textByteCount, |
| 559 | SkString lang, |
| 560 | const SkRect* bounds) { |
| 561 | this->allocInternal(font, SkTextBlob::kHorizontal_Positioning, count, textByteCount, SkPoint::Make(0, y), |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 562 | bounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 563 | return fCurrentRunBuffer; |
| 564 | } |
| 565 | |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 566 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunTextPos(const SkFont& font, int count, |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 567 | int textByteCount, |
| 568 | SkString lang, |
| 569 | const SkRect *bounds) { |
Ben Wagner | dff47af | 2017-08-30 16:24:22 -0400 | [diff] [blame] | 570 | this->allocInternal(font, SkTextBlob::kFull_Positioning, count, textByteCount, SkPoint::Make(0, 0), bounds); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 571 | return fCurrentRunBuffer; |
| 572 | } |
| 573 | |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 574 | const SkTextBlobBuilder::RunBuffer& SkTextBlobBuilder::allocRunRSXform(const SkFont& font, int count, |
| 575 | int textByteCount, |
| 576 | SkString lang, |
| 577 | const SkRect* bounds) { |
| 578 | this->allocInternal(font, SkTextBlob::kRSXform_Positioning, count, textByteCount, |
| 579 | {0, 0}, bounds); |
| 580 | return fCurrentRunBuffer; |
| 581 | } |
| 582 | |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 583 | sk_sp<SkTextBlob> SkTextBlobBuilder::make() { |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 584 | if (!fRunCount) { |
| 585 | // We don't instantiate empty blobs. |
| 586 | SkASSERT(!fStorage.get()); |
| 587 | SkASSERT(fStorageUsed == 0); |
| 588 | SkASSERT(fStorageSize == 0); |
| 589 | SkASSERT(fLastRun == 0); |
| 590 | SkASSERT(fBounds.isEmpty()); |
| 591 | return nullptr; |
| 592 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 593 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 594 | this->updateDeferredBounds(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 595 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 596 | // Tag the last run as such. |
| 597 | auto* lastRun = reinterpret_cast<SkTextBlob::RunRecord*>(fStorage.get() + fLastRun); |
| 598 | lastRun->fFlags |= SkTextBlob::RunRecord::kLast_Flag; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 599 | |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 600 | SkTextBlob* blob = new (fStorage.release()) SkTextBlob(fBounds); |
fmalita | 92d976c | 2015-10-05 11:09:57 -0700 | [diff] [blame] | 601 | SkDEBUGCODE(const_cast<SkTextBlob*>(blob)->fStorageSize = fStorageSize;) |
| 602 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 603 | SkDEBUGCODE( |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 604 | SkSafeMath safe; |
Mike Klein | 93ce79d | 2018-08-10 12:41:57 +0000 | [diff] [blame] | 605 | size_t validateSize = SkAlignPtr(sizeof(SkTextBlob)); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 606 | for (const auto* run = SkTextBlob::RunRecord::First(blob); run; |
| 607 | run = SkTextBlob::RunRecord::Next(run)) { |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 608 | validateSize += SkTextBlob::RunRecord::StorageSize( |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 609 | run->fCount, run->textSize(), run->positioning(), &safe); |
fmalita | 92d976c | 2015-10-05 11:09:57 -0700 | [diff] [blame] | 610 | run->validate(reinterpret_cast<const uint8_t*>(blob) + fStorageUsed); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 611 | fRunCount--; |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 612 | } |
| 613 | SkASSERT(validateSize == fStorageUsed); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 614 | SkASSERT(fRunCount == 0); |
Florin Malita | d923a71 | 2017-11-22 10:11:12 -0500 | [diff] [blame] | 615 | SkASSERT(safe); |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 616 | ) |
| 617 | |
fmalita | 3c196de | 2014-09-20 05:40:22 -0700 | [diff] [blame] | 618 | fStorageUsed = 0; |
| 619 | fStorageSize = 0; |
| 620 | fRunCount = 0; |
| 621 | fLastRun = 0; |
| 622 | fBounds.setEmpty(); |
| 623 | |
reed | 2ab9057 | 2016-08-10 14:16:41 -0700 | [diff] [blame] | 624 | return sk_sp<SkTextBlob>(blob); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 625 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 626 | |
| 627 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 628 | #include "SkTextToPathIter.h" |
| 629 | |
| 630 | template <SkTextInterceptsIter::TextType TextType, typename Func> |
Mike Reed | 5f6e20d | 2018-12-12 13:01:42 -0500 | [diff] [blame] | 631 | int GetTextIntercepts(const SkFont& font, const SkPaint* paint, const SkGlyphID glyphs[], |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 632 | int glyphCount, const SkScalar bounds[2], SkScalar* array, Func posMaker) { |
| 633 | SkASSERT(glyphCount == 0 || glyphs != nullptr); |
| 634 | |
| 635 | const SkPoint pos0 = posMaker(0); |
| 636 | SkTextInterceptsIter iter(glyphs, glyphCount, font, paint, bounds, pos0.x(), pos0.y(), |
| 637 | TextType); |
| 638 | |
| 639 | int i = 0; |
| 640 | int count = 0; |
| 641 | while (iter.next(array, &count)) { |
| 642 | if (TextType == SkTextInterceptsIter::TextType::kPosText) { |
| 643 | const SkPoint pos = posMaker(++i); |
| 644 | iter.setPosition(pos.x(), pos.y()); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return count; |
| 649 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 650 | |
Mike Reed | ae0d860 | 2018-12-10 22:02:17 -0500 | [diff] [blame] | 651 | int SkTextBlob::getIntercepts(const SkScalar bounds[2], SkScalar intervals[], |
| 652 | const SkPaint* paint) const { |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 653 | int count = 0; |
| 654 | SkTextBlobRunIterator it(this); |
| 655 | |
| 656 | while (!it.done()) { |
| 657 | SkScalar* runIntervals = intervals ? intervals + count : nullptr; |
| 658 | const SkFont& font = it.font(); |
| 659 | const SkGlyphID* glyphs = it.glyphs(); |
| 660 | const int glyphCount = it.glyphCount(); |
| 661 | |
| 662 | switch (it.positioning()) { |
| 663 | case SkTextBlobRunIterator::kDefault_Positioning: { |
| 664 | SkPoint loc = it.offset(); |
| 665 | count += GetTextIntercepts<SkTextInterceptsIter::TextType::kText>( |
Mike Reed | 5f6e20d | 2018-12-12 13:01:42 -0500 | [diff] [blame] | 666 | font, paint, glyphs, glyphCount, bounds, runIntervals, [loc] (int) { |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 667 | return loc; |
| 668 | }); |
| 669 | } break; |
| 670 | case SkTextBlobRunIterator::kHorizontal_Positioning: { |
| 671 | const SkScalar* xpos = it.pos(); |
| 672 | const SkScalar constY = it.offset().fY; |
| 673 | count += GetTextIntercepts<SkTextInterceptsIter::TextType::kPosText>( |
Mike Reed | 5f6e20d | 2018-12-12 13:01:42 -0500 | [diff] [blame] | 674 | font, paint, glyphs, glyphCount, bounds, runIntervals, [xpos, constY] (int i) { |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 675 | return SkPoint::Make(xpos[i], constY); |
| 676 | }); |
| 677 | } break; |
| 678 | case SkTextBlobRunIterator::kFull_Positioning: { |
| 679 | const SkPoint* pos = reinterpret_cast<const SkPoint*>(it.pos()); |
| 680 | count += GetTextIntercepts<SkTextInterceptsIter::TextType::kPosText>( |
Mike Reed | 5f6e20d | 2018-12-12 13:01:42 -0500 | [diff] [blame] | 681 | font, paint, glyphs, glyphCount, bounds, runIntervals, [pos] (int i) { |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 682 | return pos[i]; |
| 683 | }); |
| 684 | } break; |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 685 | case SkTextBlobRunIterator::kRSXform_Positioning: |
| 686 | // Unimplemented for now -- can/should we try to make this work? |
| 687 | break; |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | it.next(); |
Mike Reed | ae0d860 | 2018-12-10 22:02:17 -0500 | [diff] [blame] | 691 | } |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 692 | |
| 693 | return count; |
Mike Reed | ae0d860 | 2018-12-10 22:02:17 -0500 | [diff] [blame] | 694 | } |
| 695 | |
Mike Reed | cfb0120 | 2018-12-11 15:09:04 -0500 | [diff] [blame] | 696 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 697 | |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 698 | void SkTextBlobPriv::Flatten(const SkTextBlob& blob, SkWriteBuffer& buffer) { |
Mike Reed | 0951cc2 | 2018-12-21 16:19:24 -0500 | [diff] [blame] | 699 | // seems like we could skip this, and just recompute bounds in unflatten, but |
| 700 | // some cc_unittests fail if we remove this... |
| 701 | buffer.writeRect(blob.bounds()); |
Mike Reed | f4dd96c | 2018-12-21 20:49:47 +0000 | [diff] [blame] | 702 | |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 703 | SkTextBlobRunIterator it(&blob); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 704 | while (!it.done()) { |
| 705 | SkASSERT(it.glyphCount() > 0); |
| 706 | |
| 707 | buffer.write32(it.glyphCount()); |
| 708 | PositioningAndExtended pe; |
| 709 | pe.intValue = 0; |
| 710 | pe.positioning = it.positioning(); |
| 711 | SkASSERT((int32_t)it.positioning() == pe.intValue); // backwards compat. |
| 712 | |
| 713 | uint32_t textSize = it.textSize(); |
| 714 | pe.extended = textSize > 0; |
| 715 | buffer.write32(pe.intValue); |
| 716 | if (pe.extended) { |
| 717 | buffer.write32(textSize); |
| 718 | } |
| 719 | buffer.writePoint(it.offset()); |
Mike Reed | 0951cc2 | 2018-12-21 16:19:24 -0500 | [diff] [blame] | 720 | |
| 721 | SkFontPriv::Flatten(it.font(), buffer); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 722 | |
| 723 | buffer.writeByteArray(it.glyphs(), it.glyphCount() * sizeof(uint16_t)); |
| 724 | buffer.writeByteArray(it.pos(), |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 725 | it.glyphCount() * sizeof(SkScalar) * |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 726 | SkTextBlob::ScalarsPerGlyph( |
| 727 | SkTo<SkTextBlob::GlyphPositioning>(it.positioning()))); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 728 | if (pe.extended) { |
| 729 | buffer.writeByteArray(it.clusters(), sizeof(uint32_t) * it.glyphCount()); |
| 730 | buffer.writeByteArray(it.text(), it.textSize()); |
| 731 | } |
| 732 | |
| 733 | it.next(); |
| 734 | } |
| 735 | |
| 736 | // Marker for the last run (0 is not a valid glyph count). |
| 737 | buffer.write32(0); |
| 738 | } |
| 739 | |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 740 | sk_sp<SkTextBlob> SkTextBlobPriv::MakeFromBuffer(SkReadBuffer& reader) { |
Mike Reed | f4dd96c | 2018-12-21 20:49:47 +0000 | [diff] [blame] | 741 | SkRect bounds; |
| 742 | reader.readRect(&bounds); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 743 | |
| 744 | SkTextBlobBuilder blobBuilder; |
Khushal | cea8e3d | 2018-05-07 13:57:43 -0700 | [diff] [blame] | 745 | SkSafeMath safe; |
Mike Reed | c5166a9 | 2018-01-03 13:17:18 -0500 | [diff] [blame] | 746 | for (;;) { |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 747 | int glyphCount = reader.read32(); |
Mike Reed | c5166a9 | 2018-01-03 13:17:18 -0500 | [diff] [blame] | 748 | if (glyphCount == 0) { |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 749 | // End-of-runs marker. |
| 750 | break; |
| 751 | } |
| 752 | |
| 753 | PositioningAndExtended pe; |
| 754 | pe.intValue = reader.read32(); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 755 | const auto pos = SkTo<SkTextBlob::GlyphPositioning>(pe.positioning); |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 756 | if (glyphCount <= 0 || pos > SkTextBlob::kRSXform_Positioning) { |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 757 | return nullptr; |
| 758 | } |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 759 | int textSize = pe.extended ? reader.read32() : 0; |
Khushal | cea8e3d | 2018-05-07 13:57:43 -0700 | [diff] [blame] | 760 | if (textSize < 0) { |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 761 | return nullptr; |
| 762 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 763 | |
| 764 | SkPoint offset; |
| 765 | reader.readPoint(&offset); |
Mike Reed | 0951cc2 | 2018-12-21 16:19:24 -0500 | [diff] [blame] | 766 | SkFont font; |
| 767 | if (reader.isVersionLT(SkReadBuffer::kSerializeFonts_Version)) { |
| 768 | SkPaint paint; |
| 769 | reader.readPaint(&paint); |
| 770 | font = SkFont::LEGACY_ExtractFromPaint(paint); |
| 771 | } else { |
| 772 | SkFontPriv::Unflatten(&font, reader); |
| 773 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 774 | |
Khushal | cea8e3d | 2018-05-07 13:57:43 -0700 | [diff] [blame] | 775 | // Compute the expected size of the buffer and ensure we have enough to deserialize |
| 776 | // a run before allocating it. |
| 777 | const size_t glyphSize = safe.mul(glyphCount, sizeof(uint16_t)), |
| 778 | posSize = |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 779 | safe.mul(glyphCount, safe.mul(sizeof(SkScalar), |
| 780 | SkTextBlob::ScalarsPerGlyph(pos))), |
Khushal | cea8e3d | 2018-05-07 13:57:43 -0700 | [diff] [blame] | 781 | clusterSize = pe.extended ? safe.mul(glyphCount, sizeof(uint32_t)) : 0; |
| 782 | const size_t totalSize = |
| 783 | safe.add(safe.add(glyphSize, posSize), safe.add(clusterSize, textSize)); |
| 784 | |
| 785 | if (!reader.isValid() || !safe || totalSize > reader.available()) { |
Florin Malita | 1e18aa6 | 2017-11-19 10:22:22 -0500 | [diff] [blame] | 786 | return nullptr; |
| 787 | } |
| 788 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 789 | const SkTextBlobBuilder::RunBuffer* buf = nullptr; |
| 790 | switch (pos) { |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 791 | case SkTextBlob::kDefault_Positioning: |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 792 | buf = &blobBuilder.allocRunText(font, glyphCount, offset.x(), offset.y(), |
Mike Reed | f4dd96c | 2018-12-21 20:49:47 +0000 | [diff] [blame] | 793 | textSize, SkString(), &bounds); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 794 | break; |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 795 | case SkTextBlob::kHorizontal_Positioning: |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 796 | buf = &blobBuilder.allocRunTextPosH(font, glyphCount, offset.y(), |
Mike Reed | f4dd96c | 2018-12-21 20:49:47 +0000 | [diff] [blame] | 797 | textSize, SkString(), &bounds); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 798 | break; |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 799 | case SkTextBlob::kFull_Positioning: |
Mike Reed | f4dd96c | 2018-12-21 20:49:47 +0000 | [diff] [blame] | 800 | buf = &blobBuilder.allocRunTextPos(font, glyphCount, textSize, SkString(), &bounds); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 801 | break; |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 802 | case SkTextBlob::kRSXform_Positioning: |
Mike Reed | f4dd96c | 2018-12-21 20:49:47 +0000 | [diff] [blame] | 803 | buf = &blobBuilder.allocRunRSXform(font, glyphCount, textSize, SkString(), &bounds); |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 804 | break; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 805 | } |
| 806 | |
Florin Malita | 90dcafc | 2017-11-22 10:53:33 -0500 | [diff] [blame] | 807 | if (!buf->glyphs || |
| 808 | !buf->pos || |
| 809 | (pe.extended && (!buf->clusters || !buf->utf8text))) { |
| 810 | return nullptr; |
| 811 | } |
| 812 | |
Khushal | cea8e3d | 2018-05-07 13:57:43 -0700 | [diff] [blame] | 813 | if (!reader.readByteArray(buf->glyphs, glyphSize) || |
| 814 | !reader.readByteArray(buf->pos, posSize)) { |
| 815 | return nullptr; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | if (pe.extended) { |
Khushal | cea8e3d | 2018-05-07 13:57:43 -0700 | [diff] [blame] | 819 | if (!reader.readByteArray(buf->clusters, clusterSize) || |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 820 | !reader.readByteArray(buf->utf8text, textSize)) { |
| 821 | return nullptr; |
| 822 | } |
| 823 | } |
| 824 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 825 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 826 | return blobBuilder.make(); |
| 827 | } |
| 828 | |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 829 | sk_sp<SkTextBlob> SkTextBlob::MakeFromText(const void* text, size_t byteLength, const SkFont& font, |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 830 | SkTextEncoding encoding) { |
Mike Reed | efb518d | 2018-12-06 13:30:23 -0500 | [diff] [blame] | 831 | // Note: we deliberately promote this to fully positioned blobs, since we'd have to pay the |
| 832 | // same cost down stream (i.e. computing bounds), so its cheaper to pay the cost once now. |
| 833 | const int count = font.countText(text, byteLength, encoding); |
| 834 | SkTextBlobBuilder builder; |
| 835 | auto buffer = builder.allocRunPos(font, count); |
| 836 | font.textToGlyphs(text, byteLength, encoding, buffer.glyphs, count); |
| 837 | font.getPos(buffer.glyphs, count, reinterpret_cast<SkPoint*>(buffer.pos), {0, 0}); |
| 838 | return builder.make(); |
| 839 | } |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 840 | |
Mike Reed | efb518d | 2018-12-06 13:30:23 -0500 | [diff] [blame] | 841 | sk_sp<SkTextBlob> SkTextBlob::MakeFromPosText(const void* text, size_t byteLength, |
| 842 | const SkPoint pos[], const SkFont& font, |
| 843 | SkTextEncoding encoding) { |
| 844 | const int count = font.countText(text, byteLength, encoding); |
| 845 | SkTextBlobBuilder builder; |
| 846 | auto buffer = builder.allocRunPos(font, count); |
| 847 | font.textToGlyphs(text, byteLength, encoding, buffer.glyphs, count); |
| 848 | memcpy(buffer.pos, pos, count * sizeof(SkPoint)); |
| 849 | return builder.make(); |
| 850 | } |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 851 | |
Mike Reed | efb518d | 2018-12-06 13:30:23 -0500 | [diff] [blame] | 852 | sk_sp<SkTextBlob> SkTextBlob::MakeFromPosTextH(const void* text, size_t byteLength, |
| 853 | const SkScalar xpos[], SkScalar constY, |
| 854 | const SkFont& font, SkTextEncoding encoding) { |
| 855 | const int count = font.countText(text, byteLength, encoding); |
| 856 | SkTextBlobBuilder builder; |
| 857 | auto buffer = builder.allocRunPosH(font, count, constY); |
| 858 | font.textToGlyphs(text, byteLength, encoding, buffer.glyphs, count); |
| 859 | memcpy(buffer.pos, xpos, count * sizeof(SkScalar)); |
| 860 | return builder.make(); |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 861 | } |
| 862 | |
Mike Reed | 30cf62b | 2018-12-20 11:18:24 -0500 | [diff] [blame] | 863 | sk_sp<SkTextBlob> SkTextBlob::MakeFromRSXform(const void* text, size_t byteLength, |
| 864 | const SkRSXform xform[], const SkFont& font, |
| 865 | SkTextEncoding encoding) { |
| 866 | const int count = font.countText(text, byteLength, encoding); |
| 867 | SkTextBlobBuilder builder; |
| 868 | auto buffer = builder.allocRunRSXform(font, count); |
| 869 | font.textToGlyphs(text, byteLength, encoding, buffer.glyphs, count); |
| 870 | memcpy(buffer.pos, xform, count * sizeof(SkRSXform)); |
| 871 | return builder.make(); |
| 872 | } |
| 873 | |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 874 | sk_sp<SkData> SkTextBlob::serialize(const SkSerialProcs& procs) const { |
| 875 | SkBinaryWriteBuffer buffer; |
| 876 | buffer.setSerialProcs(procs); |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 877 | SkTextBlobPriv::Flatten(*this, buffer); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 878 | |
| 879 | size_t total = buffer.bytesWritten(); |
| 880 | sk_sp<SkData> data = SkData::MakeUninitialized(total); |
| 881 | buffer.writeToMemory(data->writable_data()); |
| 882 | return data; |
| 883 | } |
| 884 | |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 885 | sk_sp<SkTextBlob> SkTextBlob::Deserialize(const void* data, size_t length, |
| 886 | const SkDeserialProcs& procs) { |
| 887 | SkReadBuffer buffer(data, length); |
| 888 | buffer.setDeserialProcs(procs); |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 889 | return SkTextBlobPriv::MakeFromBuffer(buffer); |
Mike Reed | f6eb1f9 | 2017-12-19 17:12:13 -0500 | [diff] [blame] | 890 | } |
| 891 | |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 892 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 893 | |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 894 | size_t SkTextBlob::serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const { |
| 895 | SkBinaryWriteBuffer buffer(memory, memory_size); |
| 896 | buffer.setSerialProcs(procs); |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 897 | SkTextBlobPriv::Flatten(*this, buffer); |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 898 | return buffer.usingInitialStorage() ? buffer.bytesWritten() : 0u; |
| 899 | } |