1 /*
2 * Copyright (c) 2024 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_descriptor_cache.h"
17
18 #include <algorithm>
19 #include <cstdint>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <fstream>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include "font_config.h"
27 #include "text/common_utils.h"
28 #include "text/font_style.h"
29 #include "text/font_mgr.h"
30 #include "utils/text_log.h"
31 #include "utils/text_trace.h"
32
33 #define INSTALL_FONT_CONFIG_FILE "/data/service/el1/public/for-all-app/fonts/install_fontconfig.json"
34
35 namespace OHOS::Rosen {
36 namespace {
37 constexpr uint32_t WEIGHT_400 = 400;
38 constexpr int SPECIAL_WEIGHT_DIFF = 50;
39 constexpr int WEIGHT_MODULE = 100;
40 }
41
FontDescriptorCache()42 FontDescriptorCache::FontDescriptorCache() {}
43
~FontDescriptorCache()44 FontDescriptorCache::~FontDescriptorCache() {}
45
ClearFontFileCache()46 void FontDescriptorCache::ClearFontFileCache()
47 {
48 allFontDescriptor_.clear();
49 fontFamilyMap_.clear();
50 fullNameMap_.clear();
51 postScriptNameMap_.clear();
52 fontSubfamilyNameMap_.clear();
53 boldCache_.clear();
54 italicCache_.clear();
55 monoSpaceCache_.clear();
56 symbolicCache_.clear();
57 stylishFullNameMap_.clear();
58 dynamicFullNameMap_.clear();
59 }
60
ParserSystemFonts()61 void FontDescriptorCache::ParserSystemFonts()
62 {
63 std::lock_guard guard(mutex_);
64 // System fonts have already been parsed
65 if (!fullNameMap_.empty()) {
66 return;
67 }
68 for (auto& item : parser_.GetSystemFonts()) {
69 FontDescriptorScatter(item);
70 }
71 Dump();
72 }
73
ParserStylishFonts()74 void FontDescriptorCache::ParserStylishFonts()
75 {
76 std::lock_guard guard(mutex_);
77 // Stylish fonts have already been parsed
78 if (!stylishFullNameMap_.empty()) {
79 return;
80 }
81 std::vector<TextEngine::FontParser::FontDescriptor> descriptors = parser_.GetVisibilityFonts(TextEngine::ENGLISH);
82 for (const auto& descriptor : descriptors) {
83 FontDescSharedPtr descriptorPtr = std::make_shared<TextEngine::FontParser::FontDescriptor>(descriptor);
84 descriptorPtr->weight = WeightAlignment(descriptorPtr->weight);
85 stylishFullNameMap_[descriptorPtr->fullName].emplace(descriptorPtr);
86 }
87 }
88
ParserFontsByFontType(int32_t fontType)89 void FontDescriptorCache::ParserFontsByFontType(int32_t fontType)
90 {
91 if (static_cast<uint32_t>(fontType) & TextEngine::FontParser::SystemFontType::GENERIC) {
92 ParserSystemFonts();
93 }
94 if (static_cast<uint32_t>(fontType) & TextEngine::FontParser::SystemFontType::STYLISH) {
95 ParserStylishFonts();
96 }
97 }
98
FontDescriptorScatter(FontDescSharedPtr desc)99 void FontDescriptorCache::FontDescriptorScatter(FontDescSharedPtr desc)
100 {
101 auto ret = allFontDescriptor_.emplace(desc);
102 if (!ret.second) {
103 return;
104 }
105
106 auto handleMapScatter = [desc](auto& map, const auto& key) {
107 map[key].emplace(desc);
108 };
109
110 handleMapScatter(fontFamilyMap_, desc->fontFamily);
111 handleMapScatter(fullNameMap_, desc->fullName);
112 handleMapScatter(postScriptNameMap_, desc->postScriptName);
113 handleMapScatter(fontSubfamilyNameMap_, desc->fontSubfamily);
114
115 desc->weight = WeightAlignment(desc->weight);
116 if (static_cast<uint32_t>(desc->weight) > WEIGHT_400) {
117 boldCache_.emplace(desc);
118 }
119
120 if (desc->italic != 0) {
121 italicCache_.emplace(desc);
122 }
123
124 if (desc->monoSpace) {
125 monoSpaceCache_.emplace(desc);
126 }
127
128 if (desc->symbolic) {
129 symbolicCache_.emplace(desc);
130 }
131 }
132
ParserInstallFontsPathList(std::vector<std::string> & fontPathList)133 bool FontDescriptorCache::ParserInstallFontsPathList(std::vector<std::string>& fontPathList)
134 {
135 std::shared_ptr<Drawing::FontMgr> fontMgr = Drawing::FontMgr::CreateDynamicFontMgr();
136 if (fontMgr == nullptr) {
137 return false;
138 }
139 int ret = fontMgr->ParseInstallFontConfig(INSTALL_FONT_CONFIG_FILE, fontPathList);
140 return ret == Drawing::FontCheckCode::SUCCESSED;
141 }
142
ParserInstallFontsPathList(TextEngine::FullNameToPath & fontPathList)143 bool FontDescriptorCache::ParserInstallFontsPathList(TextEngine::FullNameToPath& fontPathList)
144 {
145 return TextEngine::FontConfigJson::ParseInstallConfig(INSTALL_FONT_CONFIG_FILE, fontPathList) == 0;
146 }
147
GetInstallFontList()148 std::unordered_set<std::string> FontDescriptorCache::GetInstallFontList()
149 {
150 std::unordered_set<std::string> fullNameList;
151 TextEngine::FullNameToPath fullNameToPath;
152 if (!ParserInstallFontsPathList(fullNameToPath)) {
153 TEXT_LOGE("Failed to parser install fonts path list");
154 return fullNameList;
155 }
156 for (const auto& item : fullNameToPath) {
157 fullNameList.emplace(item.first);
158 }
159 return fullNameList;
160 }
161
GetStylishFontList()162 std::unordered_set<std::string> FontDescriptorCache::GetStylishFontList()
163 {
164 std::unordered_set<std::string> fullNameList;
165 for (const auto& temp : stylishFullNameMap_) {
166 fullNameList.emplace(temp.first);
167 }
168 return fullNameList;
169 }
170
GetGenericFontList()171 std::unordered_set<std::string> FontDescriptorCache::GetGenericFontList()
172 {
173 std::unordered_set<std::string> fullNameList;
174 for (const auto& temp : allFontDescriptor_) {
175 fullNameList.emplace(temp->fullName);
176 }
177 return fullNameList;
178 }
179
GetDynamicFontList()180 std::unordered_set<std::string> FontDescriptorCache::GetDynamicFontList()
181 {
182 std::unordered_set<std::string> fullNameList;
183 for (const auto& temp : dynamicFullNameMap_) {
184 fullNameList.emplace(temp.first);
185 }
186 return fullNameList;
187 }
188
ProcessSystemFontType(int32_t systemFontType,int32_t & fontType)189 bool FontDescriptorCache::ProcessSystemFontType(int32_t systemFontType, int32_t& fontType)
190 {
191 if ((static_cast<uint32_t>(systemFontType) & (TextEngine::FontParser::SystemFontType::ALL |
192 TextEngine::FontParser::SystemFontType::GENERIC |
193 TextEngine::FontParser::SystemFontType::STYLISH |
194 TextEngine::FontParser::SystemFontType::INSTALLED |
195 TextEngine::FontParser::SystemFontType::CUSTOMIZED)) != systemFontType) {
196 TEXT_LOGE("Invalid system font type %{public}d", systemFontType);
197 return false;
198 }
199 fontType = systemFontType;
200 if (static_cast<uint32_t>(systemFontType) & TextEngine::FontParser::SystemFontType::ALL) {
201 fontType = TextEngine::FontParser::SystemFontType::GENERIC |
202 TextEngine::FontParser::SystemFontType::STYLISH |
203 TextEngine::FontParser::SystemFontType::INSTALLED |
204 TextEngine::FontParser::SystemFontType::CUSTOMIZED;
205 }
206 return true;
207 }
208
GetSystemFontFullNamesByType(int32_t systemFontType,std::unordered_set<std::string> & fontList)209 void FontDescriptorCache::GetSystemFontFullNamesByType(
210 int32_t systemFontType, std::unordered_set<std::string> &fontList)
211 {
212 TEXT_TRACE_FUNC();
213 if (systemFontType < 0) {
214 TEXT_LOGE("Invalid system font type %{public}d", systemFontType);
215 return;
216 }
217 int32_t fontType = 0;
218 if (!ProcessSystemFontType(systemFontType, fontType)) {
219 fontList.clear();
220 return;
221 }
222
223 ParserFontsByFontType(fontType);
224
225 uint32_t fontCategory = static_cast<uint32_t>(fontType);
226 if (fontCategory & TextEngine::FontParser::SystemFontType::GENERIC) {
227 auto fullNameList = GetGenericFontList();
228 fontList.insert(fullNameList.begin(), fullNameList.end());
229 }
230
231 if (fontCategory & TextEngine::FontParser::SystemFontType::STYLISH) {
232 auto fullNameList = GetStylishFontList();
233 fontList.insert(fullNameList.begin(), fullNameList.end());
234 }
235
236 if (fontCategory & TextEngine::FontParser::SystemFontType::INSTALLED) {
237 auto fullNameList = GetInstallFontList();
238 fontList.insert(fullNameList.begin(), fullNameList.end());
239 }
240
241 if (fontCategory & TextEngine::FontParser::SystemFontType::CUSTOMIZED) {
242 auto fullNameList = GetDynamicFontList();
243 fontList.insert(fullNameList.begin(), fullNameList.end());
244 }
245 }
246
ParseInstallFontDescSharedPtrByName(const std::string & fullName,FontDescSharedPtr & result) const247 bool FontDescriptorCache::ParseInstallFontDescSharedPtrByName(
248 const std::string& fullName, FontDescSharedPtr& result) const
249 {
250 TextEngine::FullNameToPath fullNameToPath;
251 if (!ParserInstallFontsPathList(fullNameToPath)) {
252 TEXT_LOGE("Failed to parser install fonts path list");
253 return false;
254 }
255 auto iter = fullNameToPath.find(fullName);
256 if (iter == fullNameToPath.end()) {
257 TEXT_LOGE("Failed to find font path by full name: %{public}s", fullName.c_str());
258 return false;
259 }
260 FontDescSharedPtr desc = parser_.ParserFontDescriptorFromPath(iter->second.second, iter->second.first);
261 if (desc && desc->fullName == fullName) {
262 desc->weight = WeightAlignment(desc->weight);
263 result = desc;
264 return true;
265 }
266 std::vector<FontDescSharedPtr> descriptors = parser_.ParserFontDescriptorsFromPath(iter->second.second);
267 for (const auto& item : descriptors) {
268 if (item->fullName == fullName) {
269 item->weight = WeightAlignment(item->weight);
270 result = item;
271 return true;
272 }
273 }
274 TEXT_LOGE_LIMIT3_MIN("Failed to parser installed font descriptor by full name: %{public}s", fullName.c_str());
275 return false;
276 }
277
GetFontTypeFromParams(const std::string & fullName,int32_t systemFontType,int32_t & fontType)278 bool FontDescriptorCache::GetFontTypeFromParams(const std::string& fullName,
279 int32_t systemFontType, int32_t& fontType)
280 {
281 if (fullName.empty()) {
282 TEXT_LOGE("Empty full name");
283 return false;
284 }
285 if (!ProcessSystemFontType(systemFontType, fontType)) {
286 return false;
287 }
288 if (systemFontType < 0) {
289 TEXT_LOGE("Invalid system font type %{public}d", systemFontType);
290 return false;
291 }
292 return true;
293 }
294
GetFontDescSharedPtrByFullName(const std::string & fullName,int32_t systemFontType,FontDescSharedPtr & result)295 void FontDescriptorCache::GetFontDescSharedPtrByFullName(const std::string& fullName,
296 int32_t systemFontType, FontDescSharedPtr& result)
297 {
298 TEXT_TRACE_FUNC();
299 int32_t fontType = 0;
300 if (!GetFontTypeFromParams(fullName, systemFontType, fontType)) {
301 return;
302 }
303
304 ParserFontsByFontType(fontType);
305
306 auto tryFindFontDescriptor = [&fullName, &result](const std::unordered_map<std::string,
307 std::set<FontDescSharedPtr>>& map) -> bool {
308 auto it = map.find(fullName);
309 if (it != map.end()) {
310 result = *(it->second.begin());
311 return true;
312 }
313 return false;
314 };
315
316 uint32_t fontCategory = static_cast<uint32_t>(fontType);
317 if ((fontCategory & TextEngine::FontParser::SystemFontType::GENERIC) &&
318 tryFindFontDescriptor(fullNameMap_)) {
319 return;
320 }
321 if ((fontCategory & TextEngine::FontParser::SystemFontType::STYLISH) &&
322 tryFindFontDescriptor(stylishFullNameMap_)) {
323 return;
324 }
325 if ((fontCategory & TextEngine::FontParser::SystemFontType::INSTALLED) &&
326 ParseInstallFontDescSharedPtrByName(fullName, result)) {
327 return;
328 }
329 if ((fontCategory & TextEngine::FontParser::SystemFontType::CUSTOMIZED)) {
330 if (dynamicFullNameMap_.count(fullName)) {
331 result = dynamicFullNameMap_.at(fullName);
332 return;
333 }
334 }
335 TEXT_LOGD("Failed to get fontDescriptor by full name: %{public}s", fullName.c_str());
336 result = nullptr;
337 }
338
CacheDynamicTypeface(std::shared_ptr<Drawing::Typeface> typeface,const std::string & familyName)339 void FontDescriptorCache::CacheDynamicTypeface(std::shared_ptr<Drawing::Typeface> typeface,
340 const std::string &familyName)
341 {
342 std::vector<std::shared_ptr<TextEngine::FontParser::FontDescriptor>> fontDescArr =
343 parser_.CreateFontDescriptors({typeface});
344 for (auto fontDesc : fontDescArr) {
345 fontDesc->fontFamily = familyName;
346 dynamicFullNameMap_[fontDesc->fontFamily] = fontDesc;
347 }
348 }
349
DeleteDynamicTypefaceFromCache(const std::string & familyName)350 void FontDescriptorCache::DeleteDynamicTypefaceFromCache(const std::string &familyName)
351 {
352 dynamicFullNameMap_.erase(familyName);
353 }
354
HandleMapIntersection(std::set<FontDescSharedPtr> & finishRet,const std::string & name,std::unordered_map<std::string,std::set<FontDescSharedPtr>> & map)355 bool FontDescriptorCache::HandleMapIntersection(std::set<FontDescSharedPtr>& finishRet, const std::string& name,
356 std::unordered_map<std::string, std::set<FontDescSharedPtr>>& map)
357 {
358 if (name.empty()) {
359 return true;
360 }
361 auto iter = map.find(name);
362 if (iter == map.end()) {
363 return false;
364 }
365 if (finishRet.empty()) {
366 finishRet = iter->second;
367 } else {
368 std::set<FontDescSharedPtr> temp;
369 std::set_intersection(iter->second.begin(), iter->second.end(), finishRet.begin(), finishRet.end(),
370 std::insert_iterator<std::set<FontDescSharedPtr>>(temp, temp.begin()));
371 if (temp.empty()) {
372 return false;
373 }
374 finishRet = std::move(temp);
375 }
376 return true;
377 }
378
FilterBoldCache(int weight,std::set<FontDescSharedPtr> & finishRet)379 bool FontDescriptorCache::FilterBoldCache(int weight, std::set<FontDescSharedPtr>& finishRet)
380 {
381 if (weight < 0) {
382 return false;
383 }
384
385 if (weight == 0) {
386 return true;
387 }
388
389 std::set<FontDescSharedPtr> temp;
390 std::set<FontDescSharedPtr>::iterator begin;
391 std::set<FontDescSharedPtr>::iterator end;
392 if (!finishRet.empty()) {
393 begin = finishRet.begin();
394 end = finishRet.end();
395 } else if (static_cast<uint32_t>(weight) > WEIGHT_400) {
396 begin = boldCache_.begin();
397 end = boldCache_.end();
398 } else {
399 begin = allFontDescriptor_.begin();
400 end = allFontDescriptor_.end();
401 }
402 std::for_each(begin, end, [&](FontDescSharedPtr item) {
403 if (item->weight == weight) {
404 temp.emplace(item);
405 }
406 });
407
408 if (temp.empty()) {
409 TEXT_LOGD("Failed to match weight");
410 return false;
411 }
412 finishRet = std::move(temp);
413 return true;
414 }
415
FilterWidthCache(int width,std::set<FontDescSharedPtr> & finishRet)416 bool FontDescriptorCache::FilterWidthCache(int width, std::set<FontDescSharedPtr>& finishRet)
417 {
418 if (width < 0) {
419 return false;
420 }
421
422 if (width == 0) {
423 return true;
424 }
425
426 std::set<FontDescSharedPtr> temp;
427 std::set<FontDescSharedPtr>::iterator begin;
428 std::set<FontDescSharedPtr>::iterator end;
429 if (!finishRet.empty()) {
430 begin = finishRet.begin();
431 end = finishRet.end();
432 } else {
433 begin = allFontDescriptor_.begin();
434 end = allFontDescriptor_.end();
435 }
436 std::for_each(begin, end, [&](FontDescSharedPtr item) {
437 if (item->width == width) {
438 temp.emplace(item);
439 }
440 });
441
442 if (temp.empty()) {
443 TEXT_LOGD("Failed to match width");
444 return false;
445 }
446 finishRet = std::move(temp);
447 return true;
448 }
449
FilterItalicCache(int italic,std::set<FontDescSharedPtr> & finishRet)450 bool FontDescriptorCache::FilterItalicCache(int italic, std::set<FontDescSharedPtr>& finishRet)
451 {
452 if (italic == 0) {
453 return true;
454 }
455 std::set<FontDescSharedPtr> temp;
456 if (!finishRet.empty()) {
457 std::for_each(finishRet.begin(), finishRet.end(), [&](FontDescSharedPtr item) {
458 if (item->italic != 0) {
459 temp.emplace(item);
460 }
461 });
462 } else {
463 temp = italicCache_;
464 }
465 if (temp.empty()) {
466 TEXT_LOGD("Failed to match italic");
467 return false;
468 }
469 finishRet = std::move(temp);
470 return true;
471 }
472
FilterMonoSpaceCache(bool monoSpace,std::set<FontDescSharedPtr> & finishRet)473 bool FontDescriptorCache::FilterMonoSpaceCache(bool monoSpace, std::set<FontDescSharedPtr>& finishRet)
474 {
475 if (!monoSpace) {
476 return true;
477 }
478
479 std::set<FontDescSharedPtr> temp;
480 if (!finishRet.empty()) {
481 std::for_each(finishRet.begin(), finishRet.end(), [&](FontDescSharedPtr item) {
482 if (item->monoSpace) {
483 temp.emplace(item);
484 }
485 });
486 } else {
487 temp = monoSpaceCache_;
488 }
489 if (temp.empty()) {
490 TEXT_LOGD("Failed to match monoSpace");
491 return false;
492 }
493 finishRet = std::move(temp);
494 return true;
495 }
496
FilterSymbolicCache(bool symbolic,std::set<FontDescSharedPtr> & finishRet)497 bool FontDescriptorCache::FilterSymbolicCache(bool symbolic, std::set<FontDescSharedPtr>& finishRet)
498 {
499 if (!symbolic) {
500 return true;
501 }
502 std::set<FontDescSharedPtr> temp;
503 if (!finishRet.empty()) {
504 std::for_each(finishRet.begin(), finishRet.end(), [&](FontDescSharedPtr item) {
505 if (item->symbolic) {
506 temp.emplace(item);
507 }
508 });
509 } else {
510 temp = symbolicCache_;
511 }
512 if (temp.empty()) {
513 TEXT_LOGD("Failed to match symbolic");
514 return false;
515 }
516 finishRet = std::move(temp);
517 return true;
518 }
519
IsDefault(FontDescSharedPtr desc)520 bool FontDescriptorCache::IsDefault(FontDescSharedPtr desc)
521 {
522 if (desc->fontFamily.empty() && desc->fullName.empty() && desc->postScriptName.empty()
523 && desc->fontSubfamily.empty() && desc->weight == 0 && desc->width == 0 && desc->italic == 0
524 && !desc->monoSpace && !desc->symbolic) {
525 return true;
526 }
527 return false;
528 }
529
MatchFromFontDescriptor(FontDescSharedPtr desc,std::set<FontDescSharedPtr> & result)530 void FontDescriptorCache::MatchFromFontDescriptor(FontDescSharedPtr desc, std::set<FontDescSharedPtr>& result)
531 {
532 if (desc == nullptr) {
533 TEXT_LOGE("Null desc");
534 return;
535 }
536 ParserSystemFonts();
537 if (IsDefault(desc)) {
538 result = std::set<FontDescSharedPtr>(allFontDescriptor_.begin(), allFontDescriptor_.end());
539 return;
540 }
541 desc->weight = (desc->weight > 0) ? WeightAlignment(desc->weight) : desc->weight;
542 std::set<FontDescSharedPtr> finishRet;
543 TEXT_INFO_CHECK(HandleMapIntersection(finishRet, desc->fontFamily, fontFamilyMap_), return,
544 "Failed to match font family");
545 TEXT_INFO_CHECK(HandleMapIntersection(finishRet, desc->fullName, fullNameMap_), return, "Failed to match fullName");
546 TEXT_INFO_CHECK(HandleMapIntersection(finishRet, desc->postScriptName, postScriptNameMap_), return,
547 "Failed to match post script name");
548 TEXT_INFO_CHECK(HandleMapIntersection(finishRet, desc->fontSubfamily, fontSubfamilyNameMap_), return,
549 "Failed to match font subfamily");
550
551 TEXT_CHECK(FilterBoldCache(desc->weight, finishRet), return);
552 TEXT_CHECK(FilterWidthCache(desc->width, finishRet), return);
553 TEXT_CHECK(FilterItalicCache(desc->italic, finishRet), return);
554 TEXT_CHECK(FilterMonoSpaceCache(desc->monoSpace, finishRet), return);
555 TEXT_CHECK(FilterSymbolicCache(desc->symbolic, finishRet), return);
556 result = std::move(finishRet);
557 }
558
Dump() const559 void FontDescriptorCache::Dump() const
560 {
561 TEXT_LOGD("allFontDescriptor size: %{public}zu, fontFamilyMap size: %{public}zu, fullNameMap size: %{public}zu \
562 postScriptNameMap size: %{public}zu, fontSubfamilyNameMap size: %{public}zu, boldCache size: %{public}zu \
563 italicCache size: %{public}zu, monoSpaceCache size: %{public}zu, symbolicCache size: %{public}zu",
564 allFontDescriptor_.size(), fontFamilyMap_.size(), fullNameMap_.size(), postScriptNameMap_.size(),
565 fontSubfamilyNameMap_.size(), boldCache_.size(), italicCache_.size(), monoSpaceCache_.size(),
566 symbolicCache_.size());
567 }
568
WeightAlignment(int32_t weight)569 int32_t FontDescriptorCache::WeightAlignment(int32_t weight)
570 {
571 if (weight < Drawing::FontStyle::THIN_WEIGHT) {
572 return Drawing::FontStyle::THIN_WEIGHT;
573 }
574
575 if (weight > Drawing::FontStyle::EXTRA_BLACK_WEIGHT) {
576 return Drawing::FontStyle::EXTRA_BLACK_WEIGHT;
577 }
578
579 if ((weight % WEIGHT_MODULE) == 0) {
580 return weight;
581 }
582
583 static const std::vector<int> weightType = {
584 Drawing::FontStyle::THIN_WEIGHT,
585 Drawing::FontStyle::EXTRA_LIGHT_WEIGHT,
586 Drawing::FontStyle::LIGHT_WEIGHT,
587 Drawing::FontStyle::NORMAL_WEIGHT,
588 Drawing::FontStyle::MEDIUM_WEIGHT,
589 Drawing::FontStyle::SEMI_BOLD_WEIGHT,
590 Drawing::FontStyle::BOLD_WEIGHT,
591 Drawing::FontStyle::EXTRA_BOLD_WEIGHT,
592 Drawing::FontStyle::BLACK_WEIGHT,
593 Drawing::FontStyle::EXTRA_BLACK_WEIGHT
594 };
595 // Obtain weight ranges for non-whole hundred values
596 auto it = std::lower_bound(weightType.begin(), weightType.end(), weight);
597 std::vector<int> targetRange = { *(it - 1), *it };
598
599 /**
600 * When the font weight is less than NORMAL_WEIGHT, round down as much as possible;
601 * when the font weight exceeds NORMAL_WEIGHT, round up where possible. For example, when weight is 360,
602 * the final font weight is set to 300; when weight is 620, the final font weight is set to 700.
603 */
604 uint32_t minDiff = 0xFFFFFFFF;
605 int resultWeight = 0;
606 for (const auto& item : targetRange) {
607 /**
608 * The maximum weight is EXTRA_BLACK_WEIGHT (1000), when weight and item are at the different
609 * side of NORMAL_WEIGHT, the weight difference between them should be more than 500 (1000/2).
610 */
611 uint32_t weightDiff = 0;
612 constexpr int kWeightDiffThreshold = Drawing::FontStyle::EXTRA_BLACK_WEIGHT / 2;
613 if ((weight == Drawing::FontStyle::NORMAL_WEIGHT && item == Drawing::FontStyle::MEDIUM_WEIGHT) ||
614 (weight == Drawing::FontStyle::MEDIUM_WEIGHT && item == Drawing::FontStyle::NORMAL_WEIGHT)) {
615 weightDiff = static_cast<uint32_t>(SPECIAL_WEIGHT_DIFF);
616 } else if (weight <= Drawing::FontStyle::NORMAL_WEIGHT) {
617 weightDiff = (item <= weight) ? static_cast<uint32_t>(weight - item) :
618 static_cast<uint32_t>(item - weight + kWeightDiffThreshold);
619 } else if (weight > Drawing::FontStyle::NORMAL_WEIGHT) {
620 weightDiff = (item >= weight) ? static_cast<uint32_t>(item - weight) :
621 static_cast<uint32_t>(weight - item + kWeightDiffThreshold);
622 }
623
624 // Retain the font weight with the smallest difference
625 if (weightDiff < minDiff) {
626 minDiff = weightDiff;
627 resultWeight = item;
628 }
629 }
630 return resultWeight;
631 }
632 }