• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkFontMetrics.h"
9 #include "include/core/SkFontMgr.h"
10 #include "include/core/SkStream.h"
11 #include "include/core/SkTypeface.h"
12 #include "include/private/SkMutex.h"
13 #include "include/private/SkOnce.h"
14 #include "include/utils/SkCustomTypeface.h"
15 #include "src/core/SkAdvancedTypefaceMetrics.h"
16 #include "src/core/SkEndian.h"
17 #include "src/core/SkFontDescriptor.h"
18 #include "src/core/SkFontPriv.h"
19 #include "src/core/SkLRUCache.h"
20 #include "src/core/SkScalerContext.h"
21 #include "src/core/SkSurfacePriv.h"
22 #include "src/core/SkTypefaceCache.h"
23 #include "src/sfnt/SkOTTable_OS_2.h"
24 #include "src/utils/SkUTF.h"
25 
26 #include <mutex>
27 
SkTypeface(const SkFontStyle & style,bool isFixedPitch)28 SkTypeface::SkTypeface(const SkFontStyle& style, bool isFixedPitch)
29     : fUniqueID(SkTypefaceCache::NewFontID()), fStyle(style), fIsFixedPitch(isFixedPitch) { }
30 
~SkTypeface()31 SkTypeface::~SkTypeface() { }
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 namespace {
36 
37 class SkEmptyTypeface : public SkTypeface {
38 public:
Make()39     static sk_sp<SkTypeface> Make() { return sk_sp<SkTypeface>(new SkEmptyTypeface); }
40 protected:
SkEmptyTypeface()41     SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { }
42 
onOpenStream(int * ttcIndex) const43     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; }
onMakeClone(const SkFontArguments & args) const44     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
45         return sk_ref_sp(this);
46     }
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const47     std::unique_ptr<SkScalerContext> onCreateScalerContext(
48         const SkScalerContextEffects& effects, const SkDescriptor* desc) const override
49     {
50         return SkScalerContext::MakeEmpty(
51                 sk_ref_sp(const_cast<SkEmptyTypeface*>(this)), effects, desc);
52     }
onFilterRec(SkScalerContextRec *) const53     void onFilterRec(SkScalerContextRec*) const override { }
onGetAdvancedMetrics() const54     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override {
55         return nullptr;
56     }
onGetFontDescriptor(SkFontDescriptor *,bool *) const57     void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { }
onCharsToGlyphs(const SkUnichar * chars,int count,SkGlyphID glyphs[]) const58     void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override {
59         sk_bzero(glyphs, count * sizeof(glyphs[0]));
60     }
onCountGlyphs() const61     int onCountGlyphs() const override { return 0; }
getPostScriptGlyphNames(SkString *) const62     void getPostScriptGlyphNames(SkString*) const override {}
getGlyphToUnicodeMap(SkUnichar *) const63     void getGlyphToUnicodeMap(SkUnichar*) const override {}
onGetUPEM() const64     int onGetUPEM() const override { return 0; }
65     class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
66     public:
next(SkTypeface::LocalizedString *)67         bool next(SkTypeface::LocalizedString*) override { return false; }
68     };
onGetFamilyName(SkString * familyName) const69     void onGetFamilyName(SkString* familyName) const override {
70         familyName->reset();
71     }
onGetPostScriptName(SkString *) const72     bool onGetPostScriptName(SkString*) const override {
73         return false;
74     }
onCreateFamilyNameIterator() const75     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override {
76         return new EmptyLocalizedStrings;
77     }
onGlyphMaskNeedsCurrentColor() const78     bool onGlyphMaskNeedsCurrentColor() const override {
79         return false;
80     }
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const81     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
82                                      int coordinateCount) const override
83     {
84         return 0;
85     }
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const86     int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
87                                        int parameterCount) const override
88     {
89         return 0;
90     }
onGetTableTags(SkFontTableTag tags[]) const91     int onGetTableTags(SkFontTableTag tags[]) const override { return 0; }
onGetTableData(SkFontTableTag,size_t,size_t,void *) const92     size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override {
93         return 0;
94     }
95 };
96 
97 constexpr int MAX_VARFONT_CACHE_SIZE = 64;
98 
99 class SkVarFontCache {
100 public:
SkVarFontCache()101     SkVarFontCache() : fLRUCache(MAX_VARFONT_CACHE_SIZE) {}
102 
Instance()103     static SkVarFontCache& Instance()
104     {
105         static SkVarFontCache cache;
106         return cache;
107     }
108 
GetVarFont(sk_sp<SkTypeface> typeface,const SkFontArguments & args)109     sk_sp<SkTypeface> GetVarFont(sk_sp<SkTypeface> typeface, const SkFontArguments& args)
110     {
111         if (!typeface) {
112             return nullptr;
113         }
114 
115         size_t hash = 0;
116         hash ^= std::hash<uint32_t>()(typeface->uniqueID());
117 #ifdef OHOS_SUPPORT
118         uint32_t typefaceHash = typeface->GetHash();
119         hash ^= std::hash<uint32_t>()(typefaceHash);
120 #endif
121         hash ^= std::hash<int>()(args.getCollectionIndex());
122         const auto& positions = args.getVariationDesignPosition();
123         for (int i = 0; i < positions.coordinateCount; i++) {
124             const auto& coord = positions.coordinates[i];
125             hash ^= std::hash<SkFourByteTag>()(coord.axis);
126             hash ^= std::hash<float>()(coord.value);
127         }
128 
129         std::lock_guard<std::mutex> lock(fMutex);
130         auto cached = fLRUCache.find(hash);
131         if (!cached) {
132             int ttcIndex = args.getCollectionIndex();
133             auto varTypeface = SkFontMgr::RefDefault()->makeFromStream(typeface->openStream(&ttcIndex), args);
134             if (!varTypeface) {
135                 return typeface;
136             } else {
137                 fLRUCache.insert(hash, varTypeface);
138                 return varTypeface;
139             }
140         }
141 
142         return *cached;
143     }
144 
145 private:
146     SkLRUCache<uint32_t, sk_sp<SkTypeface>> fLRUCache;
147     std::mutex fMutex;
148 };
149 
150 }  // namespace
151 
FromOldStyle(Style oldStyle)152 SkFontStyle SkTypeface::FromOldStyle(Style oldStyle) {
153     return SkFontStyle((oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight
154                                                       : SkFontStyle::kNormal_Weight,
155                        SkFontStyle::kNormal_Width,
156                        (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant
157                                                         : SkFontStyle::kUpright_Slant);
158 }
159 
GetDefaultTypeface(Style style)160 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
161     static SkOnce once[4];
162     static sk_sp<SkTypeface> defaults[4];
163 
164     SkASSERT((int)style < 4);
165     once[style]([style] {
166         sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
167         auto t = fm->legacyMakeTypeface(nullptr, FromOldStyle(style));
168         defaults[style] = t ? t : SkEmptyTypeface::Make();
169     });
170     return defaults[style].get();
171 }
172 
MakeDefault()173 sk_sp<SkTypeface> SkTypeface::MakeDefault() {
174     return sk_ref_sp(GetDefaultTypeface());
175 }
176 
UniqueID(const SkTypeface * face)177 uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
178     if (nullptr == face) {
179         face = GetDefaultTypeface();
180     }
181     return face->uniqueID();
182 }
183 
Equal(const SkTypeface * facea,const SkTypeface * faceb)184 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
185     return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
186 }
187 
188 ///////////////////////////////////////////////////////////////////////////////
189 
MakeFromName(const char name[],SkFontStyle fontStyle)190 sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[],
191                                            SkFontStyle fontStyle) {
192     if (nullptr == name && (fontStyle.slant() == SkFontStyle::kItalic_Slant ||
193                             fontStyle.slant() == SkFontStyle::kUpright_Slant) &&
194                            (fontStyle.weight() == SkFontStyle::kBold_Weight ||
195                             fontStyle.weight() == SkFontStyle::kNormal_Weight)) {
196         return sk_ref_sp(GetDefaultTypeface(static_cast<SkTypeface::Style>(
197             (fontStyle.slant() == SkFontStyle::kItalic_Slant ? SkTypeface::kItalic :
198                                                                SkTypeface::kNormal) |
199             (fontStyle.weight() == SkFontStyle::kBold_Weight ? SkTypeface::kBold :
200                                                                SkTypeface::kNormal))));
201     }
202     return SkFontMgr::RefDefault()->legacyMakeTypeface(name, fontStyle);
203 }
204 
205 #ifdef OHOS_SUPPORT
GetSystemFonts()206 std::vector<sk_sp<SkTypeface>> SkTypeface::GetSystemFonts()
207 {
208     return SkFontMgr::RefDefault()->getSystemFonts();
209 }
210 #endif
211 
MakeFromStream(std::unique_ptr<SkStreamAsset> stream,int index)212 sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) {
213     if (!stream) {
214         return nullptr;
215     }
216     return SkFontMgr::RefDefault()->makeFromStream(std::move(stream), index);
217 }
218 
MakeFromData(sk_sp<SkData> data,int index)219 sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
220     if (!data) {
221         return nullptr;
222     }
223     return SkFontMgr::RefDefault()->makeFromData(std::move(data), index);
224 }
225 
MakeFromFile(const char path[],int index)226 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) {
227     return SkFontMgr::RefDefault()->makeFromFile(path, index);
228 }
229 
makeClone(const SkFontArguments & args) const230 sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const {
231     return this->onMakeClone(args);
232 }
233 
234 ///////////////////////////////////////////////////////////////////////////////
235 
serialize(SkWStream * wstream,SerializeBehavior behavior) const236 void SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const {
237     bool isLocalData = false;
238     SkFontDescriptor desc;
239     this->onGetFontDescriptor(&desc, &isLocalData);
240 
241     bool shouldSerializeData = false;
242 
243 #ifdef ARKUI_X_ENABLE
244     // Prohibiting serializes typeface when arkui-x use custom's font
245     isLocalData = false;
246 #endif
247 
248     switch (behavior) {
249         case SerializeBehavior::kDoIncludeData:      shouldSerializeData = true;        break;
250         case SerializeBehavior::kDontIncludeData:    shouldSerializeData = false;       break;
251         case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break;
252     }
253 
254     if (shouldSerializeData) {
255         int index;
256         desc.setStream(this->openStream(&index));
257         if (desc.hasStream()) {
258             desc.setCollectionIndex(index);
259         }
260 
261         int numAxes = this->getVariationDesignPosition(nullptr, 0);
262         if (0 < numAxes) {
263             numAxes = this->getVariationDesignPosition(desc.setVariationCoordinates(numAxes), numAxes);
264             if (numAxes <= 0) {
265                 desc.setVariationCoordinates(0);
266             }
267         }
268     }
269     desc.serialize(wstream);
270 }
271 
serialize(SerializeBehavior behavior) const272 sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const {
273     SkDynamicMemoryWStream stream;
274     this->serialize(&stream, behavior);
275     return stream.detachAsData();
276 }
277 
MakeDeserialize(SkStream * stream)278 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) {
279     SkFontDescriptor desc;
280     if (!SkFontDescriptor::Deserialize(stream, &desc)) {
281         return nullptr;
282     }
283 
284     if (desc.hasStream()) {
285         if (auto tf = SkCustomTypefaceBuilder::Deserialize(desc.dupStream().get())) {
286             return tf;
287         }
288     }
289 
290     if (desc.hasStream()) {
291         SkFontArguments args;
292         args.setCollectionIndex(desc.getCollectionIndex());
293         args.setVariationDesignPosition({desc.getVariation(), desc.getVariationCoordinateCount()});
294         sk_sp<SkFontMgr> defaultFm = SkFontMgr::RefDefault();
295         sk_sp<SkTypeface> typeface = defaultFm->makeFromStream(desc.detachStream(), args);
296         if (typeface) {
297             return typeface;
298         }
299     }
300 
301     auto typeface = SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle());
302     if (desc.getVariationCoordinateCount() > 0 && typeface) {
303         SkFontArguments args;
304         args.setCollectionIndex(desc.getCollectionIndex());
305         args.setVariationDesignPosition({desc.getVariation(), desc.getVariationCoordinateCount()});
306         typeface = SkVarFontCache::Instance().GetVarFont(typeface, args);
307     }
308 
309     return typeface;
310 }
311 
isCustomTypeface() const312 bool SkTypeface::isCustomTypeface() const {
313     return fIsCustom;
314 }
315 
setIsCustomTypeface(bool isCustom)316 void SkTypeface::setIsCustomTypeface(bool isCustom) {
317     fIsCustom = isCustom;
318 }
319 
320 #ifdef OHOS_SUPPORT
isThemeTypeface() const321 bool SkTypeface::isThemeTypeface() const
322 {
323     return fIsTheme;
324 }
325 
setIsThemeTypeface(bool isTheme)326 void SkTypeface::setIsThemeTypeface(bool isTheme)
327 {
328     fIsTheme = isTheme;
329 }
330 #endif
331 
openExistingStream(int * ttcIndex) const332 std::unique_ptr<SkStreamAsset> SkTypeface::openExistingStream(int* ttcIndex) const {
333     int ttcIndexStorage;
334     if (nullptr == ttcIndex) {
335         // So our subclasses don't need to check for null param
336         ttcIndex = &ttcIndexStorage;
337     }
338     return this->onOpenExistingStream(ttcIndex);
339 }
340 
onOpenExistingStream(int * ttcIndex) const341 std::unique_ptr<SkStreamAsset> SkTypeface::onOpenExistingStream(int* ttcIndex) const {
342     return this->onOpenStream(ttcIndex);
343 }
344 
345 ///////////////////////////////////////////////////////////////////////////////
346 
glyphMaskNeedsCurrentColor() const347 bool SkTypeface::glyphMaskNeedsCurrentColor() const {
348     return this->onGlyphMaskNeedsCurrentColor();
349 }
350 
getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const351 int SkTypeface::getVariationDesignPosition(
352         SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
353 {
354     return this->onGetVariationDesignPosition(coordinates, coordinateCount);
355 }
356 
getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const357 int SkTypeface::getVariationDesignParameters(
358         SkFontParameters::Variation::Axis parameters[], int parameterCount) const
359 {
360     return this->onGetVariationDesignParameters(parameters, parameterCount);
361 }
362 
countTables() const363 int SkTypeface::countTables() const {
364     return this->onGetTableTags(nullptr);
365 }
366 
getTableTags(SkFontTableTag tags[]) const367 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
368     return this->onGetTableTags(tags);
369 }
370 
getTableSize(SkFontTableTag tag) const371 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
372     return this->onGetTableData(tag, 0, ~0U, nullptr);
373 }
374 
getTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const375 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
376                                 void* data) const {
377     return this->onGetTableData(tag, offset, length, data);
378 }
379 
copyTableData(SkFontTableTag tag) const380 sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const {
381     return this->onCopyTableData(tag);
382 }
383 
onCopyTableData(SkFontTableTag tag) const384 sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const {
385     size_t size = this->getTableSize(tag);
386     if (size) {
387         sk_sp<SkData> data = SkData::MakeUninitialized(size);
388         (void)this->getTableData(tag, 0, size, data->writable_data());
389         return data;
390     }
391     return nullptr;
392 }
393 
openStream(int * ttcIndex) const394 std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const {
395     int ttcIndexStorage;
396     if (nullptr == ttcIndex) {
397         // So our subclasses don't need to check for null param
398         ttcIndex = &ttcIndexStorage;
399     }
400     return this->onOpenStream(ttcIndex);
401 }
402 
createScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const403 std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
404         const SkScalerContextEffects& effects, const SkDescriptor* desc) const {
405     std::unique_ptr<SkScalerContext> scalerContext = this->onCreateScalerContext(effects, desc);
406     SkASSERT(scalerContext);
407     return scalerContext;
408 }
409 
unicharsToGlyphs(const SkUnichar uni[],int count,SkGlyphID glyphs[]) const410 void SkTypeface::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
411     if (count > 0 && glyphs && uni) {
412         this->onCharsToGlyphs(uni, count, glyphs);
413     }
414 }
415 
unicharToGlyph(SkUnichar uni) const416 SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const {
417     SkGlyphID glyphs[1] = { 0 };
418     this->onCharsToGlyphs(&uni, 1, glyphs);
419     return glyphs[0];
420 }
421 
422 namespace {
423 class SkConvertToUTF32 {
424 public:
SkConvertToUTF32()425     SkConvertToUTF32() {}
426 
convert(const void * text,size_t byteLength,SkTextEncoding encoding)427     const SkUnichar* convert(const void* text, size_t byteLength, SkTextEncoding encoding) {
428         const SkUnichar* uni;
429         switch (encoding) {
430             case SkTextEncoding::kUTF8: {
431                 uni = fStorage.reset(byteLength);
432                 const char* ptr = (const char*)text;
433                 const char* end = ptr + byteLength;
434                 for (int i = 0; ptr < end; ++i) {
435                     fStorage[i] = SkUTF::NextUTF8(&ptr, end);
436                 }
437             } break;
438             case SkTextEncoding::kUTF16: {
439                 uni = fStorage.reset(byteLength);
440                 const uint16_t* ptr = (const uint16_t*)text;
441                 const uint16_t* end = ptr + (byteLength >> 1);
442                 for (int i = 0; ptr < end; ++i) {
443                     fStorage[i] = SkUTF::NextUTF16(&ptr, end);
444                 }
445             } break;
446             case SkTextEncoding::kUTF32:
447                 uni = (const SkUnichar*)text;
448                 break;
449             default:
450                 SK_ABORT("unexpected enum");
451         }
452         return uni;
453     }
454 
455 private:
456     SkAutoSTMalloc<256, SkUnichar> fStorage;
457 };
458 }
459 
textToGlyphs(const void * text,size_t byteLength,SkTextEncoding encoding,SkGlyphID glyphs[],int maxGlyphCount) const460 int SkTypeface::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
461                              SkGlyphID glyphs[], int maxGlyphCount) const {
462     if (0 == byteLength) {
463         return 0;
464     }
465 
466     SkASSERT(text);
467 
468     int count = SkFontPriv::CountTextElements(text, byteLength, encoding);
469     if (!glyphs || count > maxGlyphCount) {
470         return count;
471     }
472 
473     if (encoding == SkTextEncoding::kGlyphID) {
474         memcpy(glyphs, text, count << 1);
475         return count;
476     }
477 
478     SkConvertToUTF32 storage;
479     const SkUnichar* uni = storage.convert(text, byteLength, encoding);
480 
481     this->unicharsToGlyphs(uni, count, glyphs);
482     return count;
483 }
484 
countGlyphs() const485 int SkTypeface::countGlyphs() const {
486     return this->onCountGlyphs();
487 }
488 
getUnitsPerEm() const489 int SkTypeface::getUnitsPerEm() const {
490     // should we try to cache this in the base-class?
491     return this->onGetUPEM();
492 }
493 
getKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const494 bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
495                                            int32_t adjustments[]) const {
496     SkASSERT(count >= 0);
497     // check for the only legal way to pass a nullptr.. everything is 0
498     // in which case they just want to know if this face can possibly support
499     // kerning (true) or never (false).
500     if (nullptr == glyphs || nullptr == adjustments) {
501         SkASSERT(nullptr == glyphs);
502         SkASSERT(0 == count);
503         SkASSERT(nullptr == adjustments);
504     }
505     return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
506 }
507 
createFamilyNameIterator() const508 SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
509     return this->onCreateFamilyNameIterator();
510 }
511 
getFamilyName(SkString * name) const512 void SkTypeface::getFamilyName(SkString* name) const {
513     SkASSERT(name);
514     this->onGetFamilyName(name);
515 }
516 
517 #ifdef OHOS_SUPPORT
getFontPath(SkString * path) const518 void SkTypeface::getFontPath(SkString* path) const
519 {
520     SkASSERT(path);
521     this->onGetFontPath(path);
522 }
523 #endif
524 
getPostScriptName(SkString * name) const525 bool SkTypeface::getPostScriptName(SkString* name) const {
526     return this->onGetPostScriptName(name);
527 }
528 
getGlyphToUnicodeMap(SkUnichar * dst) const529 void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const {
530     sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs());
531 }
532 
getAdvancedMetrics() const533 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const {
534     std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics();
535     if (result && result->fPostScriptName.isEmpty()) {
536         result->fPostScriptName = result->fFontName;
537     }
538     if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
539         SkOTTableOS2::Version::V2::Type::Field fsType;
540         constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG);
541         constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType);
542         if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) {
543             if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) {
544                 result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag;
545             }
546             if (fsType.NoSubsetting) {
547                 result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag;
548             }
549         }
550     }
551     return result;
552 }
553 
onGetKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const554 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
555                                              int32_t adjustments[]) const {
556     return false;
557 }
558 
559 ///////////////////////////////////////////////////////////////////////////////
560 
561 #include "include/core/SkPaint.h"
562 #include "src/core/SkDescriptor.h"
563 
getBounds() const564 SkRect SkTypeface::getBounds() const {
565     fBoundsOnce([this] {
566         if (!this->onComputeBounds(&fBounds)) {
567             fBounds.setEmpty();
568         }
569     });
570     return fBounds;
571 }
572 
onComputeBounds(SkRect * bounds) const573 bool SkTypeface::onComputeBounds(SkRect* bounds) const {
574     // we use a big size to ensure lots of significant bits from the scalercontext.
575     // then we scale back down to return our final answer (at 1-pt)
576     const SkScalar textSize = 2048;
577     const SkScalar invTextSize = 1 / textSize;
578 
579     SkFont font;
580     font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this)));
581     font.setSize(textSize);
582     font.setLinearMetrics(true);
583 
584     SkScalerContextRec rec;
585     SkScalerContextEffects effects;
586 
587     SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects);
588 
589     SkAutoDescriptor ad;
590     SkScalerContextEffects noeffects;
591     SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad);
592 
593     std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc());
594 
595     SkFontMetrics fm;
596     ctx->getFontMetrics(&fm);
597     if (!fm.hasBounds()) {
598         return false;
599     }
600     bounds->setLTRB(fm.fXMin * invTextSize, fm.fTop * invTextSize,
601                     fm.fXMax * invTextSize, fm.fBottom * invTextSize);
602     return true;
603 }
604 
onGetAdvancedMetrics() const605 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::onGetAdvancedMetrics() const {
606     SkDEBUGFAIL("Typefaces that need to work with PDF backend must override this.");
607     return nullptr;
608 }
609 
610 #ifdef OHOS_SUPPORT
GetHash() const611 uint32_t SkTypeface::GetHash() const
612 {
613     if (hash_ != 0) {
614         return hash_;
615     }
616     auto skData = serialize(SkTypeface::SerializeBehavior::kDontIncludeData);
617     if (skData == nullptr) {
618         return hash_;
619     }
620     std::unique_ptr<SkStreamAsset> ttfStream = openExistingStream(0);
621     uint32_t seed = ttfStream.get() != nullptr ? ttfStream->getLength() : 0;
622     hash_ = SkOpts::hash_fn(skData->data(), skData->size(), seed);
623     return hash_;
624 }
625 
SetHash(uint32_t hash)626 void SkTypeface::SetHash(uint32_t hash)
627 {
628     hash_ = hash;
629 }
630 #endif
631