1 /*
2 * Copyright (c) 2024 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 #include "core/interfaces/native/node/node_symbol_glyph_modifier.h"
16
17 #include "bridge/common/utils/utils.h"
18 #include "core/components/common/layout/constants.h"
19 #include "core/components/common/properties/text_style.h"
20 #include "core/components/common/properties/text_style_parser.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/base/view_abstract.h"
23 #include "core/pipeline/base/element_register.h"
24 #include "frameworks/core/components/common/layout/constants.h"
25 #include "frameworks/core/components/common/properties/text_style.h"
26 #include "frameworks/core/components_ng/pattern/symbol/symbol_model_ng.h"
27
28 namespace OHOS::Ace::NG {
29 namespace {
ConvertStrToFontWeight(ArkUI_CharPtr weight,FontWeight defaultFontWeight=FontWeight::NORMAL)30 FontWeight ConvertStrToFontWeight(ArkUI_CharPtr weight, FontWeight defaultFontWeight = FontWeight::NORMAL)
31 {
32 std::string weightStr(weight);
33 return StringUtils::StringToFontWeight(weightStr, defaultFontWeight);
34 }
35
ConvertIntToFontWeight(ArkUI_Int32 weight)36 FontWeight ConvertIntToFontWeight(ArkUI_Int32 weight)
37 {
38 static const std::unordered_map<ArkUI_Int32, FontWeight> fontWeightMap = {
39 {100, FontWeight::W100},
40 {200, FontWeight::W200},
41 {300, FontWeight::W300},
42 {400, FontWeight::W400},
43 {500, FontWeight::W500},
44 {600, FontWeight::W600},
45 {700, FontWeight::W700},
46 {800, FontWeight::W800},
47 {900, FontWeight::W900},
48 };
49 auto weightFindIter = fontWeightMap.find(weight);
50 if (weightFindIter != fontWeightMap.end()) {
51 return weightFindIter->second;
52 }
53 return FontWeight::NORMAL;
54 }
55
SetFontColor(ArkUINodeHandle node,ArkUI_Uint32 * color,int32_t size)56 void SetFontColor(ArkUINodeHandle node, ArkUI_Uint32* color, int32_t size)
57 {
58 auto* frameNode = reinterpret_cast<FrameNode*>(node);
59 CHECK_NULL_VOID(frameNode);
60 std::vector<Color> colorArray;
61 for (int32_t i = 0; i < size; i++) {
62 colorArray.emplace_back(Color(color[i]));
63 }
64 SymbolModelNG::SetFontColor(frameNode, colorArray);
65 }
66
ResetFontColor(ArkUINodeHandle node)67 void ResetFontColor(ArkUINodeHandle node)
68 {
69 auto* frameNode = reinterpret_cast<FrameNode*>(node);
70 CHECK_NULL_VOID(frameNode);
71 auto theme = GetTheme<TextTheme>();
72 CHECK_NULL_VOID(theme);
73 Color fontColor = theme->GetTextStyle().GetTextColor();
74 std::vector<Color> colorArray = { fontColor };
75 SymbolModelNG::SetFontColor(frameNode, colorArray);
76 }
77
SetFontSize(ArkUINodeHandle node,ArkUI_Float32 fontSize,ArkUI_Int32 unit)78 void SetFontSize(ArkUINodeHandle node, ArkUI_Float32 fontSize, ArkUI_Int32 unit)
79 {
80 auto* frameNode = reinterpret_cast<FrameNode*>(node);
81 CHECK_NULL_VOID(frameNode);
82 auto unitEnum = static_cast<OHOS::Ace::DimensionUnit>(unit);
83
84 if (fontSize < 0 || unitEnum < OHOS::Ace::DimensionUnit::PX || unitEnum > OHOS::Ace::DimensionUnit::CALC ||
85 unitEnum == OHOS::Ace::DimensionUnit::PERCENT) {
86 auto theme = GetTheme<TextTheme>();
87 CHECK_NULL_VOID(theme);
88 CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
89 SymbolModelNG::SetFontSize(frameNode, fontSize);
90 } else {
91 SymbolModelNG::SetFontSize(frameNode, Dimension(fontSize, static_cast<OHOS::Ace::DimensionUnit>(unit)));
92 }
93 }
94
ResetFontSize(ArkUINodeHandle node)95 void ResetFontSize(ArkUINodeHandle node)
96 {
97 auto* frameNode = reinterpret_cast<FrameNode*>(node);
98 CHECK_NULL_VOID(frameNode);
99 auto theme = GetTheme<TextTheme>();
100 CHECK_NULL_VOID(theme);
101 CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
102 SymbolModelNG::SetFontSize(frameNode, fontSize);
103 }
104
SetFontWeightStr(ArkUINodeHandle node,ArkUI_CharPtr weight)105 void SetFontWeightStr(ArkUINodeHandle node, ArkUI_CharPtr weight)
106 {
107 auto* frameNode = reinterpret_cast<FrameNode*>(node);
108 CHECK_NULL_VOID(frameNode);
109 SymbolModelNG::SetFontWeight(frameNode, ConvertStrToFontWeight(weight));
110 }
111
SetFontWeight(ArkUINodeHandle node,ArkUI_Int32 weight)112 void SetFontWeight(ArkUINodeHandle node, ArkUI_Int32 weight)
113 {
114 auto* frameNode = reinterpret_cast<FrameNode*>(node);
115 CHECK_NULL_VOID(frameNode);
116 SymbolModelNG::SetFontWeight(frameNode, ConvertIntToFontWeight(weight));
117 }
118
ResetFontWeight(ArkUINodeHandle node)119 void ResetFontWeight(ArkUINodeHandle node)
120 {
121 auto* frameNode = reinterpret_cast<FrameNode*>(node);
122 CHECK_NULL_VOID(frameNode);
123 SymbolModelNG::SetFontWeight(frameNode, Ace::FontWeight::NORMAL);
124 }
125
SetRenderingStrategy(ArkUINodeHandle node,ArkUI_Uint32 renderingStrategy)126 void SetRenderingStrategy(ArkUINodeHandle node, ArkUI_Uint32 renderingStrategy)
127 {
128 auto* frameNode = reinterpret_cast<FrameNode*>(node);
129 CHECK_NULL_VOID(frameNode);
130 SymbolModelNG::SetRenderingStrategy(frameNode, renderingStrategy);
131 }
132
ResetRenderingStrategy(ArkUINodeHandle node)133 void ResetRenderingStrategy(ArkUINodeHandle node)
134 {
135 auto* frameNode = reinterpret_cast<FrameNode*>(node);
136 CHECK_NULL_VOID(frameNode);
137 SymbolModelNG::SetRenderingStrategy(frameNode, 0);
138 }
139
SetEffectStrategy(ArkUINodeHandle node,ArkUI_Uint32 effectStrategy)140 void SetEffectStrategy(ArkUINodeHandle node, ArkUI_Uint32 effectStrategy)
141 {
142 auto* frameNode = reinterpret_cast<FrameNode*>(node);
143 CHECK_NULL_VOID(frameNode);
144 SymbolModelNG::SetSymbolEffect(frameNode, effectStrategy);
145 }
146
ResetEffectStrategy(ArkUINodeHandle node)147 void ResetEffectStrategy(ArkUINodeHandle node)
148 {
149 auto* frameNode = reinterpret_cast<FrameNode*>(node);
150 CHECK_NULL_VOID(frameNode);
151 SymbolModelNG::SetSymbolEffect(frameNode, 0);
152 }
153
SetSymbolId(ArkUINodeHandle node,ArkUI_Uint32 symbolId)154 void SetSymbolId(ArkUINodeHandle node, ArkUI_Uint32 symbolId)
155 {
156 auto* frameNode = reinterpret_cast<FrameNode*>(node);
157 CHECK_NULL_VOID(frameNode);
158 SymbolModelNG::InitialSymbol(frameNode, symbolId);
159 }
160 }
161
162 namespace NodeModifier {
GetSymbolGlyphModifier()163 const ArkUISymbolGlyphModifier* GetSymbolGlyphModifier()
164 {
165 static const ArkUISymbolGlyphModifier modifier = {
166 SetFontColor,
167 ResetFontColor,
168 SetFontSize,
169 ResetFontSize,
170 SetFontWeightStr,
171 SetFontWeight,
172 ResetFontWeight,
173 SetRenderingStrategy,
174 ResetRenderingStrategy,
175 SetEffectStrategy,
176 ResetEffectStrategy,
177 SetSymbolId,
178 };
179
180 return &modifier;
181 }
182
GetCJUISymbolGlyphModifier()183 const CJUISymbolGlyphModifier* GetCJUISymbolGlyphModifier()
184 {
185 static const CJUISymbolGlyphModifier modifier = {
186 SetFontColor,
187 ResetFontColor,
188 SetFontSize,
189 ResetFontSize,
190 SetFontWeightStr,
191 SetFontWeight,
192 ResetFontWeight,
193 SetRenderingStrategy,
194 ResetRenderingStrategy,
195 SetEffectStrategy,
196 ResetEffectStrategy,
197 SetSymbolId,
198 };
199
200 return &modifier;
201 }
202 } // namespace NodeModifier
203 } // namespace OHOS::Ace::NG