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_timer_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/components_ng/pattern/texttimer/text_timer_model_ng.h"
24 #include "core/pipeline/base/element_register.h"
25
26 namespace OHOS::Ace::NG {
27 constexpr Dimension DEFAULT_FONT_SIZE = Dimension(16.0, DimensionUnit::FP);
28 const std::vector<std::string> DEFAULT_FONT_FAMILY = { "HarmonyOS Sans" };
29 const std::string DEFAULT_FORMAT = "HH:mm:ss.SS";
30 constexpr Ace::FontWeight DEFAULT_FONT_WEIGHT = Ace::FontWeight::NORMAL;
31
32 namespace TextTimerModifier {
SetFontColor(ArkUINodeHandle node,ArkUI_Uint32 color)33 void SetFontColor(ArkUINodeHandle node, ArkUI_Uint32 color)
34 {
35 auto *frameNode = reinterpret_cast<FrameNode *>(node);
36 CHECK_NULL_VOID(frameNode);
37 TextTimerModelNG::SetFontColor(frameNode, Color(color));
38 }
39
ResetFontColor(ArkUINodeHandle node)40 void ResetFontColor(ArkUINodeHandle node)
41 {
42 auto *frameNode = reinterpret_cast<FrameNode *>(node);
43 CHECK_NULL_VOID(frameNode);
44 auto pipelineContext = PipelineBase::GetCurrentContext();
45 CHECK_NULL_VOID(pipelineContext);
46 auto theme = pipelineContext->GetTheme<TextTheme>();
47 CHECK_NULL_VOID(theme);
48 TextTimerModelNG::SetFontColor(frameNode, theme->GetTextStyle().GetTextColor());
49 }
50
SetFontSize(ArkUINodeHandle node,ArkUI_Float32 value,ArkUI_Int32 unit)51 void SetFontSize(ArkUINodeHandle node, ArkUI_Float32 value, ArkUI_Int32 unit)
52 {
53 auto *frameNode = reinterpret_cast<FrameNode *>(node);
54 CHECK_NULL_VOID(frameNode);
55 TextTimerModelNG::SetFontSize(frameNode, Dimension(value, static_cast<DimensionUnit>(unit)));
56 }
57
ResetFontSize(ArkUINodeHandle node)58 void ResetFontSize(ArkUINodeHandle node)
59 {
60 auto *frameNode = reinterpret_cast<FrameNode *>(node);
61 CHECK_NULL_VOID(frameNode);
62 TextTimerModelNG::SetFontSize(frameNode, DEFAULT_FONT_SIZE);
63 }
64
SetFontStyle(ArkUINodeHandle node,ArkUI_Uint32 value)65 void SetFontStyle(ArkUINodeHandle node, ArkUI_Uint32 value)
66 {
67 auto *frameNode = reinterpret_cast<FrameNode *>(node);
68 CHECK_NULL_VOID(frameNode);
69 TextTimerModelNG::SetFontStyle(frameNode, static_cast<Ace::FontStyle>(value));
70 }
71
ResetFontStyle(ArkUINodeHandle node)72 void ResetFontStyle(ArkUINodeHandle node)
73 {
74 auto *frameNode = reinterpret_cast<FrameNode *>(node);
75 CHECK_NULL_VOID(frameNode);
76 TextTimerModelNG::SetFontStyle(frameNode, OHOS::Ace::FontStyle::NORMAL);
77 }
78
SetFontWeight(ArkUINodeHandle node,ArkUI_CharPtr fontWeight)79 void SetFontWeight(ArkUINodeHandle node, ArkUI_CharPtr fontWeight)
80 {
81 auto *frameNode = reinterpret_cast<FrameNode *>(node);
82 CHECK_NULL_VOID(frameNode);
83 std::string fontWeightStr = fontWeight;
84 TextTimerModelNG::SetFontWeight(frameNode, Framework::ConvertStrToFontWeight(fontWeightStr));
85 }
86
ResetFontWeight(ArkUINodeHandle node)87 void ResetFontWeight(ArkUINodeHandle node)
88 {
89 auto *frameNode = reinterpret_cast<FrameNode *>(node);
90 CHECK_NULL_VOID(frameNode);
91 TextTimerModelNG::SetFontWeight(frameNode, DEFAULT_FONT_WEIGHT);
92 }
93
SetFontFamily(ArkUINodeHandle node,ArkUI_CharPtr fontFamily)94 void SetFontFamily(ArkUINodeHandle node, ArkUI_CharPtr fontFamily)
95 {
96 auto* frameNode = reinterpret_cast<FrameNode*>(node);
97 CHECK_NULL_VOID(frameNode);
98 std::string familiesStr = fontFamily;
99 std::vector<std::string> fontFamilyResult = Framework::ConvertStrToFontFamilies(familiesStr);
100 TextTimerModelNG::SetFontFamily(frameNode, fontFamilyResult);
101 }
102
ResetFontFamily(ArkUINodeHandle node)103 void ResetFontFamily(ArkUINodeHandle node)
104 {
105 auto *frameNode = reinterpret_cast<FrameNode *>(node);
106 CHECK_NULL_VOID(frameNode);
107 TextTimerModelNG::SetFontFamily(frameNode, DEFAULT_FONT_FAMILY);
108 }
109
SetFormat(ArkUINodeHandle node,ArkUI_CharPtr format)110 void SetFormat(ArkUINodeHandle node, ArkUI_CharPtr format)
111 {
112 auto* frameNode = reinterpret_cast<FrameNode*>(node);
113 CHECK_NULL_VOID(frameNode);
114 std::string formatStr = format;
115 std::smatch result;
116 std::regex pattern("(([YyMdD]+))");
117 if (std::regex_search(formatStr, result, pattern)) {
118 if (!result.empty()) {
119 formatStr = DEFAULT_FORMAT;
120 }
121 }
122
123 std::string target = "HmsS:.";
124 for (auto ch : formatStr) {
125 if (target.find(ch) == std::string::npos) {
126 formatStr = DEFAULT_FORMAT;
127 }
128 }
129
130 auto pos = formatStr.find("hh");
131 if (pos != std::string::npos) {
132 formatStr.replace(pos, sizeof("hh") - 1, "HH");
133 }
134 TextTimerModelNG::SetFormat(frameNode, formatStr);
135 }
136
ResetFormat(ArkUINodeHandle node)137 void ResetFormat(ArkUINodeHandle node)
138 {
139 auto* frameNode = reinterpret_cast<FrameNode*>(node);
140 CHECK_NULL_VOID(frameNode);
141 TextTimerModelNG::SetFormat(frameNode, DEFAULT_FORMAT);
142 }
143
SetTextShadow(ArkUINodeHandle node,struct ArkUITextShadowStruct * shadows,ArkUI_Uint32 length)144 void SetTextShadow(ArkUINodeHandle node, struct ArkUITextShadowStruct* shadows, ArkUI_Uint32 length)
145 {
146 CHECK_NULL_VOID(shadows);
147 auto* frameNode = reinterpret_cast<FrameNode*>(node);
148 CHECK_NULL_VOID(frameNode);
149 std::vector<Shadow> shadowList(length);
150 for (uint32_t i = 0; i < length; i++) {
151 Shadow shadow;
152 ArkUITextShadowStruct* shadowStruct = shadows + i;
153 shadow.SetBlurRadius(shadowStruct->radius);
154 shadow.SetShadowType(static_cast<ShadowType>(shadowStruct->type));
155 shadow.SetColor(Color(shadowStruct->color));
156 shadow.SetOffsetX(shadowStruct->offsetX);
157 shadow.SetOffsetY(shadowStruct->offsetY);
158 shadow.SetIsFilled(static_cast<bool>(shadowStruct->fill));
159 shadowList.at(i) = shadow;
160 }
161 TextTimerModelNG::SetTextShadow(frameNode, shadowList);
162 }
163
ResetTextShadow(ArkUINodeHandle node)164 void ResetTextShadow(ArkUINodeHandle node)
165 {
166 auto* frameNode = reinterpret_cast<FrameNode*>(node);
167 CHECK_NULL_VOID(frameNode);
168 Shadow shadow;
169 shadow.SetOffsetX(0.0);
170 shadow.SetOffsetY(0.0);
171 TextTimerModelNG::SetTextShadow(frameNode, std::vector<Shadow> { shadow });
172 }
173
setTextTimerOptions(ArkUINodeHandle node,ArkUI_Bool isCountDown,ArkUI_Float64 count)174 void setTextTimerOptions(ArkUINodeHandle node, ArkUI_Bool isCountDown, ArkUI_Float64 count)
175 {
176 auto* frameNode = reinterpret_cast<FrameNode*>(node);
177 CHECK_NULL_VOID(frameNode);
178 TextTimerModelNG::SetIsCountDown(frameNode, isCountDown);
179 if (isCountDown) {
180 TextTimerModelNG::SetInputCount(frameNode, count);
181 }
182 }
183 } // namespace TextTimerModifier
184
185 namespace NodeModifier {
GetTextTimerModifier()186 const ArkUITextTimerModifier* GetTextTimerModifier()
187 {
188 static const ArkUITextTimerModifier modifier = {
189 TextTimerModifier::SetFontColor,
190 TextTimerModifier::ResetFontColor,
191 TextTimerModifier::SetFontSize,
192 TextTimerModifier::ResetFontSize,
193 TextTimerModifier::SetFontStyle,
194 TextTimerModifier::ResetFontStyle,
195 TextTimerModifier::SetFontWeight,
196 TextTimerModifier::ResetFontWeight,
197 TextTimerModifier::SetFontFamily,
198 TextTimerModifier::ResetFontFamily,
199 TextTimerModifier::SetFormat,
200 TextTimerModifier::ResetFormat,
201 TextTimerModifier::SetTextShadow,
202 TextTimerModifier::ResetTextShadow,
203 TextTimerModifier::setTextTimerOptions
204 };
205
206 return &modifier;
207 }
208
GetCJUITextTimerModifier()209 const CJUITextTimerModifier* GetCJUITextTimerModifier()
210 {
211 static const CJUITextTimerModifier modifier = {
212 TextTimerModifier::SetFontColor,
213 TextTimerModifier::ResetFontColor,
214 TextTimerModifier::SetFontSize,
215 TextTimerModifier::ResetFontSize,
216 TextTimerModifier::SetFontStyle,
217 TextTimerModifier::ResetFontStyle,
218 TextTimerModifier::SetFontWeight,
219 TextTimerModifier::ResetFontWeight,
220 TextTimerModifier::SetFontFamily,
221 TextTimerModifier::ResetFontFamily,
222 TextTimerModifier::SetFormat,
223 TextTimerModifier::ResetFormat,
224 TextTimerModifier::SetTextShadow,
225 TextTimerModifier::ResetTextShadow
226 };
227
228 return &modifier;
229 }
230 } // namespace NodeModifier
231 } // namespace OHOS::Ace::NG