• 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/node_span_modifier.h"
16 
17 #include "base/utils/utf_helper.h"
18 #include "bridge/common/utils/utils.h"
19 #include "core/common/resource/resource_parse_utils.h"
20 #include "core/components_ng/pattern/text/span_model_ng.h"
21 #include "core/pipeline/base/element_register.h"
22 #include "draw/canvas.h"
23 #include "frameworks/core/components/common/properties/text_style.h"
24 
25 namespace OHOS::Ace::NG {
26 namespace {
27 constexpr TextCase DEFAULT_TEXT_CASE = TextCase::NORMAL;
28 constexpr FontWeight DEFAULT_FONT_WEIGHT = FontWeight::NORMAL;
29 constexpr Ace::FontStyle DEFAULT_FONT_STYLE_VALUE = Ace::FontStyle::NORMAL;
30 constexpr Dimension DEFAULT_FONT_SIZE = Dimension(16.0, DimensionUnit::FP);
31 constexpr Dimension DEFAULT_BASELINE_OFFSET { 0.0, DimensionUnit::FP };
32 thread_local std::string g_strValue;
33 constexpr int NUM_0 = 0;
34 constexpr int NUM_1 = 1;
35 constexpr int NUM_2 = 2;
36 constexpr int NUM_3 = 3;
37 constexpr int NUM_32 = 32;
38 constexpr int DEFAULT_LENGTH = 4;
SetSpanContent(ArkUINodeHandle node,const char * value)39 void SetSpanContent(ArkUINodeHandle node, const char* value)
40 {
41     CHECK_NULL_VOID(value);
42     auto* uiNode = reinterpret_cast<UINode*>(node);
43     CHECK_NULL_VOID(uiNode);
44     std::string content(value);
45     SpanModelNG::InitSpan(uiNode, UtfUtils::Str8DebugToStr16(content));
46 }
47 
GetSpanContent(ArkUINodeHandle node)48 const char* GetSpanContent(ArkUINodeHandle node)
49 {
50     auto* uiNode = reinterpret_cast<UINode*>(node);
51     CHECK_NULL_RETURN(uiNode, nullptr);
52     g_strValue = UtfUtils::Str16DebugToStr8(SpanModelNG::GetContent(uiNode));
53     return g_strValue.c_str();
54 }
55 
SetSpanSrc(ArkUINodeHandle node,ArkUI_CharPtr src)56 void SetSpanSrc(ArkUINodeHandle node, ArkUI_CharPtr src)
57 {
58     auto* uiNode = reinterpret_cast<UINode*>(node);
59     CHECK_NULL_VOID(uiNode);
60     std::string content(src);
61     SpanModelNG::InitSpan(uiNode, UtfUtils::Str8DebugToStr16(content));
62 }
63 
SetSpanTextCase(ArkUINodeHandle node,int32_t value)64 void SetSpanTextCase(ArkUINodeHandle node, int32_t value)
65 {
66     auto* uiNode = reinterpret_cast<UINode*>(node);
67     CHECK_NULL_VOID(uiNode);
68     SpanModelNG::SetTextCase(uiNode, static_cast<TextCase>(value));
69 }
70 
GetSpanTextCase(ArkUINodeHandle node)71 int32_t GetSpanTextCase(ArkUINodeHandle node)
72 {
73     auto defaultTextCase = static_cast<int32_t>(DEFAULT_TEXT_CASE);
74     auto* uiNode = reinterpret_cast<UINode*>(node);
75     CHECK_NULL_RETURN(uiNode, defaultTextCase);
76     return static_cast<int32_t>(SpanModelNG::GetTextCase(uiNode));
77 }
78 
ResetSpanTextCase(ArkUINodeHandle node)79 void ResetSpanTextCase(ArkUINodeHandle node)
80 {
81     auto* uiNode = reinterpret_cast<UINode*>(node);
82     CHECK_NULL_VOID(uiNode);
83     SpanModelNG::ResetTextCase(uiNode);
84 }
85 
SetSpanFontWeightStr(ArkUINodeHandle node,const char * value)86 void SetSpanFontWeightStr(ArkUINodeHandle node, const char* value)
87 {
88     auto* uiNode = reinterpret_cast<UINode*>(node);
89     CHECK_NULL_VOID(uiNode);
90     SpanModelNG::SetFontWeight(uiNode, Framework::ConvertStrToFontWeight(value));
91 }
92 
SetSpanFontWeight(ArkUINodeHandle node,ArkUI_Int32 fontWeight,void * resRawPtr)93 void SetSpanFontWeight(ArkUINodeHandle node, ArkUI_Int32 fontWeight, void* resRawPtr)
94 {
95     auto* uiNode = reinterpret_cast<UINode*>(node);
96     CHECK_NULL_VOID(uiNode);
97     SpanModelNG::SetFontWeight(uiNode, static_cast<FontWeight>(fontWeight));
98     NodeModifier::ProcessResourceObj<FontWeight>(
99         uiNode, "fontWeight", static_cast<FontWeight>(fontWeight), resRawPtr);
100 }
101 
GetSpanFontWeight(ArkUINodeHandle node)102 int32_t GetSpanFontWeight(ArkUINodeHandle node)
103 {
104     int32_t defaultFontWeight = static_cast<int32_t>(DEFAULT_FONT_WEIGHT);
105     auto* uiNode = reinterpret_cast<UINode*>(node);
106     CHECK_NULL_RETURN(uiNode, defaultFontWeight);
107     return static_cast<int32_t>(SpanModelNG::GetFontWeight(uiNode));
108 }
109 
ResetSpanFontWeight(ArkUINodeHandle node)110 void ResetSpanFontWeight(ArkUINodeHandle node)
111 {
112     auto* uiNode = reinterpret_cast<UINode*>(node);
113     CHECK_NULL_VOID(uiNode);
114     SpanModelNG::ResetFontWeight(uiNode);
115     if (SystemProperties::ConfigChangePerform()) {
116         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
117         CHECK_NULL_VOID(spanNode);
118         spanNode->UnregisterResource("fontWeight");
119     }
120 }
121 
SetSpanLineHeight(ArkUINodeHandle node,ArkUI_Float32 number,ArkUI_Int32 unit,void * lineHeightRawPtr)122 void SetSpanLineHeight(ArkUINodeHandle node, ArkUI_Float32 number, ArkUI_Int32 unit, void* lineHeightRawPtr)
123 {
124     auto* uiNode = reinterpret_cast<UINode*>(node);
125     CHECK_NULL_VOID(uiNode);
126     SpanModelNG::SetLineHeight(uiNode, Dimension(number, static_cast<DimensionUnit>(unit)));
127     NodeModifier::ProcessResourceObj<CalcDimension>(
128         uiNode, "lineHeight", Dimension(number, static_cast<DimensionUnit>(unit)), lineHeightRawPtr);
129 }
130 
GetSpanLineHeight(ArkUINodeHandle node)131 float GetSpanLineHeight(ArkUINodeHandle node)
132 {
133     auto* uiNode = reinterpret_cast<UINode*>(node);
134     CHECK_NULL_RETURN(uiNode, 0.0f);
135     return SpanModelNG::GetTextLineHeight(uiNode).ConvertToVp();
136 }
137 
ResetSpanLineHeight(ArkUINodeHandle node)138 void ResetSpanLineHeight(ArkUINodeHandle node)
139 {
140     auto* uiNode = reinterpret_cast<UINode*>(node);
141     CHECK_NULL_VOID(uiNode);
142     SpanModelNG::ResetLineHeight(uiNode);
143     if (SystemProperties::ConfigChangePerform()) {
144         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
145         CHECK_NULL_VOID(spanNode);
146         spanNode->UnregisterResource("lineHeight");
147     }
148 }
149 
SetSpanFontStyle(ArkUINodeHandle node,int32_t value)150 void SetSpanFontStyle(ArkUINodeHandle node, int32_t value)
151 {
152     auto* uiNode = reinterpret_cast<UINode*>(node);
153     CHECK_NULL_VOID(uiNode);
154     SpanModelNG::SetItalicFontStyle(uiNode, static_cast<Ace::FontStyle>(value));
155 }
156 
GetSpanFontStyle(ArkUINodeHandle node)157 int32_t GetSpanFontStyle(ArkUINodeHandle node)
158 {
159     int32_t defaultFontStyle = static_cast<int32_t>(DEFAULT_FONT_STYLE_VALUE);
160     auto* uiNode = reinterpret_cast<UINode*>(node);
161     CHECK_NULL_RETURN(uiNode, defaultFontStyle);
162     return static_cast<int32_t>(SpanModelNG::GetFontStyle(uiNode));
163 }
164 
ResetSpanFontStyle(ArkUINodeHandle node)165 void ResetSpanFontStyle(ArkUINodeHandle node)
166 {
167     auto* uiNode = reinterpret_cast<UINode*>(node);
168     CHECK_NULL_VOID(uiNode);
169     SpanModelNG::ResetItalicFontStyle(uiNode);
170 }
171 
SetSpanFontSize(ArkUINodeHandle node,ArkUI_Float32 number,ArkUI_Int32 unit,void * fontSizeRawPtr)172 void SetSpanFontSize(ArkUINodeHandle node, ArkUI_Float32 number, ArkUI_Int32 unit, void* fontSizeRawPtr)
173 {
174     auto* uiNode = reinterpret_cast<UINode*>(node);
175     CHECK_NULL_VOID(uiNode);
176     auto fontSizeValue = Dimension(number, static_cast<DimensionUnit>(unit));
177     SpanModelNG::SetFontSize(uiNode, fontSizeValue);
178     NodeModifier::ProcessResourceObj<CalcDimension>(uiNode, "fontSize", fontSizeValue, fontSizeRawPtr);
179 }
180 
GetSpanFontSize(ArkUINodeHandle node,ArkUI_Int32 unit)181 float GetSpanFontSize(ArkUINodeHandle node, ArkUI_Int32 unit)
182 {
183     auto* uiNode = reinterpret_cast<UINode*>(node);
184     CHECK_NULL_RETURN(uiNode, DEFAULT_FONT_SIZE.Value());
185     return SpanModelNG::GetFontSize(uiNode).GetNativeValue(static_cast<DimensionUnit>(unit));
186 }
187 
ResetSpanFontSize(ArkUINodeHandle node)188 void ResetSpanFontSize(ArkUINodeHandle node)
189 {
190     auto* uiNode = reinterpret_cast<UINode*>(node);
191     CHECK_NULL_VOID(uiNode);
192     SpanModelNG::ResetFontSize(uiNode);
193     if (SystemProperties::ConfigChangePerform()) {
194         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
195         CHECK_NULL_VOID(spanNode);
196         spanNode->UnregisterResource("fontSize");
197     }
198 }
199 
SetSpanFontFamily(ArkUINodeHandle node,const char ** fontFamilies,uint32_t length,void * resObj)200 void SetSpanFontFamily(ArkUINodeHandle node, const char** fontFamilies, uint32_t length, void* resObj)
201 {
202     CHECK_NULL_VOID(fontFamilies);
203     if (length <= 0) {
204         return;
205     }
206     auto* uiNode = reinterpret_cast<UINode*>(node);
207     CHECK_NULL_VOID(uiNode);
208     std::vector<std::string> families;
209     for (uint32_t i = 0; i < length; i++) {
210         const char* family = *(fontFamilies + i);
211         if (family != nullptr) {
212             families.emplace_back(std::string(family));
213         }
214     }
215     SpanModelNG::SetFontFamily(uiNode, families);
216     NodeModifier::ProcessResourceObj<std::vector<std::string>>(uiNode, "fontFamily", families, resObj);
217 }
218 
ResetSpanFontFamily(ArkUINodeHandle node)219 void ResetSpanFontFamily(ArkUINodeHandle node)
220 {
221     auto* uiNode = reinterpret_cast<UINode*>(node);
222     CHECK_NULL_VOID(uiNode);
223     SpanModelNG::ResetFontFamily(uiNode);
224     if (SystemProperties::ConfigChangePerform()) {
225         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
226         CHECK_NULL_VOID(spanNode);
227         spanNode->UnregisterResource("fontFamily");
228     }
229 }
230 
SetSpanDecoration(ArkUINodeHandle node,ArkUI_Int32 decoration,ArkUI_Uint32 color,void * colorRawPtr,ArkUI_Int32 style,ArkUI_Float32 lineThicknessScale=1.0f)231 void SetSpanDecoration(ArkUINodeHandle node, ArkUI_Int32 decoration,
232     ArkUI_Uint32 color, void* colorRawPtr, ArkUI_Int32 style, ArkUI_Float32 lineThicknessScale = 1.0f)
233 {
234     auto* uiNode = reinterpret_cast<UINode*>(node);
235     CHECK_NULL_VOID(uiNode);
236     SpanModelNG::SetTextDecoration(uiNode, static_cast<TextDecoration>(decoration));
237     SpanModelNG::SetTextDecorationStyle(uiNode, static_cast<TextDecorationStyle>(style));
238     NodeModifier::ProcessResourceObj<Color>(uiNode, "decorationColor", Color(color), colorRawPtr);
239     SpanModelNG::SetTextDecorationColor(uiNode, Color(color));
240     SpanModelNG::SetLineThicknessScale(uiNode, lineThicknessScale);
241 }
242 
SetSpanDecoration(ArkUINodeHandle node,ArkUI_Int32 decoration,ArkUI_Uint32 color,ArkUI_Int32 style)243 void SetSpanDecoration(ArkUINodeHandle node, ArkUI_Int32 decoration, ArkUI_Uint32 color, ArkUI_Int32 style)
244 {
245     SetSpanDecoration(node, decoration, color, nullptr, style, 1.0f); // make cj happy
246 }
247 
GetSpanDecoration(ArkUINodeHandle node,ArkUITextDecorationType * decoration)248 void GetSpanDecoration(ArkUINodeHandle node, ArkUITextDecorationType* decoration)
249 {
250     CHECK_NULL_VOID(decoration);
251     auto* uiNode = reinterpret_cast<UINode*>(node);
252     CHECK_NULL_VOID(uiNode);
253     decoration->decorationType = static_cast<int32_t>(SpanModelNG::GetTextDecoration(uiNode));
254     decoration->color = SpanModelNG::GetTextDecorationColor(uiNode).GetValue();
255     decoration->style = static_cast<int32_t>(SpanModelNG::GetTextDecorationStyle(uiNode));
256 }
257 
ResetSpanDecoration(ArkUINodeHandle node)258 void ResetSpanDecoration(ArkUINodeHandle node)
259 {
260     auto* uiNode = reinterpret_cast<UINode*>(node);
261     CHECK_NULL_VOID(uiNode);
262     SpanModelNG::ResetTextDecoration(uiNode);
263     SpanModelNG::ResetTextDecorationStyle(uiNode);
264     SpanModelNG::ResetTextDecorationColor(uiNode);
265     SpanModelNG::ResetLineThicknessScale(uiNode);
266     if (SystemProperties::ConfigChangePerform()) {
267         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
268         CHECK_NULL_VOID(spanNode);
269         spanNode->UnregisterResource("decorationColor");
270     }
271 }
272 
SetSpanFontColor(ArkUINodeHandle node,uint32_t textColor,void * fontColorRawPtr)273 void SetSpanFontColor(ArkUINodeHandle node, uint32_t textColor, void* fontColorRawPtr)
274 {
275     auto* uiNode = reinterpret_cast<UINode*>(node);
276     CHECK_NULL_VOID(uiNode);
277     SpanModelNG::SetTextColor(uiNode, Color(textColor));
278     NodeModifier::ProcessResourceObj<Color>(uiNode, "fontColor", Color(textColor), fontColorRawPtr);
279 }
280 
GetSpanFontColor(ArkUINodeHandle node)281 uint32_t GetSpanFontColor(ArkUINodeHandle node)
282 {
283     auto* uiNode = reinterpret_cast<UINode*>(node);
284     CHECK_NULL_RETURN(uiNode, Color::BLACK.GetValue());
285     return SpanModelNG::GetFontColor(uiNode).GetValue();
286 }
287 
ResetSpanFontColor(ArkUINodeHandle node)288 void ResetSpanFontColor(ArkUINodeHandle node)
289 {
290     auto* uiNode = reinterpret_cast<UINode*>(node);
291     CHECK_NULL_VOID(uiNode);
292     SpanModelNG::ResetTextColor(uiNode);
293     if (SystemProperties::ConfigChangePerform()) {
294         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
295         CHECK_NULL_VOID(spanNode);
296         spanNode->UnregisterResource("fontColor");
297     }
298 }
299 
SetSpanLetterSpacing(ArkUINodeHandle node,const struct ArkUIStringAndFloat * letterSpacingValue,void * letterRawPtr)300 void SetSpanLetterSpacing(ArkUINodeHandle node, const struct ArkUIStringAndFloat* letterSpacingValue,
301     void* letterRawPtr)
302 {
303     auto* uiNode = reinterpret_cast<UINode*>(node);
304     CHECK_NULL_VOID(uiNode);
305     Dimension result;
306     if (letterSpacingValue->valueStr != nullptr) {
307         result = StringUtils::StringToDimensionWithUnit(std::string(letterSpacingValue->valueStr), DimensionUnit::FP);
308     } else {
309         result = Dimension(letterSpacingValue->value, DimensionUnit::FP);
310     }
311     SpanModelNG::SetLetterSpacing(uiNode, result);
312     NodeModifier::ProcessResourceObj<CalcDimension>(uiNode, "letterSpacing", result, letterRawPtr);
313 }
314 
GetSpanLetterSpacing(ArkUINodeHandle node)315 float GetSpanLetterSpacing(ArkUINodeHandle node)
316 {
317     auto* uiNode = reinterpret_cast<UINode*>(node);
318     CHECK_NULL_RETURN(uiNode, 0.0f);
319     return SpanModelNG::GetLetterSpacing(uiNode).ConvertToVp();
320 }
321 
ResetSpanLetterSpacing(ArkUINodeHandle node)322 void ResetSpanLetterSpacing(ArkUINodeHandle node)
323 {
324     auto* uiNode = reinterpret_cast<UINode*>(node);
325     CHECK_NULL_VOID(uiNode);
326     SpanModelNG::ResetLetterSpacing(uiNode);
327     if (SystemProperties::ConfigChangePerform()) {
328         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
329         CHECK_NULL_VOID(spanNode);
330         spanNode->UnregisterResource("letterSpacing");
331     }
332 }
333 
SetSpanBaselineOffset(ArkUINodeHandle node,ArkUI_Float32 value,ArkUI_Int32 unit,void * resourceRawPtr)334 void SetSpanBaselineOffset(ArkUINodeHandle node, ArkUI_Float32 value, ArkUI_Int32 unit, void* resourceRawPtr)
335 {
336     auto* uiNode = reinterpret_cast<UINode*>(node);
337     CHECK_NULL_VOID(uiNode);
338     SpanModelNG::SetBaselineOffset(uiNode, CalcDimension(value, (DimensionUnit)unit));
339     NodeModifier::ProcessResourceObj<CalcDimension>(
340         uiNode, "baselineOffset", CalcDimension(value, (DimensionUnit)unit), resourceRawPtr);
341 }
342 
GetSpanBaselineOffset(ArkUINodeHandle node)343 float GetSpanBaselineOffset(ArkUINodeHandle node)
344 {
345     auto* uiNode = reinterpret_cast<UINode*>(node);
346     CHECK_NULL_RETURN(uiNode, 0.0f);
347     return SpanModelNG::GetBaselineOffset(uiNode).ConvertToVp();
348 }
349 
ResetSpanBaselineOffset(ArkUINodeHandle node)350 void ResetSpanBaselineOffset(ArkUINodeHandle node)
351 {
352     auto* uiNode = reinterpret_cast<UINode*>(node);
353     CHECK_NULL_VOID(uiNode);
354     SpanModelNG::SetBaselineOffset(uiNode, DEFAULT_BASELINE_OFFSET);
355     if (SystemProperties::ConfigChangePerform()) {
356         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
357         CHECK_NULL_VOID(spanNode);
358         spanNode->UnregisterResource("baselineOffset");
359     }
360 }
361 
SetSpanFont(ArkUINodeHandle node,const struct ArkUIFontStruct * fontInfo)362 void SetSpanFont(ArkUINodeHandle node, const struct ArkUIFontStruct* fontInfo)
363 {
364     CHECK_NULL_VOID(fontInfo);
365     auto* uiNode = reinterpret_cast<UINode*>(node);
366     CHECK_NULL_VOID(uiNode);
367     Font font;
368     font.fontSize = Dimension(fontInfo->fontSizeNumber, static_cast<DimensionUnit>(fontInfo->fontSizeUnit));
369     font.fontStyle = static_cast<Ace::FontStyle>(fontInfo->fontStyle);
370     font.fontWeight = static_cast<FontWeight>(fontInfo->fontWeight);
371     std::vector<std::string> families;
372     if (fontInfo->fontFamilies && fontInfo->familyLength > 0) {
373         if (fontInfo->familyLength > DEFAULT_MAX_FONT_FAMILY_LENGTH) {
374             return;
375         }
376         families.resize(fontInfo->familyLength);
377         for (uint32_t i = 0; i < fontInfo->familyLength; i++) {
378             families.at(i) = std::string(*(fontInfo->fontFamilies + i));
379         }
380     }
381     font.fontFamilies = families;
382     SpanModelNG::SetFont(uiNode, font);
383 }
384 
ResetSpanFont(ArkUINodeHandle node)385 void ResetSpanFont(ArkUINodeHandle node)
386 {
387     auto* uiNode = reinterpret_cast<UINode*>(node);
388     CHECK_NULL_VOID(uiNode);
389     SpanModelNG::ResetFont(uiNode);
390 }
391 
SetSpanTextBackgroundStyle(ArkUINodeHandle node,ArkUI_Uint32 color,const ArkUI_Float32 * values,const ArkUI_Int32 * units,ArkUI_Int32 length,void * style)392 void SetSpanTextBackgroundStyle(ArkUINodeHandle node, ArkUI_Uint32 color, const ArkUI_Float32* values,
393     const ArkUI_Int32* units, ArkUI_Int32 length, void* style)
394 {
395     auto* uiNode = reinterpret_cast<UINode*>(node);
396     CHECK_NULL_VOID(uiNode);
397     if (length != DEFAULT_LENGTH) {
398         return;
399     }
400     if (SystemProperties::ConfigChangePerform() && style) {
401         auto textBackgroundStyle = reinterpret_cast<TextBackgroundStyle*>(style);
402         SpanModelNG::SetTextBackgroundStyle(uiNode, *textBackgroundStyle);
403         return;
404     }
405     TextBackgroundStyle font;
406     NG::BorderRadiusProperty borderRadius;
407     borderRadius.radiusTopLeft = Dimension(values[NUM_0], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_0]));
408     borderRadius.radiusTopRight = Dimension(values[NUM_1], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_1]));
409     borderRadius.radiusBottomLeft = Dimension(values[NUM_2], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_2]));
410     borderRadius.radiusBottomRight = Dimension(values[NUM_3], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_3]));
411     font.backgroundColor = Color(color);
412     font.backgroundRadius = borderRadius;
413     font.backgroundRadius->multiValued = true;
414     SpanModelNG::SetTextBackgroundStyle(uiNode, font);
415 }
416 
ResetSpanTextBackgroundStyle(ArkUINodeHandle node)417 void ResetSpanTextBackgroundStyle(ArkUINodeHandle node)
418 {
419     auto* uiNode = reinterpret_cast<UINode*>(node);
420     CHECK_NULL_VOID(uiNode);
421     TextBackgroundStyle font;
422     NG::BorderRadiusProperty borderRadius;
423     borderRadius.radiusTopLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
424     borderRadius.radiusTopRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
425     borderRadius.radiusBottomLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
426     borderRadius.radiusBottomRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
427     font.backgroundColor = Color(0x00000000);
428     font.backgroundRadius = borderRadius;
429     font.backgroundRadius->multiValued = true;
430     SpanModelNG::SetTextBackgroundStyle(uiNode, font);
431     SpanModelNG::ResetLetterSpacing(uiNode);
432     if (SystemProperties::ConfigChangePerform()) {
433         auto spanNode = AceType::DynamicCast<NG::SpanNode>(uiNode);
434         CHECK_NULL_VOID(spanNode);
435         spanNode->UnregisterResource("textbackgroundStyle");
436     }
437 }
438 
GetSpanTextBackgroundStyle(ArkUINodeHandle node,ArkUITextBackgroundStyleOptions * options)439 void GetSpanTextBackgroundStyle(ArkUINodeHandle node, ArkUITextBackgroundStyleOptions* options)
440 {
441     auto* uiNode = reinterpret_cast<UINode*>(node);
442     CHECK_NULL_VOID(uiNode);
443     auto styleOptions = SpanModelNG::GetSpanTextBackgroundStyle(uiNode);
444     options->color = styleOptions.backgroundColor->GetValue();
445     options->topLeft = styleOptions.backgroundRadius->radiusTopLeft->Value();
446     options->topRight = styleOptions.backgroundRadius->radiusTopRight->Value();
447     options->bottomLeft = styleOptions.backgroundRadius->radiusBottomLeft->Value();
448     options->bottomRight= styleOptions.backgroundRadius->radiusBottomRight->Value();
449 }
450 
SetTextTextShadow(ArkUINodeHandle node,struct ArkUITextShadowStruct * shadows,ArkUI_Uint32 length,const void * radiusResArrs,const void * colorResArrs,const void * offsetXResArrs,const void * offsetYResArrs)451 void SetTextTextShadow(ArkUINodeHandle node, struct ArkUITextShadowStruct* shadows, ArkUI_Uint32 length,
452     const void* radiusResArrs, const void* colorResArrs,
453     const void* offsetXResArrs, const void* offsetYResArrs)
454 {
455     CHECK_NULL_VOID(shadows);
456     auto* frameNode = reinterpret_cast<FrameNode*>(node);
457     CHECK_NULL_VOID(frameNode);
458     std::vector<Shadow> shadowList(length);
459     std::vector<RefPtr<ResourceObject>> radiusResArr;
460     std::vector<RefPtr<ResourceObject>> colorResArr;
461     std::vector<RefPtr<ResourceObject>> offsetXResArr;
462     std::vector<RefPtr<ResourceObject>> offsetYResArr;
463     if (SystemProperties::ConfigChangePerform()) {
464         if (radiusResArrs != nullptr) {
465             radiusResArr = *(static_cast<const std::vector<RefPtr<ResourceObject>>*>(radiusResArrs));
466         }
467         if (colorResArrs != nullptr) {
468             colorResArr =
469                 *(static_cast<const std::vector<RefPtr<ResourceObject>>*>(colorResArrs));
470         }
471         if (offsetXResArrs != nullptr) {
472             offsetXResArr =
473                 *(static_cast<const std::vector<RefPtr<ResourceObject>>*>(offsetXResArrs));
474         }
475         if (offsetYResArrs != nullptr) {
476             offsetYResArr =
477             *(static_cast<const std::vector<RefPtr<ResourceObject>>*>(offsetYResArrs));
478         }
479     }
480     for (uint32_t i = 0; i < length; i++) {
481         Shadow shadow;
482         ArkUITextShadowStruct* shadowStruct = shadows + i;
483         shadow.SetBlurRadius(shadowStruct->radius);
484         shadow.SetShadowType(static_cast<ShadowType>(shadowStruct->type));
485         shadow.SetColor(Color(shadowStruct->color));
486         shadow.SetOffsetX(shadowStruct->offsetX);
487         shadow.SetOffsetY(shadowStruct->offsetY);
488         shadow.SetIsFilled(static_cast<bool>(shadowStruct->fill));
489         if (SystemProperties::ConfigChangePerform()) {
490             RefPtr<ResourceObject> radiusObject = (radiusResArr.size() > i) ? radiusResArr[i] : nullptr;
491             RefPtr<ResourceObject> colorObject = (colorResArr.size() > i) ? colorResArr[i] : nullptr;
492             RefPtr<ResourceObject> offsetXObject = (offsetXResArr.size() > i) ? offsetXResArr[i] : nullptr;
493             RefPtr<ResourceObject> offsetYObject = (offsetYResArr.size() > i) ? offsetYResArr[i] : nullptr;
494             Shadow::RegisterShadowResourceObj(shadow, radiusObject, colorObject, offsetXObject, offsetYObject);
495         }
496         shadowList.at(i) = shadow;
497     }
498     SpanModelNG::SetTextShadow(frameNode, shadowList);
499 }
500 
GetTextShadow(ArkUINodeHandle node,ArkUITextShadowStruct * shadow,uint32_t size)501 void GetTextShadow(ArkUINodeHandle node, ArkUITextShadowStruct* shadow, uint32_t size)
502 {
503     auto* frameNode = reinterpret_cast<FrameNode*>(node);
504     CHECK_NULL_VOID(frameNode);
505     std::vector<ArkUITextShadowStruct> shadowArray;
506     auto textShadowVector = SpanModelNG::GetTextShadow(frameNode);
507     for (uint32_t i = 0; i < size; i++) {
508         if (i < textShadowVector.size()) {
509             *(shadow + i) = { static_cast<float>(textShadowVector[i].GetBlurRadius()),
510                 static_cast<int32_t>(textShadowVector[i].GetShadowType()), textShadowVector[i].GetColor().GetValue(),
511                 textShadowVector[i].GetOffset().GetX(), textShadowVector[i].GetOffset().GetY(),
512                 textShadowVector[i].GetIsFilled() };
513         } else {
514             *(shadow + i) = { 0.0f, static_cast<int32_t>(ShadowType::COLOR), Color::TRANSPARENT.GetValue(), 0.0f, 0.0f,
515                 0 };
516         }
517     }
518 }
519 
ResetTextTextShadow(ArkUINodeHandle node)520 void ResetTextTextShadow(ArkUINodeHandle node)
521 {
522     auto* frameNode = reinterpret_cast<FrameNode*>(node);
523     CHECK_NULL_VOID(frameNode);
524     SpanModelNG::ResetTextShadow(frameNode);
525 }
526 
SetAccessibilityText(ArkUINodeHandle node,ArkUI_CharPtr value)527 void SetAccessibilityText(ArkUINodeHandle node, ArkUI_CharPtr value)
528 {
529     auto* frameNode = reinterpret_cast<FrameNode*>(node);
530     CHECK_NULL_VOID(frameNode);
531     std::string valueStr = value;
532     SpanModelNG::SetAccessibilityText(frameNode, valueStr);
533 }
534 
ResetAccessibilityText(ArkUINodeHandle node)535 void ResetAccessibilityText(ArkUINodeHandle node)
536 {
537     auto* frameNode = reinterpret_cast<FrameNode*>(node);
538     CHECK_NULL_VOID(frameNode);
539     SpanModelNG::SetAccessibilityText(frameNode, "");
540 }
541 
SetAccessibilityDescription(ArkUINodeHandle node,ArkUI_CharPtr value)542 void SetAccessibilityDescription(ArkUINodeHandle node, ArkUI_CharPtr value)
543 {
544     auto* frameNode = reinterpret_cast<FrameNode*>(node);
545     CHECK_NULL_VOID(frameNode);
546     CHECK_NULL_VOID(value);
547     std::string valueStr = value;
548     SpanModelNG::SetAccessibilityDescription(frameNode, valueStr);
549 }
550 
ResetAccessibilityDescription(ArkUINodeHandle node)551 void ResetAccessibilityDescription(ArkUINodeHandle node)
552 {
553     auto* frameNode = reinterpret_cast<FrameNode*>(node);
554     CHECK_NULL_VOID(frameNode);
555     SpanModelNG::SetAccessibilityDescription(frameNode, "");
556 }
557 
SetAccessibilityLevel(ArkUINodeHandle node,ArkUI_CharPtr value)558 void SetAccessibilityLevel(ArkUINodeHandle node, ArkUI_CharPtr value)
559 {
560     auto* frameNode = reinterpret_cast<FrameNode*>(node);
561     CHECK_NULL_VOID(frameNode);
562     CHECK_NULL_VOID(value);
563     std::string valueStr = value;
564     SpanModelNG::SetAccessibilityImportance(frameNode, valueStr);
565 }
566 
ResetAccessibilityLevel(ArkUINodeHandle node)567 void ResetAccessibilityLevel(ArkUINodeHandle node)
568 {
569     auto* frameNode = reinterpret_cast<FrameNode*>(node);
570     CHECK_NULL_VOID(frameNode);
571     SpanModelNG::SetAccessibilityImportance(frameNode, "");
572 }
573 
GetSpanFontFamily(ArkUINodeHandle node)574 ArkUI_CharPtr GetSpanFontFamily(ArkUINodeHandle node)
575 {
576     auto* frameNode = reinterpret_cast<UINode*>(node);
577     CHECK_NULL_RETURN(frameNode, nullptr);
578     std::vector<std::string> fontFamilies = SpanModelNG::GetSpanFontFamily(frameNode);
579     std::string families;
580     //set index start
581     uint32_t index = 0;
582     for (auto& family : fontFamilies) {
583         families += family;
584         if (index != fontFamilies.size() - 1) {
585             families += ",";
586         }
587         index++;
588     }
589     g_strValue = families;
590     return g_strValue.c_str();
591 }
592 
SetSpanOnHover(ArkUINodeHandle node,void * callback)593 void SetSpanOnHover(ArkUINodeHandle node, void* callback)
594 {
595     auto* frameNode = reinterpret_cast<FrameNode*>(node);
596     CHECK_NULL_VOID(frameNode);
597     if (callback) {
598         auto onHover = reinterpret_cast<OnHoverFunc*>(callback);
599         SpanModelNG::SetOnHover(frameNode, std::move(*onHover));
600     } else {
601         SpanModelNG::ResetOnHover(frameNode);
602     }
603 }
604 
ResetSpanOnHover(ArkUINodeHandle node)605 void ResetSpanOnHover(ArkUINodeHandle node)
606 {
607     auto* frameNode = reinterpret_cast<FrameNode*>(node);
608     CHECK_NULL_VOID(frameNode);
609     SpanModelNG::ResetOnHover(frameNode);
610 }
611 } // namespace
612 namespace NodeModifier {
GetSpanModifier()613 const ArkUISpanModifier* GetSpanModifier()
614 {
615     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
616     static const ArkUISpanModifier modifier = {
617         .setSpanSrc = SetSpanSrc,
618         .setContent = SetSpanContent,
619         .setSpanTextCase = SetSpanTextCase,
620         .resetSpanTextCase = ResetSpanTextCase,
621         .setSpanFontWeight = SetSpanFontWeight,
622         .resetSpanFontWeight = ResetSpanFontWeight,
623         .setSpanLineHeight = SetSpanLineHeight,
624         .resetSpanLineHeight = ResetSpanLineHeight,
625         .setSpanFontStyle = SetSpanFontStyle,
626         .resetSpanFontStyle = ResetSpanFontStyle,
627         .setSpanFontSize = SetSpanFontSize,
628         .resetSpanFontSize = ResetSpanFontSize,
629         .setSpanFontFamily = SetSpanFontFamily,
630         .resetSpanFontFamily = ResetSpanFontFamily,
631         .setSpanDecoration = SetSpanDecoration,
632         .resetSpanDecoration = ResetSpanDecoration,
633         .setSpanFontColor = SetSpanFontColor,
634         .resetSpanFontColor = ResetSpanFontColor,
635         .setSpanLetterSpacing = SetSpanLetterSpacing,
636         .resetSpanLetterSpacing = ResetSpanLetterSpacing,
637         .setSpanBaselineOffset = SetSpanBaselineOffset,
638         .resetSpanBaselineOffset = ResetSpanBaselineOffset,
639         .setSpanFont = SetSpanFont,
640         .resetSpanFont = ResetSpanFont,
641         .setSpanFontWeightStr = SetSpanFontWeightStr,
642         .getSpanContent = GetSpanContent,
643         .getSpanDecoration = GetSpanDecoration,
644         .getSpanFontColor = GetSpanFontColor,
645         .getSpanFontSize = GetSpanFontSize,
646         .getSpanFontStyle = GetSpanFontStyle,
647         .getSpanFontWeight = GetSpanFontWeight,
648         .getSpanLineHeight = GetSpanLineHeight,
649         .getSpanTextCase = GetSpanTextCase,
650         .getSpanLetterSpacing = GetSpanLetterSpacing,
651         .getSpanBaselineOffset = GetSpanBaselineOffset,
652         .setSpanTextBackgroundStyle = SetSpanTextBackgroundStyle,
653         .resetSpanTextBackgroundStyle = ResetSpanTextBackgroundStyle,
654         .getSpanTextBackgroundStyle = GetSpanTextBackgroundStyle,
655         .setTextShadow = SetTextTextShadow,
656         .resetTextShadow = ResetTextTextShadow,
657         .getTextShadows = GetTextShadow,
658         .getSpanFontFamily = GetSpanFontFamily,
659         .setAccessibilityText = SetAccessibilityText,
660         .resetAccessibilityText = ResetAccessibilityText,
661         .setAccessibilityDescription = SetAccessibilityDescription,
662         .resetAccessibilityDescription = ResetAccessibilityDescription,
663         .setAccessibilityLevel = SetAccessibilityLevel,
664         .resetAccessibilityLevel = ResetAccessibilityLevel,
665         .setSpanOnHover = SetSpanOnHover,
666         .resetSpanOnHover = ResetSpanOnHover,
667     };
668     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
669     return &modifier;
670 }
671 
GetCJUISpanModifier()672 const CJUISpanModifier* GetCJUISpanModifier()
673 {
674     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
675     static const CJUISpanModifier modifier = {
676         .setSpanSrc = SetSpanSrc,
677         .setContent = SetSpanContent,
678         .setSpanTextCase = SetSpanTextCase,
679         .resetSpanTextCase = ResetSpanTextCase,
680         .setSpanFontWeight = SetSpanFontWeight,
681         .resetSpanFontWeight = ResetSpanFontWeight,
682         .setSpanLineHeight = SetSpanLineHeight,
683         .resetSpanLineHeight = ResetSpanLineHeight,
684         .setSpanFontStyle = SetSpanFontStyle,
685         .resetSpanFontStyle = ResetSpanFontStyle,
686         .setSpanFontSize = SetSpanFontSize,
687         .resetSpanFontSize = ResetSpanFontSize,
688         .setSpanFontFamily = SetSpanFontFamily,
689         .resetSpanFontFamily = ResetSpanFontFamily,
690         .setSpanDecoration = SetSpanDecoration,
691         .resetSpanDecoration = ResetSpanDecoration,
692         .setSpanFontColor = SetSpanFontColor,
693         .resetSpanFontColor = ResetSpanFontColor,
694         .setSpanLetterSpacing = SetSpanLetterSpacing,
695         .resetSpanLetterSpacing = ResetSpanLetterSpacing,
696         .setSpanBaselineOffset = SetSpanBaselineOffset,
697         .resetSpanBaselineOffset = ResetSpanBaselineOffset,
698         .setSpanFont = SetSpanFont,
699         .resetSpanFont = ResetSpanFont,
700         .setSpanFontWeightStr = SetSpanFontWeightStr,
701         .getSpanContent = GetSpanContent,
702         .getSpanDecoration = GetSpanDecoration,
703         .getSpanFontColor = GetSpanFontColor,
704         .getSpanFontSize = GetSpanFontSize,
705         .getSpanFontStyle = GetSpanFontStyle,
706         .getSpanFontWeight = GetSpanFontWeight,
707         .getSpanLineHeight = GetSpanLineHeight,
708         .getSpanTextCase = GetSpanTextCase,
709         .getSpanLetterSpacing = GetSpanLetterSpacing,
710         .getSpanBaselineOffset = GetSpanBaselineOffset,
711         .setSpanTextBackgroundStyle = SetSpanTextBackgroundStyle,
712         .resetSpanTextBackgroundStyle = ResetSpanTextBackgroundStyle,
713         .getSpanTextBackgroundStyle = GetSpanTextBackgroundStyle,
714         .setTextShadow = SetTextTextShadow,
715         .resetTextShadow = ResetTextTextShadow,
716         .getTextShadows = GetTextShadow,
717     };
718     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
719     return &modifier;
720 }
721 
SetCustomSpanOnMeasure(ArkUINodeHandle node,void * extraParam)722 void SetCustomSpanOnMeasure(ArkUINodeHandle node, void* extraParam)
723 {
724     auto* frameNode = reinterpret_cast<CustomSpanNode*>(node);
725     CHECK_NULL_VOID(frameNode);
726     std::function<CustomSpanMetrics(CustomSpanMeasureInfo)> onMeasureFunc =
727         [node, extraParam](CustomSpanMeasureInfo customSpanMeasureInfo) -> CustomSpanMetrics {
728         ArkUICustomNodeEvent event;
729         event.kind = ArkUIAPINodeFlags::CUSTOM_MEASURE;
730         event.extraParam = reinterpret_cast<intptr_t>(extraParam);
731         event.numberData[0].f32 = customSpanMeasureInfo.fontSize;
732         event.numberReturnData[0].f32 = 0.0f;
733         event.numberReturnData[1].f32 = 0.0f;
734         SendArkUIAsyncCustomEvent(&event);
735         float width = std::max(event.numberReturnData[0].f32, 0.0f);
736         float height = std::max(event.numberReturnData[1].f32, 0.0f);
737         return { width, height };
738     };
739     frameNode->GetSpanItem()->onMeasure = onMeasureFunc;
740 }
741 
SetCustomSpanOnDraw(ArkUINodeHandle node,void * extraParam)742 void SetCustomSpanOnDraw(ArkUINodeHandle node, void* extraParam)
743 {
744     auto* frameNode = reinterpret_cast<CustomSpanNode*>(node);
745     CHECK_NULL_VOID(frameNode);
746     std::function<void(NG::DrawingContext&, CustomSpanOptions)> onDrawFunc =
747         [node, extraParam](NG::DrawingContext& context, CustomSpanOptions customSpanOptions) {
748         auto canvas = reinterpret_cast<uintptr_t>(&context.canvas);
749         ArkUICustomNodeEvent event;
750         event.kind = ArkUIAPINodeFlags::CUSTOM_DRAW;
751         event.extraParam = reinterpret_cast<intptr_t>(extraParam);
752         event.data[NUM_0] = (ArkUI_Int32)(canvas & 0xffffffff);
753         event.data[NUM_1] =
754             (ArkUI_Int32)((static_cast<uint64_t>(canvas) >> NUM_32) & 0xffffffff);
755         event.data[NUM_2] = context.width;
756         event.data[NUM_3] = context.height;
757         event.canvas = reinterpret_cast<intptr_t>(&context.canvas);
758         event.numberData[0].f32 = customSpanOptions.x;
759         event.numberData[1].f32 = customSpanOptions.lineTop;
760         event.numberData[2].f32 = customSpanOptions.lineBottom;
761         event.numberData[3].f32 = customSpanOptions.baseline;
762         // clip
763         context.canvas.Save();
764         auto clipInnerRect = RSRect(0, 0, context.width, context.height);
765         context.canvas.ClipRect(clipInnerRect, RSClipOp::INTERSECT);
766         SendArkUIAsyncCustomEvent(&event);
767         context.canvas.Restore();
768     };
769     frameNode->GetSpanItem()->onDraw = onDrawFunc;
770 }
771 
772 template<typename T>
ProcessResourceObj(UINode * uinode,std::string key,T value,void * objRawPtr)773 void ProcessResourceObj(UINode* uinode, std::string key, T value, void* objRawPtr)
774 {
775     CHECK_NULL_VOID(SystemProperties::ConfigChangePerform());
776     CHECK_NULL_VOID(uinode);
777     auto spanNode = AceType::DynamicCast<NG::SpanNode>(uinode);
778     CHECK_NULL_VOID(spanNode);
779     if (objRawPtr) {
780         auto resObj = AceType::Claim(reinterpret_cast<ResourceObject*>(objRawPtr));
781         spanNode->RegisterResource<T>(key, resObj, value);
782     } else {
783         spanNode->UnregisterResource(key);
784     }
785 }
786 
787 template void ProcessResourceObj<FontWeight>(UINode* uinode, std::string key, FontWeight value, void* objRawPtr);
788 template void ProcessResourceObj<Color>(UINode* uinode, std::string key, Color value, void* objRawPtr);
789 template void ProcessResourceObj<CalcDimension>(UINode* uinode, std::string key, CalcDimension value, void* objRawPtr);
790 template void ProcessResourceObj<std::vector<std::string>>(UINode* uinode, std::string key,
791     std::vector<std::string> value, void* objRawPtr);
792 } // namespace NodeModifier
793 
794 } // namespace OHOS::Ace::NG
795