| 1 |
/* |
| 2 |
* Copyright 2011 The Android Open Source Project |
| 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 |
#include "include/core/SkTypeface.h" |
| 8 |
|
| 9 |
#include "include/core/SkData.h" |
| 10 |
#include "include/core/SkFont.h" |
| 11 |
#include "include/core/SkFontMetrics.h" |
| 12 |
#include "include/core/SkFontMgr.h" |
| 13 |
#include "include/core/SkFontTypes.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkStream.h" |
| 16 |
#include "include/private/base/SkDebug.h" |
| 17 |
#include "include/private/base/SkMalloc.h" |
| 18 |
#include "include/private/base/SkOnce.h" |
| 19 |
#include "include/private/base/SkTemplates.h" |
| 20 |
#include "include/utils/SkCustomTypeface.h" |
| 21 |
#include "src/base/SkBitmaskEnum.h" |
| 22 |
#include "src/base/SkEndian.h" |
| 23 |
#include "src/base/SkNoDestructor.h" |
| 24 |
#include "src/base/SkUTF.h" |
| 25 |
#include "src/core/SkAdvancedTypefaceMetrics.h" |
| 26 |
#include "src/core/SkDescriptor.h" |
| 27 |
#include "src/core/SkFontDescriptor.h" |
| 28 |
#include "src/core/SkFontPriv.h" |
| 29 |
#include "src/core/SkScalerContext.h" |
| 30 |
#include "src/core/SkTypefaceCache.h" |
| 31 |
#include "src/sfnt/SkOTTable_OS_2.h" |
| 32 |
|
| 33 |
#if defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 34 |
#include "src/ports/SkTypeface_FreeType.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#if defined(SK_TYPEFACE_FACTORY_CORETEXT) |
| 38 |
#include "src/ports/SkTypeface_mac_ct.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#if defined(SK_TYPEFACE_FACTORY_DIRECTWRITE) |
| 42 |
#include "src/ports/SkTypeface_win_dw.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 46 |
#include "include/ports/SkTypeface_fontations.h" |
| 47 |
#include "src/ports/SkTypeface_fontations_factory.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#include <algorithm> |
| 51 |
#include <cstddef> |
| 52 |
#include <cstring> |
| 53 |
#include <vector> |
| 54 |
|
| 55 |
using namespace skia_private; |
| 56 |
|
| 57 |
SkTypeface::SkTypeface(const SkFontStyle& style, bool isFixedPitch) |
| 58 |
: fUniqueID(SkTypefaceCache::NewTypefaceID()), fStyle(style), fIsFixedPitch(isFixedPitch) { } |
| 59 |
|
| 60 |
SkTypeface::~SkTypeface() { } |
| 61 |
|
| 62 |
/////////////////////////////////////////////////////////////////////////////// |
| 63 |
|
| 64 |
namespace { |
| 65 |
|
| 66 |
class SkEmptyTypeface : public SkTypeface { |
| 67 |
public: |
| 68 |
static sk_sp<SkTypeface> Make() { |
| 69 |
static SkNoDestructor<SkEmptyTypeface> instance; |
| 70 |
return sk_ref_sp(instance.get()); |
| 71 |
} |
| 72 |
|
| 73 |
static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('e','m','t','y'); |
| 74 |
static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset> stream, |
| 75 |
const SkFontArguments&) { |
| 76 |
if (stream->getLength() == 0) { |
| 77 |
return SkEmptyTypeface::Make(); |
| 78 |
} |
| 79 |
return nullptr; |
| 80 |
} |
| 81 |
protected: |
| 82 |
friend SkNoDestructor<SkEmptyTypeface>; |
| 83 |
SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { } |
| 84 |
|
| 85 |
std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; } |
| 86 |
sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { |
| 87 |
return sk_ref_sp(this); |
| 88 |
} |
| 89 |
std::unique_ptr<SkScalerContext> onCreateScalerContext( |
| 90 |
const SkScalerContextEffects& effects, const SkDescriptor* desc) const override |
| 91 |
{ |
| 92 |
return SkScalerContext::MakeEmpty(*const_cast<SkEmptyTypeface*>(this), effects, desc); |
| 93 |
} |
| 94 |
void onFilterRec(SkScalerContextRec*) const override { } |
| 95 |
std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override { |
| 96 |
return nullptr; |
| 97 |
} |
| 98 |
void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { |
| 99 |
desc->setFactoryId(FactoryId); |
| 100 |
*serialize = false; |
| 101 |
} |
| 102 |
void onCharsToGlyphs(SkSpan<const SkUnichar> chars, SkSpan<SkGlyphID> glyphs) const override { |
| 103 |
sk_bzero(glyphs.data(), glyphs.size_bytes()); |
| 104 |
} |
| 105 |
int onCountGlyphs() const override { return 0; } |
| 106 |
void getPostScriptGlyphNames(SkString*) const override {} |
| 107 |
void getGlyphToUnicodeMap(SkSpan<SkUnichar>) const override {} |
| 108 |
int onGetUPEM() const override { return 0; } |
| 109 |
bool onComputeBounds(SkRect* bounds) const override { return false; } |
| 110 |
|
| 111 |
class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings { |
| 112 |
public: |
| 113 |
bool next(SkTypeface::LocalizedString*) override { return false; } |
| 114 |
}; |
| 115 |
void onGetFamilyName(SkString* familyName) const override { |
| 116 |
familyName->reset(); |
| 117 |
} |
| 118 |
bool onGetPostScriptName(SkString*) const override { |
| 119 |
return false; |
| 120 |
} |
| 121 |
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { |
| 122 |
return new EmptyLocalizedStrings; |
| 123 |
} |
| 124 |
bool onGlyphMaskNeedsCurrentColor() const override { |
| 125 |
return false; |
| 126 |
} |
| 127 |
int onGetVariationDesignPosition( |
| 128 |
SkSpan<SkFontArguments::VariationPosition::Coordinate>) const override |
| 129 |
{ |
| 130 |
return 0; |
| 131 |
} |
| 132 |
int onGetVariationDesignParameters(SkSpan<SkFontParameters::Variation::Axis>) const override |
| 133 |
{ |
| 134 |
return 0; |
| 135 |
} |
| 136 |
int onGetTableTags(SkSpan<SkFontTableTag>) const override { return 0; } |
| 137 |
size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
} // namespace |
| 143 |
|
| 144 |
sk_sp<SkTypeface> SkTypeface::MakeEmpty() { |
| 145 |
return SkEmptyTypeface::Make(); |
| 146 |
} |
| 147 |
|
| 148 |
bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { |
| 149 |
if (facea == faceb) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
if (!facea || !faceb) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
return facea->uniqueID() == faceb->uniqueID(); |
| 156 |
} |
| 157 |
|
| 158 |
/////////////////////////////////////////////////////////////////////////////// |
| 159 |
|
| 160 |
namespace { |
| 161 |
|
| 162 |
struct DecoderProc { |
| 163 |
SkFourByteTag id; |
| 164 |
sk_sp<SkTypeface> (*makeFromStream)(std::unique_ptr<SkStreamAsset>, const SkFontArguments&); |
| 165 |
}; |
| 166 |
|
| 167 |
std::vector<DecoderProc>* decoders() { |
| 168 |
static SkNoDestructor<std::vector<DecoderProc>> decoders{{ |
| 169 |
{ SkEmptyTypeface::FactoryId, SkEmptyTypeface::MakeFromStream }, |
| 170 |
{ SkCustomTypefaceBuilder::FactoryId, SkCustomTypefaceBuilder::MakeFromStream }, |
| 171 |
#ifdef SK_TYPEFACE_FACTORY_CORETEXT |
| 172 |
{ SkTypeface_Mac::FactoryId, SkTypeface_Mac::MakeFromStream }, |
| 173 |
#endif |
| 174 |
#ifdef SK_TYPEFACE_FACTORY_DIRECTWRITE |
| 175 |
{ DWriteFontTypeface::FactoryId, DWriteFontTypeface::MakeFromStream }, |
| 176 |
#endif |
| 177 |
#ifdef SK_TYPEFACE_FACTORY_FREETYPE |
| 178 |
{ SkTypeface_FreeType::FactoryId, SkTypeface_FreeType::MakeFromStream }, |
| 179 |
#endif |
| 180 |
#ifdef SK_TYPEFACE_FACTORY_FONTATIONS |
| 181 |
{ SkTypefaces::Fontations::FactoryId, SkTypeface_Make_Fontations }, |
| 182 |
#endif |
| 183 |
}}; |
| 184 |
return decoders.get(); |
| 185 |
} |
| 186 |
|
| 187 |
} // namespace |
| 188 |
|
| 189 |
sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const { |
| 190 |
return this->onMakeClone(args); |
| 191 |
} |
| 192 |
|
| 193 |
/////////////////////////////////////////////////////////////////////////////// |
| 194 |
|
| 195 |
void SkTypeface::Register( |
| 196 |
FactoryId id, |
| 197 |
sk_sp<SkTypeface> (*make)(std::unique_ptr<SkStreamAsset>, const SkFontArguments&)) { |
| 198 |
decoders()->push_back(DecoderProc{id, make}); |
| 199 |
} |
| 200 |
|
| 201 |
bool SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const { |
| 202 |
bool isLocalData = false; |
| 203 |
SkFontDescriptor desc; |
| 204 |
this->onGetFontDescriptor(&desc, &isLocalData); |
| 205 |
if (desc.getFactoryId() == 0) { |
| 206 |
SkDEBUGF("Factory was not set for %s.\n", desc.getFamilyName()); |
| 207 |
} |
| 208 |
|
| 209 |
bool shouldSerializeData = false; |
| 210 |
switch (behavior) { |
| 211 |
case SerializeBehavior::kDoIncludeData: shouldSerializeData = true; break; |
| 212 |
case SerializeBehavior::kDontIncludeData: shouldSerializeData = false; break; |
| 213 |
case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break; |
| 214 |
} |
| 215 |
|
| 216 |
if (shouldSerializeData) { |
| 217 |
int index; |
| 218 |
desc.setStream(this->openStream(&index)); |
| 219 |
if (desc.hasStream()) { |
| 220 |
desc.setCollectionIndex(index); |
| 221 |
} |
| 222 |
|
| 223 |
int numAxes = this->getVariationDesignPosition({}); |
| 224 |
if (0 < numAxes) { |
| 225 |
numAxes = this->getVariationDesignPosition({desc.setVariationCoordinates(numAxes), |
| 226 |
(size_t)numAxes}); |
| 227 |
if (numAxes <= 0) { |
| 228 |
desc.setVariationCoordinates(0); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return desc.serialize(wstream); |
| 233 |
} |
| 234 |
|
| 235 |
sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const { |
| 236 |
SkDynamicMemoryWStream stream; |
| 237 |
return this->serialize(&stream, behavior) ? stream.detachAsData() : nullptr; |
| 238 |
} |
| 239 |
|
| 240 |
sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream, sk_sp<SkFontMgr> lastResortMgr) { |
| 241 |
SkFontDescriptor desc; |
| 242 |
if (!SkFontDescriptor::Deserialize(stream, &desc)) { |
| 243 |
return nullptr; |
| 244 |
} |
| 245 |
|
| 246 |
if (desc.hasStream()) { |
| 247 |
for (const DecoderProc& proc : *decoders()) { |
| 248 |
if (proc.id == desc.getFactoryId()) { |
| 249 |
return proc.makeFromStream(desc.detachStream(), desc.getFontArguments()); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
[[maybe_unused]] FactoryId id = desc.getFactoryId(); |
| 254 |
SkDEBUGF("Could not find factory %c%c%c%c for %s.\n", |
| 255 |
(char)((id >> 24) & 0xFF), |
| 256 |
(char)((id >> 16) & 0xFF), |
| 257 |
(char)((id >> 8) & 0xFF), |
| 258 |
(char)((id >> 0) & 0xFF), |
| 259 |
desc.getFamilyName()); |
| 260 |
|
| 261 |
if (lastResortMgr) { |
| 262 |
// If we've gotten to here, we will try desperately to find something that might match |
| 263 |
// as a kind of last ditch effort to make something work (and maybe this SkFontMgr knows |
| 264 |
// something about the serialization and can look up the right thing by name anyway if |
| 265 |
// the user provides it). |
| 266 |
// Any time it is used the user will probably get the wrong glyphs drawn (and if they're |
| 267 |
// right it is totally by accident). But sometimes drawing something or getting lucky |
| 268 |
// while debugging is better than drawing nothing at all. |
| 269 |
sk_sp<SkTypeface> typeface = lastResortMgr->makeFromStream(desc.detachStream(), |
| 270 |
desc.getFontArguments()); |
| 271 |
if (typeface) { |
| 272 |
return typeface; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
if (lastResortMgr) { |
| 277 |
return lastResortMgr->legacyMakeTypeface(desc.getFamilyName(), desc.getStyle()); |
| 278 |
} |
| 279 |
return SkEmptyTypeface::Make(); |
| 280 |
} |
| 281 |
|
| 282 |
/////////////////////////////////////////////////////////////////////////////// |
| 283 |
|
| 284 |
bool SkTypeface::glyphMaskNeedsCurrentColor() const { |
| 285 |
return this->onGlyphMaskNeedsCurrentColor(); |
| 286 |
} |
| 287 |
|
| 288 |
int SkTypeface::getVariationDesignPosition( |
| 289 |
SkSpan<SkFontArguments::VariationPosition::Coordinate> coordinates) const |
| 290 |
{ |
| 291 |
return this->onGetVariationDesignPosition(coordinates); |
| 292 |
} |
| 293 |
|
| 294 |
int SkTypeface::getVariationDesignParameters( |
| 295 |
SkSpan<SkFontParameters::Variation::Axis> parameters) const |
| 296 |
{ |
| 297 |
return this->onGetVariationDesignParameters(parameters); |
| 298 |
} |
| 299 |
|
| 300 |
int SkTypeface::countTables() const { |
| 301 |
return this->onGetTableTags({}); |
| 302 |
} |
| 303 |
|
| 304 |
int SkTypeface::readTableTags(SkSpan<SkFontTableTag> tags) const { |
| 305 |
return this->onGetTableTags(tags); |
| 306 |
} |
| 307 |
|
| 308 |
size_t SkTypeface::getTableSize(SkFontTableTag tag) const { |
| 309 |
return this->onGetTableData(tag, 0, ~0U, nullptr); |
| 310 |
} |
| 311 |
|
| 312 |
size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length, |
| 313 |
void* data) const { |
| 314 |
return this->onGetTableData(tag, offset, length, data); |
| 315 |
} |
| 316 |
|
| 317 |
sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const { |
| 318 |
return this->onCopyTableData(tag); |
| 319 |
} |
| 320 |
|
| 321 |
sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const { |
| 322 |
size_t size = this->getTableSize(tag); |
| 323 |
if (size) { |
| 324 |
sk_sp<SkData> data = SkData::MakeUninitialized(size); |
| 325 |
(void)this->getTableData(tag, 0, size, data->writable_data()); |
| 326 |
return data; |
| 327 |
} |
| 328 |
return nullptr; |
| 329 |
} |
| 330 |
|
| 331 |
std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const { |
| 332 |
int ttcIndexStorage; |
| 333 |
if (nullptr == ttcIndex) { |
| 334 |
// So our subclasses don't need to check for null param |
| 335 |
ttcIndex = &ttcIndexStorage; |
| 336 |
} |
| 337 |
return this->onOpenStream(ttcIndex); |
| 338 |
} |
| 339 |
|
| 340 |
std::unique_ptr<SkStreamAsset> SkTypeface::openExistingStream(int* ttcIndex) const { |
| 341 |
int ttcIndexStorage; |
| 342 |
if (nullptr == ttcIndex) { |
| 343 |
// So our subclasses don't need to check for null param |
| 344 |
ttcIndex = &ttcIndexStorage; |
| 345 |
} |
| 346 |
return this->onOpenExistingStream(ttcIndex); |
| 347 |
} |
| 348 |
|
| 349 |
std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext( |
| 350 |
const SkScalerContextEffects& effects, const SkDescriptor* desc) const { |
| 351 |
std::unique_ptr<SkScalerContext> scalerContext = this->onCreateScalerContext(effects, desc); |
| 352 |
SkASSERT(scalerContext); |
| 353 |
return scalerContext; |
| 354 |
} |
| 355 |
|
| 356 |
std::unique_ptr<SkScalerContext> SkTypeface::onCreateScalerContextAsProxyTypeface |
| 357 |
(const SkScalerContextEffects&, |
| 358 |
const SkDescriptor*, |
| 359 |
SkTypeface*) const { |
| 360 |
SK_ABORT("Not implemented."); |
| 361 |
} |
| 362 |
|
| 363 |
void SkTypeface::unicharsToGlyphs(SkSpan<const SkUnichar> uni, SkSpan<SkGlyphID> glyphs) const { |
| 364 |
if (const size_t n = std::min(uni.size(), glyphs.size())) { |
| 365 |
this->onCharsToGlyphs(uni.first(n), glyphs.first(n)); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const { |
| 370 |
SkGlyphID glyphs[1] = { 0 }; |
| 371 |
this->onCharsToGlyphs({&uni, 1}, glyphs); |
| 372 |
return glyphs[0]; |
| 373 |
} |
| 374 |
|
| 375 |
namespace { |
| 376 |
class SkConvertToUTF32 { |
| 377 |
public: |
| 378 |
SkConvertToUTF32() {} |
| 379 |
|
| 380 |
const SkUnichar* convert(const void* text, size_t byteLength, SkTextEncoding encoding) { |
| 381 |
const SkUnichar* uni; |
| 382 |
switch (encoding) { |
| 383 |
case SkTextEncoding::kUTF8: { |
| 384 |
uni = fStorage.reset(byteLength); |
| 385 |
const char* ptr = (const char*)text; |
| 386 |
const char* end = ptr + byteLength; |
| 387 |
for (int i = 0; ptr < end; ++i) { |
| 388 |
fStorage[i] = SkUTF::NextUTF8(&ptr, end); |
| 389 |
} |
| 390 |
} break; |
| 391 |
case SkTextEncoding::kUTF16: { |
| 392 |
uni = fStorage.reset(byteLength); |
| 393 |
const uint16_t* ptr = (const uint16_t*)text; |
| 394 |
const uint16_t* end = ptr + (byteLength >> 1); |
| 395 |
for (int i = 0; ptr < end; ++i) { |
| 396 |
fStorage[i] = SkUTF::NextUTF16(&ptr, end); |
| 397 |
} |
| 398 |
} break; |
| 399 |
case SkTextEncoding::kUTF32: |
| 400 |
uni = (const SkUnichar*)text; |
| 401 |
break; |
| 402 |
default: |
| 403 |
SK_ABORT("unexpected enum"); |
| 404 |
} |
| 405 |
return uni; |
| 406 |
} |
| 407 |
|
| 408 |
private: |
| 409 |
AutoSTMalloc<256, SkUnichar> fStorage; |
| 410 |
}; |
| 411 |
} |
| 412 |
|
| 413 |
size_t SkTypeface::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding, |
| 414 |
SkSpan<SkGlyphID> glyphs) const { |
| 415 |
if (0 == byteLength) { |
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
SkASSERT(text); |
| 420 |
|
| 421 |
size_t count = SkFontPriv::CountTextElements(text, byteLength, encoding); |
| 422 |
if (count > glyphs.size()) { |
| 423 |
return count; |
| 424 |
} |
| 425 |
|
| 426 |
if (encoding == SkTextEncoding::kGlyphID) { |
| 427 |
memcpy(glyphs.data(), text, count << 1); |
| 428 |
return count; |
| 429 |
} |
| 430 |
|
| 431 |
SkConvertToUTF32 storage; |
| 432 |
const SkUnichar* uni = storage.convert(text, byteLength, encoding); |
| 433 |
|
| 434 |
this->unicharsToGlyphs({uni, count}, glyphs); |
| 435 |
return count; |
| 436 |
} |
| 437 |
|
| 438 |
int SkTypeface::countGlyphs() const { |
| 439 |
return this->onCountGlyphs(); |
| 440 |
} |
| 441 |
|
| 442 |
int SkTypeface::getUnitsPerEm() const { |
| 443 |
// should we try to cache this in the base-class? |
| 444 |
return this->onGetUPEM(); |
| 445 |
} |
| 446 |
|
| 447 |
bool SkTypeface::getKerningPairAdjustments(SkSpan<const SkGlyphID> glyphs, |
| 448 |
SkSpan<int32_t> adjustments) const { |
| 449 |
// We need glyphs.size() == adjustments.size() + 1 |
| 450 |
// unless either is emptyish, in which case we still call the virtual, just |
| 451 |
// to get the boolean result. |
| 452 |
if (glyphs.size() <= 1 || adjustments.empty()) { |
| 453 |
return this->onGetKerningPairAdjustments({}, {}); // just return the bool |
| 454 |
} |
| 455 |
|
| 456 |
const size_t n = std::min(glyphs.size() - 1, adjustments.size()); |
| 457 |
return this->onGetKerningPairAdjustments(glyphs.first(n + 1), adjustments.first(n)); |
| 458 |
} |
| 459 |
|
| 460 |
SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const { |
| 461 |
return this->onCreateFamilyNameIterator(); |
| 462 |
} |
| 463 |
|
| 464 |
void SkTypeface::getFamilyName(SkString* name) const { |
| 465 |
SkASSERT(name); |
| 466 |
this->onGetFamilyName(name); |
| 467 |
} |
| 468 |
|
| 469 |
bool SkTypeface::getPostScriptName(SkString* name) const { |
| 470 |
return this->onGetPostScriptName(name); |
| 471 |
} |
| 472 |
|
| 473 |
int SkTypeface::getResourceName(SkString* resourceName) const { |
| 474 |
return this->onGetResourceName(resourceName); |
| 475 |
} |
| 476 |
|
| 477 |
int SkTypeface::onGetResourceName(SkString* resourceName) const { |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
SkFontStyle SkTypeface::fontStyle() const { |
| 482 |
return this->onGetFontStyle(); |
| 483 |
} |
| 484 |
|
| 485 |
SkFontStyle SkTypeface::onGetFontStyle() const { |
| 486 |
return fStyle; |
| 487 |
} |
| 488 |
|
| 489 |
bool SkTypeface::isBold() const { |
| 490 |
return this->onGetFontStyle().weight() >= SkFontStyle::kSemiBold_Weight; |
| 491 |
} |
| 492 |
|
| 493 |
bool SkTypeface::isItalic() const { |
| 494 |
return this->onGetFontStyle().slant() != SkFontStyle::kUpright_Slant; |
| 495 |
} |
| 496 |
|
| 497 |
bool SkTypeface::isFixedPitch() const { |
| 498 |
return this->onGetFixedPitch(); |
| 499 |
} |
| 500 |
|
| 501 |
bool SkTypeface::onGetFixedPitch() const { |
| 502 |
return fIsFixedPitch; |
| 503 |
} |
| 504 |
|
| 505 |
bool SkTypeface::isSyntheticBold() const { return this->onIsSyntheticBold(); } |
| 506 |
bool SkTypeface::isSyntheticOblique() const { return this->onIsSyntheticOblique(); } |
| 507 |
bool SkTypeface::onIsSyntheticBold() const { return false; } |
| 508 |
bool SkTypeface::onIsSyntheticOblique() const { return false; } |
| 509 |
|
| 510 |
void SkTypeface::getGlyphToUnicodeMap(SkSpan<SkUnichar> dst) const { |
| 511 |
sk_bzero(dst.data(), dst.size_bytes()); |
| 512 |
} |
| 513 |
|
| 514 |
std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const { |
| 515 |
std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics(); |
| 516 |
if (result && result->fPostScriptName.isEmpty()) { |
| 517 |
if (!this->getPostScriptName(&result->fPostScriptName)) { |
| 518 |
this->getFamilyName(&result->fPostScriptName); |
| 519 |
} |
| 520 |
} |
| 521 |
if (result && (result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font || |
| 522 |
result->fType == SkAdvancedTypefaceMetrics::kCFF_Font)) { |
| 523 |
SkOTTableOS2::Version::V2::Type::Field fsType; |
| 524 |
constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG); |
| 525 |
constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType); |
| 526 |
if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) { |
| 527 |
if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) { |
| 528 |
result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag; |
| 529 |
} |
| 530 |
if (fsType.NoSubsetting) { |
| 531 |
result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag; |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
return result; |
| 536 |
} |
| 537 |
|
| 538 |
bool SkTypeface::onGetKerningPairAdjustments(SkSpan<const SkGlyphID>, SkSpan<int32_t> adj) const { |
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
std::unique_ptr<SkStreamAsset> SkTypeface::onOpenExistingStream(int* ttcIndex) const { |
| 543 |
return this->onOpenStream(ttcIndex); |
| 544 |
} |
| 545 |
|
| 546 |
/////////////////////////////////////////////////////////////////////////////// |
| 547 |
|
| 548 |
SkRect SkTypeface::getBounds() const { |
| 549 |
fBoundsOnce([this] { |
| 550 |
if (!this->onComputeBounds(&fBounds)) { |
| 551 |
fBounds.setEmpty(); |
| 552 |
} |
| 553 |
}); |
| 554 |
return fBounds; |
| 555 |
} |
| 556 |
|
| 557 |
bool SkTypeface::onComputeBounds(SkRect* bounds) const { |
| 558 |
// we use a big size to ensure lots of significant bits from the scalercontext. |
| 559 |
// then we scale back down to return our final answer (at 1-pt) |
| 560 |
const SkScalar textSize = 2048; |
| 561 |
const SkScalar invTextSize = 1 / textSize; |
| 562 |
|
| 563 |
SkFont font; |
| 564 |
font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this))); |
| 565 |
font.setSize(textSize); |
| 566 |
font.setLinearMetrics(true); |
| 567 |
|
| 568 |
SkScalerContextRec rec; |
| 569 |
SkScalerContextEffects effects; |
| 570 |
|
| 571 |
SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects); |
| 572 |
|
| 573 |
SkAutoDescriptor ad; |
| 574 |
SkScalerContextEffects noeffects; |
| 575 |
SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad); |
| 576 |
|
| 577 |
std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc()); |
| 578 |
|
| 579 |
SkFontMetrics fm; |
| 580 |
ctx->getFontMetrics(&fm); |
| 581 |
if (!fm.hasBounds()) { |
| 582 |
return false; |
| 583 |
} |
| 584 |
bounds->setLTRB(fm.fXMin * invTextSize, fm.fTop * invTextSize, |
| 585 |
fm.fXMax * invTextSize, fm.fBottom * invTextSize); |
| 586 |
return true; |
| 587 |
} |