1 /*
2  * Copyright 2014 Google Inc.
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 "SkTypes.h"
9 
10 #include "SkData.h"
11 #include "SkFixed.h"
12 #include "SkFontDescriptor.h"
13 #include "SkFontHost_FreeType_common.h"
14 #include "SkFontMgr.h"
15 #include "SkFontMgr_android.h"
16 #include "SkFontMgr_android_parser.h"
17 #include "SkFontStyle.h"
18 #include "SkMakeUnique.h"
19 #include "SkOSFile.h"
20 #include "SkPaint.h"
21 #include "SkRefCnt.h"
22 #include "SkString.h"
23 #include "SkStream.h"
24 #include "SkTArray.h"
25 #include "SkTDArray.h"
26 #include "SkTSearch.h"
27 #include "SkTemplates.h"
28 #include "SkTypefaceCache.h"
29 
30 #include <algorithm>
31 #include <limits>
32 
33 class SkData;
34 
35 class SkTypeface_Android : public SkTypeface_FreeType {
36 public:
SkTypeface_Android(const SkFontStyle & style,bool isFixedPitch,const SkString & familyName)37     SkTypeface_Android(const SkFontStyle& style,
38                        bool isFixedPitch,
39                        const SkString& familyName)
40         : INHERITED(style, isFixedPitch)
41         , fFamilyName(familyName)
42         { }
43 
44 protected:
onGetFamilyName(SkString * familyName) const45     void onGetFamilyName(SkString* familyName) const override {
46         *familyName = fFamilyName;
47     }
48 
49     SkString fFamilyName;
50 
51 private:
52     typedef SkTypeface_FreeType INHERITED;
53 };
54 
55 class SkTypeface_AndroidSystem : public SkTypeface_Android {
56 public:
SkTypeface_AndroidSystem(const SkString & pathName,const bool cacheFontFiles,int index,const SkFixed * axes,int axesCount,const SkFontStyle & style,bool isFixedPitch,const SkString & familyName,const SkTArray<SkLanguage,true> & lang,FontVariant variantStyle)57     SkTypeface_AndroidSystem(const SkString& pathName,
58                              const bool cacheFontFiles,
59                              int index,
60                              const SkFixed* axes, int axesCount,
61                              const SkFontStyle& style,
62                              bool isFixedPitch,
63                              const SkString& familyName,
64                              const SkTArray<SkLanguage, true>& lang,
65                              FontVariant variantStyle)
66         : INHERITED(style, isFixedPitch, familyName)
67         , fPathName(pathName)
68         , fIndex(index)
69         , fAxes(axes, axesCount)
70         , fLang(lang)
71         , fVariantStyle(variantStyle)
72         , fFile(cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) : nullptr) {
73         if (cacheFontFiles) {
74             SkASSERT(fFile);
75         }
76     }
77 
makeStream() const78     std::unique_ptr<SkStreamAsset> makeStream() const {
79         if (fFile) {
80             sk_sp<SkData> data(SkData::MakeFromFILE(fFile));
81             return data ? skstd::make_unique<SkMemoryStream>(std::move(data)) : nullptr;
82         }
83         return SkStream::MakeFromFile(fPathName.c_str());
84     }
85 
onGetFontDescriptor(SkFontDescriptor * desc,bool * serialize) const86     virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override {
87         SkASSERT(desc);
88         SkASSERT(serialize);
89         desc->setFamilyName(fFamilyName.c_str());
90         desc->setStyle(this->fontStyle());
91         *serialize = false;
92     }
onOpenStream(int * ttcIndex) const93     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
94         *ttcIndex = fIndex;
95         return this->makeStream().release();
96     }
onMakeFontData() const97     std::unique_ptr<SkFontData> onMakeFontData() const override {
98         return skstd::make_unique<SkFontData>(this->makeStream(), fIndex,
99                                               fAxes.begin(), fAxes.count());
100     }
onMakeClone(const SkFontArguments & args) const101     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
102         std::unique_ptr<SkFontData> data = this->cloneFontData(args);
103         if (!data) {
104             return nullptr;
105         }
106         return sk_make_sp<SkTypeface_AndroidSystem>(fPathName,
107                                                     fFile,
108                                                     fIndex,
109                                                     data->getAxis(),
110                                                     data->getAxisCount(),
111                                                     this->fontStyle(),
112                                                     this->isFixedPitch(),
113                                                     fFamilyName,
114                                                     fLang,
115                                                     fVariantStyle);
116     }
117 
118     const SkString fPathName;
119     int fIndex;
120     const SkSTArray<4, SkFixed, true> fAxes;
121     const SkSTArray<4, SkLanguage, true> fLang;
122     const FontVariant fVariantStyle;
123     SkAutoTCallVProc<FILE, sk_fclose> fFile;
124 
125     typedef SkTypeface_Android INHERITED;
126 };
127 
128 class SkTypeface_AndroidStream : public SkTypeface_Android {
129 public:
SkTypeface_AndroidStream(std::unique_ptr<SkFontData> data,const SkFontStyle & style,bool isFixedPitch,const SkString & familyName)130     SkTypeface_AndroidStream(std::unique_ptr<SkFontData> data,
131                              const SkFontStyle& style,
132                              bool isFixedPitch,
133                              const SkString& familyName)
134         : INHERITED(style, isFixedPitch, familyName)
135         , fData(std::move(data))
136     { }
137 
onGetFontDescriptor(SkFontDescriptor * desc,bool * serialize) const138     virtual void onGetFontDescriptor(SkFontDescriptor* desc,
139                                      bool* serialize) const override {
140         SkASSERT(desc);
141         SkASSERT(serialize);
142         desc->setFamilyName(fFamilyName.c_str());
143         *serialize = true;
144     }
145 
onOpenStream(int * ttcIndex) const146     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
147         *ttcIndex = fData->getIndex();
148         return fData->getStream()->duplicate().release();
149     }
150 
onMakeFontData() const151     std::unique_ptr<SkFontData> onMakeFontData() const override {
152         return skstd::make_unique<SkFontData>(*fData);
153     }
154 
onMakeClone(const SkFontArguments & args) const155     sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
156         std::unique_ptr<SkFontData> data = this->cloneFontData(args);
157         if (!data) {
158             return nullptr;
159         }
160         return sk_make_sp<SkTypeface_AndroidStream>(std::move(data),
161                                                     this->fontStyle(),
162                                                     this->isFixedPitch(),
163                                                     fFamilyName);
164     }
165 
166 private:
167     const std::unique_ptr<const SkFontData> fData;
168     typedef SkTypeface_Android INHERITED;
169 };
170 
171 class SkFontStyleSet_Android : public SkFontStyleSet {
172     typedef SkTypeface_FreeType::Scanner Scanner;
173 
174 public:
SkFontStyleSet_Android(const FontFamily & family,const Scanner & scanner,const bool cacheFontFiles)175     explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& scanner,
176                                     const bool cacheFontFiles) {
177         const SkString* cannonicalFamilyName = nullptr;
178         if (family.fNames.count() > 0) {
179             cannonicalFamilyName = &family.fNames[0];
180         }
181         fFallbackFor = family.fFallbackFor;
182 
183         // TODO? make this lazy
184         for (int i = 0; i < family.fFonts.count(); ++i) {
185             const FontFileInfo& fontFile = family.fFonts[i];
186 
187             SkString pathName(family.fBasePath);
188             pathName.append(fontFile.fFileName);
189 
190             std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(pathName.c_str());
191             if (!stream) {
192                 SkDEBUGF("Requested font file %s does not exist or cannot be opened.\n",
193                          pathName.c_str());
194                 continue;
195             }
196 
197             const int ttcIndex = fontFile.fIndex;
198             SkString familyName;
199             SkFontStyle style;
200             bool isFixedWidth;
201             Scanner::AxisDefinitions axisDefinitions;
202             if (!scanner.scanFont(stream.get(), ttcIndex,
203                                   &familyName, &style, &isFixedWidth, &axisDefinitions))
204             {
205                 SkDEBUGF("Requested font file %s exists, but is not a valid font.\n",
206                          pathName.c_str());
207                 continue;
208             }
209 
210             int weight = fontFile.fWeight != 0 ? fontFile.fWeight : style.weight();
211             SkFontStyle::Slant slant = style.slant();
212             switch (fontFile.fStyle) {
213                 case FontFileInfo::Style::kAuto: slant = style.slant(); break;
214                 case FontFileInfo::Style::kNormal: slant = SkFontStyle::kUpright_Slant; break;
215                 case FontFileInfo::Style::kItalic: slant = SkFontStyle::kItalic_Slant; break;
216                 default: SkASSERT(false); break;
217             }
218             style = SkFontStyle(weight, style.width(), slant);
219 
220             uint32_t variant = family.fVariant;
221             if (kDefault_FontVariant == variant) {
222                 variant = kCompact_FontVariant | kElegant_FontVariant;
223             }
224 
225             // The first specified family name overrides the family name found in the font.
226             // TODO: SkTypeface_AndroidSystem::onCreateFamilyNameIterator should return
227             // all of the specified family names in addition to the names found in the font.
228             if (cannonicalFamilyName != nullptr) {
229                 familyName = *cannonicalFamilyName;
230             }
231 
232             SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
233             SkFontArguments::VariationPosition position = {
234                 fontFile.fVariationDesignPosition.begin(),
235                 fontFile.fVariationDesignPosition.count()
236             };
237             Scanner::computeAxisValues(axisDefinitions, position,
238                                        axisValues, familyName);
239 
240             fStyles.push_back().reset(new SkTypeface_AndroidSystem(
241                     pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDefinitions.count(),
242                     style, isFixedWidth, familyName, family.fLanguages, variant));
243         }
244     }
245 
count()246     int count() override {
247         return fStyles.count();
248     }
getStyle(int index,SkFontStyle * style,SkString * name)249     void getStyle(int index, SkFontStyle* style, SkString* name) override {
250         if (index < 0 || fStyles.count() <= index) {
251             return;
252         }
253         if (style) {
254             *style = fStyles[index]->fontStyle();
255         }
256         if (name) {
257             name->reset();
258         }
259     }
createTypeface(int index)260     SkTypeface_AndroidSystem* createTypeface(int index) override {
261         if (index < 0 || fStyles.count() <= index) {
262             return nullptr;
263         }
264         return SkRef(fStyles[index].get());
265     }
266 
matchStyle(const SkFontStyle & pattern)267     SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override {
268         return static_cast<SkTypeface_AndroidSystem*>(this->matchStyleCSS3(pattern));
269     }
270 
271 private:
272     SkTArray<sk_sp<SkTypeface_AndroidSystem>> fStyles;
273     SkString fFallbackFor;
274 
275     friend struct NameToFamily;
276     friend class SkFontMgr_Android;
277 
278     typedef SkFontStyleSet INHERITED;
279 };
280 
281 /** On Android a single family can have many names, but our API assumes unique names.
282  *  Map names to the back end so that all names for a given family refer to the same
283  *  (non-replicated) set of typefaces.
284  *  SkTDict<> doesn't let us do index-based lookup, so we write our own mapping.
285  */
286 struct NameToFamily {
287     SkString name;
288     SkFontStyleSet_Android* styleSet;
289 };
290 
291 class SkFontMgr_Android : public SkFontMgr {
292 public:
SkFontMgr_Android(const SkFontMgr_Android_CustomFonts * custom)293     SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) {
294         SkTDArray<FontFamily*> families;
295         if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fSystemFontUse) {
296             SkString base(custom->fBasePath);
297             SkFontMgr_Android_Parser::GetCustomFontFamilies(
298                 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
299         }
300         if (!custom ||
301             (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSystemFontUse))
302         {
303             SkFontMgr_Android_Parser::GetSystemFontFamilies(families);
304         }
305         if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fSystemFontUse) {
306             SkString base(custom->fBasePath);
307             SkFontMgr_Android_Parser::GetCustomFontFamilies(
308                 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
309         }
310         this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false);
311         this->findDefaultStyleSet();
312         families.deleteAll();
313     }
314 
315 protected:
316     /** Returns not how many families we have, but how many unique names
317      *  exist among the families.
318      */
onCountFamilies() const319     int onCountFamilies() const override {
320         return fNameToFamilyMap.count();
321     }
322 
onGetFamilyName(int index,SkString * familyName) const323     void onGetFamilyName(int index, SkString* familyName) const override {
324         if (index < 0 || fNameToFamilyMap.count() <= index) {
325             familyName->reset();
326             return;
327         }
328         familyName->set(fNameToFamilyMap[index].name);
329     }
330 
onCreateStyleSet(int index) const331     SkFontStyleSet* onCreateStyleSet(int index) const override {
332         if (index < 0 || fNameToFamilyMap.count() <= index) {
333             return nullptr;
334         }
335         return SkRef(fNameToFamilyMap[index].styleSet);
336     }
337 
onMatchFamily(const char familyName[]) const338     SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
339         if (!familyName) {
340             return nullptr;
341         }
342         SkAutoAsciiToLC tolc(familyName);
343         for (int i = 0; i < fNameToFamilyMap.count(); ++i) {
344             if (fNameToFamilyMap[i].name.equals(tolc.lc())) {
345                 return SkRef(fNameToFamilyMap[i].styleSet);
346             }
347         }
348         // TODO: eventually we should not need to name fallback families.
349         for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) {
350             if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) {
351                 return SkRef(fFallbackNameToFamilyMap[i].styleSet);
352             }
353         }
354         return nullptr;
355     }
356 
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const357     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
358                                            const SkFontStyle& style) const override {
359         sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
360         return sset->matchStyle(style);
361     }
362 
onMatchFaceStyle(const SkTypeface * typeface,const SkFontStyle & style) const363     virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface,
364                                          const SkFontStyle& style) const override {
365         for (int i = 0; i < fStyleSets.count(); ++i) {
366             for (int j = 0; j < fStyleSets[i]->fStyles.count(); ++j) {
367                 if (fStyleSets[i]->fStyles[j].get() == typeface) {
368                     return fStyleSets[i]->matchStyle(style);
369                 }
370             }
371         }
372         return nullptr;
373     }
374 
find_family_style_character(const SkString & familyName,const SkTArray<NameToFamily,true> & fallbackNameToFamilyMap,const SkFontStyle & style,bool elegant,const SkString & langTag,SkUnichar character)375     static sk_sp<SkTypeface_AndroidSystem> find_family_style_character(
376             const SkString& familyName,
377             const SkTArray<NameToFamily, true>& fallbackNameToFamilyMap,
378             const SkFontStyle& style, bool elegant,
379             const SkString& langTag, SkUnichar character)
380     {
381         for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) {
382             SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet;
383             if (familyName != family->fFallbackFor) {
384                 continue;
385             }
386             sk_sp<SkTypeface_AndroidSystem> face(family->matchStyle(style));
387 
388             if (!langTag.isEmpty() &&
389                 std::none_of(face->fLang.begin(), face->fLang.end(), [&](SkLanguage lang){
390                     return lang.getTag().startsWith(langTag.c_str());
391                 }))
392             {
393                 continue;
394             }
395 
396             if (SkToBool(face->fVariantStyle & kElegant_FontVariant) != elegant) {
397                 continue;
398             }
399 
400             if (face->unicharToGlyph(character) != 0) {
401                 return face;
402             }
403         }
404         return nullptr;
405     }
406 
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const407     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
408                                                     const SkFontStyle& style,
409                                                     const char* bcp47[],
410                                                     int bcp47Count,
411                                                     SkUnichar character) const override
412     {
413         // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascent/descent'.
414         // The variant 'default' means 'compact and elegant'.
415         // As a result, it is not possible to know the variant context from the font alone.
416         // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request.
417 
418         SkString familyNameString(familyName);
419         for (const SkString& currentFamilyName : { familyNameString, SkString() }) {
420             // The first time match anything elegant, second time anything not elegant.
421             for (int elegant = 2; elegant --> 0;) {
422                 for (int bcp47Index = bcp47Count; bcp47Index --> 0;) {
423                     SkLanguage lang(bcp47[bcp47Index]);
424                     while (!lang.getTag().isEmpty()) {
425                         sk_sp<SkTypeface_AndroidSystem> matchingTypeface =
426                             find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
427                                                         style, SkToBool(elegant),
428                                                         lang.getTag(), character);
429                         if (matchingTypeface) {
430                             return matchingTypeface.release();
431                         }
432 
433                         lang = lang.getParent();
434                     }
435                 }
436                 sk_sp<SkTypeface_AndroidSystem> matchingTypeface =
437                     find_family_style_character(currentFamilyName, fFallbackNameToFamilyMap,
438                                                 style, SkToBool(elegant),
439                                                 SkString(), character);
440                 if (matchingTypeface) {
441                     return matchingTypeface.release();
442                 }
443             }
444         }
445         return nullptr;
446     }
447 
onMakeFromData(sk_sp<SkData> data,int ttcIndex) const448     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
449         return this->makeFromStream(std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))),
450                                     ttcIndex);
451     }
452 
onMakeFromFile(const char path[],int ttcIndex) const453     sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
454         std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
455         return stream.get() ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
456     }
457 
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,int ttcIndex) const458     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
459                                             int ttcIndex) const override {
460         bool isFixedPitch;
461         SkFontStyle style;
462         SkString name;
463         if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
464             return nullptr;
465         }
466         auto data = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
467         return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
468                                                               style, isFixedPitch, name));
469     }
470 
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args) const471     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
472                                            const SkFontArguments& args) const override {
473         using Scanner = SkTypeface_FreeType::Scanner;
474         bool isFixedPitch;
475         SkFontStyle style;
476         SkString name;
477         Scanner::AxisDefinitions axisDefinitions;
478         if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
479                                &name, &style, &isFixedPitch, &axisDefinitions))
480         {
481             return nullptr;
482         }
483 
484         SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
485         Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
486                                    axisValues, name);
487 
488         auto data = skstd::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
489                                                    axisValues.get(), axisDefinitions.count());
490         return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
491                                                               style, isFixedPitch, name));
492     }
493 
onMakeFromFontData(std::unique_ptr<SkFontData> data) const494     sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> data) const override {
495         SkStreamAsset* stream(data->getStream());
496         bool isFixedPitch;
497         SkFontStyle style;
498         SkString name;
499         if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixedPitch, nullptr)) {
500             return nullptr;
501         }
502         return sk_sp<SkTypeface>(new SkTypeface_AndroidStream(std::move(data),
503                                                               style, isFixedPitch, name));
504     }
505 
onLegacyMakeTypeface(const char familyName[],SkFontStyle style) const506     sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override {
507         if (familyName) {
508             // On Android, we must return nullptr when we can't find the requested
509             // named typeface so that the system/app can provide their own recovery
510             // mechanism. On other platforms we'd provide a typeface from the
511             // default family instead.
512             return sk_sp<SkTypeface>(this->onMatchFamilyStyle(familyName, style));
513         }
514         return sk_sp<SkTypeface>(fDefaultStyleSet->matchStyle(style));
515     }
516 
517 
518 private:
519 
520     SkTypeface_FreeType::Scanner fScanner;
521 
522     SkTArray<sk_sp<SkFontStyleSet_Android>> fStyleSets;
523     sk_sp<SkFontStyleSet> fDefaultStyleSet;
524 
525     SkTArray<NameToFamily, true> fNameToFamilyMap;
526     SkTArray<NameToFamily, true> fFallbackNameToFamilyMap;
527 
addFamily(FontFamily & family,const bool isolated,int familyIndex)528     void addFamily(FontFamily& family, const bool isolated, int familyIndex) {
529         SkTArray<NameToFamily, true>* nameToFamily = &fNameToFamilyMap;
530         if (family.fIsFallbackFont) {
531             nameToFamily = &fFallbackNameToFamilyMap;
532 
533             if (0 == family.fNames.count()) {
534                 SkString& fallbackName = family.fNames.push_back();
535                 fallbackName.printf("%.2x##fallback", familyIndex);
536             }
537         }
538 
539         sk_sp<SkFontStyleSet_Android> newSet =
540             sk_make_sp<SkFontStyleSet_Android>(family, fScanner, isolated);
541         if (0 == newSet->count()) {
542             return;
543         }
544 
545         for (const SkString& name : family.fNames) {
546             nameToFamily->emplace_back(NameToFamily{name, newSet.get()});
547         }
548         fStyleSets.emplace_back(std::move(newSet));
549     }
buildNameToFamilyMap(SkTDArray<FontFamily * > families,const bool isolated)550     void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolated) {
551         int familyIndex = 0;
552         for (FontFamily* family : families) {
553             addFamily(*family, isolated, familyIndex++);
554             family->fallbackFamilies.foreach([this, isolated, &familyIndex]
555                 (SkString, std::unique_ptr<FontFamily>* fallbackFamily) {
556                     addFamily(*(*fallbackFamily).get(), isolated, familyIndex++);
557                 }
558             );
559         }
560     }
561 
findDefaultStyleSet()562     void findDefaultStyleSet() {
563         SkASSERT(!fStyleSets.empty());
564 
565         static const char* defaultNames[] = { "sans-serif" };
566         for (const char* defaultName : defaultNames) {
567             fDefaultStyleSet.reset(this->onMatchFamily(defaultName));
568             if (fDefaultStyleSet) {
569                 break;
570             }
571         }
572         if (nullptr == fDefaultStyleSet) {
573             fDefaultStyleSet = fStyleSets[0];
574         }
575         SkASSERT(fDefaultStyleSet);
576     }
577 
578     typedef SkFontMgr INHERITED;
579 };
580 
581 #ifdef SK_DEBUG
582 static char const * const gSystemFontUseStrings[] = {
583     "OnlyCustom", "PreferCustom", "PreferSystem"
584 };
585 #endif
586 
SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts * custom)587 sk_sp<SkFontMgr> SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) {
588     if (custom) {
589         SkASSERT(0 <= custom->fSystemFontUse);
590         SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings));
591         SkDEBUGF("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n",
592                  gSystemFontUseStrings[custom->fSystemFontUse],
593                  custom->fBasePath,
594                  custom->fFontsXml,
595                  custom->fFallbackFontsXml);
596     }
597     return sk_make_sp<SkFontMgr_Android>(custom);
598 }
599