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 "texgine/font_manager.h"
17
18 #include <mutex>
19
20 #include "font_parser.h"
21 #include "texgine/utils/exlog.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace TextEngine {
26
27 static constexpr int MAX_WIDTH = 10;
28 static constexpr int MAX_WEIGHT = 10;
29 static constexpr int FIRST_PRIORITY = 10000;
30 static constexpr int SECOND_PRIORITY = 100;
31 static constexpr int MULTIPLE = 100;
32
GetInstance()33 std::shared_ptr<FontManager> FontManager::GetInstance()
34 {
35 static std::mutex mutex;
36 std::lock_guard<std::mutex> lock(mutex);
37 if (instance == nullptr) {
38 instance = std::make_shared<FontManager>();
39 }
40
41 return instance;
42 }
43
ResAccurateMatch(FontParser::FontDescriptor & descSrc,FontParser::FontDescriptor & descFind)44 bool FontManager::ResAccurateMatch(FontParser::FontDescriptor& descSrc, FontParser::FontDescriptor& descFind)
45 {
46 if (!descFind.postScriptName.empty() && descFind.postScriptName == descSrc.postScriptName) {
47 return true;
48 }
49 if (!descFind.fullName.empty() && descFind.fullName == descSrc.fullName) {
50 return true;
51 }
52
53 return false;
54 }
55
ResFallbackMatch(FontParser::FontDescriptor & descSrc,FontParser::FontDescriptor & descFind)56 bool FontManager::ResFallbackMatch(FontParser::FontDescriptor& descSrc, FontParser::FontDescriptor& descFind)
57 {
58 if (descFind.fontFamily != descSrc.fontFamily) {
59 return false;
60 }
61 if (!descFind.fontSubfamily.empty() && descFind.fontSubfamily != descSrc.fontSubfamily) {
62 return false;
63 }
64 // -1 means used to exclude unassigned
65 if (descFind.weight != -1 && descFind.weight != descSrc.weight) {
66 return false;
67 }
68 // -1 means used to exclude unassigned
69 if (descFind.italic != -1 && descFind.italic != descSrc.italic) {
70 return false;
71 }
72
73 return true;
74 }
75
ScoreFallbackMatch(FontParser::FontDescriptor & descSrc,FontParser::FontDescriptor & descFind)76 int FontManager::ScoreFallbackMatch(FontParser::FontDescriptor& descSrc, FontParser::FontDescriptor& descFind)
77 {
78 int score = 0;
79 score += (MAX_WIDTH - std::abs(descFind.width - descSrc.width)) * SECOND_PRIORITY;
80 score += (MAX_WIDTH - std::abs(descFind.italic - descSrc.italic)) * FIRST_PRIORITY;
81 score += (MAX_WEIGHT - std::abs(descFind.weight / MULTIPLE - descSrc.weight / MULTIPLE));
82
83 return score;
84 }
85
FindAccurateResult(ResultSet * fontSrc,FontParser::FontDescriptor & descFind)86 FontParser::FontDescriptor* FontManager::FindAccurateResult(ResultSet* fontSrc, FontParser::FontDescriptor& descFind)
87 {
88 FontParser::FontDescriptor* descResPtr = nullptr;
89 if (!resultCache.empty()) {
90 for (ResultCache::iterator it = resultCache.begin(); it != resultCache.end(); it++) {
91 if (!ResAccurateMatch(*it, descFind)) {
92 continue;
93 } else {
94 descResPtr = &(*it);
95 break;
96 }
97 }
98 if (descResPtr != nullptr) {
99 LOGEX_FUNC_LINE_DEBUG() << "cache get accurate result";
100 return descResPtr;
101 }
102 }
103 for (ResultSet::iterator it = fontSrc->begin(); it != fontSrc->end(); it++) {
104 if (!ResAccurateMatch(*it, descFind)) {
105 continue;
106 } else {
107 descResPtr = &(*it);
108 LOGEX_FUNC_LINE_DEBUG() << "os get accurate result";
109 break;
110 }
111 }
112 if (descResPtr != nullptr) {
113 resultCache.push_back(*descResPtr);
114 return descResPtr;
115 }
116
117 return descResPtr;
118 }
119
FindFallbackResult(ResultSet * fontSrc,FontParser::FontDescriptor & descFind)120 FontParser::FontDescriptor* FontManager::FindFallbackResult(ResultSet* fontSrc, FontParser::FontDescriptor& descFind)
121 {
122 FontParser::FontDescriptor* descResPtr = nullptr;
123 if (descFind.fontFamily.empty()) {
124 return descResPtr;
125 }
126 ResultSet fbkFonts;
127 if (!resultCache.empty()) {
128 for (ResultCache::iterator it = resultCache.begin(); it != resultCache.end(); it++) {
129 if (ResFallbackMatch(*it, descFind)) {
130 fbkFonts.push_back(*it);
131 LOGEX_FUNC_LINE_DEBUG() << "cache get fallback result";
132 }
133 }
134 }
135 if (fbkFonts.empty()) {
136 for (ResultSet::iterator it = fontSrc->begin(); it != fontSrc->end(); it++) {
137 if (ResFallbackMatch(*it, descFind)) {
138 fbkFonts.push_back(*it);
139 LOGEX_FUNC_LINE_DEBUG() << "os get fallback result";
140 }
141 }
142 }
143 int bestScore = 0;
144 for (ResultSet::iterator it = fbkFonts.begin(); it != fbkFonts.end(); it++) {
145 int score = ScoreFallbackMatch(*it, descFind);
146 if (score > bestScore) {
147 bestScore = score;
148 descResPtr = &(*it);
149 }
150 }
151 if (descResPtr != nullptr) {
152 resultCache.push_back(*descResPtr);
153 return descResPtr;
154 }
155
156 return descResPtr;
157 }
158
FindFont(ResultSet * fontSrc,FontParser::FontDescriptor & descFind)159 FontParser::FontDescriptor* FontManager::FindFont(ResultSet* fontSrc, FontParser::FontDescriptor& descFind)
160 {
161 FontParser::FontDescriptor* descResPtr = FindAccurateResult(fontSrc, descFind);
162 if (descResPtr == nullptr) {
163 descResPtr = FindFallbackResult(fontSrc, descFind);
164 }
165
166 return descResPtr;
167 }
168 } // namespace TextEngine
169 } // namespace Rosen
170 } // namespace OHOS
171