Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
TestFontMgr.cpp
Go to the documentation of this file.
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
12#include "tools/ToolUtils.h"
15
16#ifdef SK_XML
18#endif
19
20#include <vector>
21
22namespace {
23
24class FontStyleSet final : public SkFontStyleSet {
25public:
26 FontStyleSet(const char* familyName) : fFamilyName(familyName) {}
27 struct TypefaceEntry {
28 TypefaceEntry(sk_sp<SkTypeface> typeface, SkFontStyle style, const char* styleName)
29 : fTypeface(std::move(typeface)), fStyle(style), fStyleName(styleName) {}
30 sk_sp<SkTypeface> fTypeface;
32 const char* fStyleName;
33 };
34
35 int count() override { return fTypefaces.size(); }
36
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
46 sk_sp<SkTypeface> createTypeface(int index) override {
47 return fTypefaces[index].fTypeface;
48 }
49
50 sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override {
51 return this->matchStyleCSS3(pattern);
52 }
53
54 SkString getFamilyName() { return fFamilyName; }
55
56 std::vector<TypefaceEntry> fTypefaces;
57 SkString fFamilyName;
58};
59
60class FontMgr final : public SkFontMgr {
61public:
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
91 int onCountFamilies() const override { return fFamilies.size(); }
92
93 void onGetFamilyName(int index, SkString* familyName) const override {
94 *familyName = fFamilies[index]->getFamilyName();
95 }
96
97 sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override {
98 sk_sp<SkFontStyleSet> ref = fFamilies[index];
99 return ref;
100 }
101
102 sk_sp<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
125 sk_sp<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
131 sk_sp<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
142 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override { return nullptr; }
143 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
144 int ttcIndex) const override {
145 return nullptr;
146 }
147 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
148 const SkFontArguments&) const override {
149 return nullptr;
150 }
151 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
152 return nullptr;
153 }
154
155 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
167private:
168 std::vector<sk_sp<FontStyleSet>> fFamilies;
169 sk_sp<FontStyleSet> fDefaultFamily;
170 sk_sp<SkTypeface> fDefaultTypeface;
171};
172} // namespace
173
174namespace ToolUtils {
175sk_sp<SkFontMgr> MakePortableFontMgr() { return sk_make_sp<FontMgr>(); }
176} // namespace ToolUtils
const Face faces[]
Definition 3DSlide.cpp:137
SkStrokeRec::Style fStyle
#define SkASSERTF(cond, fmt,...)
Definition SkAssert.h:117
int32_t SkUnichar
Definition SkTypes.h:175
sk_sp< SkFontStyleSet > createStyleSet(int index) const
virtual void onGetFamilyName(int index, SkString *familyName) const =0
sk_sp< SkFontStyleSet > matchFamily(const char familyName[]) const
virtual sk_sp< SkTypeface > onMakeFromData(sk_sp< SkData >, int ttcIndex) const =0
virtual sk_sp< SkFontStyleSet > onCreateStyleSet(int index) const =0
virtual sk_sp< SkTypeface > onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle &, const char *bcp47[], int bcp47Count, SkUnichar character) const =0
virtual sk_sp< SkFontStyleSet > onMatchFamily(const char familyName[]) const =0
virtual sk_sp< SkTypeface > onMakeFromStreamIndex(std::unique_ptr< SkStreamAsset >, int ttcIndex) const =0
virtual sk_sp< SkTypeface > onMakeFromStreamArgs(std::unique_ptr< SkStreamAsset >, const SkFontArguments &) const =0
sk_sp< SkTypeface > matchFamilyStyle(const char familyName[], const SkFontStyle &) const
virtual sk_sp< SkTypeface > onMatchFamilyStyle(const char familyName[], const SkFontStyle &) const =0
virtual sk_sp< SkTypeface > onLegacyMakeTypeface(const char familyName[], SkFontStyle) const =0
virtual sk_sp< SkTypeface > onMakeFromFile(const char path[], int ttcIndex) const =0
virtual int onCountFamilies() const =0
sk_sp< SkTypeface > matchStyleCSS3(const SkFontStyle &pattern)
virtual sk_sp< SkTypeface > createTypeface(int index)=0
virtual sk_sp< SkTypeface > matchStyle(const SkFontStyle &pattern)=0
virtual int count()=0
virtual void getStyle(int index, SkFontStyle *, SkString *style)=0
static constexpr SkFontStyle Normal()
Definition SkFontStyle.h:66
void ref() const
Definition SkRefCnt.h:62
static const List & Typefaces()
const char * name
Definition fuchsia.cc:50
Definition ref_ptr.h:256