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 "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_symbol_glyph_bridge.h"
16 #include <cstdint>
17 #include "arkts_native_symbol_glyph_bridge.h"
18 #include "bridge/declarative_frontend/jsview/js_symbol.h"
19 #include "base/utils/string_utils.h"
20 #include "base/utils/utils.h"
21 #include "core/components/common/properties/shadow.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/pattern/symbol/symbol_model_ng.h"
24 #include "frameworks/base/geometry/calc_dimension.h"
25 #include "frameworks/base/geometry/dimension.h"
26 #include "frameworks/bridge/declarative_frontend/engine/js_types.h"
27 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h"
28 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
29 #include "frameworks/bridge/declarative_frontend/jsview/js_shape_abstract.h"
30 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
31 
32 namespace OHOS::Ace::NG {
33 namespace {
34 constexpr int NUM_0 = 0;
35 constexpr int NUM_1 = 1;
36 constexpr int NUM_2 = 2;
37 } // namespace
38 
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)39 ArkUINativeModuleValue SymbolGlyphBridge::SetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
40 {
41     EcmaVM* vm = runtimeCallInfo->GetVM();
42     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
43     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
44     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
45 
46     if (!secondArg->IsArray(vm)) {
47         return panda::JSValueRef::Undefined(vm);
48     }
49     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
50     auto array = Local<panda::ArrayRef>(secondArg);
51     auto length = array->Length(vm);
52 
53     std::vector<ArkUI_Uint32> colorArray;
54     for (uint32_t index = 0; index < length; index++) {
55         Local<JSValueRef> value = panda::ArrayRef::GetValueAt(vm, array, index);
56         Color color;
57         if (!ArkTSUtils::ParseJsSymbolColorAlpha(vm, value, color)) {
58             colorArray.clear();
59             break;
60         }
61         colorArray.emplace_back(color.GetValue());
62     }
63 
64     GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontColor(nativeNode, colorArray.data(), colorArray.size());
65     return panda::JSValueRef::Undefined(vm);
66 }
67 
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)68 ArkUINativeModuleValue SymbolGlyphBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
69 {
70     EcmaVM* vm = runtimeCallInfo->GetVM();
71     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
72     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
73     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
74     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontColor(nativeNode);
75     return panda::JSValueRef::Undefined(vm);
76 }
77 
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)78 ArkUINativeModuleValue SymbolGlyphBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
79 {
80     EcmaVM* vm = runtimeCallInfo->GetVM();
81     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
82     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
83     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
84     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
85     CalcDimension fontSize;
86     if (!ArkTSUtils::ParseJsDimensionFpNG(vm, secondArg, fontSize, false)) {
87         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontSize(nativeNode);
88     } else {
89         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontSize(
90             nativeNode, fontSize.Value(), static_cast<int>(fontSize.Unit()));
91     }
92     return panda::JSValueRef::Undefined(vm);
93 }
94 
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)95 ArkUINativeModuleValue SymbolGlyphBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
96 {
97     EcmaVM* vm = runtimeCallInfo->GetVM();
98     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
99     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
100     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
101     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontSize(nativeNode);
102     return panda::JSValueRef::Undefined(vm);
103 }
104 
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)105 ArkUINativeModuleValue SymbolGlyphBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
106 {
107     EcmaVM* vm = runtimeCallInfo->GetVM();
108     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
109     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
110     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
111     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
112     if (secondArg->IsString(vm)) {
113         std::string weight = secondArg->ToString(vm)->ToString(vm);
114         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontWeightStr(nativeNode, weight.c_str());
115     } else if (secondArg->IsNumber()) {
116         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontWeight(nativeNode, secondArg->Int32Value(vm));
117     } else {
118         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontWeight(nativeNode);
119     }
120     return panda::JSValueRef::Undefined(vm);
121 }
122 
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)123 ArkUINativeModuleValue SymbolGlyphBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
124 {
125     EcmaVM* vm = runtimeCallInfo->GetVM();
126     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
127     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
128     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
129     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontWeight(nativeNode);
130     return panda::JSValueRef::Undefined(vm);
131 }
132 
SetRenderingStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)133 ArkUINativeModuleValue SymbolGlyphBridge::SetRenderingStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
134 {
135     EcmaVM* vm = runtimeCallInfo->GetVM();
136     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
137     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
138     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
139     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
140     if (secondArg->IsNumber() && secondArg->Int32Value(vm) >= NUM_0 &&
141         secondArg->Int32Value(vm) <= NUM_2) {
142         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setRenderingStrategy(nativeNode, secondArg->Int32Value(vm));
143     } else {
144         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetRenderingStrategy(nativeNode);
145     }
146     return panda::JSValueRef::Undefined(vm);
147 }
148 
ResetRenderingStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)149 ArkUINativeModuleValue SymbolGlyphBridge::ResetRenderingStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
150 {
151     EcmaVM* vm = runtimeCallInfo->GetVM();
152     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
153     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
154     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
155     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetRenderingStrategy(nativeNode);
156     return panda::JSValueRef::Undefined(vm);
157 }
158 
SetEffectStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)159 ArkUINativeModuleValue SymbolGlyphBridge::SetEffectStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
160 {
161     EcmaVM* vm = runtimeCallInfo->GetVM();
162     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
163     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
164     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
165     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
166     if (secondArg->IsNumber() && secondArg->Int32Value(vm) >= NUM_0 &&
167         secondArg->Int32Value(vm) <= NUM_2) {
168         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setEffectStrategy(nativeNode, secondArg->Int32Value(vm));
169     } else {
170         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetEffectStrategy(nativeNode);
171     }
172     return panda::JSValueRef::Undefined(vm);
173 }
174 
ResetEffectStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)175 ArkUINativeModuleValue SymbolGlyphBridge::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()->getSymbolGlyphModifier()->resetEffectStrategy(nativeNode);
182     return panda::JSValueRef::Undefined(vm);
183 }
184 
SetSymbolEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)185 ArkUINativeModuleValue SymbolGlyphBridge::SetSymbolEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
186 {
187     EcmaVM* vm = runtimeCallInfo->GetVM();
188     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
189     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
190     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
191     auto* frameNode = reinterpret_cast<FrameNode*>(nativeNode);
192 
193     Framework::JsiCallbackInfo info = Framework::JsiCallbackInfo(runtimeCallInfo);
194     auto symbolEffectObj = Framework::JSRef<Framework::JSObject>::Cast(info[1]);
195     NG::SymbolEffectOptions symbolEffectOptions;
196     Framework::JSSymbol::parseSymbolEffect(symbolEffectObj, symbolEffectOptions);
197     if (info.Length() > NUM_2 && !info[NUM_2]->IsUndefined()) {
198         Framework::JSSymbol::parseSymbolSwitch(info[NUM_2], symbolEffectOptions);
199     }
200     SymbolModelNG::SetSymbolEffectOptions(frameNode, symbolEffectOptions);
201     return panda::JSValueRef::Undefined(vm);
202 }
203 
ResetSymbolEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)204 ArkUINativeModuleValue SymbolGlyphBridge::ResetSymbolEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
205 {
206     EcmaVM* vm = runtimeCallInfo->GetVM();
207     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
208     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
209     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
210     auto* frameNode = reinterpret_cast<FrameNode*>(nativeNode);
211     NG::SymbolEffectOptions symbolEffectOptions;
212     SymbolModelNG::SetSymbolEffectOptions(frameNode, symbolEffectOptions);
213     return panda::JSValueRef::Undefined(vm);
214 }
215 
SetSymbolGlyphInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)216 ArkUINativeModuleValue SymbolGlyphBridge::SetSymbolGlyphInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
217 {
218     EcmaVM* vm = runtimeCallInfo->GetVM();
219     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
220     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
221     Local<JSValueRef> valueArg = runtimeCallInfo->GetCallArgRef(1);
222     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
223     uint32_t symbolId = 0;
224     ArkTSUtils::ParseJsSymbolId(vm, valueArg, symbolId);
225     GetArkUINodeModifiers()->getSymbolGlyphModifier()->setSymbolGlyphInitialize(nativeNode, symbolId);
226     return panda::JSValueRef::Undefined(vm);
227 }
228 
ResetSymbolGlyphInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)229 ArkUINativeModuleValue SymbolGlyphBridge::ResetSymbolGlyphInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
230 {
231     EcmaVM* vm = runtimeCallInfo->GetVM();
232     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
233     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
234     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
235     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetSymbolGlyphInitialize(nativeNode);
236     return panda::JSValueRef::Undefined(vm);
237 }
238 } // namespace OHOS::Ace::NG
239