• 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/base/SkMutex.h"
13 #include "include/private/base/SkOnce.h"
14 #include "include/utils/SkCustomTypeface.h"
15 #include "src/base/SkUTF.h"
16 #include "src/core/SkAdvancedTypefaceMetrics.h"
17 #include "src/core/SkEndian.h"
18 #include "src/core/SkFontDescriptor.h"
19 #include "src/core/SkFontPriv.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 
25 #ifdef SK_TYPEFACE_FACTORY_FREETYPE
26 #include "src/ports/SkFontHost_FreeType_common.h"
27 #endif
28 
29 #ifdef SK_TYPEFACE_FACTORY_CORETEXT
30 #include "src/ports/SkTypeface_mac_ct.h"
31 #endif
32 
33 #ifdef SK_TYPEFACE_FACTORY_DIRECTWRITE
34 #include "src/ports/SkTypeface_win_dw.h"
35 #endif
36 
37 using namespace skia_private;
38 
SkTypeface(const SkFontStyle & style,bool isFixedPitch)39 SkTypeface::SkTypeface(const SkFontStyle& style, bool isFixedPitch)
40     : fUniqueID(SkTypefaceCache::NewTypefaceID()), fStyle(style), fIsFixedPitch(isFixedPitch) { }
41 
~SkTypeface()42 SkTypeface::~SkTypeface() { }
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 
46 namespace {
47 
48 class SkEmptyTypeface : public SkTypeface {
49 public:
Make()50     static sk_sp<SkTypeface> Make() { return sk_sp<SkTypeface>(new SkEmptyTypeface); }
51 
52     static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('e','m','t','y');
MakeFromStream(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments &)53     static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset> stream,
54                                             const SkFontArguments&) {
55         if (stream->getLength() == 0) {
56             return SkEmptyTypeface::Make();
57         }
58         return nullptr;
59     }
60 protected:
SkEmptyTypeface()61     SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { }
62 
onOpenStream(int * ttcIndex) const63     std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; }
onMakeClone(const SkFontArguments & args) const64     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
65         return sk_ref_sp(this);
66     }
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const67     std::unique_ptr<SkScalerContext> onCreateScalerContext(
68         const SkScalerContextEffects& effects, const SkDescriptor* desc) const override
69     {
70         return SkScalerContext::MakeEmpty(
71                 sk_ref_sp(const_cast<SkEmptyTypeface*>(this)), effects, desc);
72     }
onFilterRec(SkScalerContextRec *) const73     void onFilterRec(SkScalerContextRec*) const override { }
onGetAdvancedMetrics() const74     std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override {
75         return nullptr;
76     }
onGetFontDescriptor(SkFontDescriptor * desc,bool * serialize) const77     void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override {
78         desc->setFactoryId(FactoryId);
79         *serialize = false;
80     }
onCharsToGlyphs(const SkUnichar * chars,int count,SkGlyphID glyphs[]) const81     void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override {
82         sk_bzero(glyphs, count * sizeof(glyphs[0]));
83     }
onCountGlyphs() const84     int onCountGlyphs() const override { return 0; }
getPostScriptGlyphNames(SkString *) const85     void getPostScriptGlyphNames(SkString*) const override {}
getGlyphToUnicodeMap(SkUnichar *) const86     void getGlyphToUnicodeMap(SkUnichar*) const override {}
onGetUPEM() const87     int onGetUPEM() const override { return 0; }
88     class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
89     public:
next(SkTypeface::LocalizedString *)90         bool next(SkTypeface::LocalizedString*) override { return false; }
91     };
onGetFamilyName(SkString * familyName) const92     void onGetFamilyName(SkString* familyName) const override {
93         familyName->reset();
94     }
onGetPostScriptName(SkString *) const95     bool onGetPostScriptName(SkString*) const override {
96         return false;
97     }
onCreateFamilyNameIterator() const98     SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override {
99         return new EmptyLocalizedStrings;
100     }
onGlyphMaskNeedsCurrentColor() const101     bool onGlyphMaskNeedsCurrentColor() const override {
102         return false;
103     }
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const104     int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
105                                      int coordinateCount) const override
106     {
107         return 0;
108     }
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const109     int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
110                                        int parameterCount) const override
111     {
112         return 0;
113     }
onGetTableTags(SkFontTableTag tags[]) const114     int onGetTableTags(SkFontTableTag tags[]) const override { return 0; }
onGetTableData(SkFontTableTag,size_t,size_t,void *) const115     size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override {
116         return 0;
117     }
118 };
119 
120 }  // namespace
121 
FromOldStyle(Style oldStyle)122 SkFontStyle SkTypeface::FromOldStyle(Style oldStyle) {
123     return SkFontStyle((oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight
124                                                       : SkFontStyle::kNormal_Weight,
125                        SkFontStyle::kNormal_Width,
126                        (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant
127                                                         : SkFontStyle::kUpright_Slant);
128 }
129 
GetDefaultTypeface(Style style)130 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
131     static SkOnce once[4];
132     static sk_sp<SkTypeface> defaults[4];
133 
134     SkASSERT((int)style < 4);
135     once[style]([style] {
136         sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
137         auto t = fm->legacyMakeTypeface(nullptr, FromOldStyle(style));
138         defaults[style] = t ? t : SkEmptyTypeface::Make();
139     });
140     return defaults[style].get();
141 }
142 
MakeDefault()143 sk_sp<SkTypeface> SkTypeface::MakeDefault() {
144     return sk_ref_sp(GetDefaultTypeface());
145 }
146 
UniqueID(const SkTypeface * face)147 uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
148     if (nullptr == face) {
149         face = GetDefaultTypeface();
150     }
151     return face->uniqueID();
152 }
153 
Equal(const SkTypeface * facea,const SkTypeface * faceb)154 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
155     return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
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 
decoders()167     std::vector<DecoderProc>* decoders() {
168         static auto* decoders = new std::vector<DecoderProc> {
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         };
181         return decoders;
182     }
183 
184 }  // namespace
185 
MakeFromName(const char name[],SkFontStyle fontStyle)186 sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[],
187                                            SkFontStyle fontStyle) {
188     if (nullptr == name && (fontStyle.slant() == SkFontStyle::kItalic_Slant ||
189                             fontStyle.slant() == SkFontStyle::kUpright_Slant) &&
190                            (fontStyle.weight() == SkFontStyle::kBold_Weight ||
191                             fontStyle.weight() == SkFontStyle::kNormal_Weight)) {
192         return sk_ref_sp(GetDefaultTypeface(static_cast<SkTypeface::Style>(
193             (fontStyle.slant() == SkFontStyle::kItalic_Slant ? SkTypeface::kItalic :
194                                                                SkTypeface::kNormal) |
195             (fontStyle.weight() == SkFontStyle::kBold_Weight ? SkTypeface::kBold :
196                                                                SkTypeface::kNormal))));
197     }
198     return SkFontMgr::RefDefault()->legacyMakeTypeface(name, fontStyle);
199 }
200 
MakeFromStream(std::unique_ptr<SkStreamAsset> stream,int index)201 sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) {
202     if (!stream) {
203         return nullptr;
204     }
205     // TODO: Enable this while updating tests (FontHostStream), expectations, and nonativeFonts.
206 #if 0
207     SkFontArguments args;
208     args.setCollectionIndex(index);
209     for (const DecoderProc& proc : *decoders()) {
210         sk_sp<SkTypeface> typeface = proc.makeFromStream(stream->duplicate(), args);
211         if (typeface) {
212             return typeface;
213         }
214     }
215 #endif
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 
Register(FactoryId id,sk_sp<SkTypeface> (* make)(std::unique_ptr<SkStreamAsset>,const SkFontArguments &))236 void SkTypeface::Register(
237             FactoryId id,
238             sk_sp<SkTypeface> (*make)(std::unique_ptr<SkStreamAsset>, const SkFontArguments&)) {
239     decoders()->push_back(DecoderProc{id, make});
240 }
241 
serialize(SkWStream * wstream,SerializeBehavior behavior) const242 void SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const {
243     bool isLocalData = false;
244     SkFontDescriptor desc;
245     this->onGetFontDescriptor(&desc, &isLocalData);
246     if (desc.getFactoryId() == 0) {
247         SkDEBUGF("Factory was not set for %s.\n", desc.getFamilyName());
248     }
249 
250     bool shouldSerializeData = false;
251     switch (behavior) {
252         case SerializeBehavior::kDoIncludeData:      shouldSerializeData = true;        break;
253         case SerializeBehavior::kDontIncludeData:    shouldSerializeData = false;       break;
254         case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break;
255     }
256 
257     if (shouldSerializeData) {
258         int index;
259         desc.setStream(this->openStream(&index));
260         if (desc.hasStream()) {
261             desc.setCollectionIndex(index);
262         }
263 
264         int numAxes = this->getVariationDesignPosition(nullptr, 0);
265         if (0 < numAxes) {
266             numAxes = this->getVariationDesignPosition(desc.setVariationCoordinates(numAxes), numAxes);
267             if (numAxes <= 0) {
268                 desc.setVariationCoordinates(0);
269             }
270         }
271     }
272     desc.serialize(wstream);
273 }
274 
serialize(SerializeBehavior behavior) const275 sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const {
276     SkDynamicMemoryWStream stream;
277     this->serialize(&stream, behavior);
278     return stream.detachAsData();
279 }
280 
MakeDeserialize(SkStream * stream)281 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) {
282     SkFontDescriptor desc;
283     if (!SkFontDescriptor::Deserialize(stream, &desc)) {
284         return nullptr;
285     }
286 
287     if (desc.hasStream()) {
288         for (const DecoderProc& proc : *decoders()) {
289             if (proc.id == desc.getFactoryId()) {
290                 return proc.makeFromStream(desc.detachStream(), desc.getFontArguments());
291             }
292         }
293 
294         SkDEBUGCODE(FactoryId id = desc.getFactoryId();)
295         SkDEBUGF("Could not find factory %c%c%c%c for %s.\n",
296                  (id >> 24) & 0xFF, (id >> 16) & 0xFF, (id >> 8) & 0xFF, (id >> 0) & 0xFF,
297                  desc.getFamilyName());
298 
299         sk_sp<SkFontMgr> defaultFm = SkFontMgr::RefDefault();
300         sk_sp<SkTypeface> typeface = defaultFm->makeFromStream(desc.detachStream(),
301                                                                desc.getFontArguments());
302         if (typeface) {
303             return typeface;
304         }
305     }
306 
307     return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle());
308 }
309 
310 ///////////////////////////////////////////////////////////////////////////////
311 
glyphMaskNeedsCurrentColor() const312 bool SkTypeface::glyphMaskNeedsCurrentColor() const {
313     return this->onGlyphMaskNeedsCurrentColor();
314 }
315 
getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const316 int SkTypeface::getVariationDesignPosition(
317         SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
318 {
319     return this->onGetVariationDesignPosition(coordinates, coordinateCount);
320 }
321 
getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const322 int SkTypeface::getVariationDesignParameters(
323         SkFontParameters::Variation::Axis parameters[], int parameterCount) const
324 {
325     return this->onGetVariationDesignParameters(parameters, parameterCount);
326 }
327 
countTables() const328 int SkTypeface::countTables() const {
329     return this->onGetTableTags(nullptr);
330 }
331 
getTableTags(SkFontTableTag tags[]) const332 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
333     return this->onGetTableTags(tags);
334 }
335 
getTableSize(SkFontTableTag tag) const336 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
337     return this->onGetTableData(tag, 0, ~0U, nullptr);
338 }
339 
getTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const340 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
341                                 void* data) const {
342     return this->onGetTableData(tag, offset, length, data);
343 }
344 
copyTableData(SkFontTableTag tag) const345 sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const {
346     return this->onCopyTableData(tag);
347 }
348 
onCopyTableData(SkFontTableTag tag) const349 sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const {
350     size_t size = this->getTableSize(tag);
351     if (size) {
352         sk_sp<SkData> data = SkData::MakeUninitialized(size);
353         (void)this->getTableData(tag, 0, size, data->writable_data());
354         return data;
355     }
356     return nullptr;
357 }
358 
openStream(int * ttcIndex) const359 std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const {
360     int ttcIndexStorage;
361     if (nullptr == ttcIndex) {
362         // So our subclasses don't need to check for null param
363         ttcIndex = &ttcIndexStorage;
364     }
365     return this->onOpenStream(ttcIndex);
366 }
367 
openExistingStream(int * ttcIndex) const368 std::unique_ptr<SkStreamAsset> SkTypeface::openExistingStream(int* ttcIndex) const {
369     int ttcIndexStorage;
370     if (nullptr == ttcIndex) {
371         // So our subclasses don't need to check for null param
372         ttcIndex = &ttcIndexStorage;
373     }
374     return this->onOpenExistingStream(ttcIndex);
375 }
376 
createScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const377 std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
378         const SkScalerContextEffects& effects, const SkDescriptor* desc) const {
379     std::unique_ptr<SkScalerContext> scalerContext = this->onCreateScalerContext(effects, desc);
380     SkASSERT(scalerContext);
381     return scalerContext;
382 }
383 
unicharsToGlyphs(const SkUnichar uni[],int count,SkGlyphID glyphs[]) const384 void SkTypeface::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
385     if (count > 0 && glyphs && uni) {
386         this->onCharsToGlyphs(uni, count, glyphs);
387     }
388 }
389 
unicharToGlyph(SkUnichar uni) const390 SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const {
391     SkGlyphID glyphs[1] = { 0 };
392     this->onCharsToGlyphs(&uni, 1, glyphs);
393     return glyphs[0];
394 }
395 
396 namespace {
397 class SkConvertToUTF32 {
398 public:
SkConvertToUTF32()399     SkConvertToUTF32() {}
400 
convert(const void * text,size_t byteLength,SkTextEncoding encoding)401     const SkUnichar* convert(const void* text, size_t byteLength, SkTextEncoding encoding) {
402         const SkUnichar* uni;
403         switch (encoding) {
404             case SkTextEncoding::kUTF8: {
405                 uni = fStorage.reset(byteLength);
406                 const char* ptr = (const char*)text;
407                 const char* end = ptr + byteLength;
408                 for (int i = 0; ptr < end; ++i) {
409                     fStorage[i] = SkUTF::NextUTF8(&ptr, end);
410                 }
411             } break;
412             case SkTextEncoding::kUTF16: {
413                 uni = fStorage.reset(byteLength);
414                 const uint16_t* ptr = (const uint16_t*)text;
415                 const uint16_t* end = ptr + (byteLength >> 1);
416                 for (int i = 0; ptr < end; ++i) {
417                     fStorage[i] = SkUTF::NextUTF16(&ptr, end);
418                 }
419             } break;
420             case SkTextEncoding::kUTF32:
421                 uni = (const SkUnichar*)text;
422                 break;
423             default:
424                 SK_ABORT("unexpected enum");
425         }
426         return uni;
427     }
428 
429 private:
430     AutoSTMalloc<256, SkUnichar> fStorage;
431 };
432 }
433 
textToGlyphs(const void * text,size_t byteLength,SkTextEncoding encoding,SkGlyphID glyphs[],int maxGlyphCount) const434 int SkTypeface::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
435                              SkGlyphID glyphs[], int maxGlyphCount) const {
436     if (0 == byteLength) {
437         return 0;
438     }
439 
440     SkASSERT(text);
441 
442     int count = SkFontPriv::CountTextElements(text, byteLength, encoding);
443     if (!glyphs || count > maxGlyphCount) {
444         return count;
445     }
446 
447     if (encoding == SkTextEncoding::kGlyphID) {
448         memcpy(glyphs, text, count << 1);
449         return count;
450     }
451 
452     SkConvertToUTF32 storage;
453     const SkUnichar* uni = storage.convert(text, byteLength, encoding);
454 
455     this->unicharsToGlyphs(uni, count, glyphs);
456     return count;
457 }
458 
countGlyphs() const459 int SkTypeface::countGlyphs() const {
460     return this->onCountGlyphs();
461 }
462 
getUnitsPerEm() const463 int SkTypeface::getUnitsPerEm() const {
464     // should we try to cache this in the base-class?
465     return this->onGetUPEM();
466 }
467 
getKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const468 bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
469                                            int32_t adjustments[]) const {
470     SkASSERT(count >= 0);
471     // check for the only legal way to pass a nullptr.. everything is 0
472     // in which case they just want to know if this face can possibly support
473     // kerning (true) or never (false).
474     if (nullptr == glyphs || nullptr == adjustments) {
475         SkASSERT(nullptr == glyphs);
476         SkASSERT(0 == count);
477         SkASSERT(nullptr == adjustments);
478     }
479     return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
480 }
481 
createFamilyNameIterator() const482 SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
483     return this->onCreateFamilyNameIterator();
484 }
485 
getFamilyName(SkString * name) const486 void SkTypeface::getFamilyName(SkString* name) const {
487     SkASSERT(name);
488     this->onGetFamilyName(name);
489 }
490 
getPostScriptName(SkString * name) const491 bool SkTypeface::getPostScriptName(SkString* name) const {
492     return this->onGetPostScriptName(name);
493 }
494 
getGlyphToUnicodeMap(SkUnichar * dst) const495 void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const {
496     sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs());
497 }
498 
getAdvancedMetrics() const499 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const {
500     std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics();
501     if (result && result->fPostScriptName.isEmpty()) {
502         result->fPostScriptName = result->fFontName;
503     }
504     if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
505         SkOTTableOS2::Version::V2::Type::Field fsType;
506         constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG);
507         constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType);
508         if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) {
509             if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) {
510                 result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag;
511             }
512             if (fsType.NoSubsetting) {
513                 result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag;
514             }
515         }
516     }
517     return result;
518 }
519 
onGetKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const520 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
521                                              int32_t adjustments[]) const {
522     return false;
523 }
524 
onOpenExistingStream(int * ttcIndex) const525 std::unique_ptr<SkStreamAsset> SkTypeface::onOpenExistingStream(int* ttcIndex) const {
526     return this->onOpenStream(ttcIndex);
527 }
528 
529 ///////////////////////////////////////////////////////////////////////////////
530 
531 #include "include/core/SkPaint.h"
532 #include "src/core/SkDescriptor.h"
533 
getBounds() const534 SkRect SkTypeface::getBounds() const {
535     fBoundsOnce([this] {
536         if (!this->onComputeBounds(&fBounds)) {
537             fBounds.setEmpty();
538         }
539     });
540     return fBounds;
541 }
542 
onComputeBounds(SkRect * bounds) const543 bool SkTypeface::onComputeBounds(SkRect* bounds) const {
544     // we use a big size to ensure lots of significant bits from the scalercontext.
545     // then we scale back down to return our final answer (at 1-pt)
546     const SkScalar textSize = 2048;
547     const SkScalar invTextSize = 1 / textSize;
548 
549     SkFont font;
550     font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this)));
551     font.setSize(textSize);
552     font.setLinearMetrics(true);
553 
554     SkScalerContextRec rec;
555     SkScalerContextEffects effects;
556 
557     SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects);
558 
559     SkAutoDescriptor ad;
560     SkScalerContextEffects noeffects;
561     SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad);
562 
563     std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc());
564 
565     SkFontMetrics fm;
566     ctx->getFontMetrics(&fm);
567     if (!fm.hasBounds()) {
568         return false;
569     }
570     bounds->setLTRB(fm.fXMin * invTextSize, fm.fTop * invTextSize,
571                     fm.fXMax * invTextSize, fm.fBottom * invTextSize);
572     return true;
573 }
574 
onGetAdvancedMetrics() const575 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::onGetAdvancedMetrics() const {
576     SkDEBUGFAIL("Typefaces that need to work with PDF backend must override this.");
577     return nullptr;
578 }
579