• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 } // namespace TextTimerModifier
174 
175 namespace NodeModifier {
GetTextTimerModifier()176 const ArkUITextTimerModifier* GetTextTimerModifier()
177 {
178     static const ArkUITextTimerModifier modifier = {
179         TextTimerModifier::SetFontColor,
180         TextTimerModifier::ResetFontColor,
181         TextTimerModifier::SetFontSize,
182         TextTimerModifier::ResetFontSize,
183         TextTimerModifier::SetFontStyle,
184         TextTimerModifier::ResetFontStyle,
185         TextTimerModifier::SetFontWeight,
186         TextTimerModifier::ResetFontWeight,
187         TextTimerModifier::SetFontFamily,
188         TextTimerModifier::ResetFontFamily,
189         TextTimerModifier::SetFormat,
190         TextTimerModifier::ResetFormat,
191         TextTimerModifier::SetTextShadow,
192         TextTimerModifier::ResetTextShadow
193     };
194 
195     return &modifier;
196 }
197 
GetCJUITextTimerModifier()198 const CJUITextTimerModifier* GetCJUITextTimerModifier()
199 {
200     static const CJUITextTimerModifier modifier = {
201         TextTimerModifier::SetFontColor,
202         TextTimerModifier::ResetFontColor,
203         TextTimerModifier::SetFontSize,
204         TextTimerModifier::ResetFontSize,
205         TextTimerModifier::SetFontStyle,
206         TextTimerModifier::ResetFontStyle,
207         TextTimerModifier::SetFontWeight,
208         TextTimerModifier::ResetFontWeight,
209         TextTimerModifier::SetFontFamily,
210         TextTimerModifier::ResetFontFamily,
211         TextTimerModifier::SetFormat,
212         TextTimerModifier::ResetFormat,
213         TextTimerModifier::SetTextShadow,
214         TextTimerModifier::ResetTextShadow
215     };
216 
217     return &modifier;
218 }
219 } // namespace NodeModifier
220 } // namespace OHOS::Ace::NG