1 /*
2  * Copyright 2017 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 "SkFontDescriptor.h"
9 #include "SkTestFontMgr.h"
10 #include "SkTestTypeface.h"
11 #include "sk_tool_utils.h"
12 
13 #ifdef SK_XML
14 #include "SkTestSVGTypeface.h"
15 #endif
16 
17 #include <vector>
18 
19 namespace {
20 
21 #include "test_font_monospace.inc"
22 #include "test_font_sans_serif.inc"
23 #include "test_font_serif.inc"
24 #include "test_font_index.inc"
25 
26 class FontStyleSet final : public SkFontStyleSet {
27 public:
FontStyleSet(const char * familyName)28     FontStyleSet(const char* familyName) : fFamilyName(familyName) { }
29     struct TypefaceEntry {
TypefaceEntry__anon5b07664d0111::FontStyleSet::TypefaceEntry30         TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName)
31             : fTypeface(std::move(typeface))
32             , fStyle(style)
33             , fStyleName(styleName)
34         {}
35         sk_sp<SkTypeface> fTypeface;
36         SkFontStyle fStyle;
37         const char* fStyleName;
38     };
39 
count()40     int count() override { return fTypefaces.size(); }
41 
getStyle(int index,SkFontStyle * style,SkString * name)42     void getStyle(int index, SkFontStyle* style, SkString* name) override {
43         if (style) { *style = fTypefaces[index].fStyle; }
44         if (name) { *name = fTypefaces[index].fStyleName; }
45     }
46 
createTypeface(int index)47     SkTypeface* createTypeface(int index) override {
48         return SkRef(fTypefaces[index].fTypeface.get());
49     }
50 
matchStyle(const SkFontStyle & pattern)51     SkTypeface* matchStyle(const SkFontStyle& pattern) override {
52         return this->matchStyleCSS3(pattern);
53     }
54 
getFamilyName()55     SkString getFamilyName() { return fFamilyName; }
56 
57     std::vector<TypefaceEntry> fTypefaces;
58     SkString fFamilyName;
59 };
60 
61 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
62 
63 class FontMgr final : public SkFontMgr {
64 public:
FontMgr()65     FontMgr() {
66         for (const auto& sub : gSubFonts) {
67             sk_sp<SkTestTypeface> typeface =
68                     sk_make_sp<SkTestTypeface>(sk_make_sp<SkTestFont>(sub.fFont), sub.fStyle);
69             bool defaultFamily = false;
70             if (&sub - gSubFonts == gDefaultFontIndex) {
71                 defaultFamily = true;
72                 fDefaultTypeface = typeface;
73             }
74             bool found = false;
75             for (const auto& family : fFamilies) {
76                 if (family->getFamilyName().equals(sub.fFamilyName)) {
77                     family->fTypefaces.emplace_back(std::move(typeface), sub.fStyle, sub.fStyleName);
78                     found = true;
79                     if (defaultFamily) {
80                         fDefaultFamily = family;
81                     }
82                     break;
83                 }
84             }
85             if (!found) {
86                 fFamilies.emplace_back(sk_make_sp<FontStyleSet>(sub.fFamilyName));
87                 // NOLINTNEXTLINE(bugprone-use-after-move)
88                 fFamilies.back()->fTypefaces.emplace_back(std::move(typeface), sub.fStyle, sub.fStyleName);
89                 if (defaultFamily) {
90                     fDefaultFamily = fFamilies.back();
91                 }
92             }
93         }
94         #ifdef SK_XML
95         fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Emoji"));
96         fFamilies.back()->fTypefaces.emplace_back(SkTestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal");
97         #endif
98     }
99 
onCountFamilies() const100     int onCountFamilies() const override { return fFamilies.size(); }
101 
onGetFamilyName(int index,SkString * familyName) const102     void onGetFamilyName(int index, SkString* familyName) const override {
103         *familyName = fFamilies[index]->getFamilyName();
104     }
105 
onCreateStyleSet(int index) const106     SkFontStyleSet* onCreateStyleSet(int index) const override {
107         sk_sp<SkFontStyleSet> ref = fFamilies[index];
108         return ref.release();
109     }
110 
onMatchFamily(const char familyName[]) const111     SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
112         if (familyName) {
113             if (strstr(familyName,  "ono")) { return this->createStyleSet(0); }
114             if (strstr(familyName,  "ans")) { return this->createStyleSet(1); }
115             if (strstr(familyName, "erif")) { return this->createStyleSet(2); }
116             #ifdef SK_XML
117             if (strstr(familyName,  "oji")) { return this->createStyleSet(6); }
118             #endif
119         }
120         return nullptr;
121     }
122 
123 
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const124     SkTypeface* onMatchFamilyStyle(const char familyName[],
125                                    const SkFontStyle& style) const override {
126         sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
127         return styleSet->matchStyle(style);
128     }
129 
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const130     SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
131                                             const SkFontStyle& style,
132                                             const char* bcp47[], int bcp47Count,
133                                             SkUnichar character) const override {
134         (void)bcp47;
135         (void)bcp47Count;
136         (void)character;
137         return this->matchFamilyStyle(familyName, style);
138     }
139 
onMatchFaceStyle(const SkTypeface * tf,const SkFontStyle & style) const140     SkTypeface* onMatchFaceStyle(const SkTypeface* tf,
141                                  const SkFontStyle& style) const override {
142         SkString familyName;
143         tf->getFamilyName(&familyName);
144         return this->matchFamilyStyle(familyName.c_str(), style);
145     }
146 
onMakeFromData(sk_sp<SkData>,int ttcIndex) const147     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override {
148         return nullptr;
149     }
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int ttcIndex) const150     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
151                                             int ttcIndex) const override {
152         return nullptr;
153     }
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &) const154     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
155                                            const SkFontArguments&) const override {
156         return nullptr;
157     }
onMakeFromFontData(std::unique_ptr<SkFontData>) const158     sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const override {
159         return nullptr;
160     }
onMakeFromFile(const char path[],int ttcIndex) const161     sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
162         return nullptr;
163     }
164 
onLegacyMakeTypeface(const char familyName[],SkFontStyle style) const165     sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
166                                            SkFontStyle style) const override {
167         if (familyName == nullptr) {
168             return sk_sp<SkTypeface>(fDefaultFamily->matchStyle(style));
169         }
170         sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
171         if (!typeface) {
172             typeface = fDefaultTypeface;
173         }
174         return typeface;
175     }
176 
177 private:
178     std::vector<sk_sp<FontStyleSet>> fFamilies;
179     sk_sp<FontStyleSet> fDefaultFamily;
180     sk_sp<SkTypeface> fDefaultTypeface;
181 };
182 }
183 
184 namespace sk_tool_utils {
MakePortableFontMgr()185 sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
186 } // namespace sk_tool_utils
187