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/geometry/dimension.h"
18 #include "base/geometry/dimension_size.h"
19 #include "base/utils/utils.h"
20 #include "bridge/common/utils/utils.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/common/properties/alignment.h"
23 #include "core/components/common/properties/text_style.h"
24 #include "core/components_ng/base/frame_node.h"
25 #include "core/components_ng/base/view_abstract.h"
26 #include "core/components_ng/pattern/text/span_model_ng.h"
27 #include "core/pipeline/base/element_register.h"
28 #include "draw/canvas.h"
29
30 namespace OHOS::Ace::NG {
31 namespace {
32 constexpr TextCase DEFAULT_TEXT_CASE = TextCase::NORMAL;
33 constexpr FontWeight DEFAULT_FONT_WEIGHT = FontWeight::NORMAL;
34 constexpr Ace::FontStyle DEFAULT_FONT_STYLE_VALUE = Ace::FontStyle::NORMAL;
35 constexpr Dimension DEFAULT_FONT_SIZE = Dimension(16.0, DimensionUnit::FP);
36 constexpr Dimension DEFAULT_BASELINE_OFFSET { 0.0, DimensionUnit::FP };
37 std::string g_strValue;
38 constexpr int NUM_0 = 0;
39 constexpr int NUM_1 = 1;
40 constexpr int NUM_2 = 2;
41 constexpr int NUM_3 = 3;
42 constexpr int NUM_32 = 32;
43 constexpr int DEFAULT_LENGTH = 4;
SetSpanContent(ArkUINodeHandle node,const char * value)44 void SetSpanContent(ArkUINodeHandle node, const char* value)
45 {
46 auto* uiNode = reinterpret_cast<UINode*>(node);
47 CHECK_NULL_VOID(uiNode);
48 std::string content(value);
49 SpanModelNG::InitSpan(uiNode, content);
50 }
51
GetSpanContent(ArkUINodeHandle node)52 const char* GetSpanContent(ArkUINodeHandle node)
53 {
54 auto* uiNode = reinterpret_cast<UINode*>(node);
55 CHECK_NULL_RETURN(uiNode, nullptr);
56 g_strValue = SpanModelNG::GetContent(uiNode);
57 return g_strValue.c_str();
58 }
59
SetSpanSrc(ArkUINodeHandle node,ArkUI_CharPtr src)60 void SetSpanSrc(ArkUINodeHandle node, ArkUI_CharPtr src)
61 {
62 auto* uiNode = reinterpret_cast<UINode*>(node);
63 CHECK_NULL_VOID(uiNode);
64 SpanModelNG::InitSpan(uiNode, src);
65 }
66
SetSpanTextCase(ArkUINodeHandle node,int32_t value)67 void SetSpanTextCase(ArkUINodeHandle node, int32_t value)
68 {
69 auto* uiNode = reinterpret_cast<UINode*>(node);
70 CHECK_NULL_VOID(uiNode);
71 SpanModelNG::SetTextCase(uiNode, static_cast<TextCase>(value));
72 }
73
GetSpanTextCase(ArkUINodeHandle node)74 int32_t GetSpanTextCase(ArkUINodeHandle node)
75 {
76 auto defaultTextCase = static_cast<int32_t>(DEFAULT_TEXT_CASE);
77 auto* uiNode = reinterpret_cast<UINode*>(node);
78 CHECK_NULL_RETURN(uiNode, defaultTextCase);
79 return static_cast<int32_t>(SpanModelNG::GetTextCase(uiNode));
80 }
81
ResetSpanTextCase(ArkUINodeHandle node)82 void ResetSpanTextCase(ArkUINodeHandle node)
83 {
84 auto* uiNode = reinterpret_cast<UINode*>(node);
85 CHECK_NULL_VOID(uiNode);
86 SpanModelNG::ResetTextCase(uiNode);
87 }
88
SetSpanFontWeightStr(ArkUINodeHandle node,const char * value)89 void SetSpanFontWeightStr(ArkUINodeHandle node, const char* value)
90 {
91 auto* uiNode = reinterpret_cast<UINode*>(node);
92 CHECK_NULL_VOID(uiNode);
93 SpanModelNG::SetFontWeight(uiNode, Framework::ConvertStrToFontWeight(value));
94 }
95
SetSpanFontWeight(ArkUINodeHandle node,ArkUI_Int32 fontWeight)96 void SetSpanFontWeight(ArkUINodeHandle node, ArkUI_Int32 fontWeight)
97 {
98 auto* uiNode = reinterpret_cast<UINode*>(node);
99 CHECK_NULL_VOID(uiNode);
100 SpanModelNG::SetFontWeight(uiNode, static_cast<FontWeight>(fontWeight));
101 }
102
GetSpanFontWeight(ArkUINodeHandle node)103 int32_t GetSpanFontWeight(ArkUINodeHandle node)
104 {
105 int32_t defaultFontWeight = static_cast<int32_t>(DEFAULT_FONT_WEIGHT);
106 auto* uiNode = reinterpret_cast<UINode*>(node);
107 CHECK_NULL_RETURN(uiNode, defaultFontWeight);
108 return static_cast<int32_t>(SpanModelNG::GetFontWeight(uiNode));
109 }
110
ResetSpanFontWeight(ArkUINodeHandle node)111 void ResetSpanFontWeight(ArkUINodeHandle node)
112 {
113 auto* uiNode = reinterpret_cast<UINode*>(node);
114 CHECK_NULL_VOID(uiNode);
115 SpanModelNG::ResetFontWeight(uiNode);
116 }
117
SetSpanLineHeight(ArkUINodeHandle node,ArkUI_Float32 number,ArkUI_Int32 unit)118 void SetSpanLineHeight(ArkUINodeHandle node, ArkUI_Float32 number, ArkUI_Int32 unit)
119 {
120 auto* uiNode = reinterpret_cast<UINode*>(node);
121 CHECK_NULL_VOID(uiNode);
122 SpanModelNG::SetLineHeight(uiNode, Dimension(number, static_cast<DimensionUnit>(unit)));
123 }
124
GetSpanLineHeight(ArkUINodeHandle node)125 float GetSpanLineHeight(ArkUINodeHandle node)
126 {
127 auto* uiNode = reinterpret_cast<UINode*>(node);
128 CHECK_NULL_RETURN(uiNode, 0.0f);
129 return SpanModelNG::GetTextLineHeight(uiNode).ConvertToVp();
130 }
131
ResetSpanLineHeight(ArkUINodeHandle node)132 void ResetSpanLineHeight(ArkUINodeHandle node)
133 {
134 auto* uiNode = reinterpret_cast<UINode*>(node);
135 CHECK_NULL_VOID(uiNode);
136 SpanModelNG::ResetLineHeight(uiNode);
137 }
138
SetSpanFontStyle(ArkUINodeHandle node,int32_t value)139 void SetSpanFontStyle(ArkUINodeHandle node, int32_t value)
140 {
141 auto* uiNode = reinterpret_cast<UINode*>(node);
142 CHECK_NULL_VOID(uiNode);
143 SpanModelNG::SetItalicFontStyle(uiNode, static_cast<Ace::FontStyle>(value));
144 }
145
GetSpanFontStyle(ArkUINodeHandle node)146 int32_t GetSpanFontStyle(ArkUINodeHandle node)
147 {
148 int32_t defaultFontStyle = static_cast<int32_t>(DEFAULT_FONT_STYLE_VALUE);
149 auto* uiNode = reinterpret_cast<UINode*>(node);
150 CHECK_NULL_RETURN(uiNode, defaultFontStyle);
151 return static_cast<int32_t>(SpanModelNG::GetFontStyle(uiNode));
152 }
153
ResetSpanFontStyle(ArkUINodeHandle node)154 void ResetSpanFontStyle(ArkUINodeHandle node)
155 {
156 auto* uiNode = reinterpret_cast<UINode*>(node);
157 CHECK_NULL_VOID(uiNode);
158 SpanModelNG::ResetItalicFontStyle(uiNode);
159 }
160
SetSpanFontSize(ArkUINodeHandle node,ArkUI_Float32 number,ArkUI_Int32 unit)161 void SetSpanFontSize(ArkUINodeHandle node, ArkUI_Float32 number, ArkUI_Int32 unit)
162 {
163 auto* uiNode = reinterpret_cast<UINode*>(node);
164 CHECK_NULL_VOID(uiNode);
165 SpanModelNG::SetFontSize(uiNode, Dimension(number, static_cast<DimensionUnit>(unit)));
166 }
167
GetSpanFontSize(ArkUINodeHandle node,ArkUI_Int32 unit)168 float GetSpanFontSize(ArkUINodeHandle node, ArkUI_Int32 unit)
169 {
170 auto* uiNode = reinterpret_cast<UINode*>(node);
171 CHECK_NULL_RETURN(uiNode, DEFAULT_FONT_SIZE.Value());
172 return SpanModelNG::GetFontSize(uiNode).GetNativeValue(static_cast<DimensionUnit>(unit));
173 }
174
ResetSpanFontSize(ArkUINodeHandle node)175 void ResetSpanFontSize(ArkUINodeHandle node)
176 {
177 auto* uiNode = reinterpret_cast<UINode*>(node);
178 CHECK_NULL_VOID(uiNode);
179 SpanModelNG::ResetFontSize(uiNode);
180 }
181
SetSpanFontFamily(ArkUINodeHandle node,const char ** fontFamilies,uint32_t length)182 void SetSpanFontFamily(ArkUINodeHandle node, const char** fontFamilies, uint32_t length)
183 {
184 CHECK_NULL_VOID(fontFamilies);
185 if (length <= 0) {
186 return;
187 }
188 auto* uiNode = reinterpret_cast<UINode*>(node);
189 CHECK_NULL_VOID(uiNode);
190 std::vector<std::string> families;
191 for (uint32_t i = 0; i < length; i++) {
192 const char* family = *(fontFamilies + i);
193 if (family != nullptr) {
194 families.emplace_back(std::string(family));
195 }
196 }
197 SpanModelNG::SetFontFamily(uiNode, families);
198 }
199
ResetSpanFontFamily(ArkUINodeHandle node)200 void ResetSpanFontFamily(ArkUINodeHandle node)
201 {
202 auto* uiNode = reinterpret_cast<UINode*>(node);
203 CHECK_NULL_VOID(uiNode);
204 SpanModelNG::ResetFontFamily(uiNode);
205 }
206
SetSpanDecoration(ArkUINodeHandle node,ArkUI_Int32 decoration,ArkUI_Uint32 color,ArkUI_Int32 style)207 void SetSpanDecoration(ArkUINodeHandle node, ArkUI_Int32 decoration, ArkUI_Uint32 color, ArkUI_Int32 style)
208 {
209 auto* uiNode = reinterpret_cast<UINode*>(node);
210 CHECK_NULL_VOID(uiNode);
211 SpanModelNG::SetTextDecoration(uiNode, static_cast<TextDecoration>(decoration));
212 SpanModelNG::SetTextDecorationStyle(uiNode, static_cast<TextDecorationStyle>(style));
213 SpanModelNG::SetTextDecorationColor(uiNode, Color(color));
214 }
215
GetSpanDecoration(ArkUINodeHandle node,ArkUITextDecorationType * decoration)216 void GetSpanDecoration(ArkUINodeHandle node, ArkUITextDecorationType* decoration)
217 {
218 CHECK_NULL_VOID(decoration);
219 auto* uiNode = reinterpret_cast<UINode*>(node);
220 CHECK_NULL_VOID(uiNode);
221 decoration->decorationType = static_cast<int32_t>(SpanModelNG::GetTextDecoration(uiNode));
222 decoration->color = SpanModelNG::GetTextDecorationColor(uiNode).GetValue();
223 decoration->style = static_cast<int32_t>(SpanModelNG::GetTextDecorationStyle(uiNode));
224 }
225
ResetSpanDecoration(ArkUINodeHandle node)226 void ResetSpanDecoration(ArkUINodeHandle node)
227 {
228 auto* uiNode = reinterpret_cast<UINode*>(node);
229 CHECK_NULL_VOID(uiNode);
230 SpanModelNG::ResetTextDecoration(uiNode);
231 SpanModelNG::ResetTextDecorationStyle(uiNode);
232 SpanModelNG::ResetTextDecorationColor(uiNode);
233 }
234
SetSpanFontColor(ArkUINodeHandle node,uint32_t textColor)235 void SetSpanFontColor(ArkUINodeHandle node, uint32_t textColor)
236 {
237 auto* uiNode = reinterpret_cast<UINode*>(node);
238 CHECK_NULL_VOID(uiNode);
239 SpanModelNG::SetTextColor(uiNode, Color(textColor));
240 }
241
GetSpanFontColor(ArkUINodeHandle node)242 uint32_t GetSpanFontColor(ArkUINodeHandle node)
243 {
244 auto* uiNode = reinterpret_cast<UINode*>(node);
245 CHECK_NULL_RETURN(uiNode, Color::BLACK.GetValue());
246 return SpanModelNG::GetFontColor(uiNode).GetValue();
247 }
248
ResetSpanFontColor(ArkUINodeHandle node)249 void ResetSpanFontColor(ArkUINodeHandle node)
250 {
251 auto* uiNode = reinterpret_cast<UINode*>(node);
252 CHECK_NULL_VOID(uiNode);
253 SpanModelNG::ResetTextColor(uiNode);
254 }
255
SetSpanLetterSpacing(ArkUINodeHandle node,const struct ArkUIStringAndFloat * letterSpacingValue)256 void SetSpanLetterSpacing(ArkUINodeHandle node, const struct ArkUIStringAndFloat* letterSpacingValue)
257 {
258 auto* uiNode = reinterpret_cast<UINode*>(node);
259 CHECK_NULL_VOID(uiNode);
260 Dimension result;
261 if (letterSpacingValue->valueStr != nullptr) {
262 result = StringUtils::StringToDimensionWithUnit(std::string(letterSpacingValue->valueStr), DimensionUnit::FP);
263 } else {
264 result = Dimension(letterSpacingValue->value, DimensionUnit::FP);
265 }
266 SpanModelNG::SetLetterSpacing(uiNode, result);
267 }
268
GetSpanLetterSpacing(ArkUINodeHandle node)269 float GetSpanLetterSpacing(ArkUINodeHandle node)
270 {
271 auto* uiNode = reinterpret_cast<UINode*>(node);
272 CHECK_NULL_RETURN(uiNode, 0.0f);
273 return SpanModelNG::GetLetterSpacing(uiNode).ConvertToVp();
274 }
275
ResetSpanLetterSpacing(ArkUINodeHandle node)276 void ResetSpanLetterSpacing(ArkUINodeHandle node)
277 {
278 auto* uiNode = reinterpret_cast<UINode*>(node);
279 CHECK_NULL_VOID(uiNode);
280 SpanModelNG::ResetLetterSpacing(uiNode);
281 }
282
SetSpanBaselineOffset(ArkUINodeHandle node,ArkUI_Float32 value,ArkUI_Int32 unit)283 void SetSpanBaselineOffset(ArkUINodeHandle node, ArkUI_Float32 value, ArkUI_Int32 unit)
284 {
285 auto* uiNode = reinterpret_cast<UINode*>(node);
286 CHECK_NULL_VOID(uiNode);
287 SpanModelNG::SetBaselineOffset(uiNode, CalcDimension(value, (DimensionUnit)unit));
288 }
289
GetSpanBaselineOffset(ArkUINodeHandle node)290 float GetSpanBaselineOffset(ArkUINodeHandle node)
291 {
292 auto* uiNode = reinterpret_cast<UINode*>(node);
293 CHECK_NULL_RETURN(uiNode, 0.0f);
294 return SpanModelNG::GetBaselineOffset(uiNode).ConvertToVp();
295 }
296
ResetSpanBaselineOffset(ArkUINodeHandle node)297 void ResetSpanBaselineOffset(ArkUINodeHandle node)
298 {
299 auto* uiNode = reinterpret_cast<UINode*>(node);
300 CHECK_NULL_VOID(uiNode);
301 SpanModelNG::SetBaselineOffset(uiNode, DEFAULT_BASELINE_OFFSET);
302 }
303
SetSpanFont(ArkUINodeHandle node,const struct ArkUIFontStruct * fontInfo)304 void SetSpanFont(ArkUINodeHandle node, const struct ArkUIFontStruct* fontInfo)
305 {
306 CHECK_NULL_VOID(fontInfo);
307 auto* uiNode = reinterpret_cast<UINode*>(node);
308 CHECK_NULL_VOID(uiNode);
309 Font font;
310 font.fontSize = Dimension(fontInfo->fontSizeNumber, static_cast<DimensionUnit>(fontInfo->fontSizeUnit));
311 font.fontStyle = static_cast<Ace::FontStyle>(fontInfo->fontStyle);
312 font.fontWeight = static_cast<FontWeight>(fontInfo->fontWeight);
313 std::vector<std::string> families;
314 if (fontInfo->fontFamilies && fontInfo->familyLength > 0) {
315 families.resize(fontInfo->familyLength);
316 for (uint32_t i = 0; i < fontInfo->familyLength; i++) {
317 families.at(i) = std::string(*(fontInfo->fontFamilies + i));
318 }
319 }
320 font.fontFamilies = families;
321 SpanModelNG::SetFont(uiNode, font);
322 }
323
ResetSpanFont(ArkUINodeHandle node)324 void ResetSpanFont(ArkUINodeHandle node)
325 {
326 auto* uiNode = reinterpret_cast<UINode*>(node);
327 CHECK_NULL_VOID(uiNode);
328 SpanModelNG::ResetFont(uiNode);
329 }
330
SetSpanTextBackgroundStyle(ArkUINodeHandle node,ArkUI_Uint32 color,const ArkUI_Float32 * values,const ArkUI_Int32 * units,ArkUI_Int32 length)331 void SetSpanTextBackgroundStyle(
332 ArkUINodeHandle node, ArkUI_Uint32 color, const ArkUI_Float32* values, const ArkUI_Int32* units, ArkUI_Int32 length)
333 {
334 auto* uiNode = reinterpret_cast<UINode*>(node);
335 CHECK_NULL_VOID(uiNode);
336 if (length != DEFAULT_LENGTH) {
337 return;
338 }
339 TextBackgroundStyle font;
340 NG::BorderRadiusProperty borderRadius;
341 borderRadius.radiusTopLeft = Dimension(values[NUM_0], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_0]));
342 borderRadius.radiusTopRight = Dimension(values[NUM_1], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_1]));
343 borderRadius.radiusBottomLeft = Dimension(values[NUM_2], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_2]));
344 borderRadius.radiusBottomRight = Dimension(values[NUM_3], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_3]));
345 font.backgroundColor = Color(color);
346 font.backgroundRadius = borderRadius;
347 font.backgroundRadius->multiValued = true;
348 SpanModelNG::SetTextBackgroundStyle(uiNode, font);
349 }
350
ResetSpanTextBackgroundStyle(ArkUINodeHandle node)351 void ResetSpanTextBackgroundStyle(ArkUINodeHandle node)
352 {
353 auto* uiNode = reinterpret_cast<UINode*>(node);
354 CHECK_NULL_VOID(uiNode);
355 TextBackgroundStyle font;
356 NG::BorderRadiusProperty borderRadius;
357 borderRadius.radiusTopLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
358 borderRadius.radiusTopRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
359 borderRadius.radiusBottomLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
360 borderRadius.radiusBottomRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
361 font.backgroundColor = Color(0x00000000);
362 font.backgroundRadius = borderRadius;
363 font.backgroundRadius->multiValued = true;
364 SpanModelNG::SetTextBackgroundStyle(uiNode, font);
365 }
366
GetSpanTextBackgroundStyle(ArkUINodeHandle node,ArkUITextBackgroundStyleOptions * options)367 void GetSpanTextBackgroundStyle(ArkUINodeHandle node, ArkUITextBackgroundStyleOptions* options)
368 {
369 auto* uiNode = reinterpret_cast<UINode*>(node);
370 CHECK_NULL_VOID(uiNode);
371 auto styleOptions = SpanModelNG::GetSpanTextBackgroundStyle(uiNode);
372 options->color = styleOptions.backgroundColor->GetValue();
373 options->topLeft = styleOptions.backgroundRadius->radiusTopLeft->Value();
374 options->topRight = styleOptions.backgroundRadius->radiusTopRight->Value();
375 options->bottomLeft = styleOptions.backgroundRadius->radiusBottomLeft->Value();
376 options->bottomLeft = styleOptions.backgroundRadius->radiusBottomLeft->Value();
377 }
378
SetTextTextShadow(ArkUINodeHandle node,struct ArkUITextShadowStruct * shadows,ArkUI_Uint32 length)379 void SetTextTextShadow(ArkUINodeHandle node, struct ArkUITextShadowStruct* shadows, ArkUI_Uint32 length)
380 {
381 CHECK_NULL_VOID(shadows);
382 auto* frameNode = reinterpret_cast<FrameNode*>(node);
383 CHECK_NULL_VOID(frameNode);
384 std::vector<Shadow> shadowList(length);
385 for (uint32_t i = 0; i < length; i++) {
386 Shadow shadow;
387 ArkUITextShadowStruct* shadowStruct = shadows + i;
388 shadow.SetBlurRadius(shadowStruct->radius);
389 shadow.SetShadowType(static_cast<ShadowType>(shadowStruct->type));
390 shadow.SetColor(Color(shadowStruct->color));
391 shadow.SetOffsetX(shadowStruct->offsetX);
392 shadow.SetOffsetY(shadowStruct->offsetY);
393 shadow.SetIsFilled(static_cast<bool>(shadowStruct->fill));
394 shadowList.at(i) = shadow;
395 }
396 SpanModelNG::SetTextShadow(frameNode, shadowList);
397 }
398
GetTextShadow(ArkUINodeHandle node,ArkUITextShadowStruct * shadow,uint32_t size)399 void GetTextShadow(ArkUINodeHandle node, ArkUITextShadowStruct* shadow, uint32_t size)
400 {
401 auto* frameNode = reinterpret_cast<FrameNode*>(node);
402 CHECK_NULL_VOID(frameNode);
403 std::vector<ArkUITextShadowStruct> shadowArray;
404 auto textShadowVector = SpanModelNG::GetTextShadow(frameNode);
405 for (uint32_t i = 0; i < size; i++) {
406 if (i < textShadowVector.size()) {
407 *(shadow + i) = { static_cast<float>(textShadowVector[i].GetBlurRadius()),
408 static_cast<int32_t>(textShadowVector[i].GetShadowType()), textShadowVector[i].GetColor().GetValue(),
409 textShadowVector[i].GetOffset().GetX(), textShadowVector[i].GetOffset().GetY(),
410 textShadowVector[i].GetIsFilled() };
411 } else {
412 *(shadow + i) = { 0.0f, static_cast<int32_t>(ShadowType::COLOR), Color::TRANSPARENT.GetValue(), 0.0f, 0.0f,
413 0 };
414 }
415 }
416 }
417
ResetTextTextShadow(ArkUINodeHandle node)418 void ResetTextTextShadow(ArkUINodeHandle node)
419 {
420 auto* frameNode = reinterpret_cast<FrameNode*>(node);
421 CHECK_NULL_VOID(frameNode);
422 SpanModelNG::ResetTextShadow(frameNode);
423 }
424
GetSpanFontFamily(ArkUINodeHandle node)425 ArkUI_CharPtr GetSpanFontFamily(ArkUINodeHandle node)
426 {
427 auto* frameNode = reinterpret_cast<UINode*>(node);
428 CHECK_NULL_RETURN(frameNode, nullptr);
429 std::vector<std::string> fontFamilies = SpanModelNG::GetSpanFontFamily(frameNode);
430 std::string families;
431 //set index start
432 uint32_t index = 0;
433 for (auto& family : fontFamilies) {
434 families += family;
435 if (index != fontFamilies.size() - 1) {
436 families += ",";
437 }
438 index++;
439 }
440 g_strValue = families;
441 return g_strValue.c_str();
442 }
443
SetAccessibilityText(ArkUINodeHandle node,ArkUI_CharPtr value)444 void SetAccessibilityText(ArkUINodeHandle node, ArkUI_CharPtr value)
445 {
446 auto* frameNode = reinterpret_cast<FrameNode*>(node);
447 CHECK_NULL_VOID(frameNode);
448 std::string valueStr = value;
449 SpanModelNG::SetAccessibilityText(frameNode, valueStr);
450 }
451
ResetAccessibilityText(ArkUINodeHandle node)452 void ResetAccessibilityText(ArkUINodeHandle node)
453 {
454 auto* frameNode = reinterpret_cast<FrameNode*>(node);
455 CHECK_NULL_VOID(frameNode);
456 SpanModelNG::SetAccessibilityText(frameNode, "");
457 }
458
SetAccessibilityDescription(ArkUINodeHandle node,ArkUI_CharPtr value)459 void SetAccessibilityDescription(ArkUINodeHandle node, ArkUI_CharPtr value)
460 {
461 auto* frameNode = reinterpret_cast<FrameNode*>(node);
462 CHECK_NULL_VOID(frameNode);
463 CHECK_NULL_VOID(value);
464 std::string valueStr = value;
465 SpanModelNG::SetAccessibilityDescription(frameNode, valueStr);
466 }
467
ResetAccessibilityDescription(ArkUINodeHandle node)468 void ResetAccessibilityDescription(ArkUINodeHandle node)
469 {
470 auto* frameNode = reinterpret_cast<FrameNode*>(node);
471 CHECK_NULL_VOID(frameNode);
472 SpanModelNG::SetAccessibilityDescription(frameNode, "");
473 }
474
SetAccessibilityLevel(ArkUINodeHandle node,ArkUI_CharPtr value)475 void SetAccessibilityLevel(ArkUINodeHandle node, ArkUI_CharPtr value)
476 {
477 auto* frameNode = reinterpret_cast<FrameNode*>(node);
478 CHECK_NULL_VOID(frameNode);
479 CHECK_NULL_VOID(value);
480 std::string valueStr = value;
481 SpanModelNG::SetAccessibilityImportance(frameNode, valueStr);
482 }
483
ResetAccessibilityLevel(ArkUINodeHandle node)484 void ResetAccessibilityLevel(ArkUINodeHandle node)
485 {
486 auto* frameNode = reinterpret_cast<FrameNode*>(node);
487 CHECK_NULL_VOID(frameNode);
488 SpanModelNG::SetAccessibilityImportance(frameNode, "");
489 }
490 } // namespace
491 namespace NodeModifier {
GetSpanModifier()492 const ArkUISpanModifier* GetSpanModifier()
493 {
494 static const ArkUISpanModifier modifier = { SetSpanSrc, SetSpanContent, SetSpanTextCase, ResetSpanTextCase,
495 SetSpanFontWeight, ResetSpanFontWeight, SetSpanLineHeight, ResetSpanLineHeight, SetSpanFontStyle,
496 ResetSpanFontStyle, SetSpanFontSize, ResetSpanFontSize, SetSpanFontFamily, ResetSpanFontFamily,
497 SetSpanDecoration, ResetSpanDecoration, SetSpanFontColor, ResetSpanFontColor, SetSpanLetterSpacing,
498 ResetSpanLetterSpacing, SetSpanBaselineOffset, ResetSpanBaselineOffset, SetSpanFont, ResetSpanFont,
499 SetSpanFontWeightStr, GetSpanContent, GetSpanDecoration, GetSpanFontColor, GetSpanFontSize, GetSpanFontStyle,
500 GetSpanFontWeight, GetSpanLineHeight, GetSpanTextCase, GetSpanLetterSpacing, GetSpanBaselineOffset,
501 SetSpanTextBackgroundStyle, ResetSpanTextBackgroundStyle, GetSpanTextBackgroundStyle, SetTextTextShadow,
502 ResetTextTextShadow, GetTextShadow, GetSpanFontFamily,
503 SetAccessibilityText, ResetAccessibilityText, SetAccessibilityDescription, ResetAccessibilityDescription,
504 SetAccessibilityLevel, ResetAccessibilityLevel };
505 return &modifier;
506 }
507
GetCJUISpanModifier()508 const CJUISpanModifier* GetCJUISpanModifier()
509 {
510 static const CJUISpanModifier modifier = { SetSpanSrc, SetSpanContent, SetSpanTextCase, ResetSpanTextCase,
511 SetSpanFontWeight, ResetSpanFontWeight, SetSpanLineHeight, ResetSpanLineHeight, SetSpanFontStyle,
512 ResetSpanFontStyle, SetSpanFontSize, ResetSpanFontSize, SetSpanFontFamily, ResetSpanFontFamily,
513 SetSpanDecoration, ResetSpanDecoration, SetSpanFontColor, ResetSpanFontColor, SetSpanLetterSpacing,
514 ResetSpanLetterSpacing, SetSpanBaselineOffset, ResetSpanBaselineOffset, SetSpanFont, ResetSpanFont,
515 SetSpanFontWeightStr, GetSpanContent, GetSpanDecoration, GetSpanFontColor, GetSpanFontSize, GetSpanFontStyle,
516 GetSpanFontWeight, GetSpanLineHeight, GetSpanTextCase, GetSpanLetterSpacing, GetSpanBaselineOffset,
517 SetSpanTextBackgroundStyle, ResetSpanTextBackgroundStyle, GetSpanTextBackgroundStyle, SetTextTextShadow,
518 ResetTextTextShadow, GetTextShadow };
519 return &modifier;
520 }
521
SetCustomSpanOnMeasure(ArkUINodeHandle node,void * extraParam)522 void SetCustomSpanOnMeasure(ArkUINodeHandle node, void* extraParam)
523 {
524 auto* frameNode = reinterpret_cast<CustomSpanNode*>(node);
525 CHECK_NULL_VOID(frameNode);
526 std::function<CustomSpanMetrics(CustomSpanMeasureInfo)> onMeasureFunc =
527 [node, extraParam](CustomSpanMeasureInfo customSpanMeasureInfo) -> CustomSpanMetrics {
528 ArkUICustomNodeEvent event;
529 event.kind = ArkUIAPINodeFlags::CUSTOM_MEASURE;
530 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
531 event.numberData[0].f32 = customSpanMeasureInfo.fontSize;
532 event.numberReturnData[0].f32 = 0.0f;
533 event.numberReturnData[1].f32 = 0.0f;
534 SendArkUIAsyncCustomEvent(&event);
535 float width = std::max(event.numberReturnData[0].f32, 0.0f);
536 float height = std::max(event.numberReturnData[1].f32, 0.0f);
537 return { width, height };
538 };
539 frameNode->GetSpanItem()->onMeasure = onMeasureFunc;
540 }
541
SetCustomSpanOnDraw(ArkUINodeHandle node,void * extraParam)542 void SetCustomSpanOnDraw(ArkUINodeHandle node, void* extraParam)
543 {
544 auto* frameNode = reinterpret_cast<CustomSpanNode*>(node);
545 CHECK_NULL_VOID(frameNode);
546 std::function<void(NG::DrawingContext&, CustomSpanOptions)> onDrawFunc =
547 [node, extraParam](NG::DrawingContext& context, CustomSpanOptions customSpanOptions) {
548 auto canvas = reinterpret_cast<uintptr_t>(&context.canvas);
549 ArkUICustomNodeEvent event;
550 event.kind = ArkUIAPINodeFlags::CUSTOM_DRAW;
551 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
552 event.data[NUM_0] = (ArkUI_Int32)(canvas & 0xffffffff);
553 event.data[NUM_1] =
554 (ArkUI_Int32)((static_cast<uint64_t>(canvas) >> NUM_32) & 0xffffffff);
555 event.data[NUM_2] = context.width;
556 event.data[NUM_3] = context.height;
557 event.canvas = reinterpret_cast<intptr_t>(&context.canvas);
558 event.numberData[0].f32 = customSpanOptions.x;
559 event.numberData[1].f32 = customSpanOptions.lineTop;
560 event.numberData[2].f32 = customSpanOptions.lineBottom;
561 event.numberData[3].f32 = customSpanOptions.baseline;
562 // clip
563 context.canvas.Save();
564 auto clipInnerRect = RSRect(0, 0, context.width, context.height);
565 context.canvas.ClipRect(clipInnerRect, RSClipOp::INTERSECT);
566 SendArkUIAsyncCustomEvent(&event);
567 context.canvas.Restore();
568 };
569 frameNode->GetSpanItem()->onDraw = onDrawFunc;
570 }
571 } // namespace NodeModifier
572
573 } // namespace OHOS::Ace::NG