/src/skia/tools/fonts/TestFontMgr.cpp
Line | Count | Source (jump to first uncovered line) |
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: |
26 | 368 | FontStyleSet(const char* familyName) : fFamilyName(familyName) {} |
27 | | struct TypefaceEntry { |
28 | | TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName) |
29 | 1.19k | : fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {} |
30 | | sk_sp<SkTypeface> fTypeface; |
31 | | SkFontStyle fStyle; |
32 | | const char* fStyleName; |
33 | | }; |
34 | | |
35 | 356k | int count() override { return fTypefaces.size(); } |
36 | | |
37 | 1.39M | void getStyle(int index, SkFontStyle* style, SkString* name) override { |
38 | 1.39M | if (style) { |
39 | 1.39M | *style = fTypefaces[index].fStyle; |
40 | 1.39M | } |
41 | 1.39M | if (name) { |
42 | 0 | *name = fTypefaces[index].fStyleName; |
43 | 0 | } |
44 | 1.39M | } |
45 | | |
46 | 356k | sk_sp<SkTypeface> createTypeface(int index) override { |
47 | 356k | return fTypefaces[index].fTypeface; |
48 | 356k | } |
49 | | |
50 | 349k | sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override { |
51 | 349k | return this->matchStyleCSS3(pattern); |
52 | 349k | } |
53 | | |
54 | 0 | SkString getFamilyName() { return fFamilyName; } |
55 | | |
56 | | std::vector<TypefaceEntry> fTypefaces; |
57 | | SkString fFamilyName; |
58 | | }; |
59 | | |
60 | | class FontMgr final : public SkFontMgr { |
61 | | public: |
62 | 46 | FontMgr() { |
63 | 46 | auto&& list = TestTypeface::Typefaces(); |
64 | 276 | for (auto&& family : list.families) { |
65 | 276 | auto&& ss = fFamilies.emplace_back(sk_make_sp<FontStyleSet>(family.name)); |
66 | 1.10k | for (auto&& face : family.faces) { |
67 | 1.10k | ss->fTypefaces.emplace_back(face.typeface, face.typeface->fontStyle(), face.name); |
68 | 1.10k | if (face.isDefault) { |
69 | 46 | fDefaultFamily = ss; |
70 | 46 | fDefaultTypeface = face.typeface; |
71 | 46 | } |
72 | 1.10k | } |
73 | 276 | } |
74 | 46 | if (!fDefaultFamily) { |
75 | 0 | SkASSERTF(false, "expected TestTypeface to return a default"); |
76 | 0 | fDefaultFamily = fFamilies[0]; |
77 | 0 | fDefaultTypeface = fDefaultFamily->fTypefaces[0].fTypeface; |
78 | 0 | } |
79 | | |
80 | 46 | #if defined(SK_ENABLE_SVG) |
81 | 46 | fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Emoji")); |
82 | 46 | fFamilies.back()->fTypefaces.emplace_back( |
83 | 46 | TestSVGTypeface::Default(), SkFontStyle::Normal(), "Normal"); |
84 | | |
85 | 46 | fFamilies.emplace_back(sk_make_sp<FontStyleSet>("Planet")); |
86 | 46 | fFamilies.back()->fTypefaces.emplace_back( |
87 | 46 | TestSVGTypeface::Planets(), SkFontStyle::Normal(), "Normal"); |
88 | 46 | #endif |
89 | 46 | } |
90 | | |
91 | 7.23k | int onCountFamilies() const override { return fFamilies.size(); } |
92 | | |
93 | 0 | void onGetFamilyName(int index, SkString* familyName) const override { |
94 | 0 | *familyName = fFamilies[index]->getFamilyName(); |
95 | 0 | } |
96 | | |
97 | 7.46k | sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override { |
98 | 7.46k | sk_sp<SkFontStyleSet> ref = fFamilies[index]; |
99 | 7.46k | return ref; |
100 | 7.46k | } |
101 | | |
102 | 261 | sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override { |
103 | 261 | if (familyName) { |
104 | 238 | if (strstr(familyName, "ono")) { |
105 | 0 | return this->createStyleSet(0); |
106 | 0 | } |
107 | 238 | if (strstr(familyName, "ans")) { |
108 | 233 | return this->createStyleSet(1); |
109 | 233 | } |
110 | 5 | if (strstr(familyName, "erif")) { |
111 | 0 | return this->createStyleSet(2); |
112 | 0 | } |
113 | 5 | #if defined(SK_ENABLE_SVG) |
114 | 5 | if (strstr(familyName, "oji")) { |
115 | 0 | return this->createStyleSet(6); |
116 | 0 | } |
117 | 5 | if (strstr(familyName, "Planet")) { |
118 | 0 | return this->createStyleSet(7); |
119 | 0 | } |
120 | 5 | #endif |
121 | 5 | } |
122 | 28 | return nullptr; |
123 | 261 | } |
124 | | |
125 | | sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[], |
126 | 261 | const SkFontStyle& style) const override { |
127 | 261 | sk_sp<SkFontStyleSet> styleSet(this->matchFamily(familyName)); |
128 | 261 | return styleSet->matchStyle(style); |
129 | 261 | } |
130 | | |
131 | | sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[], |
132 | | const SkFontStyle& style, |
133 | | const char* bcp47[], |
134 | | int bcp47Count, |
135 | 23 | SkUnichar character) const override { |
136 | 23 | (void)bcp47; |
137 | 23 | (void)bcp47Count; |
138 | 23 | (void)character; |
139 | 23 | return this->matchFamilyStyle(familyName, style); |
140 | 23 | } |
141 | | |
142 | 0 | sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override { return nullptr; } |
143 | | sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, |
144 | 0 | int ttcIndex) const override { |
145 | 0 | return nullptr; |
146 | 0 | } |
147 | | sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, |
148 | 0 | const SkFontArguments&) const override { |
149 | 0 | return nullptr; |
150 | 0 | } |
151 | 0 | sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override { |
152 | 0 | return nullptr; |
153 | 0 | } |
154 | | |
155 | | sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], |
156 | 349k | SkFontStyle style) const override { |
157 | 349k | if (familyName == nullptr) { |
158 | 348k | return sk_sp<SkTypeface>(fDefaultFamily->matchStyle(style)); |
159 | 348k | } |
160 | 238 | sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(this->matchFamilyStyle(familyName, style)); |
161 | 238 | if (!typeface) { |
162 | 5 | typeface = fDefaultTypeface; |
163 | 5 | } |
164 | 238 | return typeface; |
165 | 349k | } |
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 { |
175 | 46 | sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); } |
176 | | } // namespace ToolUtils |