• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkPathBuilder.h"
9 #include "include/private/base/SkAssert.h"
10 #include "include/utils/SkCustomTypeface.h"
11 #include "src/core/SkFontDescriptor.h"
12 #include "tools/ToolUtils.h"
13 #include "tools/fonts/TestFontMgr.h"
14 #include "tools/fonts/TestTypeface.h"
15 
16 #ifdef SK_XML
17 #include "tools/fonts/TestSVGTypeface.h"
18 #endif
19 
20 #include <vector>
21 
22 namespace {
23 
24 class FontStyleSet final : public SkFontStyleSet {
25 public:
FontStyleSet(const char * familyName)26     FontStyleSet(const char* familyName) : fFamilyName(familyName) {}
27     struct TypefaceEntry {
TypefaceEntry__anon8210e2d40111::FontStyleSet::TypefaceEntry28         TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName)
29                 : fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {}
30         sk_sp<SkTypeface> fTypeface;
31         SkFontStyle       fStyle;
32         const char*       fStyleName;
33     };
34 
count()35     int count() override { return fTypefaces.size(); }
36 
getStyle(int index,SkFontStyle * style,SkString * name)37     void getStyle(int index, SkFontStyle* style, SkString* name) override {
38         if (style) {
39             *style = fTypefaces[index].fStyle;
40         }
41         if (name) {
42             *name = fTypefaces[index].fStyleName;
43         }
44     }
45 
createTypeface(int index)46     SkTypeface* createTypeface(int index) override {
47         return SkRef(fTypefaces[index].fTypeface.get());
48     }
49 
matchStyle(const SkFontStyle & pattern)50     SkTypeface* matchStyle(const SkFontStyle& pattern) override {
51         return this->matchStyleCSS3(pattern);
52     }
53 
getFamilyName()54     SkString getFamilyName() { return fFamilyName; }
55 
56     std::vector<TypefaceEntry> fTypefaces;
57     SkString                   fFamilyName;
58 };
59 
60 class FontMgr final : public SkFontMgr {
61 public:
FontMgr()62     FontMgr() {
63         auto&& list = TestTypeface::Typefaces();
64         for (auto&& family : list.families) {
65             auto&& ss = fFamilies.emplace_back(sk_make_sp<FontStyleSet>(family.name));
66             for (auto&& face : family.faces) {
67                 ss->fTypefaces.emplace_back(face.typeface, face.typeface->fontStyle(), face.name);
68                 if (face.isDefault) {
69                     fDefaultFamily = ss;
70                     fDefaultTypeface = face.typeface;
71                 }
72             }
73         }
74         if (!fDefaultFamily) {
75             SkASSERTF(false, "expected TestTypeface to return a default");
76             fDefaultFamily = fFamilies[0];
77             fDefaultTypeface = fDefaultFamily->fTypefaces[0].fTypeface;
78         }
79 
80 #if defined(SK_ENABLE_SVG)
81         fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Emoji"));
82         fFamilies.back()->fTypefaces.emplace_back(
83                 TestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal");
84 
85         fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Planet"));
86         fFamilies.back()->fTypefaces.emplace_back(
87                 TestSVGTypeface::Planets(), SkFontStyle::Normal(), "Normal");
88 #endif
89     }
90 
onCountFamilies() const91     int onCountFamilies() const override { return fFamilies.size(); }
92 
onGetFamilyName(int index,SkString * familyName) const93     void onGetFamilyName(int index, SkString* familyName) const override {
94         *familyName = fFamilies[index]->getFamilyName();
95     }
96 
onCreateStyleSet(int index) const97     SkFontStyleSet* onCreateStyleSet(int index) const override {
98         sk_sp<SkFontStyleSet> ref = fFamilies[index];
99         return ref.release();
100     }
101 
onMatchFamily(const char familyName[]) const102     SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
103         if (familyName) {
104             if (strstr(familyName, "ono")) {
105                 return this->createStyleSet(0);
106             }
107             if (strstr(familyName, "ans")) {
108                 return this->createStyleSet(1);
109             }
110             if (strstr(familyName, "erif")) {
111                 return this->createStyleSet(2);
112             }
113 #if defined(SK_ENABLE_SVG)
114             if (strstr(familyName, "oji")) {
115                 return this->createStyleSet(6);
116             }
117             if (strstr(familyName, "Planet")) {
118                 return this->createStyleSet(7);
119             }
120 #endif
121         }
122         return nullptr;
123     }
124 
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const125     SkTypeface* onMatchFamilyStyle(const char         familyName[],
126                                    const SkFontStyle& style) const override {
127         sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName));
128         return styleSet->matchStyle(style);
129     }
130 
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const131     SkTypeface* onMatchFamilyStyleCharacter(const char         familyName[],
132                                             const SkFontStyle& style,
133                                             const char*        bcp47[],
134                                             int                bcp47Count,
135                                             SkUnichar          character) const override {
136         (void)bcp47;
137         (void)bcp47Count;
138         (void)character;
139         return this->matchFamilyStyle(familyName, style);
140     }
141 
onMakeFromData(sk_sp<SkData>,int ttcIndex) const142     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override { return nullptr; }
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int ttcIndex) const143     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
144                                             int ttcIndex) const override {
145         return nullptr;
146     }
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &) const147     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
148                                            const SkFontArguments&) const override {
149         return nullptr;
150     }
onMakeFromFile(const char path[],int ttcIndex) const151     sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
152         return nullptr;
153     }
154 
onLegacyMakeTypeface(const char familyName[],SkFontStyle style) const155     sk_sp<SkTypeface> onLegacyMakeTypeface(const char  familyName[],
156                                            SkFontStyle style) const override {
157         if (familyName == nullptr) {
158             return sk_sp<SkTypeface>(fDefaultFamily->matchStyle(style));
159         }
160         sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style));
161         if (!typeface) {
162             typeface = fDefaultTypeface;
163         }
164         return typeface;
165     }
166 
167 private:
168     std::vector<sk_sp<FontStyleSet>> fFamilies;
169     sk_sp<FontStyleSet>              fDefaultFamily;
170     sk_sp<SkTypeface>                fDefaultTypeface;
171 };
172 }  // namespace
173 
174 namespace ToolUtils {
MakePortableFontMgr()175 sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
176 }  // namespace ToolUtils
177