1 /*
2 * Copyright (c) 2021-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
16 #include "constants_converter.h"
17
18 #include "rosen_text/hm_symbol_txt.h"
19 #include "rosen_text/typography_create.h"
20 #include "rosen_text/typography_style.h"
21
22 #include "base/i18n/localization.h"
23
24 namespace OHOS::Ace::Constants {
25 namespace {
26 const std::string FONTWEIGHT = "wght";
27 constexpr float DEFAULT_MULTIPLE = 100.0f;
28 constexpr float MIN_FONT_WEIGHT = 100.0f;
29 constexpr float DEFAULT_FONT_WEIGHT = 400.0f;
30 constexpr float MAX_FONT_WEIGHT = 900.0f;
31 constexpr int32_t SCALE_EFFECT = 2;
32 constexpr int32_t NONE_EFFECT = 0;
33 constexpr float ORIGINAL_LINE_HEIGHT_SCALE = 1.0f;
34 const std::string DEFAULT_SYMBOL_FONTFAMILY = "HM Symbol";
35 } // namespace
36
ConvertTxtFontWeight(FontWeight fontWeight)37 Rosen::FontWeight ConvertTxtFontWeight(FontWeight fontWeight)
38 {
39 Rosen::FontWeight convertValue;
40 switch (fontWeight) {
41 case FontWeight::W100:
42 case FontWeight::LIGHTER:
43 convertValue = Rosen::FontWeight::W100;
44 break;
45 case FontWeight::W200:
46 convertValue = Rosen::FontWeight::W200;
47 break;
48 case FontWeight::W300:
49 convertValue = Rosen::FontWeight::W300;
50 break;
51 case FontWeight::W400:
52 case FontWeight::NORMAL:
53 case FontWeight::REGULAR:
54 convertValue = Rosen::FontWeight::W400;
55 break;
56 case FontWeight::W500:
57 case FontWeight::MEDIUM:
58 convertValue = Rosen::FontWeight::W500;
59 break;
60 case FontWeight::W600:
61 convertValue = Rosen::FontWeight::W600;
62 break;
63 case FontWeight::W700:
64 case FontWeight::BOLD:
65 convertValue = Rosen::FontWeight::W700;
66 break;
67 case FontWeight::W800:
68 convertValue = Rosen::FontWeight::W800;
69 break;
70 case FontWeight::W900:
71 case FontWeight::BOLDER:
72 convertValue = Rosen::FontWeight::W900;
73 break;
74 default:
75 TAG_LOGW(AceLogTag::ACE_FONT, "FontWeight setting error! Now using default FontWeight.");
76 convertValue = Rosen::FontWeight::W400;
77 break;
78 }
79 return convertValue;
80 }
81
ConvertTxtSymbolType(SymbolType symbolType)82 Rosen::SymbolType ConvertTxtSymbolType(SymbolType symbolType)
83 {
84 Rosen::SymbolType txtSymbolType;
85 switch (symbolType) {
86 case SymbolType::SYSTEM:
87 txtSymbolType = Rosen::SymbolType::SYSTEM;
88 break;
89 case SymbolType::CUSTOM:
90 txtSymbolType = Rosen::SymbolType::CUSTOM;
91 break;
92 default:
93 LOGE("SymbolType setting error! Now using default SymbolType.");
94 txtSymbolType = Rosen::SymbolType::SYSTEM;
95 break;
96 }
97 return txtSymbolType;
98 }
99
ConvertTxtFontStyle(FontStyle fontStyle)100 Rosen::FontStyle ConvertTxtFontStyle(FontStyle fontStyle)
101 {
102 Rosen::FontStyle convertValue;
103 switch (fontStyle) {
104 case FontStyle::NORMAL:
105 convertValue = Rosen::FontStyle::NORMAL;
106 break;
107 case FontStyle::ITALIC:
108 convertValue = Rosen::FontStyle::ITALIC;
109 break;
110 default:
111 TAG_LOGW(AceLogTag::ACE_FONT, "FontStyle setting error! Now using default FontStyle");
112 convertValue = Rosen::FontStyle::NORMAL;
113 break;
114 }
115 return convertValue;
116 }
117
ConvertTxtTextBaseline(TextBaseline textBaseline)118 Rosen::TextBaseline ConvertTxtTextBaseline(TextBaseline textBaseline)
119 {
120 Rosen::TextBaseline convertValue;
121 switch (textBaseline) {
122 case TextBaseline::ALPHABETIC:
123 convertValue = Rosen::TextBaseline::ALPHABETIC;
124 break;
125 case TextBaseline::IDEOGRAPHIC:
126 convertValue = Rosen::TextBaseline::IDEOGRAPHIC;
127 break;
128 default:
129 convertValue = Rosen::TextBaseline::ALPHABETIC;
130 break;
131 }
132 return convertValue;
133 }
134
ConvertTxtTextAlign(TextAlign textAlign)135 Rosen::TextAlign ConvertTxtTextAlign(TextAlign textAlign)
136 {
137 Rosen::TextAlign convertValue;
138 switch (textAlign) {
139 case TextAlign::LEFT:
140 convertValue = Rosen::TextAlign::LEFT;
141 break;
142 case TextAlign::RIGHT:
143 convertValue = Rosen::TextAlign::RIGHT;
144 break;
145 case TextAlign::CENTER:
146 convertValue = Rosen::TextAlign::CENTER;
147 break;
148 case TextAlign::JUSTIFY:
149 convertValue = Rosen::TextAlign::JUSTIFY;
150 break;
151 case TextAlign::START:
152 convertValue = Rosen::TextAlign::START;
153 break;
154 case TextAlign::END:
155 convertValue = Rosen::TextAlign::END;
156 break;
157 default:
158 TAG_LOGW(AceLogTag::ACE_FONT, "TextAlign setting error! Now using default TextAlign");
159 convertValue = Rosen::TextAlign::START;
160 break;
161 }
162 return convertValue;
163 }
164
ConvertTxtRectHeightStyle(RectHeightStyle heightStyle)165 Rosen::TextRectHeightStyle ConvertTxtRectHeightStyle(RectHeightStyle heightStyle)
166 {
167 switch (heightStyle) {
168 case RectHeightStyle::TIGHT:
169 return Rosen::TextRectHeightStyle::TIGHT;
170 case RectHeightStyle::MAX:
171 return Rosen::TextRectHeightStyle::COVER_TOP_AND_BOTTOM;
172 case RectHeightStyle::INCLUDE_LINE_SPACE_MIDDLE:
173 return Rosen::TextRectHeightStyle::COVER_HALF_TOP_AND_BOTTOM;
174 case RectHeightStyle::INCLUDE_LINE_SPACE_TOP:
175 return Rosen::TextRectHeightStyle::COVER_TOP;
176 case RectHeightStyle::INCLUDE_LINE_SPACE_BOTTOM:
177 return Rosen::TextRectHeightStyle::COVER_BOTTOM;
178 case RectHeightStyle::STRUT:
179 return Rosen::TextRectHeightStyle::FOLLOW_BY_STRUT;
180 default:
181 return Rosen::TextRectHeightStyle::TIGHT;
182 }
183 }
184
ConvertTxtRectWidthStyle(RectWidthStyle widthStyle)185 Rosen::TextRectWidthStyle ConvertTxtRectWidthStyle(RectWidthStyle widthStyle)
186 {
187 switch (widthStyle) {
188 case RectWidthStyle::TIGHT:
189 return Rosen::TextRectWidthStyle::TIGHT;
190 case RectWidthStyle::MAX:
191 return Rosen::TextRectWidthStyle::MAX;
192 default:
193 return Rosen::TextRectWidthStyle::TIGHT;
194 }
195 }
196
ConvertTxtTextDirection(TextDirection textDirection)197 Rosen::TextDirection ConvertTxtTextDirection(TextDirection textDirection)
198 {
199 Rosen::TextDirection convertValue;
200 switch (textDirection) {
201 case TextDirection::RTL:
202 convertValue = Rosen::TextDirection::RTL;
203 break;
204 case TextDirection::LTR:
205 convertValue = Rosen::TextDirection::LTR;
206 break;
207 default:
208 TAG_LOGW(AceLogTag::ACE_FONT, "TextDirection setting error! Now using default TextDirection");
209 convertValue = Rosen::TextDirection::LTR;
210 break;
211 }
212 return convertValue;
213 }
214
ConvertSkColor(Color color)215 SkColor ConvertSkColor(Color color)
216 {
217 return color.GetValue();
218 }
219
ConvertTxtTextDecoration(TextDecoration textDecoration)220 Rosen::TextDecoration ConvertTxtTextDecoration(TextDecoration textDecoration)
221 {
222 Rosen::TextDecoration convertValue = Rosen::TextDecoration::NONE;
223 switch (textDecoration) {
224 case TextDecoration::NONE:
225 convertValue = Rosen::TextDecoration::NONE;
226 break;
227 case TextDecoration::UNDERLINE:
228 convertValue = Rosen::TextDecoration::UNDERLINE;
229 break;
230 case TextDecoration::OVERLINE:
231 convertValue = Rosen::TextDecoration::OVERLINE;
232 break;
233 case TextDecoration::LINE_THROUGH:
234 convertValue = Rosen::TextDecoration::LINE_THROUGH;
235 break;
236 default:
237 TAG_LOGW(AceLogTag::ACE_FONT, "TextDecoration setting error! Now using default TextDecoration");
238 break;
239 }
240 return convertValue;
241 }
ConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)242 Rosen::TextDecorationStyle ConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)
243 {
244 Rosen::TextDecorationStyle convertValue = Rosen::TextDecorationStyle::SOLID;
245 switch (textDecorationStyle) {
246 case TextDecorationStyle::SOLID:
247 convertValue = Rosen::TextDecorationStyle::SOLID;
248 break;
249 case TextDecorationStyle::DOUBLE:
250 convertValue = Rosen::TextDecorationStyle::DOUBLE;
251 break;
252 case TextDecorationStyle::DOTTED:
253 convertValue = Rosen::TextDecorationStyle::DOTTED;
254 break;
255 case TextDecorationStyle::DASHED:
256 convertValue = Rosen::TextDecorationStyle::DASHED;
257 break;
258 case TextDecorationStyle::WAVY:
259 convertValue = Rosen::TextDecorationStyle::WAVY;
260 break;
261 default:
262 TAG_LOGW(AceLogTag::ACE_FONT, "TextDecorationStyle setting error! Now using default TextDecorationStyle");
263 break;
264 }
265 return convertValue;
266 }
267
NormalizeToPx(const Dimension & dimension)268 double NormalizeToPx(const Dimension& dimension)
269 {
270 if ((dimension.Unit() == DimensionUnit::VP) || (dimension.Unit() == DimensionUnit::FP)) {
271 return (dimension.Value() * SystemProperties::GetResolution());
272 }
273 return dimension.Value();
274 }
275
ConvertTxtStyle(const TextStyle & textStyle,Rosen::TextStyle & txtStyle)276 void ConvertTxtStyle(const TextStyle& textStyle, Rosen::TextStyle& txtStyle)
277 {
278 txtStyle.color = ConvertSkColor(textStyle.GetTextColor());
279 txtStyle.fontWeight = ConvertTxtFontWeight(textStyle.GetFontWeight());
280
281 txtStyle.fontSize = NormalizeToPx(textStyle.GetFontSize());
282
283 txtStyle.fontStyle = ConvertTxtFontStyle(textStyle.GetFontStyle());
284
285 if (textStyle.GetWordSpacing().Unit() == DimensionUnit::PERCENT) {
286 txtStyle.wordSpacing = textStyle.GetWordSpacing().Value() * txtStyle.fontSize;
287 } else {
288 txtStyle.wordSpacing = NormalizeToPx(textStyle.GetWordSpacing());
289 }
290
291 txtStyle.letterSpacing = NormalizeToPx(textStyle.GetLetterSpacing());
292 txtStyle.baseLineShift = -NormalizeToPx(textStyle.GetBaselineOffset());
293 txtStyle.fontFamilies = textStyle.GetFontFamilies();
294 ConvertSymbolTxtStyle(textStyle, txtStyle);
295 txtStyle.baseline = ConvertTxtTextBaseline(textStyle.GetTextBaseline());
296 txtStyle.decoration = ConvertTxtTextDecoration(textStyle.GetTextDecoration());
297 txtStyle.decorationColor = ConvertSkColor(textStyle.GetTextDecorationColor());
298 txtStyle.decorationStyle = ConvertTxtTextDecorationStyle(textStyle.GetTextDecorationStyle());
299 txtStyle.locale = Localization::GetInstance()->GetFontLocale();
300 txtStyle.halfLeading = textStyle.GetHalfLeading();
301
302 for (auto& spanShadow : textStyle.GetTextShadows()) {
303 Rosen::TextShadow txtShadow;
304 txtShadow.color = spanShadow.GetColor().GetValue();
305 txtShadow.offset.SetX(spanShadow.GetOffset().GetX());
306 txtShadow.offset.SetY(spanShadow.GetOffset().GetY());
307 txtShadow.blurRadius = spanShadow.GetBlurRadius();
308 txtStyle.shadows.emplace_back(txtShadow);
309 }
310
311 if (textStyle.GetLineHeight().Unit() == DimensionUnit::PERCENT) {
312 txtStyle.heightOnly = true;
313 txtStyle.heightScale = textStyle.GetLineHeight().Value();
314 } else {
315 double fontSize = txtStyle.fontSize;
316 double lineHeight = textStyle.GetLineHeight().Value();
317
318 lineHeight = NormalizeToPx(textStyle.GetLineHeight());
319
320 txtStyle.heightOnly = textStyle.HasHeightOverride();
321 if (!NearEqual(lineHeight, fontSize) && (lineHeight > 0.0) && (!NearZero(fontSize))) {
322 txtStyle.heightScale = lineHeight / fontSize;
323 } else {
324 txtStyle.heightScale = 1;
325 if (NearZero(lineHeight) || NearEqual(lineHeight, fontSize)) {
326 txtStyle.heightOnly = false;
327 }
328 }
329 }
330
331 // set font variant
332 auto fontFeatures = textStyle.GetFontFeatures();
333 if (!fontFeatures.empty()) {
334 Rosen::FontFeatures features;
335 for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
336 features.SetFeature(iter->first, iter->second);
337 }
338 txtStyle.fontFeatures = features;
339 }
340 auto textBackgroundStyle = textStyle.GetTextBackgroundStyle();
341 CHECK_NULL_VOID(textBackgroundStyle.has_value());
342 txtStyle.styleId = textBackgroundStyle->groupId;
343 if (textBackgroundStyle->backgroundColor.has_value()) {
344 txtStyle.backgroundRect.color = textBackgroundStyle->backgroundColor.value().GetValue();
345 }
346 auto radius = textBackgroundStyle->backgroundRadius;
347 CHECK_NULL_VOID(radius.has_value());
348 auto radiusConverter = [](const std::optional<Dimension>& radius) -> double {
349 CHECK_NULL_RETURN(radius.has_value(), 0);
350 return NormalizeToPx(radius.value());
351 };
352 txtStyle.backgroundRect.leftTopRadius = radiusConverter(radius->radiusTopLeft);
353 txtStyle.backgroundRect.rightTopRadius = radiusConverter(radius->radiusTopRight);
354 txtStyle.backgroundRect.leftBottomRadius = radiusConverter(radius->radiusBottomLeft);
355 txtStyle.backgroundRect.rightBottomRadius = radiusConverter(radius->radiusBottomRight);
356 }
357
ConvertTxtStyle(const TextStyle & textStyle,const WeakPtr<PipelineBase> & context,Rosen::TextStyle & txtStyle)358 void ConvertTxtStyle(const TextStyle& textStyle, const WeakPtr<PipelineBase>& context, Rosen::TextStyle& txtStyle)
359 {
360 txtStyle.color = ConvertSkColor(textStyle.GetTextColor());
361 txtStyle.fontWeight = ConvertTxtFontWeight(textStyle.GetFontWeight());
362 txtStyle.symbol.SetSymbolType(ConvertTxtSymbolType(textStyle.GetSymbolType()));
363 auto fontWeightValue = (static_cast<int32_t>(
364 ConvertTxtFontWeight(textStyle.GetFontWeight())) + 1) * DEFAULT_MULTIPLE;
365 auto pipelineContext = context.Upgrade();
366 if (pipelineContext) {
367 fontWeightValue = fontWeightValue * pipelineContext->GetFontWeightScale();
368 }
369 if (textStyle.GetEnableVariableFontWeight()) {
370 fontWeightValue = textStyle.GetVariableFontWeight();
371 if (LessNotEqual(fontWeightValue, MIN_FONT_WEIGHT) || GreatNotEqual(fontWeightValue, MAX_FONT_WEIGHT)) {
372 fontWeightValue = DEFAULT_FONT_WEIGHT;
373 }
374 }
375 txtStyle.fontVariations.SetAxisValue(FONTWEIGHT, fontWeightValue);
376 // Font size must be px when transferring to Rosen::TextStyle
377 txtStyle.fontSize = textStyle.GetFontSize().ConvertToPxDistribute(
378 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
379 txtStyle.fontStyle = ConvertTxtFontStyle(textStyle.GetFontStyle());
380
381 if (textStyle.GetWordSpacing().Unit() == DimensionUnit::PERCENT) {
382 txtStyle.wordSpacing = textStyle.GetWordSpacing().Value() * txtStyle.fontSize;
383 } else {
384 if (pipelineContext) {
385 txtStyle.wordSpacing = textStyle.GetWordSpacing().ConvertToPxDistribute(
386 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
387 } else {
388 txtStyle.wordSpacing = textStyle.GetWordSpacing().Value();
389 }
390 }
391 if (pipelineContext) {
392 txtStyle.letterSpacing = textStyle.GetLetterSpacing().ConvertToPxDistribute(
393 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
394 txtStyle.baseLineShift = -textStyle.GetBaselineOffset().ConvertToPxDistribute(
395 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
396 }
397
398 txtStyle.fontFamilies = textStyle.GetFontFamilies();
399 ConvertSymbolTxtStyle(textStyle, txtStyle);
400 txtStyle.baseline = ConvertTxtTextBaseline(textStyle.GetTextBaseline());
401 txtStyle.decoration = ConvertTxtTextDecoration(textStyle.GetTextDecoration());
402 txtStyle.decorationColor = ConvertSkColor(textStyle.GetTextDecorationColor());
403 txtStyle.decorationStyle = ConvertTxtTextDecorationStyle(textStyle.GetTextDecorationStyle());
404 txtStyle.locale = Localization::GetInstance()->GetFontLocale();
405 txtStyle.halfLeading = textStyle.GetHalfLeading();
406
407 for (auto& spanShadow : textStyle.GetTextShadows()) {
408 Rosen::TextShadow txtShadow;
409 txtShadow.color = spanShadow.GetColor().GetValue();
410 txtShadow.offset.SetX(spanShadow.GetOffset().GetX());
411 txtShadow.offset.SetY(spanShadow.GetOffset().GetY());
412 txtShadow.blurRadius = spanShadow.GetBlurRadius();
413 txtStyle.shadows.emplace_back(txtShadow);
414 }
415
416 double lineHeightScale = 0.0;
417 double lineSpacingScale = 0.0;
418 bool lineHeightOnly = false;
419 bool lineSpacingOnly = false;
420 if (textStyle.GetLineHeight().Unit() == DimensionUnit::PERCENT) {
421 lineHeightOnly = true;
422 lineHeightScale = textStyle.GetLineHeight().Value();
423 } else {
424 double fontSize = txtStyle.fontSize;
425 double lineHeight = textStyle.GetLineHeight().Value();
426 if (pipelineContext) {
427 lineHeight = textStyle.GetLineHeight().ConvertToPxDistribute(
428 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
429 }
430 lineHeightOnly = textStyle.HasHeightOverride();
431 if (!NearEqual(lineHeight, fontSize) && (lineHeight > 0.0) && (!NearZero(fontSize))) {
432 lineHeightScale = lineHeight / fontSize;
433 } else {
434 lineHeightScale = 1;
435 static const int32_t BEGIN_VERSION = 6;
436 auto isBeginVersion = pipelineContext && pipelineContext->GetMinPlatformVersion() >= BEGIN_VERSION;
437 if (NearZero(lineHeight) || (!isBeginVersion && NearEqual(lineHeight, fontSize))) {
438 lineHeightOnly = false;
439 }
440 }
441 }
442 if (textStyle.GetLineSpacing().Unit() == DimensionUnit::PERCENT) {
443 lineSpacingOnly = true;
444 lineSpacingScale = textStyle.GetLineSpacing().Value();
445 } else {
446 double fontSize = txtStyle.fontSize;
447 double lineSpacing = textStyle.GetLineSpacing().Value();
448 if (pipelineContext) {
449 lineSpacing = textStyle.GetLineSpacing().ConvertToPxDistribute(
450 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
451 }
452 lineSpacingOnly = true;
453 if (!NearEqual(lineSpacing, fontSize) && (lineSpacing > 0.0) && (!NearZero(fontSize))) {
454 lineSpacingScale = lineSpacing / fontSize;
455 } else {
456 lineSpacingScale = 1;
457 if (NearZero(lineSpacing)) {
458 lineSpacingOnly = false;
459 }
460 }
461 }
462
463 txtStyle.heightOnly = lineHeightOnly || lineSpacingOnly;
464 if (lineHeightOnly && lineSpacingOnly) {
465 txtStyle.heightScale = lineHeightScale + lineSpacingScale;
466 } else if (lineHeightOnly && !lineSpacingOnly) {
467 txtStyle.heightScale = lineHeightScale;
468 } else if (!lineHeightOnly && lineSpacingOnly) {
469 txtStyle.heightScale = ORIGINAL_LINE_HEIGHT_SCALE + lineSpacingScale;
470 } else {
471 txtStyle.heightScale = 1;
472 }
473
474 // set font variant
475 auto fontFeatures = textStyle.GetFontFeatures();
476 if (!fontFeatures.empty()) {
477 Rosen::FontFeatures features;
478 for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
479 features.SetFeature(iter->first, iter->second);
480 }
481 txtStyle.fontFeatures = features;
482 }
483 auto textBackgroundStyle = textStyle.GetTextBackgroundStyle();
484 CHECK_NULL_VOID(textBackgroundStyle.has_value());
485 txtStyle.styleId = textBackgroundStyle->groupId;
486 if (textBackgroundStyle->backgroundColor.has_value()) {
487 txtStyle.backgroundRect.color = textBackgroundStyle->backgroundColor.value().GetValue();
488 }
489 auto radius = textBackgroundStyle->backgroundRadius;
490 CHECK_NULL_VOID(radius.has_value());
491 auto radiusConverter = [context = pipelineContext, textStyle](const std::optional<Dimension>& radius) -> double {
492 CHECK_NULL_RETURN(radius.has_value(), 0);
493 CHECK_NULL_RETURN(context, radius->Value());
494 return radius.value().ConvertToPxDistribute(
495 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
496 };
497 txtStyle.backgroundRect.leftTopRadius = radiusConverter(radius->radiusTopLeft);
498 txtStyle.backgroundRect.rightTopRadius = radiusConverter(radius->radiusTopRight);
499 txtStyle.backgroundRect.leftBottomRadius = radiusConverter(radius->radiusBottomLeft);
500 txtStyle.backgroundRect.rightBottomRadius = radiusConverter(radius->radiusBottomRight);
501 }
502
ConvertSymbolTxtStyle(const TextStyle & textStyle,Rosen::TextStyle & txtStyle)503 void ConvertSymbolTxtStyle(const TextStyle& textStyle, Rosen::TextStyle& txtStyle)
504 {
505 if (!textStyle.isSymbolGlyph_) {
506 return;
507 }
508
509 txtStyle.isSymbolGlyph = true;
510 txtStyle.symbol.SetRenderMode(textStyle.GetRenderStrategy());
511 const std::vector<Color>& symbolColor = textStyle.GetSymbolColorList();
512 std::vector<Rosen::Drawing::Color> symbolColors;
513 for (size_t i = 0; i < symbolColor.size(); i++) {
514 symbolColors.emplace_back(ConvertSkColor(symbolColor[i]));
515 }
516 txtStyle.symbol.SetRenderColor(symbolColors);
517 if (textStyle.GetSymbolEffectOptions().has_value()) {
518 auto options = textStyle.GetSymbolEffectOptions().value();
519 auto effectType = options.GetEffectType();
520 txtStyle.symbol.SetSymbolEffect(static_cast<uint32_t>(effectType));
521 txtStyle.symbol.SetAnimationStart(options.GetIsTxtActive());
522 if (options.GetCommonSubType().has_value()) {
523 auto commonType = static_cast<uint16_t>(options.GetCommonSubType().value());
524 txtStyle.symbol.SetCommonSubType(commonType == 1 ? Rosen::Drawing::DrawingCommonSubType::UP
525 : Rosen::Drawing::DrawingCommonSubType::DOWN);
526 }
527 if (effectType == SymbolEffectType::HIERARCHICAL && options.GetFillStyle().has_value()) {
528 txtStyle.symbol.SetAnimationMode(static_cast<uint16_t>(options.GetFillStyle().value()));
529 } else {
530 if (options.GetScopeType().has_value()) {
531 txtStyle.symbol.SetAnimationMode(static_cast<uint16_t>(options.GetScopeType().value()));
532 }
533 }
534 } else {
535 auto effectStrategyValue = textStyle.GetEffectStrategy();
536 if (effectStrategyValue < NONE_EFFECT || effectStrategyValue > SCALE_EFFECT) {
537 effectStrategyValue = NONE_EFFECT;
538 }
539 txtStyle.symbol.SetSymbolEffect(effectStrategyValue);
540 txtStyle.symbol.SetAnimationStart(true);
541 }
542 if (txtStyle.symbol.GetSymbolType() != Rosen::SymbolType::CUSTOM) {
543 txtStyle.fontFamilies.push_back(DEFAULT_SYMBOL_FONTFAMILY);
544 }
545 }
546
ConvertSkRect(const Rosen::Drawing::RectF & skRect)547 Rect ConvertSkRect(const Rosen::Drawing::RectF& skRect)
548 {
549 Rect result;
550 result.SetLeft(skRect.GetLeft());
551 result.SetTop(skRect.GetTop());
552 result.SetWidth(skRect.GetWidth());
553 result.SetHeight(skRect.GetHeight());
554 return result;
555 }
556
ConvertPlaceholderAlignment(PlaceholderAlignment textDecoration)557 Rosen::PlaceholderVerticalAlignment ConvertPlaceholderAlignment(PlaceholderAlignment textDecoration)
558 {
559 Rosen::PlaceholderVerticalAlignment convertValue = Rosen::PlaceholderVerticalAlignment::OFFSET_AT_BASELINE;
560 switch (textDecoration) {
561 case PlaceholderAlignment::BASELINE:
562 convertValue = Rosen::PlaceholderVerticalAlignment::OFFSET_AT_BASELINE;
563 break;
564 case PlaceholderAlignment::ABOVEBASELINE:
565 convertValue = Rosen::PlaceholderVerticalAlignment::ABOVE_BASELINE;
566 break;
567 case PlaceholderAlignment::BELOWBASELINE:
568 convertValue = Rosen::PlaceholderVerticalAlignment::BELOW_BASELINE;
569 break;
570 case PlaceholderAlignment::TOP:
571 convertValue = Rosen::PlaceholderVerticalAlignment::TOP_OF_ROW_BOX;
572 break;
573 case PlaceholderAlignment::BOTTOM:
574 convertValue = Rosen::PlaceholderVerticalAlignment::BOTTOM_OF_ROW_BOX;
575 break;
576 case PlaceholderAlignment::MIDDLE:
577 convertValue = Rosen::PlaceholderVerticalAlignment::CENTER_OF_ROW_BOX;
578 break;
579 default:
580 TAG_LOGW(AceLogTag::ACE_FONT, "PlaceholderAlignment setting error! Now using default PlaceholderAlignment");
581 break;
582 }
583 return convertValue;
584 }
585
ConvertPlaceholderRun(const PlaceholderRun & span,Rosen::PlaceholderSpan & txtSpan)586 void ConvertPlaceholderRun(const PlaceholderRun& span, Rosen::PlaceholderSpan& txtSpan)
587 {
588 txtSpan.width = span.width;
589 txtSpan.height = span.height;
590 txtSpan.alignment = ConvertPlaceholderAlignment(span.alignment);
591 txtSpan.baseline = ConvertTxtTextBaseline(span.baseline);
592 txtSpan.baselineOffset = span.baseline_offset;
593 }
594
GetVariableFontWeight(FontWeight fontWeight)595 float GetVariableFontWeight(FontWeight fontWeight)
596 {
597 return (static_cast<int32_t>(ConvertTxtFontWeight(fontWeight)) + 1) * DEFAULT_MULTIPLE;
598 }
599 } // namespace OHOS::Ace::Constants
600