| 1 |
/* |
| 2 |
* Copyright 2018 Google LLC |
| 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 "src/core/SkGlyph.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkDrawable.h" |
| 13 |
#include "include/core/SkPicture.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkSerialProcs.h" |
| 16 |
#include "include/core/SkSpan.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "include/private/base/SkTFitsIn.h" |
| 19 |
#include "include/private/base/SkTo.h" |
| 20 |
#include "src/base/SkArenaAlloc.h" |
| 21 |
#include "src/base/SkBezierCurves.h" |
| 22 |
#include "src/core/SkReadBuffer.h" |
| 23 |
#include "src/core/SkScalerContext.h" |
| 24 |
#include "src/core/SkWriteBuffer.h" |
| 25 |
#include "src/text/StrikeForGPU.h" |
| 26 |
|
| 27 |
#include <cstring> |
| 28 |
#include <optional> |
| 29 |
#include <tuple> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
using namespace skglyph; |
| 33 |
using namespace sktext; |
| 34 |
|
| 35 |
// -- SkPictureBackedGlyphDrawable ----------------------------------------------------------------- |
| 36 |
sk_sp<SkPictureBackedGlyphDrawable> |
| 37 |
SkPictureBackedGlyphDrawable::MakeFromBuffer(SkReadBuffer& buffer) { |
| 38 |
SkASSERT(buffer.isValid()); |
| 39 |
|
| 40 |
sk_sp<SkData> pictureData = buffer.readByteArrayAsData(); |
| 41 |
|
| 42 |
// Return nullptr if invalid or there an empty drawable, which is represented by nullptr. |
| 43 |
if (!buffer.isValid() || pictureData->empty()) { |
| 44 |
return nullptr; |
| 45 |
} |
| 46 |
|
| 47 |
// Propagate the outer buffer's allow-SkSL setting to the picture decoder, using the flag on |
| 48 |
// the deserial procs. |
| 49 |
SkDeserialProcs procs; |
| 50 |
procs.fAllowSkSL = buffer.allowSkSL(); |
| 51 |
sk_sp<SkPicture> picture = SkPicture::MakeFromData(pictureData.get(), &procs); |
| 52 |
if (!buffer.validate(picture != nullptr)) { |
| 53 |
return nullptr; |
| 54 |
} |
| 55 |
|
| 56 |
return sk_make_sp<SkPictureBackedGlyphDrawable>(std::move(picture)); |
| 57 |
} |
| 58 |
|
| 59 |
void SkPictureBackedGlyphDrawable::FlattenDrawable(SkWriteBuffer& buffer, SkDrawable* drawable) { |
| 60 |
if (drawable == nullptr) { |
| 61 |
buffer.writeByteArray(nullptr, 0); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
sk_sp<SkPicture> picture = drawable->makePictureSnapshot(); |
| 66 |
// These drawables should not have SkImages, SkTypefaces or SkPictures inside of them, so |
| 67 |
// the default SkSerialProcs are sufficient. |
| 68 |
sk_sp<SkData> data = picture->serialize(); |
| 69 |
|
| 70 |
// If the picture is too big, or there is no picture, then drop by sending an empty byte array. |
| 71 |
if (!SkTFitsIn<uint32_t>(data->size()) || data->empty()) { |
| 72 |
buffer.writeByteArray(nullptr, 0); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
buffer.writeByteArray(data->data(), data->size()); |
| 77 |
} |
| 78 |
|
| 79 |
SkPictureBackedGlyphDrawable::SkPictureBackedGlyphDrawable(sk_sp<SkPicture> picture) |
| 80 |
: fPicture(std::move(picture)) {} |
| 81 |
|
| 82 |
SkRect SkPictureBackedGlyphDrawable::onGetBounds() { |
| 83 |
return fPicture->cullRect(); |
| 84 |
} |
| 85 |
|
| 86 |
size_t SkPictureBackedGlyphDrawable::onApproximateBytesUsed() { |
| 87 |
return sizeof(SkPictureBackedGlyphDrawable) + fPicture->approximateBytesUsed(); |
| 88 |
} |
| 89 |
|
| 90 |
void SkPictureBackedGlyphDrawable::onDraw(SkCanvas* canvas) { |
| 91 |
canvas->drawPicture(fPicture); |
| 92 |
} |
| 93 |
|
| 94 |
//-- SkGlyph --------------------------------------------------------------------------------------- |
| 95 |
std::optional<SkGlyph> SkGlyph::MakeFromBuffer(SkReadBuffer& buffer) { |
| 96 |
SkASSERT(buffer.isValid()); |
| 97 |
const SkPackedGlyphID packedID{buffer.readUInt()}; |
| 98 |
const SkVector advance = buffer.readPoint(); |
| 99 |
const uint32_t dimensions = buffer.readUInt(); |
| 100 |
const uint32_t leftTop = buffer.readUInt(); |
| 101 |
const SkMask::Format format = SkTo<SkMask::Format>(buffer.readUInt()); |
| 102 |
|
| 103 |
if (!buffer.validate(SkMask::IsValidFormat(format))) { |
| 104 |
return std::nullopt; |
| 105 |
} |
| 106 |
|
| 107 |
SkGlyph glyph{packedID}; |
| 108 |
glyph.fAdvanceX = advance.x(); |
| 109 |
glyph.fAdvanceY = advance.y(); |
| 110 |
glyph.fWidth = dimensions >> 16; |
| 111 |
glyph.fHeight = dimensions & 0xffffu; |
| 112 |
glyph.fLeft = leftTop >> 16; |
| 113 |
glyph.fTop = leftTop & 0xffffu; |
| 114 |
glyph.fMaskFormat = format; |
| 115 |
SkDEBUGCODE(glyph.fAdvancesBoundsFormatAndInitialPathDone = true;) |
| 116 |
return glyph; |
| 117 |
} |
| 118 |
|
| 119 |
SkMask SkGlyph::mask() const { |
| 120 |
SkIRect bounds = SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); |
| 121 |
return SkMask(static_cast<const uint8_t*>(fImage), bounds, this->rowBytes(), fMaskFormat); |
| 122 |
} |
| 123 |
|
| 124 |
SkMask SkGlyph::mask(SkPoint position) const { |
| 125 |
SkASSERT(SkScalarIsInt(position.x()) && SkScalarIsInt(position.y())); |
| 126 |
SkIRect bounds = SkIRect::MakeXYWH(fLeft, fTop, fWidth, fHeight); |
| 127 |
bounds.offset(SkScalarFloorToInt(position.x()), SkScalarFloorToInt(position.y())); |
| 128 |
return SkMask(static_cast<const uint8_t*>(fImage), bounds, this->rowBytes(), fMaskFormat); |
| 129 |
} |
| 130 |
|
| 131 |
void SkGlyph::zeroMetrics() { |
| 132 |
fAdvanceX = 0; |
| 133 |
fAdvanceY = 0; |
| 134 |
fWidth = 0; |
| 135 |
fHeight = 0; |
| 136 |
fTop = 0; |
| 137 |
fLeft = 0; |
| 138 |
} |
| 139 |
|
| 140 |
static size_t bits_to_bytes(size_t bits) { |
| 141 |
return (bits + 7) >> 3; |
| 142 |
} |
| 143 |
|
| 144 |
static size_t format_alignment(SkMask::Format format) { |
| 145 |
switch (format) { |
| 146 |
case SkMask::kBW_Format: [[fallthrough]]; |
| 147 |
case SkMask::kA8_Format: [[fallthrough]]; |
| 148 |
case SkMask::k3D_Format: [[fallthrough]]; |
| 149 |
case SkMask::kSDF_Format: |
| 150 |
return alignof(uint8_t); |
| 151 |
case SkMask::kARGB32_Format: |
| 152 |
return alignof(uint32_t); |
| 153 |
case SkMask::kLCD16_Format: |
| 154 |
return alignof(uint16_t); |
| 155 |
} |
| 156 |
SkUNREACHABLE; |
| 157 |
} |
| 158 |
|
| 159 |
static size_t format_rowbytes(int width, SkMask::Format format) { |
| 160 |
return format == SkMask::kBW_Format ? bits_to_bytes(width) |
| 161 |
: width * format_alignment(format); |
| 162 |
} |
| 163 |
|
| 164 |
size_t SkGlyph::formatAlignment() const { |
| 165 |
return format_alignment(this->maskFormat()); |
| 166 |
} |
| 167 |
|
| 168 |
size_t SkGlyph::allocImage(SkArenaAlloc* alloc) { |
| 169 |
SkASSERT(!this->isEmpty()); |
| 170 |
auto size = this->imageSize(); |
| 171 |
fImage = alloc->makeBytesAlignedTo(size, this->formatAlignment()); |
| 172 |
|
| 173 |
return size; |
| 174 |
} |
| 175 |
|
| 176 |
bool SkGlyph::setImage(SkArenaAlloc* alloc, SkScalerContext* scalerContext) { |
| 177 |
if (!this->setImageHasBeenCalled()) { |
| 178 |
// It used to be that getImage() could change the fMaskFormat. Extra checking to make |
| 179 |
// sure there are no regressions. |
| 180 |
SkDEBUGCODE(SkMask::Format oldFormat = this->maskFormat()); |
| 181 |
this->allocImage(alloc); |
| 182 |
scalerContext->getImage(*this); |
| 183 |
SkASSERT(oldFormat == this->maskFormat()); |
| 184 |
return true; |
| 185 |
} |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
bool SkGlyph::setImage(SkArenaAlloc* alloc, const void* image) { |
| 190 |
if (!this->setImageHasBeenCalled()) { |
| 191 |
this->allocImage(alloc); |
| 192 |
memcpy(fImage, image, this->imageSize()); |
| 193 |
return true; |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
size_t SkGlyph::setMetricsAndImage(SkArenaAlloc* alloc, const SkGlyph& from) { |
| 199 |
// Since the code no longer tries to find replacement glyphs, the image should always be |
| 200 |
// nullptr. |
| 201 |
SkASSERT(fImage == nullptr || from.fImage == nullptr); |
| 202 |
|
| 203 |
// TODO(herb): remove "if" when we are sure there are no colliding glyphs. |
| 204 |
if (fImage == nullptr) { |
| 205 |
fAdvanceX = from.fAdvanceX; |
| 206 |
fAdvanceY = from.fAdvanceY; |
| 207 |
fWidth = from.fWidth; |
| 208 |
fHeight = from.fHeight; |
| 209 |
fTop = from.fTop; |
| 210 |
fLeft = from.fLeft; |
| 211 |
fScalerContextBits = from.fScalerContextBits; |
| 212 |
fMaskFormat = from.fMaskFormat; |
| 213 |
|
| 214 |
// From glyph may not have an image because the glyph is too large. |
| 215 |
if (from.fImage != nullptr && this->setImage(alloc, from.image())) { |
| 216 |
return this->imageSize(); |
| 217 |
} |
| 218 |
|
| 219 |
SkDEBUGCODE(fAdvancesBoundsFormatAndInitialPathDone = from.fAdvancesBoundsFormatAndInitialPathDone;) |
| 220 |
} |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
size_t SkGlyph::rowBytes() const { |
| 225 |
return format_rowbytes(fWidth, fMaskFormat); |
| 226 |
} |
| 227 |
|
| 228 |
size_t SkGlyph::rowBytesUsingFormat(SkMask::Format format) const { |
| 229 |
return format_rowbytes(fWidth, format); |
| 230 |
} |
| 231 |
|
| 232 |
size_t SkGlyph::imageSize() const { |
| 233 |
if (this->isEmpty() || this->imageTooLarge()) { return 0; } |
| 234 |
|
| 235 |
size_t size = this->rowBytes() * fHeight; |
| 236 |
|
| 237 |
if (fMaskFormat == SkMask::k3D_Format) { |
| 238 |
size *= 3; |
| 239 |
} |
| 240 |
|
| 241 |
return size; |
| 242 |
} |
| 243 |
|
| 244 |
void SkGlyph::installPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline, bool modified) { |
| 245 |
SkASSERT(fPathData == nullptr); |
| 246 |
SkASSERT(!this->setPathHasBeenCalled()); |
| 247 |
fPathData = alloc->make<SkGlyph::PathData>(); |
| 248 |
if (path != nullptr) { |
| 249 |
fPathData->fPath = *path; |
| 250 |
fPathData->fPath.updateBoundsCache(); |
| 251 |
fPathData->fPath.getGenerationID(); |
| 252 |
fPathData->fHasPath = true; |
| 253 |
fPathData->fHairline = hairline; |
| 254 |
fPathData->fModified = modified; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
bool SkGlyph::setPath(SkArenaAlloc* alloc, SkScalerContext* scalerContext) { |
| 259 |
if (!this->setPathHasBeenCalled()) { |
| 260 |
scalerContext->getPath(*this, alloc); |
| 261 |
SkASSERT(this->setPathHasBeenCalled()); |
| 262 |
return this->path() != nullptr; |
| 263 |
} |
| 264 |
|
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
bool SkGlyph::setPath(SkArenaAlloc* alloc, const SkPath* path, bool hairline, bool modified) { |
| 269 |
if (!this->setPathHasBeenCalled()) { |
| 270 |
this->installPath(alloc, path, hairline, modified); |
| 271 |
return this->path() != nullptr; |
| 272 |
} |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
const SkPath* SkGlyph::path() const { |
| 277 |
// setPath must have been called previously. |
| 278 |
SkASSERT(this->setPathHasBeenCalled()); |
| 279 |
if (fPathData->fHasPath) { |
| 280 |
return &fPathData->fPath; |
| 281 |
} |
| 282 |
return nullptr; |
| 283 |
} |
| 284 |
|
| 285 |
bool SkGlyph::pathIsHairline() const { |
| 286 |
// setPath must have been called previously. |
| 287 |
SkASSERT(this->setPathHasBeenCalled()); |
| 288 |
return fPathData->fHairline; |
| 289 |
} |
| 290 |
|
| 291 |
bool SkGlyph::pathIsModified() const { |
| 292 |
// setPath must have been called previously. |
| 293 |
SkASSERT(this->setPathHasBeenCalled()); |
| 294 |
return fPathData->fModified; |
| 295 |
} |
| 296 |
|
| 297 |
void SkGlyph::installDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) { |
| 298 |
SkASSERT(fDrawableData == nullptr); |
| 299 |
SkASSERT(!this->setDrawableHasBeenCalled()); |
| 300 |
fDrawableData = alloc->make<SkGlyph::DrawableData>(); |
| 301 |
if (drawable != nullptr) { |
| 302 |
fDrawableData->fDrawable = std::move(drawable); |
| 303 |
fDrawableData->fDrawable->getGenerationID(); |
| 304 |
fDrawableData->fHasDrawable = true; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
bool SkGlyph::setDrawable(SkArenaAlloc* alloc, SkScalerContext* scalerContext) { |
| 309 |
if (!this->setDrawableHasBeenCalled()) { |
| 310 |
sk_sp<SkDrawable> drawable = scalerContext->getDrawable(*this); |
| 311 |
this->installDrawable(alloc, std::move(drawable)); |
| 312 |
return this->drawable() != nullptr; |
| 313 |
} |
| 314 |
return false; |
| 315 |
} |
| 316 |
|
| 317 |
bool SkGlyph::setDrawable(SkArenaAlloc* alloc, sk_sp<SkDrawable> drawable) { |
| 318 |
if (!this->setDrawableHasBeenCalled()) { |
| 319 |
this->installDrawable(alloc, std::move(drawable)); |
| 320 |
return this->drawable() != nullptr; |
| 321 |
} |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
SkDrawable* SkGlyph::drawable() const { |
| 326 |
// setDrawable must have been called previously. |
| 327 |
SkASSERT(this->setDrawableHasBeenCalled()); |
| 328 |
if (fDrawableData->fHasDrawable) { |
| 329 |
return fDrawableData->fDrawable.get(); |
| 330 |
} |
| 331 |
return nullptr; |
| 332 |
} |
| 333 |
|
| 334 |
void SkGlyph::flattenMetrics(SkWriteBuffer& buffer) const { |
| 335 |
buffer.writeUInt(fID.value()); |
| 336 |
buffer.writePoint({fAdvanceX, fAdvanceY}); |
| 337 |
buffer.writeUInt(fWidth << 16 | fHeight); |
| 338 |
// Note: << has undefined behavior for negative values, so convert everything to the bit |
| 339 |
// values of uint16_t. Using the cast keeps the signed values fLeft and fTop from sign |
| 340 |
// extending. |
| 341 |
const uint32_t left = static_cast<uint16_t>(fLeft); |
| 342 |
const uint32_t top = static_cast<uint16_t>(fTop); |
| 343 |
buffer.writeUInt(left << 16 | top); |
| 344 |
buffer.writeUInt(SkTo<uint32_t>(fMaskFormat)); |
| 345 |
} |
| 346 |
|
| 347 |
void SkGlyph::flattenImage(SkWriteBuffer& buffer) const { |
| 348 |
SkASSERT(this->setImageHasBeenCalled()); |
| 349 |
|
| 350 |
// If the glyph is empty or too big, then no image data is sent. |
| 351 |
if (!this->isEmpty() && SkGlyphDigest::FitsInAtlas(*this)) { |
| 352 |
buffer.writeByteArray(this->image(), this->imageSize()); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
size_t SkGlyph::addImageFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) { |
| 357 |
SkASSERT(buffer.isValid()); |
| 358 |
|
| 359 |
// If the glyph is empty or too big, then no image data is received. |
| 360 |
if (this->isEmpty() || !SkGlyphDigest::FitsInAtlas(*this)) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
size_t memoryIncrease = 0; |
| 365 |
|
| 366 |
void* imageData = alloc->makeBytesAlignedTo(this->imageSize(), this->formatAlignment()); |
| 367 |
buffer.readByteArray(imageData, this->imageSize()); |
| 368 |
if (buffer.isValid()) { |
| 369 |
this->installImage(imageData); |
| 370 |
memoryIncrease += this->imageSize(); |
| 371 |
} |
| 372 |
|
| 373 |
return memoryIncrease; |
| 374 |
} |
| 375 |
|
| 376 |
void SkGlyph::flattenPath(SkWriteBuffer& buffer) const { |
| 377 |
SkASSERT(this->setPathHasBeenCalled()); |
| 378 |
|
| 379 |
const bool hasPath = this->path() != nullptr; |
| 380 |
buffer.writeBool(hasPath); |
| 381 |
if (hasPath) { |
| 382 |
buffer.writeBool(this->pathIsHairline()); |
| 383 |
buffer.writeBool(this->pathIsModified()); |
| 384 |
buffer.writePath(*this->path()); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
size_t SkGlyph::addPathFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) { |
| 389 |
SkASSERT(buffer.isValid()); |
| 390 |
|
| 391 |
size_t memoryIncrease = 0; |
| 392 |
const bool hasPath = buffer.readBool(); |
| 393 |
// Check if the buffer is invalid, so as to not make a logical decision on invalid data. |
| 394 |
if (!buffer.isValid()) { |
| 395 |
return 0; |
| 396 |
} |
| 397 |
if (hasPath) { |
| 398 |
const bool pathIsHairline = buffer.readBool(); |
| 399 |
const bool pathIsModified = buffer.readBool(); |
| 400 |
if (auto path = buffer.readPath()) { |
| 401 |
if (this->setPath(alloc, &path.value(), pathIsHairline, pathIsModified)) { |
| 402 |
memoryIncrease += path->approximateBytesUsed(); |
| 403 |
} |
| 404 |
} |
| 405 |
} else { |
| 406 |
this->setPath(alloc, nullptr, false, false); |
| 407 |
} |
| 408 |
|
| 409 |
return memoryIncrease; |
| 410 |
} |
| 411 |
|
| 412 |
void SkGlyph::flattenDrawable(SkWriteBuffer& buffer) const { |
| 413 |
SkASSERT(this->setDrawableHasBeenCalled()); |
| 414 |
|
| 415 |
if (this->isEmpty() || this->drawable() == nullptr) { |
| 416 |
SkPictureBackedGlyphDrawable::FlattenDrawable(buffer, nullptr); |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
SkPictureBackedGlyphDrawable::FlattenDrawable(buffer, this->drawable()); |
| 421 |
} |
| 422 |
|
| 423 |
size_t SkGlyph::addDrawableFromBuffer(SkReadBuffer& buffer, SkArenaAlloc* alloc) { |
| 424 |
SkASSERT(buffer.isValid()); |
| 425 |
|
| 426 |
sk_sp<SkDrawable> drawable = SkPictureBackedGlyphDrawable::MakeFromBuffer(buffer); |
| 427 |
if (!buffer.isValid()) { |
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
if (this->setDrawable(alloc, std::move(drawable))) { |
| 432 |
return this->drawable()->approximateBytesUsed(); |
| 433 |
} |
| 434 |
|
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
static std::tuple<SkScalar, SkScalar> calculate_path_gap( |
| 439 |
SkScalar topOffset, SkScalar bottomOffset, const SkPath& path) { |
| 440 |
|
| 441 |
// Left and Right of an ever expanding gap around the path. |
| 442 |
SkScalar left = SK_ScalarMax, |
| 443 |
right = SK_ScalarMin; |
| 444 |
|
| 445 |
auto expandGap = [&left, &right](SkScalar v) { |
| 446 |
left = std::min(left, v); |
| 447 |
right = std::max(right, v); |
| 448 |
}; |
| 449 |
|
| 450 |
// Handle all the different verbs for the path. |
| 451 |
auto addLine = [&](SkSpan<const SkPoint> pts, SkScalar offset) { |
| 452 |
SkScalar t = sk_ieee_float_divide(offset - pts[0].fY, pts[1].fY - pts[0].fY); |
| 453 |
if (0 <= t && t < 1) { // this handles divide by zero above |
| 454 |
expandGap(pts[0].fX + t * (pts[1].fX - pts[0].fX)); |
| 455 |
} |
| 456 |
}; |
| 457 |
|
| 458 |
auto addQuad = [&](SkSpan<const SkPoint> pts, SkScalar offset) { |
| 459 |
float intersectionStorage[2]; |
| 460 |
auto intersections = SkBezierQuad::IntersectWithHorizontalLine( |
| 461 |
pts, offset, intersectionStorage); |
| 462 |
|
| 463 |
for(float intersection : intersections) { |
| 464 |
expandGap(intersection); |
| 465 |
} |
| 466 |
}; |
| 467 |
|
| 468 |
auto addCubic = [&](SkSpan<const SkPoint> pts, SkScalar offset) { |
| 469 |
float intersectionStorage[3]; |
| 470 |
auto intersections = SkBezierCubic::IntersectWithHorizontalLine( |
| 471 |
pts, offset, intersectionStorage); |
| 472 |
|
| 473 |
for(float intersection : intersections) { |
| 474 |
expandGap(intersection); |
| 475 |
} |
| 476 |
}; |
| 477 |
|
| 478 |
// Handle when a verb's points are in the gap between top and bottom. |
| 479 |
auto addPts = [&expandGap, topOffset, bottomOffset](SkSpan<const SkPoint> pts) { |
| 480 |
for (const SkPoint p : pts) { |
| 481 |
if (topOffset < p.fY && p.fY < bottomOffset) { |
| 482 |
expandGap(p.fX); |
| 483 |
} |
| 484 |
} |
| 485 |
}; |
| 486 |
|
| 487 |
SkPath::Iter iter(path, false); |
| 488 |
while (auto rec = iter.next()) { |
| 489 |
const SkSpan<const SkPoint> pts = rec->fPoints; |
| 490 |
switch (rec->fVerb) { |
| 491 |
case SkPathVerb::kMove: { |
| 492 |
break; |
| 493 |
} |
| 494 |
case SkPathVerb::kLine: { |
| 495 |
auto [lineTop, lineBottom] = std::minmax({pts[0].fY, pts[1].fY}); |
| 496 |
|
| 497 |
// The y-coordinates of the points intersect the top and bottom offsets. |
| 498 |
if (topOffset <= lineBottom && lineTop <= bottomOffset) { |
| 499 |
addLine(pts, topOffset); |
| 500 |
addLine(pts, bottomOffset); |
| 501 |
addPts(pts); |
| 502 |
} |
| 503 |
break; |
| 504 |
} |
| 505 |
case SkPathVerb::kQuad: { |
| 506 |
auto [quadTop, quadBottom] = std::minmax({pts[0].fY, pts[1].fY, pts[2].fY}); |
| 507 |
|
| 508 |
// The y-coordinates of the points intersect the top and bottom offsets. |
| 509 |
if (topOffset <= quadBottom && quadTop <= bottomOffset) { |
| 510 |
addQuad(pts, topOffset); |
| 511 |
addQuad(pts, bottomOffset); |
| 512 |
addPts(pts); |
| 513 |
} |
| 514 |
break; |
| 515 |
} |
| 516 |
case SkPathVerb::kConic: { |
| 517 |
SkDEBUGFAIL("There should be no conic primitives in glyph outlines."); |
| 518 |
break; |
| 519 |
} |
| 520 |
case SkPathVerb::kCubic: { |
| 521 |
auto [cubicTop, cubicBottom] = |
| 522 |
std::minmax({pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY}); |
| 523 |
|
| 524 |
// The y-coordinates of the points intersect the top and bottom offsets. |
| 525 |
if (topOffset <= cubicBottom && cubicTop <= bottomOffset) { |
| 526 |
addCubic(pts, topOffset); |
| 527 |
addCubic(pts, bottomOffset); |
| 528 |
addPts(pts); |
| 529 |
} |
| 530 |
break; |
| 531 |
} |
| 532 |
case SkPathVerb::kClose: { |
| 533 |
break; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
return std::tie(left, right); |
| 539 |
} |
| 540 |
|
| 541 |
void SkGlyph::ensureIntercepts(const SkScalar* bounds, SkScalar scale, SkScalar xPos, |
| 542 |
SkScalar* array, int* count, SkArenaAlloc* alloc) { |
| 543 |
|
| 544 |
auto offsetResults = [scale, xPos]( |
| 545 |
const SkGlyph::Intercept* intercept,SkScalar* array, int* count) { |
| 546 |
if (array) { |
| 547 |
array += *count; |
| 548 |
for (int index = 0; index < 2; index++) { |
| 549 |
*array++ = intercept->fInterval[index] * scale + xPos; |
| 550 |
} |
| 551 |
} |
| 552 |
*count += 2; |
| 553 |
}; |
| 554 |
|
| 555 |
const SkGlyph::Intercept* match = |
| 556 |
[this](const SkScalar bounds[2]) -> const SkGlyph::Intercept* { |
| 557 |
if (fPathData == nullptr) { |
| 558 |
return nullptr; |
| 559 |
} |
| 560 |
const SkGlyph::Intercept* intercept = fPathData->fIntercept; |
| 561 |
while (intercept != nullptr) { |
| 562 |
if (bounds[0] == intercept->fBounds[0] && bounds[1] == intercept->fBounds[1]) { |
| 563 |
return intercept; |
| 564 |
} |
| 565 |
intercept = intercept->fNext; |
| 566 |
} |
| 567 |
return nullptr; |
| 568 |
}(bounds); |
| 569 |
|
| 570 |
if (match != nullptr) { |
| 571 |
if (match->fInterval[0] < match->fInterval[1]) { |
| 572 |
offsetResults(match, array, count); |
| 573 |
} |
| 574 |
return; |
| 575 |
} |
| 576 |
|
| 577 |
SkGlyph::Intercept* intercept = alloc->make<SkGlyph::Intercept>(); |
| 578 |
intercept->fNext = fPathData->fIntercept; |
| 579 |
intercept->fBounds[0] = bounds[0]; |
| 580 |
intercept->fBounds[1] = bounds[1]; |
| 581 |
intercept->fInterval[0] = SK_ScalarMax; |
| 582 |
intercept->fInterval[1] = SK_ScalarMin; |
| 583 |
fPathData->fIntercept = intercept; |
| 584 |
const SkPath* path = &(fPathData->fPath); |
| 585 |
const SkRect& pathBounds = path->getBounds(); |
| 586 |
if (pathBounds.fBottom < bounds[0] || bounds[1] < pathBounds.fTop) { |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
std::tie(intercept->fInterval[0], intercept->fInterval[1]) |
| 591 |
= calculate_path_gap(bounds[0], bounds[1], *path); |
| 592 |
|
| 593 |
if (intercept->fInterval[0] >= intercept->fInterval[1]) { |
| 594 |
intercept->fInterval[0] = SK_ScalarMax; |
| 595 |
intercept->fInterval[1] = SK_ScalarMin; |
| 596 |
return; |
| 597 |
} |
| 598 |
offsetResults(intercept, array, count); |
| 599 |
} |
| 600 |
|
| 601 |
namespace { |
| 602 |
uint32_t init_actions(const SkGlyph& glyph) { |
| 603 |
constexpr uint32_t kAllUnset = 0; |
| 604 |
constexpr uint32_t kDrop = SkTo<uint32_t>(GlyphAction::kDrop); |
| 605 |
constexpr uint32_t kAllDrop = |
| 606 |
kDrop << kDirectMask | |
| 607 |
kDrop << kDirectMaskCPU | |
| 608 |
kDrop << kMask | |
| 609 |
kDrop << kSDFT | |
| 610 |
kDrop << kPath | |
| 611 |
kDrop << kDrawable; |
| 612 |
return glyph.isEmpty() ? kAllDrop : kAllUnset; |
| 613 |
} |
| 614 |
} // namespace |
| 615 |
|
| 616 |
// -- SkGlyphDigest -------------------------------------------------------------------------------- |
| 617 |
SkGlyphDigest::SkGlyphDigest(size_t index, const SkGlyph& glyph) |
| 618 |
: fPackedID{SkTo<uint64_t>(glyph.getPackedID().value())} |
| 619 |
, fIndex{SkTo<uint64_t>(index)} |
| 620 |
, fIsEmpty(glyph.isEmpty()) |
| 621 |
, fFormat(glyph.maskFormat()) |
| 622 |
, fActions{init_actions(glyph)} |
| 623 |
, fLeft{SkTo<int16_t>(glyph.left())} |
| 624 |
, fTop{SkTo<int16_t>(glyph.top())} |
| 625 |
, fWidth{SkTo<uint16_t>(glyph.width())} |
| 626 |
, fHeight{SkTo<uint16_t>(glyph.height())} {} |
| 627 |
|
| 628 |
void SkGlyphDigest::setActionFor(skglyph::ActionType actionType, |
| 629 |
SkGlyph* glyph, |
| 630 |
StrikeForGPU* strike) { |
| 631 |
// We don't have to do any more if the glyph is marked as kDrop because it was isEmpty(). |
| 632 |
if (this->actionFor(actionType) == GlyphAction::kUnset) { |
| 633 |
GlyphAction action = GlyphAction::kReject; |
| 634 |
switch (actionType) { |
| 635 |
case kDirectMask: { |
| 636 |
if (this->fitsInAtlasDirect()) { |
| 637 |
action = GlyphAction::kAccept; |
| 638 |
} |
| 639 |
break; |
| 640 |
} |
| 641 |
case kDirectMaskCPU: { |
| 642 |
if (strike->prepareForImage(glyph)) { |
| 643 |
SkASSERT(!glyph->isEmpty()); |
| 644 |
action = GlyphAction::kAccept; |
| 645 |
} |
| 646 |
break; |
| 647 |
} |
| 648 |
case kMask: { |
| 649 |
if (this->fitsInAtlasInterpolated()) { |
| 650 |
action = GlyphAction::kAccept; |
| 651 |
} |
| 652 |
break; |
| 653 |
} |
| 654 |
case kSDFT: { |
| 655 |
if (this->fitsInAtlasDirect() && |
| 656 |
this->maskFormat() == SkMask::Format::kSDF_Format) { |
| 657 |
action = GlyphAction::kAccept; |
| 658 |
} |
| 659 |
break; |
| 660 |
} |
| 661 |
case kPath: { |
| 662 |
if (strike->prepareForPath(glyph)) { |
| 663 |
action = GlyphAction::kAccept; |
| 664 |
} |
| 665 |
break; |
| 666 |
} |
| 667 |
case kDrawable: { |
| 668 |
if (strike->prepareForDrawable(glyph)) { |
| 669 |
action = GlyphAction::kAccept; |
| 670 |
} |
| 671 |
break; |
| 672 |
} |
| 673 |
} |
| 674 |
this->setAction(actionType, action); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
bool SkGlyphDigest::FitsInAtlas(const SkGlyph& glyph) { |
| 679 |
return glyph.maxDimension() <= kSkSideTooBigForAtlas; |
| 680 |
} |
| 681 |
|
| 682 |
// -- SkGlyphPositionRoundingSpec ------------------------------------------------------------------ |
| 683 |
SkVector SkGlyphPositionRoundingSpec::HalfAxisSampleFreq( |
| 684 |
bool isSubpixel, SkAxisAlignment axisAlignment) { |
| 685 |
if (!isSubpixel) { |
| 686 |
return {SK_ScalarHalf, SK_ScalarHalf}; |
| 687 |
} else { |
| 688 |
switch (axisAlignment) { |
| 689 |
case SkAxisAlignment::kX: |
| 690 |
return {SkPackedGlyphID::kSubpixelRound, SK_ScalarHalf}; |
| 691 |
case SkAxisAlignment::kY: |
| 692 |
return {SK_ScalarHalf, SkPackedGlyphID::kSubpixelRound}; |
| 693 |
case SkAxisAlignment::kNone: |
| 694 |
return {SkPackedGlyphID::kSubpixelRound, SkPackedGlyphID::kSubpixelRound}; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
// Some compilers need this. |
| 699 |
return {0, 0}; |
| 700 |
} |
| 701 |
|
| 702 |
SkIPoint SkGlyphPositionRoundingSpec::IgnorePositionMask( |
| 703 |
bool isSubpixel, SkAxisAlignment axisAlignment) { |
| 704 |
return SkIPoint::Make((!isSubpixel || axisAlignment == SkAxisAlignment::kY) ? 0 : ~0, |
| 705 |
(!isSubpixel || axisAlignment == SkAxisAlignment::kX) ? 0 : ~0); |
| 706 |
} |
| 707 |
|
| 708 |
SkIPoint SkGlyphPositionRoundingSpec::IgnorePositionFieldMask(bool isSubpixel, |
| 709 |
SkAxisAlignment axisAlignment) { |
| 710 |
SkIPoint ignoreMask = IgnorePositionMask(isSubpixel, axisAlignment); |
| 711 |
SkIPoint answer{ignoreMask.x() & SkPackedGlyphID::kXYFieldMask.x(), |
| 712 |
ignoreMask.y() & SkPackedGlyphID::kXYFieldMask.y()}; |
| 713 |
return answer; |
| 714 |
} |
| 715 |
|
| 716 |
SkGlyphPositionRoundingSpec::SkGlyphPositionRoundingSpec( |
| 717 |
bool isSubpixel, SkAxisAlignment axisAlignment) |
| 718 |
: halfAxisSampleFreq{HalfAxisSampleFreq(isSubpixel, axisAlignment)} |
| 719 |
, ignorePositionMask{IgnorePositionMask(isSubpixel, axisAlignment)} |
| 720 |
, ignorePositionFieldMask {IgnorePositionFieldMask(isSubpixel, axisAlignment)} {} |