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 "core/components/text/render_text.h"
20 #include "core/pipeline/base/render_node.h"
21
22 namespace OHOS::Ace {
23
24 float FontManager::fontWeightScale_ = 1.0f;
25
RegisterFont(const std::string & familyName,const std::string & familySrc,const RefPtr<PipelineContext> & context)26 void FontManager::RegisterFont(
27 const std::string& familyName, const std::string& familySrc, const RefPtr<PipelineContext>& context)
28 {
29 if (std::find(std::begin(fontNames_), std::end(fontNames_), familyName) == std::end(fontNames_)) {
30 fontNames_.emplace_back(familyName);
31 }
32
33 for (auto iter = fontLoaders_.begin(); iter != fontLoaders_.end(); ++iter) {
34 auto& fontLoader = *iter;
35 if (fontLoader->GetFamilyName() == familyName) {
36 LOGI("Font is already loaded!");
37 return;
38 }
39 }
40 RefPtr<FontLoader> fontLoader = FontLoader::Create(familyName, familySrc);
41 fontLoaders_.emplace_back(fontLoader);
42 fontLoader->AddFont(context);
43
44 fontLoader->SetVariationChanged([weak = WeakClaim(this), familyName]() {
45 auto fontManager = weak.Upgrade();
46 if (fontManager) {
47 fontManager->VaryFontCollectionWithFontWeightScale();
48 }
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 if (!callback) {
56 return;
57 }
58 for (auto& fontLoader : fontLoaders_) {
59 if (fontLoader->GetFamilyName() == familyName) {
60 fontLoader->SetOnLoaded(node, callback);
61 }
62 }
63 }
64
GetFontNames() const65 const std::vector<std::string>& FontManager::GetFontNames() const
66 {
67 return fontNames_;
68 }
69
AddFontNode(const WeakPtr<RenderNode> & node)70 void FontManager::AddFontNode(const WeakPtr<RenderNode>& node)
71 {
72 if (fontNodes_.find(node) == fontNodes_.end()) {
73 fontNodes_.emplace(node);
74 }
75 }
76
RemoveFontNode(const WeakPtr<RenderNode> & node)77 void FontManager::RemoveFontNode(const WeakPtr<RenderNode>& node)
78 {
79 fontNodes_.erase(node);
80 }
81
RebuildFontNode()82 void FontManager::RebuildFontNode()
83 {
84 for (auto iter = fontNodes_.begin(); iter != fontNodes_.end();) {
85 auto fontNode = iter->Upgrade();
86 if (fontNode) {
87 fontNode->MarkNeedLayout();
88 ++iter;
89 } else {
90 iter = fontNodes_.erase(iter);
91 }
92 }
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 for (const auto& node : variationNodes_) {
126 auto refNode = node.Upgrade();
127 if (refNode) {
128 auto text = DynamicCast<RenderText>(refNode);
129 if (text) {
130 text->MarkNeedMeasure();
131 }
132 refNode->MarkNeedLayout();
133 }
134 }
135 }
136
137 } // namespace OHOS::Ace
138