• 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 
MakeFromStream(std::unique_ptr<SkStreamAsset> stream,int index)205 sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) {
206     if (!stream) {
207         return nullptr;
208     }
209     return SkFontMgr::RefDefault()->makeFromStream(std::move(stream), index);
210 }
211 
MakeFromData(sk_sp<SkData> data,int index)212 sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
213     if (!data) {
214         return nullptr;
215     }
216     return SkFontMgr::RefDefault()->makeFromData(std::move(data), index);
217 }
218 
MakeFromFile(const char path[],int index)219 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) {
220     return SkFontMgr::RefDefault()->makeFromFile(path, index);
221 }
222 
makeClone(const SkFontArguments & args) const223 sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const {
224     return this->onMakeClone(args);
225 }
226 
227 ///////////////////////////////////////////////////////////////////////////////
228 
serialize(SkWStream * wstream,SerializeBehavior behavior) const229 void SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const {
230     bool isLocalData = false;
231     SkFontDescriptor desc;
232     this->onGetFontDescriptor(&desc, &isLocalData);
233 
234     bool shouldSerializeData = false;
235     switch (behavior) {
236         case SerializeBehavior::kDoIncludeData:      shouldSerializeData = true;        break;
237         case SerializeBehavior::kDontIncludeData:    shouldSerializeData = false;       break;
238         case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break;
239     }
240 
241     if (shouldSerializeData) {
242         int index;
243         desc.setStream(this->openStream(&index));
244         if (desc.hasStream()) {
245             desc.setCollectionIndex(index);
246         }
247 
248         int numAxes = this->getVariationDesignPosition(nullptr, 0);
249         if (0 < numAxes) {
250             numAxes = this->getVariationDesignPosition(desc.setVariationCoordinates(numAxes), numAxes);
251             if (numAxes <= 0) {
252                 desc.setVariationCoordinates(0);
253             }
254         }
255     }
256     desc.serialize(wstream);
257 }
258 
serialize(SerializeBehavior behavior) const259 sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const {
260     SkDynamicMemoryWStream stream;
261     this->serialize(&stream, behavior);
262     return stream.detachAsData();
263 }
264 
MakeDeserialize(SkStream * stream)265 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) {
266     SkFontDescriptor desc;
267     if (!SkFontDescriptor::Deserialize(stream, &desc)) {
268         return nullptr;
269     }
270 
271     if (desc.hasStream()) {
272         if (auto tf = SkCustomTypefaceBuilder::Deserialize(desc.dupStream().get())) {
273             return tf;
274         }
275     }
276 
277     if (desc.hasStream()) {
278         SkFontArguments args;
279         args.setCollectionIndex(desc.getCollectionIndex());
280         args.setVariationDesignPosition({desc.getVariation(), desc.getVariationCoordinateCount()});
281         sk_sp<SkFontMgr> defaultFm = SkFontMgr::RefDefault();
282         sk_sp<SkTypeface> typeface = defaultFm->makeFromStream(desc.detachStream(), args);
283         if (typeface) {
284             return typeface;
285         }
286     }
287 
288     auto typeface = SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle());
289     if (desc.getVariationCoordinateCount() > 0 && typeface) {
290         SkFontArguments args;
291         args.setCollectionIndex(desc.getCollectionIndex());
292         args.setVariationDesignPosition({desc.getVariation(), desc.getVariationCoordinateCount()});
293         typeface = SkVarFontCache::Instance().GetVarFont(typeface, args);
294     }
295 
296     return typeface;
297 }
298 
isCustomTypeface() const299 bool SkTypeface::isCustomTypeface() const {
300     return fIsCustom;
301 }
302 
setIsCustomTypeface(bool isCustom)303 void SkTypeface::setIsCustomTypeface(bool isCustom) {
304     fIsCustom = isCustom;
305 }
306 
openExistingStream(int * ttcIndex) const307 std::unique_ptr<SkStreamAsset> SkTypeface::openExistingStream(int* ttcIndex) const {
308     int ttcIndexStorage;
309     if (nullptr == ttcIndex) {
310         // So our subclasses don't need to check for null param
311         ttcIndex = &ttcIndexStorage;
312     }
313     return this->onOpenExistingStream(ttcIndex);
314 }
315 
onOpenExistingStream(int * ttcIndex) const316 std::unique_ptr<SkStreamAsset> SkTypeface::onOpenExistingStream(int* ttcIndex) const {
317     return this->onOpenStream(ttcIndex);
318 }
319 
320 ///////////////////////////////////////////////////////////////////////////////
321 
glyphMaskNeedsCurrentColor() const322 bool SkTypeface::glyphMaskNeedsCurrentColor() const {
323     return this->onGlyphMaskNeedsCurrentColor();
324 }
325 
getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const326 int SkTypeface::getVariationDesignPosition(
327         SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
328 {
329     return this->onGetVariationDesignPosition(coordinates, coordinateCount);
330 }
331 
getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const332 int SkTypeface::getVariationDesignParameters(
333         SkFontParameters::Variation::Axis parameters[], int parameterCount) const
334 {
335     return this->onGetVariationDesignParameters(parameters, parameterCount);
336 }
337 
countTables() const338 int SkTypeface::countTables() const {
339     return this->onGetTableTags(nullptr);
340 }
341 
getTableTags(SkFontTableTag tags[]) const342 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
343     return this->onGetTableTags(tags);
344 }
345 
getTableSize(SkFontTableTag tag) const346 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
347     return this->onGetTableData(tag, 0, ~0U, nullptr);
348 }
349 
getTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const350 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
351                                 void* data) const {
352     return this->onGetTableData(tag, offset, length, data);
353 }
354 
copyTableData(SkFontTableTag tag) const355 sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const {
356     return this->onCopyTableData(tag);
357 }
358 
onCopyTableData(SkFontTableTag tag) const359 sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const {
360     size_t size = this->getTableSize(tag);
361     if (size) {
362         sk_sp<SkData> data = SkData::MakeUninitialized(size);
363         (void)this->getTableData(tag, 0, size, data->writable_data());
364         return data;
365     }
366     return nullptr;
367 }
368 
openStream(int * ttcIndex) const369 std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const {
370     int ttcIndexStorage;
371     if (nullptr == ttcIndex) {
372         // So our subclasses don't need to check for null param
373         ttcIndex = &ttcIndexStorage;
374     }
375     return this->onOpenStream(ttcIndex);
376 }
377 
createScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const378 std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
379         const SkScalerContextEffects& effects, const SkDescriptor* desc) const {
380     std::unique_ptr<SkScalerContext> scalerContext = this->onCreateScalerContext(effects, desc);
381     SkASSERT(scalerContext);
382     return scalerContext;
383 }
384 
unicharsToGlyphs(const SkUnichar uni[],int count,SkGlyphID glyphs[]) const385 void SkTypeface::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
386     if (count > 0 && glyphs && uni) {
387         this->onCharsToGlyphs(uni, count, glyphs);
388     }
389 }
390 
unicharToGlyph(SkUnichar uni) const391 SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const {
392     SkGlyphID glyphs[1] = { 0 };
393     this->onCharsToGlyphs(&uni, 1, glyphs);
394     return glyphs[0];
395 }
396 
397 namespace {
398 class SkConvertToUTF32 {
399 public:
SkConvertToUTF32()400     SkConvertToUTF32() {}
401 
convert(const void * text,size_t byteLength,SkTextEncoding encoding)402     const SkUnichar* convert(const void* text, size_t byteLength, SkTextEncoding encoding) {
403         const SkUnichar* uni;
404         switch (encoding) {
405             case SkTextEncoding::kUTF8: {
406                 uni = fStorage.reset(byteLength);
407                 const char* ptr = (const char*)text;
408                 const char* end = ptr + byteLength;
409                 for (int i = 0; ptr < end; ++i) {
410                     fStorage[i] = SkUTF::NextUTF8(&ptr, end);
411                 }
412             } break;
413             case SkTextEncoding::kUTF16: {
414                 uni = fStorage.reset(byteLength);
415                 const uint16_t* ptr = (const uint16_t*)text;
416                 const uint16_t* end = ptr + (byteLength >> 1);
417                 for (int i = 0; ptr < end; ++i) {
418                     fStorage[i] = SkUTF::NextUTF16(&ptr, end);
419                 }
420             } break;
421             case SkTextEncoding::kUTF32:
422                 uni = (const SkUnichar*)text;
423                 break;
424             default:
425                 SK_ABORT("unexpected enum");
426         }
427         return uni;
428     }
429 
430 private:
431     SkAutoSTMalloc<256, SkUnichar> fStorage;
432 };
433 }
434 
textToGlyphs(const void * text,size_t byteLength,SkTextEncoding encoding,SkGlyphID glyphs[],int maxGlyphCount) const435 int SkTypeface::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
436                              SkGlyphID glyphs[], int maxGlyphCount) const {
437     if (0 == byteLength) {
438         return 0;
439     }
440 
441     SkASSERT(text);
442 
443     int count = SkFontPriv::CountTextElements(text, byteLength, encoding);
444     if (!glyphs || count > maxGlyphCount) {
445         return count;
446     }
447 
448     if (encoding == SkTextEncoding::kGlyphID) {
449         memcpy(glyphs, text, count << 1);
450         return count;
451     }
452 
453     SkConvertToUTF32 storage;
454     const SkUnichar* uni = storage.convert(text, byteLength, encoding);
455 
456     this->unicharsToGlyphs(uni, count, glyphs);
457     return count;
458 }
459 
countGlyphs() const460 int SkTypeface::countGlyphs() const {
461     return this->onCountGlyphs();
462 }
463 
getUnitsPerEm() const464 int SkTypeface::getUnitsPerEm() const {
465     // should we try to cache this in the base-class?
466     return this->onGetUPEM();
467 }
468 
getKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const469 bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
470                                            int32_t adjustments[]) const {
471     SkASSERT(count >= 0);
472     // check for the only legal way to pass a nullptr.. everything is 0
473     // in which case they just want to know if this face can possibly support
474     // kerning (true) or never (false).
475     if (nullptr == glyphs || nullptr == adjustments) {
476         SkASSERT(nullptr == glyphs);
477         SkASSERT(0 == count);
478         SkASSERT(nullptr == adjustments);
479     }
480     return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
481 }
482 
createFamilyNameIterator() const483 SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
484     return this->onCreateFamilyNameIterator();
485 }
486 
getFamilyName(SkString * name) const487 void SkTypeface::getFamilyName(SkString* name) const {
488     SkASSERT(name);
489     this->onGetFamilyName(name);
490 }
491 
getPostScriptName(SkString * name) const492 bool SkTypeface::getPostScriptName(SkString* name) const {
493     return this->onGetPostScriptName(name);
494 }
495 
getGlyphToUnicodeMap(SkUnichar * dst) const496 void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const {
497     sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs());
498 }
499 
getAdvancedMetrics() const500 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const {
501     std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics();
502     if (result && result->fPostScriptName.isEmpty()) {
503         result->fPostScriptName = result->fFontName;
504     }
505     if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
506         SkOTTableOS2::Version::V2::Type::Field fsType;
507         constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG);
508         constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType);
509         if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) {
510             if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) {
511                 result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag;
512             }
513             if (fsType.NoSubsetting) {
514                 result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag;
515             }
516         }
517     }
518     return result;
519 }
520 
onGetKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const521 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
522                                              int32_t adjustments[]) const {
523     return false;
524 }
525 
526 ///////////////////////////////////////////////////////////////////////////////
527 
528 #include "include/core/SkPaint.h"
529 #include "src/core/SkDescriptor.h"
530 
getBounds() const531 SkRect SkTypeface::getBounds() const {
532     fBoundsOnce([this] {
533         if (!this->onComputeBounds(&fBounds)) {
534             fBounds.setEmpty();
535         }
536     });
537     return fBounds;
538 }
539 
onComputeBounds(SkRect * bounds) const540 bool SkTypeface::onComputeBounds(SkRect* bounds) const {
541     // we use a big size to ensure lots of significant bits from the scalercontext.
542     // then we scale back down to return our final answer (at 1-pt)
543     const SkScalar textSize = 2048;
544     const SkScalar invTextSize = 1 / textSize;
545 
546     SkFont font;
547     font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this)));
548     font.setSize(textSize);
549     font.setLinearMetrics(true);
550 
551     SkScalerContextRec rec;
552     SkScalerContextEffects effects;
553 
554     SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects);
555 
556     SkAutoDescriptor ad;
557     SkScalerContextEffects noeffects;
558     SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad);
559 
560     std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc());
561 
562     SkFontMetrics fm;
563     ctx->getFontMetrics(&fm);
564     if (!fm.hasBounds()) {
565         return false;
566     }
567     bounds->setLTRB(fm.fXMin * invTextSize, fm.fTop * invTextSize,
568                     fm.fXMax * invTextSize, fm.fBottom * invTextSize);
569     return true;
570 }
571 
onGetAdvancedMetrics() const572 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::onGetAdvancedMetrics() const {
573     SkDEBUGFAIL("Typefaces that need to work with PDF backend must override this.");
574     return nullptr;
575 }
576 
577 #ifdef OHOS_SUPPORT
GetHash() const578 uint32_t SkTypeface::GetHash() const
579 {
580     if (hash_ != 0) {
581         return hash_;
582     }
583     auto skData = serialize(SkTypeface::SerializeBehavior::kDontIncludeData);
584     if (skData == nullptr) {
585         return hash_;
586     }
587     std::unique_ptr<SkStreamAsset> ttfStream = openExistingStream(0);
588     uint32_t seed = ttfStream.get() != nullptr ? ttfStream->getLength() : 0;
589     hash_ = SkOpts::hash_fn(skData->data(), skData->size(), seed);
590     return hash_;
591 }
592 
SetHash(uint32_t hash)593 void SkTypeface::SetHash(uint32_t hash)
594 {
595     hash_ = hash;
596 }
597 #endif
598