1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "font_collection.h"
17
18 #include "texgine/dynamic_font_provider.h"
19 #include "texgine/system_font_provider.h"
20 #include "texgine/theme_font_provider.h"
21 #include "texgine/typography_types.h"
22 #include "texgine/utils/exlog.h"
23 #include "texgine_string.h"
24 #include "texgine_font.h"
25 #include "utils/log.h"
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace TextEngine {
30 #define MAX_WIDTH 10
31 #define MAX_SLANT 2
32 #define MAX_WEIGHT 10
33 #define FIRST_PRIORITY 10000
34 #define SECOND_PRIORITY 100
35 #define MULTIPLE 100
36 #define SUPPORTFILE 1
FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> && fontStyleSets)37 FontCollection::FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> &&fontStyleSets)
38 : fontStyleSets_(fontStyleSets)
39 {
40 FillDefaultItalicSupportFile();
41 FillDefaultChinesePointUnicode();
42 }
43
SortTypeface(FontStyles & style) const44 void FontCollection::SortTypeface(FontStyles &style) const
45 {
46 std::vector<std::shared_ptr<TexgineTypeface>> typefaces;
47 for (const auto &fontStyleSet : fontStyleSets_) {
48 if (fontStyleSet == nullptr) {
49 continue;
50 }
51 for (int i = 0; i < fontStyleSet->Count(); i++) {
52 typefaces.push_back(fontStyleSet->CreateTypeface(i));
53 }
54 }
55
56 std::sort(typefaces.begin(), typefaces.end(),
57 [](std::shared_ptr<TexgineTypeface> &ty1, const std::shared_ptr<TexgineTypeface> &ty2) {
58 auto fs1 = ty1->GetFontStyle()->GetFontStyle();
59 auto fs2 = ty2->GetFontStyle()->GetFontStyle();
60 #ifndef USE_ROSEN_DRAWING
61 return (fs1->weight() != fs2->weight()) ? fs1->weight() < fs2->weight() : fs1->slant() < fs2->slant();
62 #else
63 return (fs1->GetWeight() != fs2->GetWeight()) ? fs1->GetWeight() < fs2->GetWeight()
64 : fs1->GetSlant() < fs2->GetSlant();
65 #endif
66 }
67 );
68
69 std::vector<int> weights;
70 for (auto ty : typefaces) {
71 #ifndef USE_ROSEN_DRAWING
72 auto weight = ty->GetFontStyle()->GetFontStyle()->weight() / MULTIPLE;
73 #else
74 auto weight = ty->GetFontStyle()->GetFontStyle()->GetWeight() / MULTIPLE;
75 #endif
76 weights.push_back(weight);
77 }
78
79 for (size_t i = 0; i < weights.size(); i++) {
80 if (weights[i] == style.GetWeight()) {
81 break;
82 }
83 if (weights[i] > style.GetWeight()) {
84 if (i > 0) {
85 style.SetWeight(weights[i - 1]);
86 break;
87 }
88 }
89 }
90 }
91
FillDefaultItalicSupportFile()92 void FontCollection::FillDefaultItalicSupportFile()
93 {
94 supportScript_.insert(std::make_pair("Latn", SUPPORTFILE)); // Latn means Latin Language
95 supportScript_.insert(std::make_pair("Zyyy", SUPPORTFILE)); // Zyyy means department of mathematics
96 }
97
FillDefaultChinesePointUnicode()98 void FontCollection::FillDefaultChinesePointUnicode()
99 {
100 chinesePointUnicode_.insert(std::make_pair(0xFF0C, SUPPORTFILE)); // 0xFF0C is ,
101 chinesePointUnicode_.insert(std::make_pair(0x3002, SUPPORTFILE)); // 0x3002 is 。
102 chinesePointUnicode_.insert(std::make_pair(0xFF01, SUPPORTFILE)); // 0xFF01 is !
103 chinesePointUnicode_.insert(std::make_pair(0xFF1B, SUPPORTFILE)); // 0xFF1B is ;
104 chinesePointUnicode_.insert(std::make_pair(0x3001, SUPPORTFILE)); // 0x3001 is 、
105 chinesePointUnicode_.insert(std::make_pair(0xFFE5, SUPPORTFILE)); // 0xFFE5 is ¥
106 chinesePointUnicode_.insert(std::make_pair(0xFF08, SUPPORTFILE)); // 0xFF08 is (
107 chinesePointUnicode_.insert(std::make_pair(0xFF09, SUPPORTFILE)); // 0xFF09 is )
108 chinesePointUnicode_.insert(std::make_pair(0x300A, SUPPORTFILE)); // 0x300A is 《
109 chinesePointUnicode_.insert(std::make_pair(0x300B, SUPPORTFILE)); // 0x300B is 》
110 chinesePointUnicode_.insert(std::make_pair(0x3010, SUPPORTFILE)); // 0x3010 is 【
111 chinesePointUnicode_.insert(std::make_pair(0x3011, SUPPORTFILE)); // 0x3011 is 】
112 chinesePointUnicode_.insert(std::make_pair(0xFF1F, SUPPORTFILE)); // 0xFF1F is ?
113 chinesePointUnicode_.insert(std::make_pair(0xFF1A, SUPPORTFILE)); // 0xFF1A is :
114 }
115
DetectionScript(const std::string script) const116 int FontCollection::DetectionScript(const std::string script) const
117 {
118 return supportScript_.find(script)->second;
119 }
120
DetectChinesePointUnicode(uint32_t ch) const121 int FontCollection::DetectChinesePointUnicode(uint32_t ch) const
122 {
123 return chinesePointUnicode_.find(ch)->second;
124 }
125
GetTypefaceForChar(const uint32_t & ch,FontStyles & style,const std::string & script,const std::string & locale) const126 std::shared_ptr<Typeface> FontCollection::GetTypefaceForChar(const uint32_t &ch, FontStyles &style,
127 const std::string &script, const std::string &locale) const
128 {
129 SortTypeface(style);
130 auto fs = std::make_shared<TexgineFontStyle>();
131 *fs = style.ToTexgineFontStyle();
132 for (const auto &fontStyleSet : fontStyleSets_) {
133 std::shared_ptr<Typeface> typeface = nullptr;
134 struct TypefaceCacheKey key = {.fss = fontStyleSet, .fs = style};
135 if (auto it = typefaceCache_.find(key); it != typefaceCache_.end()) {
136 typeface = it->second;
137 } else {
138 if (fontStyleSet == nullptr) {
139 continue;
140 }
141 auto texgineTypeface = fontStyleSet->MatchStyle(fs);
142 if (texgineTypeface == nullptr || texgineTypeface->GetTypeface() == nullptr) {
143 continue;
144 }
145 typeface = std::make_shared<Typeface>(texgineTypeface);
146 typefaceCache_[key] = typeface;
147 }
148
149 if (typeface->Has(ch)) {
150 typeface->ComputeFakeryItalic(style.GetFontStyle());
151 typeface->ComputeFakery(style.GetWeight());
152 return typeface;
153 }
154 }
155 auto typeface = FindThemeTypeface(style);
156 if (typeface) {
157 typeface->ComputeFakery(style.GetWeight());
158 typeface->ComputeFakeryItalic(style.GetFontStyle());
159 }
160 if (typeface == nullptr) {
161 typeface = FindFallBackTypeface(ch, style, script, locale);
162 }
163 if (typeface == nullptr) {
164 typeface = GetTypefaceForFontStyles(style, script, locale);
165 }
166
167 return typeface;
168 }
169
FindThemeTypeface(const FontStyles & style) const170 std::shared_ptr<Typeface> FontCollection::FindThemeTypeface(const FontStyles &style) const
171 {
172 std::shared_ptr<VariantFontStyleSet> styleSet = ThemeFontProvider::GetInstance()->MatchFamily("");
173 if (styleSet != nullptr) {
174 auto fs = std::make_shared<TexgineFontStyle>();
175 *fs = style.ToTexgineFontStyle();
176 auto texgineTypeface = styleSet->MatchStyle(fs);
177 if (texgineTypeface != nullptr && texgineTypeface->GetTypeface() != nullptr) {
178 return std::make_shared<Typeface>(texgineTypeface);
179 }
180 }
181 return nullptr;
182 }
183
GetTypefaceForFontStyles(const FontStyles & style,const std::string & script,const std::string & locale) const184 std::shared_ptr<Typeface> FontCollection::GetTypefaceForFontStyles(const FontStyles &style,
185 const std::string &script, const std::string &locale) const
186 {
187 auto providingStyle = style.ToTexgineFontStyle();
188
189 int bestScore = 0;
190 int bestIndex = 0;
191 std::shared_ptr<VariantFontStyleSet> bestFontStyleSet = nullptr;
192 for (auto &fontStyleSet : fontStyleSets_) {
193 auto count = fontStyleSet->Count();
194 for (auto i = 0; i < count; i++) {
195 auto matchingStyle = std::make_shared<TexgineFontStyle>();
196 auto styleName = std::make_shared<TexgineString>();
197 fontStyleSet->GetStyle(i, matchingStyle, styleName);
198
199 int score = 0;
200 #ifndef USE_ROSEN_DRAWING
201 score += (MAX_WIDTH - std::abs(providingStyle.GetFontStyle()->width() -
202 matchingStyle->GetFontStyle()->width())) * SECOND_PRIORITY;
203 score += (MAX_SLANT - std::abs(providingStyle.GetFontStyle()->slant() -
204 matchingStyle->GetFontStyle()->slant())) * FIRST_PRIORITY;
205 score += (MAX_WEIGHT - std::abs(providingStyle.GetFontStyle()->weight() / MULTIPLE -
206 matchingStyle->GetFontStyle()->weight() / MULTIPLE));
207 #else
208 score += (MAX_WIDTH - std::abs(providingStyle.GetFontStyle()->GetWidth() -
209 matchingStyle->GetFontStyle()->GetWidth())) * SECOND_PRIORITY;
210 score += (MAX_SLANT - std::abs(providingStyle.GetFontStyle()->GetSlant() -
211 matchingStyle->GetFontStyle()->GetSlant())) * FIRST_PRIORITY;
212 score += (MAX_WEIGHT - std::abs(providingStyle.GetFontStyle()->GetWeight() / MULTIPLE -
213 matchingStyle->GetFontStyle()->GetWeight() / MULTIPLE));
214 #endif
215 if (score > bestScore) {
216 bestScore = score;
217 bestIndex = i;
218 bestFontStyleSet = fontStyleSet;
219 }
220 }
221 }
222
223 if (bestFontStyleSet != nullptr &&
224 (bestFontStyleSet->TryToTexgineFontStyleSet() != nullptr || bestFontStyleSet->TryToDynamicFontStyleSet())) {
225 auto ttf = bestFontStyleSet->CreateTypeface(bestIndex);
226 return std::make_shared<Typeface>(ttf);
227 }
228
229 return FindFallBackTypeface(' ', style, script, locale);
230 }
231
FindFallBackTypeface(const uint32_t & ch,const FontStyles & style,const std::string & script,const std::string & locale) const232 std::shared_ptr<Typeface> FontCollection::FindFallBackTypeface(const uint32_t &ch, const FontStyles &style,
233 const std::string &script, const std::string &locale) const
234 {
235 if (!enableFallback_) {
236 return nullptr;
237 }
238 // fallback cache
239 struct FallbackCacheKey key = {.script = script, .locale = locale, .fs = style};
240 if (auto it = fallbackCache_.find(key); it != fallbackCache_.end() && it->second->Has(ch)) {
241 return it->second;
242 }
243
244 // fallback
245 std::vector<const char *> bcp47;
246 if (!script.empty()) {
247 bcp47.push_back(script.data());
248 } else if (!locale.empty()) {
249 bcp47.push_back(locale.data());
250 }
251
252 auto fm = TexgineFontManager::RefDefault();
253 if (fm == nullptr) {
254 return nullptr;
255 }
256 TexgineFontStyle tfs = style.ToTexgineFontStyle();
257 if (style.GetFontStyle()) {
258 std::shared_ptr<TexgineTypeface> fallbackTypeface = nullptr;
259 std::shared_ptr<Typeface> typeface = nullptr;
260 if (DetectionScript(script) == SUPPORTFILE && DetectChinesePointUnicode(ch) != SUPPORTFILE) {
261 fallbackTypeface = fm->MatchFamilyStyle("", tfs);
262 if (fallbackTypeface == nullptr || fallbackTypeface->GetTypeface() == nullptr) {
263 LOGE("No have fallbackTypeface from matchFamilyStyle");
264 return nullptr;
265 }
266 typeface = std::make_shared<Typeface>(fallbackTypeface);
267 typeface->ComputeFakeryItalic(false); // false means value method for obtaining italics
268 } else {
269 fallbackTypeface = fm->MatchFamilyStyleCharacter("", tfs, bcp47.data(), bcp47.size(), ch);
270 if (fallbackTypeface == nullptr || fallbackTypeface->GetTypeface() == nullptr) {
271 LOGE("No have fallbackTypeface from matchFamilyStyleCharacter");
272 return nullptr;
273 }
274 typeface = std::make_shared<Typeface>(fallbackTypeface);
275 typeface->ComputeFakeryItalic(true); // true means text offset produces italic method
276 }
277
278 return typeface;
279 }
280 std::shared_ptr<TexgineTypeface> fallbackTypeface = fm->MatchFamilyStyleCharacter("", tfs,
281 bcp47.data(), bcp47.size(), ch);
282 if (fallbackTypeface == nullptr || fallbackTypeface->GetTypeface() == nullptr) {
283 return nullptr;
284 }
285 auto typeface = std::make_shared<Typeface>(fallbackTypeface);
286 return typeface;
287 }
288
DisableFallback()289 void FontCollection::DisableFallback()
290 {
291 enableFallback_ = false;
292 }
293 } // namespace TextEngine
294 } // namespace Rosen
295 } // namespace OHOS
296