1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
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 <algorithm>
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "text_bundle_config_parser.h"
28 #include "txt/platform.h"
29 #include "txt/text_style.h"
30 
31 namespace txt {
FontCollection()32 FontCollection::FontCollection() : enableFontFallback_(true) {}
33 
~FontCollection()34 FontCollection::~FontCollection() {}
35 
GetFontManagersCount() const36 size_t FontCollection::GetFontManagersCount() const
37 {
38     return GetFontManagerOrder().size();
39 }
40 
SetupDefaultFontManager()41 void FontCollection::SetupDefaultFontManager()
42 {
43     std::unique_lock lock(collectionMutex_);
44     if (!defaultFontManager_) {
45         defaultFontManager_ = OHOS::Rosen::SPText::GetDefaultFontManager();
46         sktFontCollection_.reset();
47     }
48 }
49 
SetDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager)50 void FontCollection::SetDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager)
51 {
52     std::unique_lock lock(collectionMutex_);
53     if (defaultFontManager_ != fontManager) {
54         defaultFontManager_ = fontManager;
55         sktFontCollection_.reset();
56     }
57 }
58 
SetAssetFontManager(std::shared_ptr<RSFontMgr> fontManager)59 void FontCollection::SetAssetFontManager(std::shared_ptr<RSFontMgr> fontManager)
60 {
61     std::unique_lock lock(collectionMutex_);
62     if (assetFontManager_ != fontManager) {
63         assetFontManager_ = fontManager;
64         sktFontCollection_.reset();
65     }
66 }
67 
SetDynamicFontManager(std::shared_ptr<RSFontMgr> fontManager)68 void FontCollection::SetDynamicFontManager(std::shared_ptr<RSFontMgr> fontManager)
69 {
70     std::unique_lock lock(collectionMutex_);
71     if (dynamicFontManager_ != fontManager) {
72         dynamicFontManager_ = fontManager;
73         sktFontCollection_.reset();
74     }
75 }
76 
SetTestFontManager(std::shared_ptr<RSFontMgr> fontManager)77 void FontCollection::SetTestFontManager(std::shared_ptr<RSFontMgr> fontManager)
78 {
79     std::unique_lock lock(collectionMutex_);
80     if (testFontManager_ != fontManager) {
81         testFontManager_ = fontManager;
82         sktFontCollection_.reset();
83     }
84 }
85 
GetFontManagerOrder() const86 std::vector<std::shared_ptr<RSFontMgr>> FontCollection::GetFontManagerOrder() const
87 {
88     std::unique_lock lock(collectionMutex_);
89     std::vector<std::shared_ptr<RSFontMgr>> order;
90     if (dynamicFontManager_)
91         order.push_back(dynamicFontManager_);
92     if (assetFontManager_)
93         order.push_back(assetFontManager_);
94     if (testFontManager_)
95         order.push_back(testFontManager_);
96     if (defaultFontManager_)
97         order.push_back(defaultFontManager_);
98     return order;
99 }
100 
DisableFontFallback()101 void FontCollection::DisableFontFallback()
102 {
103     std::unique_lock lock(collectionMutex_);
104     enableFontFallback_ = false;
105     if (sktFontCollection_) {
106         sktFontCollection_->disableFontFallback();
107     }
108 }
109 
ClearFontFamilyCache()110 void FontCollection::ClearFontFamilyCache()
111 {
112     std::unique_lock lock(collectionMutex_);
113     if (sktFontCollection_) {
114         sktFontCollection_->clearCaches();
115     }
116 }
117 
CreateSktFontCollection()118 sk_sp<skia::textlayout::FontCollection> FontCollection::CreateSktFontCollection()
119 {
120     std::unique_lock lock(collectionMutex_);
121     if (!sktFontCollection_) {
122         skia::textlayout::FontCollection::SetAdapterTextHeightEnabled(
123             OHOS::Rosen::SPText::TextBundleConfigParser::IsAdapterTextHeightEnabled());
124         sktFontCollection_ = sk_make_sp<skia::textlayout::FontCollection>();
125 
126         std::vector<SkString> defaultFontFamilies;
127         for (const std::string& family : OHOS::Rosen::SPText::GetDefaultFontFamilies()) {
128             defaultFontFamilies.emplace_back(family);
129         }
130         sktFontCollection_->setDefaultFontManager(defaultFontManager_, defaultFontFamilies);
131         sktFontCollection_->setAssetFontManager(assetFontManager_);
132         sktFontCollection_->setDynamicFontManager(dynamicFontManager_);
133         sktFontCollection_->setTestFontManager(testFontManager_);
134         if (!enableFontFallback_) {
135             sktFontCollection_->disableFontFallback();
136         }
137     }
138 
139     return sktFontCollection_;
140 }
141 } // namespace txt
142