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