Debian Codegraph @

modules/skparagraph/src/FontCollection.cpp

raw
1 // Copyright 2019 Google LLC.
2 #include "modules/skparagraph/include/FontCollection.h"
3
4 #include "include/core/SkTypeface.h"
5 #include "modules/skparagraph/include/Paragraph.h"
6 #include "modules/skparagraph/src/ParagraphImpl.h"
7 #include "modules/skshaper/include/SkShaper_harfbuzz.h"
8
9 namespace {
10 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
11 const char* kColorEmojiFontMac = "Apple Color Emoji";
12 #else
13 const char* kColorEmojiLocale = "und-Zsye";
14 #endif
15 }
16 namespace skia {
17 namespace textlayout {
18
19 bool FontCollection::FamilyKey::operator==(const FontCollection::FamilyKey& other) const {
20 return fFamilyNames == other.fFamilyNames &&
21 fFontStyle == other.fFontStyle &&
22 fFontArguments == other.fFontArguments;
23 }
24
25 size_t FontCollection::FamilyKey::Hasher::operator()(const FontCollection::FamilyKey& key) const {
26 size_t hash = 0;
27 for (const SkString& family : key.fFamilyNames) {
28 hash ^= std::hash<std::string>()(family.c_str());
29 }
30 return hash ^
31 std::hash<uint32_t>()(key.fFontStyle.weight()) ^
32 std::hash<uint32_t>()(key.fFontStyle.slant()) ^
33 std::hash<std::optional<FontArguments>>()(key.fFontArguments);
34 }
35
36 FontCollection::FontCollection()
37 : fEnableFontFallback(true)
38 , fDefaultFamilyNames({SkString(DEFAULT_FONT_FAMILY)}) { }
39
40 size_t FontCollection::getFontManagersCount() const { return this->getFontManagerOrder().size(); }
41
42 void FontCollection::setAssetFontManager(sk_sp<SkFontMgr> font_manager) {
43 fAssetFontManager = std::move(font_manager);
44 }
45
46 void FontCollection::setDynamicFontManager(sk_sp<SkFontMgr> font_manager) {
47 fDynamicFontManager = std::move(font_manager);
48 }
49
50 void FontCollection::setTestFontManager(sk_sp<SkFontMgr> font_manager) {
51 fTestFontManager = std::move(font_manager);
52 }
53
54 void FontCollection::setDefaultFontManager(sk_sp<SkFontMgr> fontManager,
55 const char defaultFamilyName[]) {
56 fDefaultFontManager = std::move(fontManager);
57 fDefaultFamilyNames.emplace_back(defaultFamilyName);
58 }
59
60 void FontCollection::setDefaultFontManager(sk_sp<SkFontMgr> fontManager,
61 const std::vector<SkString>& defaultFamilyNames) {
62 fDefaultFontManager = std::move(fontManager);
63 fDefaultFamilyNames = defaultFamilyNames;
64 }
65
66 void FontCollection::setDefaultFontManager(sk_sp<SkFontMgr> fontManager) {
67 fDefaultFontManager = std::move(fontManager);
68 }
69
70 // Return the available font managers in the order they should be queried.
71 std::vector<sk_sp<SkFontMgr>> FontCollection::getFontManagerOrder() const {
72 std::vector<sk_sp<SkFontMgr>> order;
73 if (fDynamicFontManager) {
74 order.push_back(fDynamicFontManager);
75 }
76 if (fAssetFontManager) {
77 order.push_back(fAssetFontManager);
78 }
79 if (fTestFontManager) {
80 order.push_back(fTestFontManager);
81 }
82 if (fDefaultFontManager && fEnableFontFallback) {
83 order.push_back(fDefaultFontManager);
84 }
85 return order;
86 }
87
88 std::vector<sk_sp<SkTypeface>> FontCollection::findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle) {
89 return findTypefaces(familyNames, fontStyle, std::nullopt);
90 }
91
92 std::vector<sk_sp<SkTypeface>> FontCollection::findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle, const std::optional<FontArguments>& fontArgs) {
93 // Look inside the font collections cache first
94 FamilyKey familyKey(familyNames, fontStyle, fontArgs);
95 auto found = fTypefaces.find(familyKey);
96 if (found) {
97 return *found;
98 }
99
100 std::vector<sk_sp<SkTypeface>> typefaces;
101 for (const SkString& familyName : familyNames) {
102 sk_sp<SkTypeface> match = matchTypeface(familyName, fontStyle);
103 if (match && fontArgs) {
104 match = fontArgs->CloneTypeface(match);
105 }
106 if (match) {
107 typefaces.emplace_back(std::move(match));
108 }
109 }
110
111 if (typefaces.empty()) {
112 sk_sp<SkTypeface> match;
113 for (const SkString& familyName : fDefaultFamilyNames) {
114 match = matchTypeface(familyName, fontStyle);
115 if (match) {
116 break;
117 }
118 }
119 if (!match) {
120 for (const auto& manager : this->getFontManagerOrder()) {
121 match = manager->legacyMakeTypeface(nullptr, fontStyle);
122 if (match) {
123 break;
124 }
125 }
126 }
127 if (match) {
128 if (fontArgs) {
129 match = fontArgs->CloneTypeface(match);
130 }
131 typefaces.emplace_back(std::move(match));
132 }
133 }
134
135 fTypefaces.set(familyKey, typefaces);
136 return typefaces;
137 }
138
139 sk_sp<SkTypeface> FontCollection::matchTypeface(const SkString& familyName, SkFontStyle fontStyle) {
140 for (const auto& manager : this->getFontManagerOrder()) {
141 sk_sp<SkFontStyleSet> set(manager->matchFamily(familyName.c_str()));
142 if (!set || set->count() == 0) {
143 continue;
144 }
145
146 sk_sp<SkTypeface> match(set->matchStyle(fontStyle));
147 if (match) {
148 return match;
149 }
150 }
151
152 return nullptr;
153 }
154
155 // Find ANY font in available font managers that resolves the unicode codepoint
156 sk_sp<SkTypeface> FontCollection::defaultFallback(SkUnichar unicode,
157 const std::vector<SkString>& families,
158 SkFontStyle fontStyle,
159 const SkString& locale,
160 const std::optional<FontArguments>& fontArgs) {
161
162 for (const auto& manager : this->getFontManagerOrder()) {
163 std::vector<const char*> bcp47;
164 if (!locale.isEmpty()) {
165 bcp47.push_back(locale.c_str());
166 }
167 const char* familyName = families.empty() ? nullptr : families[0].c_str();
168 sk_sp<SkTypeface> typeface(manager->matchFamilyStyleCharacter(
169 familyName, fontStyle, bcp47.data(), bcp47.size(), unicode));
170
171 if (typeface != nullptr) {
172 if (fontArgs) {
173 typeface = fontArgs->CloneTypeface(typeface);
174 }
175 return typeface;
176 }
177 }
178 return nullptr;
179 }
180
181 // Find ANY font in available font managers that resolves this emojiStart
182 sk_sp<SkTypeface> FontCollection::defaultEmojiFallback(SkUnichar emojiStart,
183 SkFontStyle fontStyle,
184 const SkString& locale) {
185
186 for (const auto& manager : this->getFontManagerOrder()) {
187 std::vector<const char*> bcp47;
188 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
189 sk_sp<SkTypeface> emojiTypeface =
190 fDefaultFontManager->matchFamilyStyle(kColorEmojiFontMac, SkFontStyle());
191 if (emojiTypeface != nullptr) {
192 return emojiTypeface;
193 }
194 #else
195 bcp47.push_back(kColorEmojiLocale);
196 #endif
197 if (!locale.isEmpty()) {
198 bcp47.push_back(locale.c_str());
199 }
200
201 // Not really ideal since the first codepoint may not be the best one
202 // but we start from a good colored emoji at least
203 sk_sp<SkTypeface> typeface(manager->matchFamilyStyleCharacter(
204 nullptr, fontStyle, bcp47.data(), bcp47.size(), emojiStart));
205 if (typeface != nullptr) {
206 // ... and stop as soon as we find something in hope it will work for all of them
207 return typeface;
208 }
209 }
210 return nullptr;
211 }
212
213 sk_sp<SkTypeface> FontCollection::defaultFallback() {
214 if (fDefaultFontManager == nullptr) {
215 return nullptr;
216 }
217 for (const SkString& familyName : fDefaultFamilyNames) {
218 sk_sp<SkTypeface> match = fDefaultFontManager->matchFamilyStyle(familyName.c_str(),
219 SkFontStyle());
220 if (match) {
221 return match;
222 }
223 }
224 return nullptr;
225 }
226
227 void FontCollection::disableFontFallback() { fEnableFontFallback = false; }
228 void FontCollection::enableFontFallback() { fEnableFontFallback = true; }
229
230 void FontCollection::clearCaches() {
231 fParagraphCache.reset();
232 fTypefaces.reset();
233 SkShapers::HB::PurgeCaches();
234 }
235
236 } // namespace textlayout
237 } // namespace skia