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