• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/typography_types.h"
21 #include "texgine/utils/exlog.h"
22 #include "texgine_string.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace TextEngine {
27 #define MAX_WIDTH 10
28 #define MAX_SLANT 2
29 #define MAX_WEIGHT 10
30 #define FIRST_PRIORITY 10000
31 #define SECOND_PRIORITY 100
32 #define MULTIPLE 100
33 
FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> && fontStyleSets)34 FontCollection::FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> &&fontStyleSets)
35     : fontStyleSets_(fontStyleSets)
36 {
37 }
38 
GetTypefaceForChar(const uint32_t & ch,const FontStyles & style,const std::string & script,const std::string & locale) const39 std::shared_ptr<Typeface> FontCollection::GetTypefaceForChar(const uint32_t &ch,
40     const FontStyles &style, const std::string &script, const std::string &locale) const
41 {
42     for (const auto &fontStyleSet : fontStyleSets_) {
43         std::shared_ptr<Typeface> typeface = nullptr;
44         struct TypefaceCacheKey key = {.fss = fontStyleSet, .fs = style};
45         if (auto it = typefaceCache_.find(key); it != typefaceCache_.end()) {
46             typeface = it->second;
47         } else {
48             if (fontStyleSet == nullptr) {
49                 continue;
50             }
51             auto fs = std::make_shared<TexgineFontStyle>();
52             *fs = style.ToTexgineFontStyle();
53             auto texgineTypeface = fontStyleSet->MatchStyle(fs);
54             if (texgineTypeface == nullptr || texgineTypeface->GetTypeface() == nullptr) {
55                 continue;
56             }
57             typeface = std::make_shared<Typeface>(texgineTypeface);
58             typefaceCache_[key] = typeface;
59         }
60 
61         if (typeface->Has(ch)) {
62             return typeface;
63         }
64     }
65     return FindFallBackTypeface(ch, style, script, locale);
66 }
67 
GetTypefaceForFontStyles(const FontStyles & style,const std::string & script,const std::string & locale) const68 std::shared_ptr<Typeface> FontCollection::GetTypefaceForFontStyles(const FontStyles &style,
69     const std::string &script, const std::string &locale) const
70 {
71     auto providingStyle = style.ToTexgineFontStyle();
72 
73     int bestScore = 0;
74     int bestIndex = 0;
75     std::shared_ptr<VariantFontStyleSet> bestFontStyleSet = nullptr;
76     for (auto &fontStyleSet : fontStyleSets_) {
77         auto count = fontStyleSet->Count();
78         for (auto i = 0; i < count; i++) {
79             auto matchingStyle = std::make_shared<TexgineFontStyle>();
80             auto styleName = std::make_shared<TexgineString>();
81             fontStyleSet->GetStyle(i, matchingStyle, styleName);
82 
83             int score = 0;
84             score += (MAX_WIDTH - std::abs(providingStyle.GetFontStyle()->width() -
85                                           matchingStyle->GetFontStyle()->width())) * SECOND_PRIORITY;
86             score += (MAX_SLANT - std::abs(providingStyle.GetFontStyle()->slant() -
87                                           matchingStyle->GetFontStyle()->slant())) * FIRST_PRIORITY;
88             score += (MAX_WEIGHT - std::abs(providingStyle.GetFontStyle()->weight() / MULTIPLE -
89                                            matchingStyle->GetFontStyle()->weight() / MULTIPLE));
90             if (score > bestScore) {
91                 bestScore = score;
92                 bestIndex = i;
93                 bestFontStyleSet = fontStyleSet;
94             }
95         }
96     }
97 
98     if (bestFontStyleSet != nullptr &&
99         (bestFontStyleSet->TryToTexgineFontStyleSet() != nullptr || bestFontStyleSet->TryToDynamicFontStyleSet())) {
100         auto ttf = bestFontStyleSet->CreateTypeface(bestIndex);
101         return std::make_shared<Typeface>(ttf);
102     }
103 
104     return FindFallBackTypeface(' ', style, script, locale);
105 }
106 
FindFallBackTypeface(const uint32_t & ch,const FontStyles & style,const std::string & script,const std::string & locale) const107 std::shared_ptr<Typeface> FontCollection::FindFallBackTypeface(const uint32_t &ch, const FontStyles &style,
108     const std::string &script, const std::string &locale) const
109 {
110     if (!enableFallback_) {
111         return nullptr;
112     }
113     // fallback cache
114     struct FallbackCacheKey key = {.script = script, .locale = locale, .fs = style};
115     if (auto it = fallbackCache_.find(key); it != fallbackCache_.end()) {
116         return it->second;
117     }
118 
119     // fallback
120     std::vector<const char *> bcp47;
121     if (!script.empty()) {
122         bcp47.push_back(script.data());
123     } else if (!locale.empty()) {
124         bcp47.push_back(locale.data());
125     }
126 
127     auto fm = TexgineFontManager::RefDefault();
128     if (fm == nullptr) {
129         return nullptr;
130     }
131 
132     auto tfs = style.ToTexgineFontStyle();
133     auto fallbackTypeface = fm->MatchFamilyStyleCharacter("", tfs,
134         bcp47.data(), bcp47.size(), ch);
135 
136     if (fallbackTypeface == nullptr || fallbackTypeface->GetTypeface() == nullptr) {
137         return nullptr;
138     }
139 
140     auto typeface = std::make_shared<Typeface>(fallbackTypeface);
141     fallbackCache_[key] = typeface;
142     return typeface;
143 }
144 
DisableFallback()145 void FontCollection::DisableFallback()
146 {
147     enableFallback_ = false;
148 }
149 } // namespace TextEngine
150 } // namespace Rosen
151 } // namespace OHOS
152