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 #include "core/interfaces/native/node/text_clock_modifier.h"
16
17 #include "bridge/common/utils/utils.h"
18 #include "core/components/common/properties/text_style_parser.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/base/view_abstract.h"
21 #include "core/components_ng/pattern/text_clock/text_clock_model_ng.h"
22 #include "core/pipeline/base/element_register.h"
23
24 namespace OHOS::Ace::NG {
25 constexpr Ace::FontStyle DEFAULT_FONT_STYLE = Ace::FontStyle::NORMAL;
26 const std::vector<OHOS::Ace::FontStyle> FONT_STYLES = { OHOS::Ace::FontStyle::NORMAL, OHOS::Ace::FontStyle::ITALIC };
27 constexpr Ace::FontWeight DEFAULT_FONT_WEIGHT = Ace::FontWeight::NORMAL;
28 constexpr Dimension DEFAULT_FONT_SIZE = Dimension(16.0, DimensionUnit::FP);
29 const std::string DEFAULT_FONT_FAMILY = "HarmonyOS Sans";
30
31 namespace TextClockModifier {
SetFormat(ArkUINodeHandle node,ArkUI_CharPtr format)32 void SetFormat(ArkUINodeHandle node, ArkUI_CharPtr format)
33 {
34 auto* frameNode = reinterpret_cast<FrameNode*>(node);
35 CHECK_NULL_VOID(frameNode);
36 TextClockModelNG::SetFormat(frameNode, format);
37 }
38
ResetFormat(ArkUINodeHandle node)39 void ResetFormat(ArkUINodeHandle node)
40 {
41 auto* frameNode = reinterpret_cast<FrameNode*>(node);
42 CHECK_NULL_VOID(frameNode);
43 TextClockModelNG::SetFormat(frameNode, "");
44 }
45
SetFontColor(ArkUINodeHandle node,ArkUI_Uint32 color)46 void SetFontColor(ArkUINodeHandle node, ArkUI_Uint32 color)
47 {
48 auto* frameNode = reinterpret_cast<FrameNode*>(node);
49 CHECK_NULL_VOID(frameNode);
50 TextClockModelNG::SetFontColor(frameNode, Color(color));
51 }
52
ResetFontColor(ArkUINodeHandle node)53 void ResetFontColor(ArkUINodeHandle node)
54 {
55 auto* frameNode = reinterpret_cast<FrameNode*>(node);
56 CHECK_NULL_VOID(frameNode);
57 auto pipelineContext = PipelineBase::GetCurrentContext();
58 CHECK_NULL_VOID(pipelineContext);
59 auto theme = pipelineContext->GetTheme<TextTheme>();
60 CHECK_NULL_VOID(theme);
61 TextClockModelNG::SetFontColor(frameNode, theme->GetTextStyle().GetTextColor());
62 }
63
SetFontSize(ArkUINodeHandle node,ArkUI_Float32 fontSizeValue,ArkUI_Int32 fontSizeUnit)64 void SetFontSize(ArkUINodeHandle node, ArkUI_Float32 fontSizeValue, ArkUI_Int32 fontSizeUnit)
65 {
66 auto* frameNode = reinterpret_cast<FrameNode*>(node);
67 CHECK_NULL_VOID(frameNode);
68 TextClockModelNG::SetFontSize(frameNode, CalcDimension(fontSizeValue, (DimensionUnit)fontSizeUnit));
69 }
70
ResetFontSize(ArkUINodeHandle node)71 void ResetFontSize(ArkUINodeHandle node)
72 {
73 auto* frameNode = reinterpret_cast<FrameNode*>(node);
74 CHECK_NULL_VOID(frameNode);
75 TextClockModelNG::SetFontSize(frameNode, DEFAULT_FONT_SIZE);
76 }
77
SetFontStyle(ArkUINodeHandle node,ArkUI_Uint32 fontStyle)78 void SetFontStyle(ArkUINodeHandle node, ArkUI_Uint32 fontStyle)
79 {
80 auto* frameNode = reinterpret_cast<FrameNode*>(node);
81 CHECK_NULL_VOID(frameNode);
82 TextClockModelNG::SetFontStyle(frameNode, FONT_STYLES[fontStyle]);
83 }
84
ResetFontStyle(ArkUINodeHandle node)85 void ResetFontStyle(ArkUINodeHandle node)
86 {
87 auto* frameNode = reinterpret_cast<FrameNode*>(node);
88 CHECK_NULL_VOID(frameNode);
89 TextClockModelNG::SetFontStyle(frameNode, DEFAULT_FONT_STYLE);
90 }
91
SetFontWeight(ArkUINodeHandle node,ArkUI_CharPtr fontWeight)92 void SetFontWeight(ArkUINodeHandle node, ArkUI_CharPtr fontWeight)
93 {
94 auto* frameNode = reinterpret_cast<FrameNode*>(node);
95 CHECK_NULL_VOID(frameNode);
96 std::string fontWeightStr = fontWeight;
97 TextClockModelNG::SetFontWeight(frameNode, Framework::ConvertStrToFontWeight(fontWeightStr));
98 }
99
ResetFontWeight(ArkUINodeHandle node)100 void ResetFontWeight(ArkUINodeHandle node)
101 {
102 auto* frameNode = reinterpret_cast<FrameNode*>(node);
103 CHECK_NULL_VOID(frameNode);
104 TextClockModelNG::SetFontWeight(frameNode, DEFAULT_FONT_WEIGHT);
105 }
106
SetFontFamily(ArkUINodeHandle node,ArkUI_CharPtr fontFamily)107 void SetFontFamily(ArkUINodeHandle node, ArkUI_CharPtr fontFamily)
108 {
109 auto* frameNode = reinterpret_cast<FrameNode*>(node);
110 CHECK_NULL_VOID(frameNode);
111 std::string familiesStr = fontFamily;
112 std::vector<std::string> fontFamilyResult = Framework::ConvertStrToFontFamilies(familiesStr);
113 TextClockModelNG::SetFontFamily(frameNode, fontFamilyResult);
114 }
115
ResetFontFamily(ArkUINodeHandle node)116 void ResetFontFamily(ArkUINodeHandle node)
117 {
118 auto* frameNode = reinterpret_cast<FrameNode*>(node);
119 CHECK_NULL_VOID(frameNode);
120 std::string familiesStr = DEFAULT_FONT_FAMILY;
121 std::vector<std::string> fontFamilyResult = Framework::ConvertStrToFontFamilies(familiesStr);
122 TextClockModelNG::SetFontFamily(frameNode, fontFamilyResult);
123 }
124
SetTextShadow(ArkUINodeHandle node,struct ArkUITextShadowStruct * shadows,ArkUI_Uint32 length)125 void SetTextShadow(ArkUINodeHandle node, struct ArkUITextShadowStruct* shadows, ArkUI_Uint32 length)
126 {
127 CHECK_NULL_VOID(shadows);
128 auto* frameNode = reinterpret_cast<FrameNode*>(node);
129 CHECK_NULL_VOID(frameNode);
130 std::vector<Shadow> shadowList(length);
131 for (uint32_t i = 0; i < length; i++) {
132 Shadow shadow;
133 ArkUITextShadowStruct* shadowStruct = shadows + i;
134 shadow.SetBlurRadius(shadowStruct->radius);
135 shadow.SetShadowType(static_cast<ShadowType>(shadowStruct->type));
136 shadow.SetColor(Color(shadowStruct->color));
137 shadow.SetOffsetX(shadowStruct->offsetX);
138 shadow.SetOffsetY(shadowStruct->offsetY);
139 shadow.SetIsFilled(static_cast<bool>(shadowStruct->fill));
140 shadowList.at(i) = shadow;
141 }
142 TextClockModelNG::SetTextShadow(frameNode, shadowList);
143 }
144
ResetTextShadow(ArkUINodeHandle node)145 void ResetTextShadow(ArkUINodeHandle node)
146 {
147 auto* frameNode = reinterpret_cast<FrameNode*>(node);
148 CHECK_NULL_VOID(frameNode);
149 Shadow shadow;
150 shadow.SetOffsetX(0.0);
151 shadow.SetOffsetY(0.0);
152 TextClockModelNG::SetTextShadow(frameNode, std::vector<Shadow> { shadow });
153 }
154
SetFontFeature(ArkUINodeHandle node,ArkUI_CharPtr value)155 void SetFontFeature(ArkUINodeHandle node, ArkUI_CharPtr value)
156 {
157 auto* frameNode = reinterpret_cast<FrameNode*>(node);
158 CHECK_NULL_VOID(frameNode);
159 std::string strValue = value;
160 TextClockModelNG::SetFontFeature(frameNode, ParseFontFeatureSettings(strValue));
161 }
162
ResetFontFeature(ArkUINodeHandle node)163 void ResetFontFeature(ArkUINodeHandle node)
164 {
165 auto* frameNode = reinterpret_cast<FrameNode*>(node);
166 CHECK_NULL_VOID(frameNode);
167 std::string strValue = "";
168 TextClockModelNG::SetFontFeature(frameNode, ParseFontFeatureSettings(strValue));
169 }
170
SetDateTimeOptions(ArkUINodeHandle node,ArkUI_Int32 hourType)171 void SetDateTimeOptions(ArkUINodeHandle node, ArkUI_Int32 hourType)
172 {
173 auto* frameNode = reinterpret_cast<FrameNode*>(node);
174 CHECK_NULL_VOID(frameNode);
175 ZeroPrefixType hour = static_cast<ZeroPrefixType>(hourType);
176 TextClockModelNG::SetDateTimeOptions(frameNode, hour);
177 }
178
ResetDateTimeOptions(ArkUINodeHandle node)179 void ResetDateTimeOptions(ArkUINodeHandle node)
180 {
181 auto* frameNode = reinterpret_cast<FrameNode*>(node);
182 CHECK_NULL_VOID(frameNode);
183 ZeroPrefixType hourType = ZeroPrefixType::AUTO;
184 TextClockModelNG::SetDateTimeOptions(frameNode, hourType);
185 }
186
SetTextClockTimeZoneOffset(ArkUINodeHandle node,ArkUI_Float32 timeZoneOffset)187 void SetTextClockTimeZoneOffset(ArkUINodeHandle node, ArkUI_Float32 timeZoneOffset)
188 {
189 auto* frameNode = reinterpret_cast<FrameNode*>(node);
190 CHECK_NULL_VOID(frameNode);
191 TextClockModelNG::SetHoursWest(frameNode, timeZoneOffset);
192 }
193 } // namespace TextClockModifier
194
195 namespace NodeModifier {
GetTextClockModifier()196 const ArkUITextClockModifier* GetTextClockModifier()
197 {
198 static const ArkUITextClockModifier modifier = {
199 TextClockModifier::SetFormat,
200 TextClockModifier::ResetFormat,
201 TextClockModifier::SetFontColor,
202 TextClockModifier::ResetFontColor,
203 TextClockModifier::SetFontSize,
204 TextClockModifier::ResetFontSize,
205 TextClockModifier::SetFontStyle,
206 TextClockModifier::ResetFontStyle,
207 TextClockModifier::SetFontWeight,
208 TextClockModifier::ResetFontWeight,
209 TextClockModifier::SetFontFamily,
210 TextClockModifier::ResetFontFamily,
211 TextClockModifier::SetTextShadow,
212 TextClockModifier::ResetTextShadow,
213 TextClockModifier::SetFontFeature,
214 TextClockModifier::ResetFontFeature,
215 TextClockModifier::SetDateTimeOptions,
216 TextClockModifier::ResetDateTimeOptions,
217 TextClockModifier::SetTextClockTimeZoneOffset
218 };
219
220 return &modifier;
221 }
222
GetCJUITextClockModifier()223 const CJUITextClockModifier* GetCJUITextClockModifier()
224 {
225 static const CJUITextClockModifier modifier = {
226 TextClockModifier::SetFormat,
227 TextClockModifier::ResetFormat,
228 TextClockModifier::SetFontColor,
229 TextClockModifier::ResetFontColor,
230 TextClockModifier::SetFontSize,
231 TextClockModifier::ResetFontSize,
232 TextClockModifier::SetFontStyle,
233 TextClockModifier::ResetFontStyle,
234 TextClockModifier::SetFontWeight,
235 TextClockModifier::ResetFontWeight,
236 TextClockModifier::SetFontFamily,
237 TextClockModifier::ResetFontFamily,
238 TextClockModifier::SetTextShadow,
239 TextClockModifier::ResetTextShadow,
240 TextClockModifier::SetFontFeature,
241 TextClockModifier::ResetFontFeature
242 };
243
244 return &modifier;
245 }
246 } // namespace NodeModifier
247 } // namespace OHOS::Ace::NG