1 /*
2 * Copyright (c) 2024 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 "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_symbol_span_bridge.h"
16
17 #include "frameworks/base/geometry/calc_dimension.h"
18 #include "frameworks/base/geometry/dimension.h"
19 #include "frameworks/base/utils/string_utils.h"
20 #include "frameworks/base/utils/utils.h"
21 #include "frameworks/bridge/declarative_frontend/engine/js_types.h"
22 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
23
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr int NUM_0 = 0;
27 constexpr int NUM_1 = 1;
28 constexpr int NUM_2 = 2;
29 constexpr int32_t SYSTEM_SYMBOL_BOUNDARY = 0XFFFFF;
30 } // namespace
31
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)32 ArkUINativeModuleValue SymbolSpanBridge::SetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
33 {
34 EcmaVM* vm = runtimeCallInfo->GetVM();
35 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
36 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
37 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
38
39 if (!secondArg->IsArray(vm)) {
40 return panda::JSValueRef::Undefined(vm);
41 }
42 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
43 auto array = Local<panda::ArrayRef>(secondArg);
44 auto length = array->Length(vm);
45
46 std::vector<ArkUI_Uint32> colorArray;
47 for (uint32_t index = 0; index < length; index++) {
48 Local<JSValueRef> value = panda::ArrayRef::GetValueAt(vm, array, index);
49 Color color;
50 if (ArkTSUtils::ParseJsColorAlpha(vm, value, color)) {
51 colorArray.emplace_back(color.GetValue());
52 } else {
53 colorArray.clear();
54 break;
55 }
56 }
57 if (static_cast<uint32_t>(length) == colorArray.size() && (static_cast<uint32_t>(length) & 1)) {
58 for (uint32_t i = 0; i < length; i++) {
59 colorArray.emplace_back(colorArray[i]);
60 }
61 }
62 GetArkUINodeModifiers()->getSymbolSpanModifier()->setSymbolSpanFontColor(
63 nativeNode, colorArray.data(), colorArray.size());
64 return panda::JSValueRef::Undefined(vm);
65 }
66
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)67 ArkUINativeModuleValue SymbolSpanBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
68 {
69 EcmaVM* vm = runtimeCallInfo->GetVM();
70 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
71 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
72 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
73 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanFontColor(nativeNode);
74 return panda::JSValueRef::Undefined(vm);
75 }
76
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)77 ArkUINativeModuleValue SymbolSpanBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
78 {
79 EcmaVM* vm = runtimeCallInfo->GetVM();
80 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
81 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
82 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
83 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
84 CalcDimension fontSize;
85 if (!ArkTSUtils::ParseJsDimensionFpNG(vm, secondArg, fontSize, false)) {
86 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanFontSize(nativeNode);
87 } else {
88 GetArkUINodeModifiers()->getSymbolSpanModifier()->setSymbolSpanFontSize(
89 nativeNode, fontSize.Value(), static_cast<int>(fontSize.Unit()));
90 }
91 return panda::JSValueRef::Undefined(vm);
92 }
93
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)94 ArkUINativeModuleValue SymbolSpanBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
95 {
96 EcmaVM* vm = runtimeCallInfo->GetVM();
97 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
98 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
99 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
100 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanFontSize(nativeNode);
101 return panda::JSValueRef::Undefined(vm);
102 }
103
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)104 ArkUINativeModuleValue SymbolSpanBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
105 {
106 EcmaVM* vm = runtimeCallInfo->GetVM();
107 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
108 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
109 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
110 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
111 if (secondArg->IsString(vm)) {
112 std::string weight = secondArg->ToString(vm)->ToString(vm);
113 GetArkUINodeModifiers()->getSymbolSpanModifier()->setSymbolSpanFontWeightStr(
114 nativeNode, weight.c_str());
115 } else {
116 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanFontWeight(nativeNode);
117 }
118 return panda::JSValueRef::Undefined(vm);
119 }
120
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)121 ArkUINativeModuleValue SymbolSpanBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
122 {
123 EcmaVM* vm = runtimeCallInfo->GetVM();
124 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
125 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
126 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
127 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanFontWeight(nativeNode);
128 return panda::JSValueRef::Undefined(vm);
129 }
130
SetRenderingStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)131 ArkUINativeModuleValue SymbolSpanBridge::SetRenderingStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
132 {
133 EcmaVM* vm = runtimeCallInfo->GetVM();
134 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
135 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
136 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
137 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
138 if (secondArg->IsNumber() && secondArg->Int32Value(vm) >= NUM_0 &&
139 secondArg->Int32Value(vm) <= NUM_2) {
140 GetArkUINodeModifiers()->getSymbolSpanModifier()->setSymbolSpanRenderingStrategy(
141 nativeNode, secondArg->Int32Value(vm));
142 } else {
143 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanRenderingStrategy(nativeNode);
144 }
145 return panda::JSValueRef::Undefined(vm);
146 }
147
ResetRenderingStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)148 ArkUINativeModuleValue SymbolSpanBridge::ResetRenderingStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
149 {
150 EcmaVM* vm = runtimeCallInfo->GetVM();
151 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
152 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
153 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
154 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanRenderingStrategy(nativeNode);
155 return panda::JSValueRef::Undefined(vm);
156 }
157
SetEffectStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)158 ArkUINativeModuleValue SymbolSpanBridge::SetEffectStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
159 {
160 EcmaVM* vm = runtimeCallInfo->GetVM();
161 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
162 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
163 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
164 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
165 if (secondArg->IsNumber() && secondArg->Int32Value(vm) >= NUM_0 &&
166 secondArg->Int32Value(vm) <= NUM_2) {
167 GetArkUINodeModifiers()->getSymbolSpanModifier()->setSymbolSpanEffectStrategy(
168 nativeNode, secondArg->Int32Value(vm));
169 } else {
170 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanEffectStrategy(nativeNode);
171 }
172 return panda::JSValueRef::Undefined(vm);
173 }
174
ResetEffectStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)175 ArkUINativeModuleValue SymbolSpanBridge::ResetEffectStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
176 {
177 EcmaVM* vm = runtimeCallInfo->GetVM();
178 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
179 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
180 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
181 GetArkUINodeModifiers()->getSymbolSpanModifier()->resetSymbolSpanEffectStrategy(nativeNode);
182 return panda::JSValueRef::Undefined(vm);
183 }
184
SetId(ArkUIRuntimeCallInfo * runtimeCallInfo)185 ArkUINativeModuleValue SymbolSpanBridge::SetId(ArkUIRuntimeCallInfo* runtimeCallInfo)
186 {
187 EcmaVM* vm = runtimeCallInfo->GetVM();
188 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
189 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
190 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
191 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
192 uint32_t content = 0;
193 if (ArkTSUtils::ParseJsSymbolId(vm, secondArg, content)) {
194 if (content > SYSTEM_SYMBOL_BOUNDARY) {
195 std::string symbolFontFamilyName;
196 ArkTSUtils::ParseJsSymbolFontFamilyName(vm, secondArg, symbolFontFamilyName);
197 GetArkUINodeModifiers()->getSymbolSpanModifier()->setCustomSymbolSpanId(nativeNode, content,
198 symbolFontFamilyName.c_str());
199 } else {
200 GetArkUINodeModifiers()->getSymbolSpanModifier()->setSymbolSpanId(nativeNode, content);
201 }
202 }
203 return panda::JSValueRef::Undefined(vm);
204 }
205
206 } // namespace OHOS::Ace::NG
207