1 /*
2 * Copyright (c) 2021 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 "core/common/font_manager.h"
17
18 #include "base/utils/system_properties.h"
19 #include "base/utils/utils.h"
20 #include "core/components/text/render_text.h"
21 #include "core/pipeline/base/render_node.h"
22
23 namespace OHOS::Ace {
24
25 float FontManager::fontWeightScale_ = 1.0f;
26
RegisterFont(const std::string & familyName,const std::string & familySrc,const RefPtr<PipelineBase> & context)27 void FontManager::RegisterFont(
28 const std::string& familyName, const std::string& familySrc, const RefPtr<PipelineBase>& context)
29 {
30 if (std::find(std::begin(fontNames_), std::end(fontNames_), familyName) == std::end(fontNames_)) {
31 fontNames_.emplace_back(familyName);
32 }
33
34 for (auto iter = fontLoaders_.begin(); iter != fontLoaders_.end(); ++iter) {
35 auto& fontLoader = *iter;
36 if (fontLoader->GetFamilyName() == familyName) {
37 LOGI("Font is already loaded!");
38 return;
39 }
40 }
41 RefPtr<FontLoader> fontLoader = FontLoader::Create(familyName, familySrc);
42 fontLoaders_.emplace_back(fontLoader);
43 fontLoader->AddFont(context);
44
45 fontLoader->SetVariationChanged([weak = WeakClaim(this), familyName]() {
46 auto fontManager = weak.Upgrade();
47 CHECK_NULL_VOID_NOLOG(fontManager);
48 fontManager->VaryFontCollectionWithFontWeightScale();
49 });
50 }
51
RegisterCallback(const WeakPtr<RenderNode> & node,const std::string & familyName,const std::function<void ()> & callback)52 void FontManager::RegisterCallback(
53 const WeakPtr<RenderNode>& node, const std::string& familyName, const std::function<void()>& callback)
54 {
55 CHECK_NULL_VOID_NOLOG(callback);
56 for (auto& fontLoader : fontLoaders_) {
57 if (fontLoader->GetFamilyName() == familyName) {
58 fontLoader->SetOnLoaded(node, callback);
59 }
60 }
61 }
62
GetFontNames() const63 const std::vector<std::string>& FontManager::GetFontNames() const
64 {
65 return fontNames_;
66 }
67
AddFontNode(const WeakPtr<RenderNode> & node)68 void FontManager::AddFontNode(const WeakPtr<RenderNode>& node)
69 {
70 if (fontNodes_.find(node) == fontNodes_.end()) {
71 fontNodes_.emplace(node);
72 }
73 }
74
RemoveFontNode(const WeakPtr<RenderNode> & node)75 void FontManager::RemoveFontNode(const WeakPtr<RenderNode>& node)
76 {
77 fontNodes_.erase(node);
78 }
79
RebuildFontNode()80 void FontManager::RebuildFontNode()
81 {
82 #ifndef NG_BUILD
83 for (auto iter = fontNodes_.begin(); iter != fontNodes_.end();) {
84 auto fontNode = iter->Upgrade();
85 if (fontNode) {
86 fontNode->MarkNeedLayout();
87 ++iter;
88 } else {
89 iter = fontNodes_.erase(iter);
90 }
91 }
92 #endif
93 }
94
UnRegisterCallback(const WeakPtr<RenderNode> & node)95 void FontManager::UnRegisterCallback(const WeakPtr<RenderNode>& node)
96 {
97 for (auto& fontLoader : fontLoaders_) {
98 fontLoader->RemoveCallback(node);
99 }
100 }
101
UpdateFontWeightScale()102 void FontManager::UpdateFontWeightScale()
103 {
104 float fontWeightScale = SystemProperties::GetFontWeightScale();
105 if (!NearEqual(fontWeightScale, fontWeightScale_)) {
106 fontWeightScale_ = fontWeightScale;
107 VaryFontCollectionWithFontWeightScale();
108 }
109 }
110
AddVariationNode(const WeakPtr<RenderNode> & node)111 void FontManager::AddVariationNode(const WeakPtr<RenderNode>& node)
112 {
113 if (variationNodes_.find(node) == variationNodes_.end()) {
114 variationNodes_.emplace(node);
115 }
116 }
117
RemoveVariationNode(const WeakPtr<RenderNode> & node)118 void FontManager::RemoveVariationNode(const WeakPtr<RenderNode>& node)
119 {
120 variationNodes_.erase(node);
121 }
122
NotifyVariationNodes()123 void FontManager::NotifyVariationNodes()
124 {
125 #ifndef NG_BUILD
126 for (const auto& node : variationNodes_) {
127 auto refNode = node.Upgrade();
128 if (refNode) {
129 auto text = DynamicCast<RenderText>(refNode);
130 if (text) {
131 text->MarkNeedMeasure();
132 }
133 refNode->MarkNeedLayout();
134 }
135 }
136 #endif
137 }
138
139 } // namespace OHOS::Ace
140