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 "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_span_bridge.h"
16 
17 #include <string>
18 #include "base/geometry/calc_dimension.h"
19 #include "base/geometry/dimension.h"
20 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/text/text_theme.h"
23 #include "bridge/declarative_frontend/engine/jsi/jsi_types.h"
24 
25 
26 namespace OHOS::Ace::NG {
27 namespace {
28 constexpr int SIZE_OF_TEXT_CASES = 2;
29 constexpr TextDecorationStyle DEFAULT_DECORATION_STYLE = TextDecorationStyle::SOLID;
30 constexpr Ace::FontStyle DEFAULT_FONT_STYLE = Ace::FontStyle::NORMAL;
31 constexpr Color DEFAULT_DECORATION_COLOR = Color(0xff000000);
32 const std::string DEFAULT_FONT_WEIGHT = "400";
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_4 = 4;
38 constexpr int NUM_5 = 5;
39 constexpr int NUM_6 = 6;
40 constexpr int NUM_7 = 7;
41 const std::vector<OHOS::Ace::FontStyle> FONT_STYLES = { OHOS::Ace::FontStyle::NORMAL, OHOS::Ace::FontStyle::ITALIC };
42 
ParseOuterBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<ArkUI_Float32> & values,std::vector<ArkUI_Int32> & units,int32_t argsIndex)43 void ParseOuterBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<ArkUI_Float32>& values,
44     std::vector<ArkUI_Int32>& units, int32_t argsIndex)
45 {
46     Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(argsIndex);
47     Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(argsIndex + NUM_1);
48     Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(argsIndex + NUM_2);
49     Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(argsIndex + NUM_3);
50 
51     std::optional<CalcDimension> topLeftOptional;
52     std::optional<CalcDimension> topRightOptional;
53     std::optional<CalcDimension> bottomLeftOptional;
54     std::optional<CalcDimension> bottomRightOptional;
55 
56     ArkTSUtils::ParseOuterBorder(vm, topLeftArgs, topLeftOptional);
57     ArkTSUtils::ParseOuterBorder(vm, topRightArgs, topRightOptional);
58     ArkTSUtils::ParseOuterBorder(vm, bottomLeftArgs, bottomLeftOptional);
59     ArkTSUtils::ParseOuterBorder(vm, bottomRightArgs, bottomRightOptional);
60 
61     ArkTSUtils::PushOuterBorderDimensionVector(topLeftOptional, values, units);
62     ArkTSUtils::PushOuterBorderDimensionVector(topRightOptional, values, units);
63     ArkTSUtils::PushOuterBorderDimensionVector(bottomLeftOptional, values, units);
64     ArkTSUtils::PushOuterBorderDimensionVector(bottomRightOptional, values, units);
65 }
66 
ParseTextShadow(ArkUIRuntimeCallInfo * runtimeCallInfo,uint32_t length,std::vector<ArkUITextShadowStruct> & textShadowArray)67 bool ParseTextShadow(ArkUIRuntimeCallInfo* runtimeCallInfo, uint32_t length,
68     std::vector<ArkUITextShadowStruct>& textShadowArray)
69 {
70     EcmaVM* vm = runtimeCallInfo->GetVM();
71     CHECK_NULL_RETURN(vm, false);
72     Local<JSValueRef> radiusArg = runtimeCallInfo->GetCallArgRef(NUM_1);
73     Local<JSValueRef> typeArg = runtimeCallInfo->GetCallArgRef(NUM_2);
74     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_3);
75     Local<JSValueRef> offsetXArg = runtimeCallInfo->GetCallArgRef(NUM_4);
76     Local<JSValueRef> offsetYArg = runtimeCallInfo->GetCallArgRef(NUM_5);
77     Local<JSValueRef> fillArg = runtimeCallInfo->GetCallArgRef(NUM_6);
78     auto radiusArray = std::make_unique<double[]>(length);
79     auto typeArray = std::make_unique<uint32_t[]>(length);
80     auto colorArray = std::make_unique<uint32_t[]>(length);
81     auto offsetXArray = std::make_unique<double[]>(length);
82     auto offsetYArray = std::make_unique<double[]>(length);
83     auto fillArray = std::make_unique<uint32_t[]>(length);
84     bool radiusParseResult =
85         ArkTSUtils::ParseArray<double>(vm, radiusArg, radiusArray.get(), length, ArkTSUtils::parseShadowRadius);
86     bool typeParseResult =
87         ArkTSUtils::ParseArray<uint32_t>(vm, typeArg, typeArray.get(), length, ArkTSUtils::parseShadowType);
88     bool colorParseResult =
89         ArkTSUtils::ParseArray<uint32_t>(vm, colorArg, colorArray.get(), length, ArkTSUtils::parseShadowColor);
90     bool offsetXParseResult =
91         ArkTSUtils::ParseArray<double>(vm, offsetXArg, offsetXArray.get(), length, ArkTSUtils::parseShadowOffset);
92     bool offsetYParseResult =
93         ArkTSUtils::ParseArray<double>(vm, offsetYArg, offsetYArray.get(), length, ArkTSUtils::parseShadowOffset);
94     bool fillParseResult =
95         ArkTSUtils::ParseArray<uint32_t>(vm, fillArg, fillArray.get(), length, ArkTSUtils::parseShadowFill);
96     if (!radiusParseResult || !colorParseResult || !offsetXParseResult || !offsetYParseResult || !fillParseResult ||
97         !typeParseResult) {
98         return false;
99     }
100     for (uint32_t i = 0; i < length; i++) {
101         ArkUITextShadowStruct textShadow;
102         textShadow.radius = radiusArray[i];
103         textShadow.type = typeArray[i];
104         textShadow.color = colorArray[i];
105         textShadow.offsetX = offsetXArray[i];
106         textShadow.offsetY = offsetYArray[i];
107         textShadow.fill = fillArray[i];
108         textShadowArray.emplace_back(textShadow);
109     }
110     return true;
111 }
112 } // namespace
113 
SetSpanSrc(ArkUIRuntimeCallInfo * runtimeCallInfo)114 ArkUINativeModuleValue SpanBridge::SetSpanSrc(ArkUIRuntimeCallInfo *runtimeCallInfo)
115 {
116     EcmaVM *vm = runtimeCallInfo->GetVM();
117     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
118     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
119     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
120     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
121     std::string src;
122     if (!ArkTSUtils::ParseJsString(vm, secondArg, src)) {
123         return panda::JSValueRef::Undefined(vm);
124     }
125     GetArkUINodeModifiers()->getSpanModifier()->setSpanSrc(nativeNode, src.c_str());
126     return panda::JSValueRef::Undefined(vm);
127 }
128 
SetTextCase(ArkUIRuntimeCallInfo * runtimeCallInfo)129 ArkUINativeModuleValue SpanBridge::SetTextCase(ArkUIRuntimeCallInfo *runtimeCallInfo)
130 {
131     EcmaVM *vm = runtimeCallInfo->GetVM();
132     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
133     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
134     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
135     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
136     if (secondArg->IsNumber() && secondArg->Int32Value(vm) >= NUM_0 &&
137         secondArg->Int32Value(vm) <= SIZE_OF_TEXT_CASES) {
138         GetArkUINodeModifiers()->getSpanModifier()->setSpanTextCase(nativeNode, secondArg->Int32Value(vm));
139     } else {
140         GetArkUINodeModifiers()->getSpanModifier()->resetSpanTextCase(nativeNode);
141     }
142     return panda::JSValueRef::Undefined(vm);
143 }
144 
ResetTextCase(ArkUIRuntimeCallInfo * runtimeCallInfo)145 ArkUINativeModuleValue SpanBridge::ResetTextCase(ArkUIRuntimeCallInfo *runtimeCallInfo)
146 {
147     EcmaVM *vm = runtimeCallInfo->GetVM();
148     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
149     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
150     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
151     GetArkUINodeModifiers()->getSpanModifier()->resetSpanTextCase(nativeNode);
152     return panda::JSValueRef::Undefined(vm);
153 }
154 
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)155 ArkUINativeModuleValue SpanBridge::SetFontWeight(ArkUIRuntimeCallInfo *runtimeCallInfo)
156 {
157     EcmaVM *vm = runtimeCallInfo->GetVM();
158     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
159     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
160     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
161     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
162 
163     std::string weight = DEFAULT_FONT_WEIGHT;
164     if (!secondArg->IsNull()) {
165         if (secondArg->IsNumber()) {
166             weight = std::to_string(secondArg->Int32Value(vm));
167         } else if (secondArg->IsString(vm)) {
168             weight = secondArg->ToString(vm)->ToString(vm);
169         }
170     }
171 
172     GetArkUINodeModifiers()->getSpanModifier()->setSpanFontWeight(nativeNode,
173         static_cast<ArkUI_Int32>(Framework::ConvertStrToFontWeight(weight)));
174     return panda::JSValueRef::Undefined(vm);
175 }
176 
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)177 ArkUINativeModuleValue SpanBridge::ResetFontWeight(ArkUIRuntimeCallInfo *runtimeCallInfo)
178 {
179     EcmaVM *vm = runtimeCallInfo->GetVM();
180     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
181     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
182     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
183     GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontWeight(nativeNode);
184     return panda::JSValueRef::Undefined(vm);
185 }
186 
SetLineHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)187 ArkUINativeModuleValue SpanBridge::SetLineHeight(ArkUIRuntimeCallInfo *runtimeCallInfo)
188 {
189     EcmaVM *vm = runtimeCallInfo->GetVM();
190     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
191     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
192     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
193     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
194     CalcDimension lineHeight(0.0, DimensionUnit::PX);
195     if (!ArkTSUtils::ParseJsDimensionFpNG(vm, secondArg, lineHeight) || lineHeight.IsNegative()) {
196         lineHeight.Reset();
197     }
198     GetArkUINodeModifiers()->getSpanModifier()->setSpanLineHeight(
199         nativeNode, lineHeight.Value(), static_cast<int8_t>(lineHeight.Unit()));
200     return panda::JSValueRef::Undefined(vm);
201 }
202 
ResetLineHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)203 ArkUINativeModuleValue SpanBridge::ResetLineHeight(ArkUIRuntimeCallInfo *runtimeCallInfo)
204 {
205     EcmaVM *vm = runtimeCallInfo->GetVM();
206     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
207     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
208     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
209     GetArkUINodeModifiers()->getSpanModifier()->resetSpanLineHeight(nativeNode);
210     return panda::JSValueRef::Undefined(vm);
211 }
212 
SetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)213 ArkUINativeModuleValue SpanBridge::SetFontStyle(ArkUIRuntimeCallInfo *runtimeCallInfo)
214 {
215     EcmaVM *vm = runtimeCallInfo->GetVM();
216     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
217     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
218     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
219     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
220     if (secondArg->IsNumber()) {
221         int32_t value = secondArg->Int32Value(vm);
222         if (value >= 0 && value < static_cast<int32_t>(FONT_STYLES.size())) {
223             GetArkUINodeModifiers()->getSpanModifier()->setSpanFontStyle(nativeNode, value);
224         } else {
225             GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontStyle(nativeNode);
226             return panda::JSValueRef::Undefined(vm);
227         }
228     } else {
229         GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontStyle(nativeNode);
230     }
231     return panda::JSValueRef::Undefined(vm);
232 }
233 
ResetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)234 ArkUINativeModuleValue SpanBridge::ResetFontStyle(ArkUIRuntimeCallInfo *runtimeCallInfo)
235 {
236     EcmaVM *vm = runtimeCallInfo->GetVM();
237     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
238     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
239     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
240     GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontStyle(nativeNode);
241     return panda::JSValueRef::Undefined(vm);
242 }
243 
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)244 ArkUINativeModuleValue SpanBridge::SetFontSize(ArkUIRuntimeCallInfo *runtimeCallInfo)
245 {
246     EcmaVM *vm = runtimeCallInfo->GetVM();
247     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
248     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
249     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
250     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
251     auto theme = GetTheme<TextTheme>();
252     CHECK_NULL_RETURN(theme, panda::JSValueRef::Undefined(vm));
253 
254     CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
255     if (!ArkTSUtils::ParseJsDimensionFpNG(vm, secondArg, fontSize, false) || fontSize.IsNegative()) {
256         fontSize = theme->GetTextStyle().GetFontSize();
257     }
258     GetArkUINodeModifiers()->getSpanModifier()->setSpanFontSize(nativeNode, fontSize.Value(),
259         static_cast<int8_t>(fontSize.Unit()));
260     return panda::JSValueRef::Undefined(vm);
261 }
262 
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)263 ArkUINativeModuleValue SpanBridge::ResetFontSize(ArkUIRuntimeCallInfo *runtimeCallInfo)
264 {
265     EcmaVM *vm = runtimeCallInfo->GetVM();
266     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
267     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
268     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
269     GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontSize(nativeNode);
270     return panda::JSValueRef::Undefined(vm);
271 }
272 
SetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)273 ArkUINativeModuleValue SpanBridge::SetFontFamily(ArkUIRuntimeCallInfo *runtimeCallInfo)
274 {
275     EcmaVM *vm = runtimeCallInfo->GetVM();
276     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
277     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
278     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
279     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
280 
281     std::vector<std::string> fontFamilies;
282     if (!ArkTSUtils::ParseJsFontFamilies(vm, secondArg, fontFamilies)) {
283         GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontFamily(nativeNode);
284         return panda::JSValueRef::Undefined(vm);
285     }
286     auto families = std::make_unique<char *[]>(fontFamilies.size());
287     for (uint32_t i = 0; i < fontFamilies.size(); i++) {
288         families[i] = const_cast<char *>(fontFamilies.at(i).c_str());
289     }
290     GetArkUINodeModifiers()->getSpanModifier()->setSpanFontFamily(nativeNode,
291         const_cast<const char **>(families.get()), fontFamilies.size());
292     return panda::JSValueRef::Undefined(vm);
293 }
294 
ResetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)295 ArkUINativeModuleValue SpanBridge::ResetFontFamily(ArkUIRuntimeCallInfo *runtimeCallInfo)
296 {
297     EcmaVM *vm = runtimeCallInfo->GetVM();
298     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
299     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
300     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
301 
302     GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontFamily(nativeNode);
303     return panda::JSValueRef::Undefined(vm);
304 }
305 
SetDecoration(ArkUIRuntimeCallInfo * runtimeCallInfo)306 ArkUINativeModuleValue SpanBridge::SetDecoration(ArkUIRuntimeCallInfo *runtimeCallInfo)
307 {
308     EcmaVM *vm = runtimeCallInfo->GetVM();
309     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
310     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
311     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
312     Local<JSValueRef> thirdArg = runtimeCallInfo->GetCallArgRef(NUM_2);
313     Local<JSValueRef> fourthArg = runtimeCallInfo->GetCallArgRef(NUM_3);
314     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
315     int32_t textDecoration = static_cast<int32_t>(TextDecoration::NONE);
316     Color color = DEFAULT_DECORATION_COLOR;
317     int32_t style = static_cast<int32_t>(DEFAULT_DECORATION_STYLE);
318     if (secondArg->IsInt()) {
319         textDecoration = secondArg->Int32Value(vm);
320     }
321     if (!ArkTSUtils::ParseJsColorAlpha(vm, thirdArg, color)) {
322         color = DEFAULT_DECORATION_COLOR;
323     }
324     if (fourthArg->IsInt()) {
325         style = fourthArg->Int32Value(vm);
326     }
327     GetArkUINodeModifiers()->getSpanModifier()->setSpanDecoration(nativeNode, textDecoration, color.GetValue(), style);
328     return panda::JSValueRef::Undefined(vm);
329 }
330 
ResetDecoration(ArkUIRuntimeCallInfo * runtimeCallInfo)331 ArkUINativeModuleValue SpanBridge::ResetDecoration(ArkUIRuntimeCallInfo *runtimeCallInfo)
332 {
333     EcmaVM *vm = runtimeCallInfo->GetVM();
334     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
335     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
336     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
337     GetArkUINodeModifiers()->getSpanModifier()->resetSpanDecoration(nativeNode);
338     return panda::JSValueRef::Undefined(vm);
339 }
340 
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)341 ArkUINativeModuleValue SpanBridge::SetFontColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
342 {
343     EcmaVM *vm = runtimeCallInfo->GetVM();
344     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
345     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
346     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
347     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
348     auto theme = GetTheme<TextTheme>();
349     CHECK_NULL_RETURN(theme, panda::JSValueRef::Undefined(vm));
350 
351     Color textColor = theme->GetTextStyle().GetTextColor();
352     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, textColor)) {
353         textColor = theme->GetTextStyle().GetTextColor();
354     }
355     GetArkUINodeModifiers()->getSpanModifier()->setSpanFontColor(nativeNode, textColor.GetValue());
356     return panda::JSValueRef::Undefined(vm);
357 }
358 
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)359 ArkUINativeModuleValue SpanBridge::ResetFontColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
360 {
361     EcmaVM *vm = runtimeCallInfo->GetVM();
362     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
363     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
364     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
365 
366     GetArkUINodeModifiers()->getSpanModifier()->resetSpanFontColor(nativeNode);
367     return panda::JSValueRef::Undefined(vm);
368 }
369 
SetLetterSpacing(ArkUIRuntimeCallInfo * runtimeCallInfo)370 ArkUINativeModuleValue SpanBridge::SetLetterSpacing(ArkUIRuntimeCallInfo *runtimeCallInfo)
371 {
372     EcmaVM *vm = runtimeCallInfo->GetVM();
373     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
374     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
375     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
376     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
377     struct ArkUIStringAndFloat letterSpacingValue = { 0.0, nullptr };
378     if (secondArg->IsNumber()) {
379         letterSpacingValue.value = secondArg->ToNumber(vm)->Value();
380         GetArkUINodeModifiers()->getSpanModifier()->setSpanLetterSpacing(nativeNode, &letterSpacingValue);
381     } else if (secondArg->IsString(vm)) {
382         std::string tempValueStr = secondArg->ToString(vm)->ToString(vm);
383         letterSpacingValue.valueStr = tempValueStr.c_str();
384         GetArkUINodeModifiers()->getSpanModifier()->setSpanLetterSpacing(nativeNode, &letterSpacingValue);
385     } else {
386         GetArkUINodeModifiers()->getSpanModifier()->resetSpanLetterSpacing(nativeNode);
387     }
388     return panda::JSValueRef::Undefined(vm);
389 }
390 
ResetLetterSpacing(ArkUIRuntimeCallInfo * runtimeCallInfo)391 ArkUINativeModuleValue SpanBridge::ResetLetterSpacing(ArkUIRuntimeCallInfo *runtimeCallInfo)
392 {
393     EcmaVM *vm = runtimeCallInfo->GetVM();
394     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
395     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
396     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
397     GetArkUINodeModifiers()->getSpanModifier()->resetSpanLetterSpacing(nativeNode);
398     return panda::JSValueRef::Undefined(vm);
399 }
400 
SetBaselineOffset(ArkUIRuntimeCallInfo * runtimeCallInfo)401 ArkUINativeModuleValue SpanBridge::SetBaselineOffset(ArkUIRuntimeCallInfo *runtimeCallInfo)
402 {
403     EcmaVM *vm = runtimeCallInfo->GetVM();
404     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
405     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
406     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
407     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
408     CalcDimension result;
409     if (secondArg->IsObject(vm) && ArkTSUtils::ParseJsLengthMetrics(vm, secondArg, result) &&
410         result.Unit() != DimensionUnit::PERCENT && !std::isnan(result.Value())) {
411         GetArkUINodeModifiers()->getSpanModifier()->setSpanBaselineOffset(
412             nativeNode, result.Value(), static_cast<int8_t>(result.Unit()));
413     } else {
414         GetArkUINodeModifiers()->getSpanModifier()->resetSpanBaselineOffset(nativeNode);
415     }
416     return panda::JSValueRef::Undefined(vm);
417 }
418 
ResetBaselineOffset(ArkUIRuntimeCallInfo * runtimeCallInfo)419 ArkUINativeModuleValue SpanBridge::ResetBaselineOffset(ArkUIRuntimeCallInfo* runtimeCallInfo)
420 {
421     EcmaVM* vm = runtimeCallInfo->GetVM();
422     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
423     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
424     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
425     GetArkUINodeModifiers()->getSpanModifier()->resetSpanBaselineOffset(nativeNode);
426     return panda::JSValueRef::Undefined(vm);
427 }
428 
SetFont(ArkUIRuntimeCallInfo * runtimeCallInfo)429 ArkUINativeModuleValue SpanBridge::SetFont(ArkUIRuntimeCallInfo *runtimeCallInfo)
430 {
431     EcmaVM *vm = runtimeCallInfo->GetVM();
432     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
433     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
434     Local<JSValueRef> sizeArg = runtimeCallInfo->GetCallArgRef(NUM_1);
435     Local<JSValueRef> weightArg = runtimeCallInfo->GetCallArgRef(NUM_2);
436     Local<JSValueRef> familyArg = runtimeCallInfo->GetCallArgRef(NUM_3);
437     Local<JSValueRef> styleArg = runtimeCallInfo->GetCallArgRef(NUM_4);
438     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
439     ArkUIFontStruct fontInfo;
440     auto theme = GetTheme<TextTheme>();
441     CHECK_NULL_RETURN(theme, panda::JSValueRef::Undefined(vm));
442 
443     CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
444     if (sizeArg->IsNull() || !ArkTSUtils::ParseJsDimensionFpNG(vm, sizeArg, fontSize, false) || fontSize.IsNegative()) {
445         fontSize = theme->GetTextStyle().GetFontSize();
446     }
447     fontInfo.fontSizeNumber = fontSize.Value();
448     fontInfo.fontSizeUnit = static_cast<int8_t>(fontSize.Unit());
449 
450     std::string weight = DEFAULT_FONT_WEIGHT;
451     if (!weightArg->IsNull()) {
452         if (weightArg->IsNumber()) {
453             weight = std::to_string(weightArg->Int32Value(vm));
454         } else if (weightArg->IsString(vm)) {
455             weight = weightArg->ToString(vm)->ToString(vm);
456         }
457     }
458     fontInfo.fontWeight = static_cast<uint8_t>(Framework::ConvertStrToFontWeight(weight));
459     int32_t style = static_cast<int32_t>(DEFAULT_FONT_STYLE);
460     if (styleArg->IsInt()) {
461         style = styleArg->Int32Value(vm);
462         if (style <= 0 || style > static_cast<int32_t>(FONT_STYLES.size())) {
463             style = static_cast<int32_t>(DEFAULT_FONT_STYLE);
464         }
465     }
466     fontInfo.fontStyle = static_cast<uint8_t>(style);
467     std::vector<std::string> fontFamilies;
468     fontInfo.fontFamilies = nullptr;
469     if (!familyArg->IsNull() && ArkTSUtils::ParseJsFontFamilies(vm, familyArg, fontFamilies)) {
470         fontInfo.familyLength = fontFamilies.size();
471         auto families = std::make_unique<const char* []>(fontInfo.familyLength);
472         for (uint32_t i = 0; i < fontFamilies.size(); i++) {
473             families[i] = fontFamilies[i].c_str();
474         }
475         fontInfo.fontFamilies = families.get();
476         GetArkUINodeModifiers()->getSpanModifier()->setSpanFont(nativeNode, &fontInfo);
477     } else {
478         GetArkUINodeModifiers()->getSpanModifier()->setSpanFont(nativeNode, &fontInfo);
479     }
480     return panda::JSValueRef::Undefined(vm);
481 }
482 
ResetFont(ArkUIRuntimeCallInfo * runtimeCallInfo)483 ArkUINativeModuleValue SpanBridge::ResetFont(ArkUIRuntimeCallInfo *runtimeCallInfo)
484 {
485     EcmaVM *vm = runtimeCallInfo->GetVM();
486     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
487     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
488     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
489     GetArkUINodeModifiers()->getSpanModifier()->resetSpanFont(nativeNode);
490     return panda::JSValueRef::Undefined(vm);
491 }
492 
SetTextBackgroundStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)493 ArkUINativeModuleValue SpanBridge::SetTextBackgroundStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
494 {
495     EcmaVM* vm = runtimeCallInfo->GetVM();
496     Color color;
497     std::vector<ArkUI_Float32> radiusArray;
498     std::vector<ArkUI_Int32> valueUnits;
499     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
500     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
501     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
502     if (!(ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color))) {
503         color = Color::TRANSPARENT;
504     }
505     ParseOuterBorderRadius(runtimeCallInfo, vm, radiusArray, valueUnits, NUM_2); // Border Radius args start index
506     GetArkUINodeModifiers()->getSpanModifier()->setSpanTextBackgroundStyle(
507         nativeNode, color.GetValue(), radiusArray.data(), valueUnits.data(), static_cast<int32_t>(radiusArray.size()));
508     return panda::JSValueRef::Undefined(vm);
509 }
510 
ResetTextBackgroundStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)511 ArkUINativeModuleValue SpanBridge::ResetTextBackgroundStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
512 {
513     EcmaVM* vm = runtimeCallInfo->GetVM();
514     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
515     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
516     GetArkUINodeModifiers()->getSpanModifier()->resetSpanTextBackgroundStyle(nativeNode);
517     return panda::JSValueRef::Undefined(vm);
518 }
519 
SetTextShadow(ArkUIRuntimeCallInfo * runtimeCallInfo)520 ArkUINativeModuleValue SpanBridge::SetTextShadow(ArkUIRuntimeCallInfo* runtimeCallInfo)
521 {
522     EcmaVM* vm = runtimeCallInfo->GetVM();
523     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
524     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
525     Local<JSValueRef> lengthArg = runtimeCallInfo->GetCallArgRef(NUM_7);
526     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
527     uint32_t length;
528     if (!lengthArg->IsNumber() || lengthArg->Uint32Value(vm) == 0) {
529         return panda::JSValueRef::Undefined(vm);
530     }
531     length = lengthArg->Uint32Value(vm);
532     std::vector<ArkUITextShadowStruct> textShadowArray;
533     ParseTextShadow(runtimeCallInfo, length, textShadowArray);
534     GetArkUINodeModifiers()->getSpanModifier()->setTextShadow(nativeNode, textShadowArray.data(), length);
535     return panda::JSValueRef::Undefined(vm);
536 }
537 
ResetTextShadow(ArkUIRuntimeCallInfo * runtimeCallInfo)538 ArkUINativeModuleValue SpanBridge::ResetTextShadow(ArkUIRuntimeCallInfo* runtimeCallInfo)
539 {
540     EcmaVM* vm = runtimeCallInfo->GetVM();
541     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
542     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
543     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
544     GetArkUINodeModifiers()->getSpanModifier()->resetTextShadow(nativeNode);
545     return panda::JSValueRef::Undefined(vm);
546 }
547 
SetAccessibilityText(ArkUIRuntimeCallInfo * runtimeCallInfo)548 ArkUINativeModuleValue SpanBridge::SetAccessibilityText(ArkUIRuntimeCallInfo *runtimeCallInfo)
549 {
550     EcmaVM* vm = runtimeCallInfo->GetVM();
551     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
552     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
553     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
554     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
555     if (secondArg->IsString(vm)) {
556         std::string stringValue = secondArg->ToString(vm)->ToString(vm);
557         GetArkUINodeModifiers()->getSpanModifier()->setAccessibilityText(nativeNode, stringValue.c_str());
558     } else {
559         GetArkUINodeModifiers()->getSpanModifier()->resetAccessibilityText(nativeNode);
560     }
561     return panda::JSValueRef::Undefined(vm);
562 }
563 
ResetAccessibilityText(ArkUIRuntimeCallInfo * runtimeCallInfo)564 ArkUINativeModuleValue SpanBridge::ResetAccessibilityText(ArkUIRuntimeCallInfo *runtimeCallInfo)
565 {
566     EcmaVM* vm = runtimeCallInfo->GetVM();
567     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
568     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
569     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
570     GetArkUINodeModifiers()->getSpanModifier()->resetAccessibilityText(nativeNode);
571     return panda::JSValueRef::Undefined(vm);
572 }
573 
SetAccessibilityDescription(ArkUIRuntimeCallInfo * runtimeCallInfo)574 ArkUINativeModuleValue SpanBridge::SetAccessibilityDescription(ArkUIRuntimeCallInfo *runtimeCallInfo)
575 {
576     EcmaVM* vm = runtimeCallInfo->GetVM();
577     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
578     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
579     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
580     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
581     if (secondArg->IsString(vm)) {
582         std::string stringValue = secondArg->ToString(vm)->ToString(vm);
583         GetArkUINodeModifiers()->getSpanModifier()->setAccessibilityDescription(nativeNode, stringValue.c_str());
584     } else {
585         GetArkUINodeModifiers()->getSpanModifier()->resetAccessibilityDescription(nativeNode);
586     }
587     return panda::JSValueRef::Undefined(vm);
588 }
589 
ResetAccessibilityDescription(ArkUIRuntimeCallInfo * runtimeCallInfo)590 ArkUINativeModuleValue SpanBridge::ResetAccessibilityDescription(ArkUIRuntimeCallInfo *runtimeCallInfo)
591 {
592     EcmaVM* vm = runtimeCallInfo->GetVM();
593     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
594     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
595     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
596     GetArkUINodeModifiers()->getSpanModifier()->resetAccessibilityDescription(nativeNode);
597     return panda::JSValueRef::Undefined(vm);
598 }
599 
SetAccessibilityLevel(ArkUIRuntimeCallInfo * runtimeCallInfo)600 ArkUINativeModuleValue SpanBridge::SetAccessibilityLevel(ArkUIRuntimeCallInfo *runtimeCallInfo)
601 {
602     EcmaVM* vm = runtimeCallInfo->GetVM();
603     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
604     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
605     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
606     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
607     if (secondArg->IsString(vm)) {
608         std::string stringValue = secondArg->ToString(vm)->ToString(vm);
609         GetArkUINodeModifiers()->getSpanModifier()->setAccessibilityLevel(nativeNode, stringValue.c_str());
610     } else {
611         GetArkUINodeModifiers()->getSpanModifier()->resetAccessibilityLevel(nativeNode);
612     }
613     return panda::JSValueRef::Undefined(vm);
614 }
615 
ResetAccessibilityLevel(ArkUIRuntimeCallInfo * runtimeCallInfo)616 ArkUINativeModuleValue SpanBridge::ResetAccessibilityLevel(ArkUIRuntimeCallInfo *runtimeCallInfo)
617 {
618     EcmaVM* vm = runtimeCallInfo->GetVM();
619     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
620     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
621     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
622     GetArkUINodeModifiers()->getSpanModifier()->resetAccessibilityLevel(nativeNode);
623     return panda::JSValueRef::Undefined(vm);
624 }
625 } // namespace OHOS::Ace::NG
626