• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
26 
27 namespace OHOS::Ace::NG {
28 namespace {
29 constexpr int NUM_0 = 0;
30 constexpr int NUM_1 = 1;
31 constexpr int NUM_2 = 2;
32 constexpr int32_t SYSTEM_SYMBOL_BOUNDARY = 0XFFFFF;
33 } // namespace
34 
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)35 ArkUINativeModuleValue SymbolGlyphBridge::SetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
36 {
37     EcmaVM* vm = runtimeCallInfo->GetVM();
38     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
39     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
40     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
41 
42     if (!secondArg->IsArray(vm)) {
43         return panda::JSValueRef::Undefined(vm);
44     }
45     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
46     auto array = Local<panda::ArrayRef>(secondArg);
47     auto length = array->Length(vm);
48 
49     std::vector<ArkUI_Uint32> colorArray;
50     for (uint32_t index = 0; index < length; index++) {
51         Local<JSValueRef> value = panda::ArrayRef::GetValueAt(vm, array, index);
52         Color color;
53         if (!ArkTSUtils::ParseJsSymbolColorAlpha(vm, value, color)) {
54             colorArray.clear();
55             break;
56         }
57         colorArray.emplace_back(color.GetValue());
58     }
59 
60     GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontColor(nativeNode, colorArray.data(), colorArray.size());
61     return panda::JSValueRef::Undefined(vm);
62 }
63 
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)64 ArkUINativeModuleValue SymbolGlyphBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
65 {
66     EcmaVM* vm = runtimeCallInfo->GetVM();
67     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
68     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
69     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
70     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontColor(nativeNode);
71     return panda::JSValueRef::Undefined(vm);
72 }
73 
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)74 ArkUINativeModuleValue SymbolGlyphBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
75 {
76     EcmaVM* vm = runtimeCallInfo->GetVM();
77     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
78     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
79     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
80     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
81     CalcDimension fontSize;
82     if (!ArkTSUtils::ParseJsDimensionFpNG(vm, secondArg, fontSize, false)) {
83         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontSize(nativeNode);
84     } else {
85         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontSize(
86             nativeNode, fontSize.Value(), static_cast<int>(fontSize.Unit()));
87     }
88     return panda::JSValueRef::Undefined(vm);
89 }
90 
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)91 ArkUINativeModuleValue SymbolGlyphBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
92 {
93     EcmaVM* vm = runtimeCallInfo->GetVM();
94     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
95     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
96     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
97     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontSize(nativeNode);
98     return panda::JSValueRef::Undefined(vm);
99 }
100 
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)101 ArkUINativeModuleValue SymbolGlyphBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
102 {
103     EcmaVM* vm = runtimeCallInfo->GetVM();
104     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
105     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
106     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
107     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
108     if (secondArg->IsString(vm)) {
109         std::string weight = secondArg->ToString(vm)->ToString(vm);
110         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontWeightStr(nativeNode, weight.c_str());
111     } else if (secondArg->IsNumber()) {
112         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setFontWeight(nativeNode, secondArg->Int32Value(vm));
113     } else {
114         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontWeight(nativeNode);
115     }
116     return panda::JSValueRef::Undefined(vm);
117 }
118 
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)119 ArkUINativeModuleValue SymbolGlyphBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
120 {
121     EcmaVM* vm = runtimeCallInfo->GetVM();
122     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
123     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
124     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
125     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetFontWeight(nativeNode);
126     return panda::JSValueRef::Undefined(vm);
127 }
128 
SetRenderingStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)129 ArkUINativeModuleValue SymbolGlyphBridge::SetRenderingStrategy(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) <= NUM_2) {
138         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setRenderingStrategy(nativeNode, secondArg->Int32Value(vm));
139     } else {
140         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetRenderingStrategy(nativeNode);
141     }
142     return panda::JSValueRef::Undefined(vm);
143 }
144 
ResetRenderingStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)145 ArkUINativeModuleValue SymbolGlyphBridge::ResetRenderingStrategy(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()->getSymbolGlyphModifier()->resetRenderingStrategy(nativeNode);
152     return panda::JSValueRef::Undefined(vm);
153 }
154 
SetEffectStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)155 ArkUINativeModuleValue SymbolGlyphBridge::SetEffectStrategy(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     if (secondArg->IsNumber() && secondArg->Int32Value(vm) >= NUM_0 &&
163         secondArg->Int32Value(vm) <= NUM_2) {
164         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setEffectStrategy(nativeNode, secondArg->Int32Value(vm));
165     } else {
166         GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetEffectStrategy(nativeNode);
167     }
168     return panda::JSValueRef::Undefined(vm);
169 }
170 
ResetEffectStrategy(ArkUIRuntimeCallInfo * runtimeCallInfo)171 ArkUINativeModuleValue SymbolGlyphBridge::ResetEffectStrategy(ArkUIRuntimeCallInfo* runtimeCallInfo)
172 {
173     EcmaVM* vm = runtimeCallInfo->GetVM();
174     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
175     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
176     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
177     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetEffectStrategy(nativeNode);
178     return panda::JSValueRef::Undefined(vm);
179 }
180 
SetSymbolEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)181 ArkUINativeModuleValue SymbolGlyphBridge::SetSymbolEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
182 {
183     EcmaVM* vm = runtimeCallInfo->GetVM();
184     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
185     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
186     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
187     auto* frameNode = reinterpret_cast<FrameNode*>(nativeNode);
188 
189     Framework::JsiCallbackInfo info = Framework::JsiCallbackInfo(runtimeCallInfo);
190     auto symbolEffectObj = Framework::JSRef<Framework::JSObject>::Cast(info[1]);
191     NG::SymbolEffectOptions symbolEffectOptions;
192     Framework::JSSymbol::parseSymbolEffect(symbolEffectObj, symbolEffectOptions);
193     if (info.Length() > NUM_2 && !info[NUM_2]->IsUndefined()) {
194         Framework::JSSymbol::parseSymbolSwitch(info[NUM_2], symbolEffectOptions);
195     }
196     SymbolModelNG::SetSymbolEffectOptions(frameNode, symbolEffectOptions);
197     return panda::JSValueRef::Undefined(vm);
198 }
199 
ResetSymbolEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)200 ArkUINativeModuleValue SymbolGlyphBridge::ResetSymbolEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
201 {
202     EcmaVM* vm = runtimeCallInfo->GetVM();
203     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
204     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
205     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
206     auto* frameNode = reinterpret_cast<FrameNode*>(nativeNode);
207     NG::SymbolEffectOptions symbolEffectOptions;
208     SymbolModelNG::SetSymbolEffectOptions(frameNode, symbolEffectOptions);
209     return panda::JSValueRef::Undefined(vm);
210 }
211 
SetSymbolGlyphInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)212 ArkUINativeModuleValue SymbolGlyphBridge::SetSymbolGlyphInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
213 {
214     EcmaVM* vm = runtimeCallInfo->GetVM();
215     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
216     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
217     Local<JSValueRef> valueArg = runtimeCallInfo->GetCallArgRef(1);
218     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
219     uint32_t symbolId = 0;
220     ArkTSUtils::ParseJsSymbolId(vm, valueArg, symbolId);
221     if (symbolId > SYSTEM_SYMBOL_BOUNDARY) {
222         std::string symbolFontFamilyName;
223         ArkTSUtils::ParseJsSymbolFontFamilyName(vm, valueArg, symbolFontFamilyName);
224         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setCustomSymbolGlyphInitialize(nativeNode, symbolId,
225             symbolFontFamilyName.c_str());
226     } else {
227         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setSymbolGlyphInitialize(nativeNode, symbolId);
228     }
229     return panda::JSValueRef::Undefined(vm);
230 }
231 
ResetSymbolGlyphInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)232 ArkUINativeModuleValue SymbolGlyphBridge::ResetSymbolGlyphInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
233 {
234     EcmaVM* vm = runtimeCallInfo->GetVM();
235     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
236     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
237     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
238     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetSymbolGlyphInitialize(nativeNode);
239     return panda::JSValueRef::Undefined(vm);
240 }
241 
SetMinFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)242 ArkUINativeModuleValue SymbolGlyphBridge::SetMinFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
243 {
244     EcmaVM* vm = runtimeCallInfo->GetVM();
245     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
246     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
247     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
248     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
249     double minFontScale = 0.0;
250     if (ArkTSUtils::ParseJsDouble(vm, secondArg, minFontScale)) {
251         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setMinFontScale(nativeNode,
252             static_cast<float>(minFontScale));
253     }
254     return panda::JSValueRef::Undefined(vm);
255 }
256 
ResetMinFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)257 ArkUINativeModuleValue SymbolGlyphBridge::ResetMinFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
258 {
259     EcmaVM* vm = runtimeCallInfo->GetVM();
260     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
261     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
262     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
263     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetMinFontScale(nativeNode);
264     return panda::JSValueRef::Undefined(vm);
265 }
266 
SetMaxFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)267 ArkUINativeModuleValue SymbolGlyphBridge::SetMaxFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
268 {
269     EcmaVM* vm = runtimeCallInfo->GetVM();
270     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
271     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
272     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
273     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
274     double maxFontScale = 0.0;
275     if (ArkTSUtils::ParseJsDouble(vm, secondArg, maxFontScale)) {
276         GetArkUINodeModifiers()->getSymbolGlyphModifier()->setMaxFontScale(nativeNode,
277             static_cast<float>(maxFontScale));
278     }
279     return panda::JSValueRef::Undefined(vm);
280 }
281 
ResetMaxFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)282 ArkUINativeModuleValue SymbolGlyphBridge::ResetMaxFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
283 {
284     EcmaVM* vm = runtimeCallInfo->GetVM();
285     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
286     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
287     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
288     GetArkUINodeModifiers()->getSymbolGlyphModifier()->resetMaxFontScale(nativeNode);
289     return panda::JSValueRef::Undefined(vm);
290 }
291 } // namespace OHOS::Ace::NG
292