| 1 |
/* |
| 2 |
* Copyright 2020 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 "include/utils/SkCustomTypeface.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkDrawable.h" |
| 14 |
#include "include/core/SkFontArguments.h" |
| 15 |
#include "include/core/SkFontMetrics.h" |
| 16 |
#include "include/core/SkFontParameters.h" |
| 17 |
#include "include/core/SkFontStyle.h" |
| 18 |
#include "include/core/SkFontTypes.h" |
| 19 |
#include "include/core/SkMatrix.h" |
| 20 |
#include "include/core/SkPaint.h" |
| 21 |
#include "include/core/SkPath.h" |
| 22 |
#include "include/core/SkPoint.h" |
| 23 |
#include "include/core/SkRect.h" |
| 24 |
#include "include/core/SkRefCnt.h" |
| 25 |
#include "include/core/SkScalar.h" |
| 26 |
#include "include/core/SkSerialProcs.h" |
| 27 |
#include "include/core/SkSpan.h" |
| 28 |
#include "include/core/SkStream.h" |
| 29 |
#include "include/core/SkString.h" |
| 30 |
#include "include/core/SkTypeface.h" |
| 31 |
#include "include/core/SkTypes.h" |
| 32 |
#include "include/private/base/SkAlign.h" |
| 33 |
#include "include/private/base/SkFixed.h" |
| 34 |
#include "include/private/base/SkMalloc.h" |
| 35 |
#include "include/private/base/SkTo.h" |
| 36 |
#include "src/core/SkAdvancedTypefaceMetrics.h" // IWYU pragma: keep |
| 37 |
#include "src/core/SkFontDescriptor.h" |
| 38 |
#include "src/core/SkGlyph.h" |
| 39 |
#include "src/core/SkMask.h" |
| 40 |
#include "src/core/SkScalerContext.h" |
| 41 |
#include "src/core/SkStreamPriv.h" |
| 42 |
|
| 43 |
#include <algorithm> |
| 44 |
#include <cstdint> |
| 45 |
#include <cstring> |
| 46 |
#include <memory> |
| 47 |
#include <optional> |
| 48 |
#include <utility> |
| 49 |
#include <vector> |
| 50 |
|
| 51 |
class SkArenaAlloc; |
| 52 |
class SkDescriptor; |
| 53 |
|
| 54 |
namespace { |
| 55 |
static inline const constexpr bool kSkShowTextBlitCoverage = false; |
| 56 |
} |
| 57 |
|
| 58 |
static SkFontMetrics scale_fontmetrics(const SkFontMetrics& src, float sx, float sy) { |
| 59 |
SkFontMetrics dst = src; |
| 60 |
|
| 61 |
#define SCALE_X(field) dst.field *= sx |
| 62 |
#define SCALE_Y(field) dst.field *= sy |
| 63 |
|
| 64 |
SCALE_X(fAvgCharWidth); |
| 65 |
SCALE_X(fMaxCharWidth); |
| 66 |
SCALE_X(fXMin); |
| 67 |
SCALE_X(fXMax); |
| 68 |
|
| 69 |
SCALE_Y(fTop); |
| 70 |
SCALE_Y(fAscent); |
| 71 |
SCALE_Y(fDescent); |
| 72 |
SCALE_Y(fBottom); |
| 73 |
SCALE_Y(fLeading); |
| 74 |
SCALE_Y(fXHeight); |
| 75 |
SCALE_Y(fCapHeight); |
| 76 |
SCALE_Y(fUnderlineThickness); |
| 77 |
SCALE_Y(fUnderlinePosition); |
| 78 |
SCALE_Y(fStrikeoutThickness); |
| 79 |
SCALE_Y(fStrikeoutPosition); |
| 80 |
|
| 81 |
#undef SCALE_X |
| 82 |
#undef SCALE_Y |
| 83 |
|
| 84 |
return dst; |
| 85 |
} |
| 86 |
|
| 87 |
class SkUserTypeface final : public SkTypeface { |
| 88 |
private: |
| 89 |
friend class SkCustomTypefaceBuilder; |
| 90 |
friend class SkUserScalerContext; |
| 91 |
|
| 92 |
explicit SkUserTypeface(SkFontStyle style, const SkFontMetrics& metrics, |
| 93 |
std::vector<SkCustomTypefaceBuilder::GlyphRec>&& recs) |
| 94 |
: SkTypeface(style) |
| 95 |
, fGlyphRecs(std::move(recs)) |
| 96 |
, fMetrics(metrics) |
| 97 |
{} |
| 98 |
|
| 99 |
const std::vector<SkCustomTypefaceBuilder::GlyphRec> fGlyphRecs; |
| 100 |
const SkFontMetrics fMetrics; |
| 101 |
|
| 102 |
std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&, |
| 103 |
const SkDescriptor* desc) const override; |
| 104 |
void onFilterRec(SkScalerContextRec* rec) const override; |
| 105 |
void getGlyphToUnicodeMap(SkSpan<SkUnichar>) const override; |
| 106 |
std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; |
| 107 |
|
| 108 |
void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override; |
| 109 |
|
| 110 |
void onCharsToGlyphs(SkSpan<const SkUnichar>, SkSpan<SkGlyphID>) const override; |
| 111 |
|
| 112 |
void onGetFamilyName(SkString* familyName) const override; |
| 113 |
bool onGetPostScriptName(SkString*) const override; |
| 114 |
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 115 |
|
| 116 |
std::unique_ptr<SkStreamAsset> onOpenStream(int*) const override; |
| 117 |
|
| 118 |
// trivial |
| 119 |
|
| 120 |
std::unique_ptr<SkStreamAsset> onOpenExistingStream(int*) const override { return nullptr; } |
| 121 |
|
| 122 |
sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { |
| 123 |
return sk_ref_sp(this); |
| 124 |
} |
| 125 |
int onCountGlyphs() const override { return this->glyphCount(); } |
| 126 |
int onGetUPEM() const override { return 2048; /* ?? */ } |
| 127 |
bool onComputeBounds(SkRect* bounds) const override { |
| 128 |
bounds->setLTRB(fMetrics.fXMin, fMetrics.fTop, fMetrics.fXMax, fMetrics.fBottom); |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
// noops |
| 133 |
|
| 134 |
void getPostScriptGlyphNames(SkString*) const override {} |
| 135 |
bool onGlyphMaskNeedsCurrentColor() const override { return false; } |
| 136 |
int onGetVariationDesignPosition( |
| 137 |
SkSpan<SkFontArguments::VariationPosition::Coordinate>) const override { return 0; } |
| 138 |
int onGetVariationDesignParameters(SkSpan<SkFontParameters::Variation::Axis>) const override { |
| 139 |
return 0; |
| 140 |
} |
| 141 |
int onGetTableTags(SkSpan<SkFontTableTag>) const override { return 0; } |
| 142 |
size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override { return 0; } |
| 143 |
|
| 144 |
int glyphCount() const { |
| 145 |
return SkToInt(fGlyphRecs.size()); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
SkCustomTypefaceBuilder::SkCustomTypefaceBuilder() { |
| 150 |
sk_bzero(&fMetrics, sizeof(fMetrics)); |
| 151 |
} |
| 152 |
|
| 153 |
void SkCustomTypefaceBuilder::setMetrics(const SkFontMetrics& fm, float scale) { |
| 154 |
fMetrics = scale_fontmetrics(fm, scale, scale); |
| 155 |
} |
| 156 |
|
| 157 |
void SkCustomTypefaceBuilder::setFontStyle(SkFontStyle style) { |
| 158 |
fStyle = style; |
| 159 |
} |
| 160 |
|
| 161 |
SkCustomTypefaceBuilder::GlyphRec& SkCustomTypefaceBuilder::ensureStorage(SkGlyphID index) { |
| 162 |
if (index >= fGlyphRecs.size()) { |
| 163 |
fGlyphRecs.resize(SkToSizeT(index) + 1); |
| 164 |
} |
| 165 |
|
| 166 |
return fGlyphRecs[index]; |
| 167 |
} |
| 168 |
|
| 169 |
void SkCustomTypefaceBuilder::setGlyph(SkGlyphID index, float advance, const SkPath& path) { |
| 170 |
auto& rec = this->ensureStorage(index); |
| 171 |
rec.fAdvance = advance; |
| 172 |
rec.fPath = path; |
| 173 |
rec.fDrawable = nullptr; |
| 174 |
} |
| 175 |
|
| 176 |
void SkCustomTypefaceBuilder::setGlyph(SkGlyphID index, float advance, |
| 177 |
sk_sp<SkDrawable> drawable, const SkRect& bounds) { |
| 178 |
auto& rec = this->ensureStorage(index); |
| 179 |
rec.fAdvance = advance; |
| 180 |
rec.fDrawable = std::move(drawable); |
| 181 |
rec.fBounds = bounds; |
| 182 |
rec.fPath = SkPath(); |
| 183 |
} |
| 184 |
|
| 185 |
sk_sp<SkTypeface> SkCustomTypefaceBuilder::detach() { |
| 186 |
if (fGlyphRecs.empty()) return nullptr; |
| 187 |
|
| 188 |
// initially inverted, so that any "union" will overwrite the first time |
| 189 |
SkRect bounds = {SK_ScalarMax, SK_ScalarMax, -SK_ScalarMax, -SK_ScalarMax}; |
| 190 |
|
| 191 |
for (const auto& rec : fGlyphRecs) { |
| 192 |
bounds.join(rec.isDrawable() |
| 193 |
? rec.fBounds |
| 194 |
: rec.fPath.getBounds()); |
| 195 |
} |
| 196 |
|
| 197 |
fMetrics.fTop = bounds.top(); |
| 198 |
fMetrics.fBottom = bounds.bottom(); |
| 199 |
fMetrics.fXMin = bounds.left(); |
| 200 |
fMetrics.fXMax = bounds.right(); |
| 201 |
|
| 202 |
return sk_sp<SkUserTypeface>(new SkUserTypeface(fStyle, fMetrics, std::move(fGlyphRecs))); |
| 203 |
} |
| 204 |
|
| 205 |
///////////// |
| 206 |
|
| 207 |
void SkUserTypeface::onFilterRec(SkScalerContextRec* rec) const { |
| 208 |
rec->useStrokeForFakeBold(); |
| 209 |
rec->setHinting(SkFontHinting::kNone); |
| 210 |
} |
| 211 |
|
| 212 |
void SkUserTypeface::getGlyphToUnicodeMap(SkSpan<SkUnichar> glyphToUnicode) const { |
| 213 |
const int n = std::min(this->glyphCount(), (int)glyphToUnicode.size()); |
| 214 |
for (int gid = 0; gid < n; ++gid) { |
| 215 |
glyphToUnicode[gid] = SkTo<SkUnichar>(gid); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
std::unique_ptr<SkAdvancedTypefaceMetrics> SkUserTypeface::onGetAdvancedMetrics() const { |
| 220 |
return nullptr; |
| 221 |
} |
| 222 |
|
| 223 |
void SkUserTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const { |
| 224 |
desc->setFactoryId(SkCustomTypefaceBuilder::FactoryId); |
| 225 |
*isLocal = true; |
| 226 |
} |
| 227 |
|
| 228 |
void SkUserTypeface::onCharsToGlyphs(SkSpan<const SkUnichar> chars, |
| 229 |
SkSpan<SkGlyphID> glyphs) const { |
| 230 |
SkASSERT(chars.size() == glyphs.size()); |
| 231 |
const int glyphCount = this->glyphCount(); |
| 232 |
for (size_t i = 0; i < chars.size(); ++i) { |
| 233 |
glyphs[i] = chars[i] < glyphCount ? SkTo<SkGlyphID>(chars[i]) : 0; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void SkUserTypeface::onGetFamilyName(SkString* familyName) const { |
| 238 |
*familyName = ""; |
| 239 |
} |
| 240 |
|
| 241 |
bool SkUserTypeface::onGetPostScriptName(SkString*) const { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
SkTypeface::LocalizedStrings* SkUserTypeface::onCreateFamilyNameIterator() const { |
| 246 |
return nullptr; |
| 247 |
} |
| 248 |
|
| 249 |
////////////// |
| 250 |
|
| 251 |
class SkUserScalerContext : public SkScalerContext { |
| 252 |
public: |
| 253 |
SkUserScalerContext(SkUserTypeface& face, |
| 254 |
const SkScalerContextEffects& effects, |
| 255 |
const SkDescriptor* desc) |
| 256 |
: SkScalerContext(face, effects, desc) |
| 257 |
, fMatrix(fRec.getSingleMatrix()) |
| 258 |
{} |
| 259 |
|
| 260 |
const SkUserTypeface* userTF() const { |
| 261 |
return static_cast<SkUserTypeface*>(this->getTypeface()); |
| 262 |
} |
| 263 |
|
| 264 |
protected: |
| 265 |
GlyphMetrics generateMetrics(const SkGlyph& glyph, SkArenaAlloc*) override { |
| 266 |
GlyphMetrics mx(glyph.maskFormat()); |
| 267 |
|
| 268 |
const SkUserTypeface* tf = this->userTF(); |
| 269 |
const SkGlyphID gid = glyph.getGlyphID(); |
| 270 |
if (gid >= tf->fGlyphRecs.size()) { |
| 271 |
mx.neverRequestPath = true; |
| 272 |
return mx; |
| 273 |
} |
| 274 |
|
| 275 |
const auto& rec = tf->fGlyphRecs[gid]; |
| 276 |
mx.advance = fMatrix.mapPoint({rec.fAdvance, 0}); |
| 277 |
|
| 278 |
if (rec.isDrawable()) { |
| 279 |
mx.maskFormat = SkMask::kARGB32_Format; |
| 280 |
|
| 281 |
SkRect bounds = fMatrix.mapRect(rec.fBounds); |
| 282 |
bounds.offset(SkFixedToScalar(glyph.getSubXFixed()), |
| 283 |
SkFixedToScalar(glyph.getSubYFixed())); |
| 284 |
bounds.roundOut(&mx.bounds); |
| 285 |
|
| 286 |
// These do not have an outline path. |
| 287 |
mx.neverRequestPath = true; |
| 288 |
} else { |
| 289 |
mx.computeFromPath = true; |
| 290 |
} |
| 291 |
return mx; |
| 292 |
} |
| 293 |
|
| 294 |
void generateImage(const SkGlyph& glyph, void* imageBuffer) override { |
| 295 |
const auto& rec = this->userTF()->fGlyphRecs[glyph.getGlyphID()]; |
| 296 |
if (!rec.isDrawable()) { |
| 297 |
this->generateImageFromPath(glyph, imageBuffer); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
auto canvas = SkCanvas::MakeRasterDirectN32(glyph.width(), glyph.height(), |
| 302 |
static_cast<SkPMColor*>(imageBuffer), |
| 303 |
glyph.rowBytes()); |
| 304 |
if constexpr (kSkShowTextBlitCoverage) { |
| 305 |
canvas->clear(0x33FF0000); |
| 306 |
} else { |
| 307 |
canvas->clear(SK_ColorTRANSPARENT); |
| 308 |
} |
| 309 |
|
| 310 |
canvas->translate(-glyph.left(), -glyph.top()); |
| 311 |
canvas->translate(SkFixedToScalar(glyph.getSubXFixed()), |
| 312 |
SkFixedToScalar(glyph.getSubYFixed())); |
| 313 |
canvas->drawDrawable(rec.fDrawable.get(), &fMatrix); |
| 314 |
} |
| 315 |
|
| 316 |
std::optional<SkScalerContext::GeneratedPath> generatePath(const SkGlyph& glyph) override { |
| 317 |
const auto& rec = this->userTF()->fGlyphRecs[glyph.getGlyphID()]; |
| 318 |
|
| 319 |
SkASSERT(!rec.isDrawable()); |
| 320 |
|
| 321 |
return {{rec.fPath.makeTransform(fMatrix), false}}; |
| 322 |
} |
| 323 |
|
| 324 |
sk_sp<SkDrawable> generateDrawable(const SkGlyph& glyph) override { |
| 325 |
class DrawableMatrixWrapper final : public SkDrawable { |
| 326 |
public: |
| 327 |
DrawableMatrixWrapper(sk_sp<SkDrawable> drawable, const SkMatrix& m) |
| 328 |
: fDrawable(std::move(drawable)) |
| 329 |
, fMatrix(m) |
| 330 |
{} |
| 331 |
|
| 332 |
SkRect onGetBounds() override { |
| 333 |
return fMatrix.mapRect(fDrawable->getBounds()); |
| 334 |
} |
| 335 |
|
| 336 |
size_t onApproximateBytesUsed() override { |
| 337 |
return fDrawable->approximateBytesUsed() + sizeof(DrawableMatrixWrapper); |
| 338 |
} |
| 339 |
|
| 340 |
void onDraw(SkCanvas* canvas) override { |
| 341 |
if constexpr (kSkShowTextBlitCoverage) { |
| 342 |
SkPaint paint; |
| 343 |
paint.setColor(0x3300FF00); |
| 344 |
paint.setStyle(SkPaint::kFill_Style); |
| 345 |
canvas->drawRect(this->onGetBounds(), paint); |
| 346 |
} |
| 347 |
canvas->drawDrawable(fDrawable.get(), &fMatrix); |
| 348 |
} |
| 349 |
private: |
| 350 |
const sk_sp<SkDrawable> fDrawable; |
| 351 |
const SkMatrix fMatrix; |
| 352 |
}; |
| 353 |
|
| 354 |
const auto& rec = this->userTF()->fGlyphRecs[glyph.getGlyphID()]; |
| 355 |
|
| 356 |
return rec.fDrawable |
| 357 |
? sk_make_sp<DrawableMatrixWrapper>(rec.fDrawable, fMatrix) |
| 358 |
: nullptr; |
| 359 |
} |
| 360 |
|
| 361 |
void generateFontMetrics(SkFontMetrics* metrics) override { |
| 362 |
auto [sx, sy] = fMatrix.mapPoint({1, 1}); |
| 363 |
*metrics = scale_fontmetrics(this->userTF()->fMetrics, sx, sy); |
| 364 |
} |
| 365 |
|
| 366 |
private: |
| 367 |
const SkMatrix fMatrix; |
| 368 |
}; |
| 369 |
|
| 370 |
std::unique_ptr<SkScalerContext> SkUserTypeface::onCreateScalerContext( |
| 371 |
const SkScalerContextEffects& effects, const SkDescriptor* desc) const |
| 372 |
{ |
| 373 |
return std::make_unique<SkUserScalerContext>(*const_cast<SkUserTypeface*>(this), effects, desc); |
| 374 |
} |
| 375 |
|
| 376 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 377 |
|
| 378 |
static constexpr int kMaxGlyphCount = 65536; |
| 379 |
static constexpr size_t kHeaderSize = 16; |
| 380 |
static const char gHeaderString[] = "SkUserTypeface01"; |
| 381 |
static_assert(sizeof(gHeaderString) == 1 + kHeaderSize, "need header to be 16 bytes"); |
| 382 |
|
| 383 |
enum GlyphType : uint32_t { kPath, kDrawable }; |
| 384 |
|
| 385 |
std::unique_ptr<SkStreamAsset> SkUserTypeface::onOpenStream(int* ttcIndex) const { |
| 386 |
SkDynamicMemoryWStream wstream; |
| 387 |
|
| 388 |
wstream.write(gHeaderString, kHeaderSize); |
| 389 |
|
| 390 |
wstream.write(&fMetrics, sizeof(fMetrics)); |
| 391 |
|
| 392 |
SkFontStyle style = this->fontStyle(); |
| 393 |
wstream.write(&style, sizeof(style)); |
| 394 |
|
| 395 |
wstream.write32(this->glyphCount()); |
| 396 |
|
| 397 |
for (const auto& rec : fGlyphRecs) { |
| 398 |
wstream.write32(rec.isDrawable() ? GlyphType::kDrawable : GlyphType::kPath); |
| 399 |
|
| 400 |
wstream.writeScalar(rec.fAdvance); |
| 401 |
|
| 402 |
wstream.write(&rec.fBounds, sizeof(rec.fBounds)); |
| 403 |
|
| 404 |
auto data = rec.isDrawable() |
| 405 |
? rec.fDrawable->serialize() |
| 406 |
: rec.fPath.serialize(); |
| 407 |
|
| 408 |
const size_t sz = data->size(); |
| 409 |
SkASSERT(SkIsAlign4(sz)); |
| 410 |
wstream.write(&sz, sizeof(sz)); |
| 411 |
wstream.write(data->data(), sz); |
| 412 |
} |
| 413 |
|
| 414 |
*ttcIndex = 0; |
| 415 |
return wstream.detachAsStream(); |
| 416 |
} |
| 417 |
|
| 418 |
class AutoRestorePosition { |
| 419 |
SkStream* fStream; |
| 420 |
size_t fPosition; |
| 421 |
public: |
| 422 |
AutoRestorePosition(SkStream* stream) : fStream(stream) { |
| 423 |
fPosition = stream->getPosition(); |
| 424 |
} |
| 425 |
|
| 426 |
~AutoRestorePosition() { |
| 427 |
if (fStream) { |
| 428 |
fStream->seek(fPosition); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
// So we don't restore the position |
| 433 |
void markDone() { fStream = nullptr; } |
| 434 |
}; |
| 435 |
|
| 436 |
sk_sp<SkTypeface> SkCustomTypefaceBuilder::Deserialize(SkStream* stream) { |
| 437 |
AutoRestorePosition arp(stream); |
| 438 |
|
| 439 |
char header[kHeaderSize]; |
| 440 |
if (stream->read(header, kHeaderSize) != kHeaderSize || |
| 441 |
0 != memcmp(header, gHeaderString, kHeaderSize)) |
| 442 |
{ |
| 443 |
return nullptr; |
| 444 |
} |
| 445 |
|
| 446 |
SkFontMetrics metrics; |
| 447 |
if (stream->read(&metrics, sizeof(metrics)) != sizeof(metrics)) { |
| 448 |
return nullptr; |
| 449 |
} |
| 450 |
|
| 451 |
SkFontStyle style; |
| 452 |
if (stream->read(&style, sizeof(style)) != sizeof(style)) { |
| 453 |
return nullptr; |
| 454 |
} |
| 455 |
|
| 456 |
int glyphCount; |
| 457 |
if (!stream->readS32(&glyphCount) || glyphCount < 0 || glyphCount > kMaxGlyphCount) { |
| 458 |
return nullptr; |
| 459 |
} |
| 460 |
|
| 461 |
SkCustomTypefaceBuilder builder; |
| 462 |
|
| 463 |
builder.setMetrics(metrics); |
| 464 |
builder.setFontStyle(style); |
| 465 |
|
| 466 |
for (int i = 0; i < glyphCount; ++i) { |
| 467 |
uint32_t gtype; |
| 468 |
if (!stream->readU32(>ype) || |
| 469 |
(gtype != GlyphType::kDrawable && gtype != GlyphType::kPath)) { |
| 470 |
return nullptr; |
| 471 |
} |
| 472 |
|
| 473 |
float advance; |
| 474 |
if (!stream->readScalar(&advance)) { |
| 475 |
return nullptr; |
| 476 |
} |
| 477 |
|
| 478 |
SkRect bounds; |
| 479 |
if (stream->read(&bounds, sizeof(bounds)) != sizeof(bounds) || !bounds.isFinite()) { |
| 480 |
return nullptr; |
| 481 |
} |
| 482 |
|
| 483 |
// SkPath and SkDrawable cannot read from a stream, so we have to page them into ram |
| 484 |
size_t sz; |
| 485 |
if (stream->read(&sz, sizeof(sz)) != sizeof(sz)) { |
| 486 |
return nullptr; |
| 487 |
} |
| 488 |
|
| 489 |
// The amount of bytes in the stream must be at least as big as sz, otherwise |
| 490 |
// sz is invalid. |
| 491 |
if (SkStreamPriv::RemainingLengthIsBelow(stream, sz)) { |
| 492 |
return nullptr; |
| 493 |
} |
| 494 |
|
| 495 |
auto data = SkData::MakeUninitialized(sz); |
| 496 |
if (stream->read(data->writable_data(), sz) != sz) { |
| 497 |
return nullptr; |
| 498 |
} |
| 499 |
|
| 500 |
switch (gtype) { |
| 501 |
case GlyphType::kDrawable: { |
| 502 |
SkDeserialProcs procs; |
| 503 |
procs.fAllowSkSL = false; |
| 504 |
auto drawable = SkDrawable::Deserialize(data->data(), data->size(), &procs); |
| 505 |
if (!drawable) { |
| 506 |
return nullptr; |
| 507 |
} |
| 508 |
builder.setGlyph(i, advance, std::move(drawable), bounds); |
| 509 |
} break; |
| 510 |
case GlyphType::kPath: { |
| 511 |
size_t bytesRead = 0; |
| 512 |
auto path = SkPath::ReadFromMemory(data->data(), data->size(), &bytesRead); |
| 513 |
if (path.has_value() && (bytesRead == data->size())) { |
| 514 |
builder.setGlyph(i, advance, *path); |
| 515 |
} else { |
| 516 |
return nullptr; |
| 517 |
} |
| 518 |
} break; |
| 519 |
default: |
| 520 |
return nullptr; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
arp.markDone(); |
| 525 |
return builder.detach(); |
| 526 |
} |
| 527 |
|
| 528 |
sk_sp<SkTypeface> SkCustomTypefaceBuilder::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, |
| 529 |
const SkFontArguments&) { |
| 530 |
return Deserialize(stream.get()); |
| 531 |
} |