• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef USE_GRAPHIC_TEXT_GINE
19 #include "txt/font_style.h"
20 #include "txt/font_weight.h"
21 #include "txt/paragraph_style.h"
22 #include "txt/text_decoration.h"
23 #else
24 #include "rosen_text/hm_symbol_txt.h"
25 #include "rosen_text/typography_create.h"
26 #include "rosen_text/typography_style.h"
27 #endif
28 
29 #include "base/i18n/localization.h"
30 #include "core/components/common/properties/color.h"
31 #include "core/components/common/properties/text_style.h"
32 
33 namespace OHOS::Ace::Constants {
34 
35 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtFontWeight(FontWeight fontWeight)36 txt::FontWeight ConvertTxtFontWeight(FontWeight fontWeight)
37 {
38     txt::FontWeight convertValue;
39     switch (fontWeight) {
40         case FontWeight::W100:
41         case FontWeight::LIGHTER:
42             convertValue = txt::FontWeight::w100;
43             break;
44         case FontWeight::W200:
45             convertValue = txt::FontWeight::w200;
46             break;
47         case FontWeight::W300:
48             convertValue = txt::FontWeight::w300;
49             break;
50         case FontWeight::W400:
51         case FontWeight::NORMAL:
52         case FontWeight::REGULAR:
53             convertValue = txt::FontWeight::w400;
54             break;
55         case FontWeight::W500:
56         case FontWeight::MEDIUM:
57             convertValue = txt::FontWeight::w500;
58             break;
59         case FontWeight::W600:
60             convertValue = txt::FontWeight::w600;
61             break;
62         case FontWeight::W700:
63         case FontWeight::BOLD:
64             convertValue = txt::FontWeight::w700;
65             break;
66         case FontWeight::W800:
67             convertValue = txt::FontWeight::w800;
68             break;
69         case FontWeight::W900:
70         case FontWeight::BOLDER:
71             convertValue = txt::FontWeight::w900;
72             break;
73         default:
74             LOGW("FontWeight setting error! Now using default FontWeight.");
75             convertValue = txt::FontWeight::w400;
76             break;
77     }
78     return convertValue;
79 }
80 #else
81 Rosen::FontWeight ConvertTxtFontWeight(FontWeight fontWeight)
82 {
83     Rosen::FontWeight convertValue;
84     switch (fontWeight) {
85         case FontWeight::W100:
86         case FontWeight::LIGHTER:
87             convertValue = Rosen::FontWeight::W100;
88             break;
89         case FontWeight::W200:
90             convertValue = Rosen::FontWeight::W200;
91             break;
92         case FontWeight::W300:
93             convertValue = Rosen::FontWeight::W300;
94             break;
95         case FontWeight::W400:
96         case FontWeight::NORMAL:
97         case FontWeight::REGULAR:
98             convertValue = Rosen::FontWeight::W400;
99             break;
100         case FontWeight::W500:
101         case FontWeight::MEDIUM:
102             convertValue = Rosen::FontWeight::W500;
103             break;
104         case FontWeight::W600:
105             convertValue = Rosen::FontWeight::W600;
106             break;
107         case FontWeight::W700:
108         case FontWeight::BOLD:
109             convertValue = Rosen::FontWeight::W700;
110             break;
111         case FontWeight::W800:
112             convertValue = Rosen::FontWeight::W800;
113             break;
114         case FontWeight::W900:
115         case FontWeight::BOLDER:
116             convertValue = Rosen::FontWeight::W900;
117             break;
118         default:
119             LOGW("FontWeight setting error! Now using default FontWeight.");
120             convertValue = Rosen::FontWeight::W400;
121             break;
122     }
123     return convertValue;
124 }
125 #endif
126 
127 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtFontStyle(FontStyle fontStyle)128 txt::FontStyle ConvertTxtFontStyle(FontStyle fontStyle)
129 {
130     txt::FontStyle convertValue;
131     switch (fontStyle) {
132         case FontStyle::NORMAL:
133             convertValue = txt::FontStyle::normal;
134             break;
135         case FontStyle::ITALIC:
136             convertValue = txt::FontStyle::italic;
137             break;
138         default:
139             LOGW("FontStyle setting error! Now using default FontStyle");
140             convertValue = txt::FontStyle::normal;
141             break;
142     }
143     return convertValue;
144 }
145 #else
ConvertTxtFontStyle(FontStyle fontStyle)146 Rosen::FontStyle ConvertTxtFontStyle(FontStyle fontStyle)
147 {
148     Rosen::FontStyle convertValue;
149     switch (fontStyle) {
150         case FontStyle::NORMAL:
151             convertValue = Rosen::FontStyle::NORMAL;
152             break;
153         case FontStyle::ITALIC:
154             convertValue = Rosen::FontStyle::ITALIC;
155             break;
156         default:
157             LOGW("FontStyle setting error! Now using default FontStyle");
158             convertValue = Rosen::FontStyle::NORMAL;
159             break;
160     }
161     return convertValue;
162 }
163 #endif
164 
165 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtTextBaseline(TextBaseline textBaseline)166 txt::TextBaseline ConvertTxtTextBaseline(TextBaseline textBaseline)
167 {
168     txt::TextBaseline convertValue;
169     switch (textBaseline) {
170         case TextBaseline::ALPHABETIC:
171             convertValue = txt::TextBaseline::kAlphabetic;
172             break;
173         case TextBaseline::IDEOGRAPHIC:
174             convertValue = txt::TextBaseline::kIdeographic;
175             break;
176         default:
177             convertValue = txt::TextBaseline::kAlphabetic;
178             break;
179     }
180     return convertValue;
181 }
182 #else
ConvertTxtTextBaseline(TextBaseline textBaseline)183 Rosen::TextBaseline ConvertTxtTextBaseline(TextBaseline textBaseline)
184 {
185     Rosen::TextBaseline convertValue;
186     switch (textBaseline) {
187         case TextBaseline::ALPHABETIC:
188             convertValue = Rosen::TextBaseline::ALPHABETIC;
189             break;
190         case TextBaseline::IDEOGRAPHIC:
191             convertValue = Rosen::TextBaseline::IDEOGRAPHIC;
192             break;
193         default:
194             convertValue = Rosen::TextBaseline::ALPHABETIC;
195             break;
196     }
197     return convertValue;
198 }
199 #endif
200 
201 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtTextAlign(TextAlign textAlign)202 txt::TextAlign ConvertTxtTextAlign(TextAlign textAlign)
203 {
204     txt::TextAlign convertValue;
205     switch (textAlign) {
206         case TextAlign::LEFT:
207             convertValue = txt::TextAlign::left;
208             break;
209         case TextAlign::RIGHT:
210             convertValue = txt::TextAlign::right;
211             break;
212         case TextAlign::CENTER:
213             convertValue = txt::TextAlign::center;
214             break;
215         case TextAlign::JUSTIFY:
216             convertValue = txt::TextAlign::justify;
217             break;
218         case TextAlign::START:
219             convertValue = txt::TextAlign::start;
220             break;
221         case TextAlign::END:
222             convertValue = txt::TextAlign::end;
223             break;
224         default:
225             LOGW("TextAlign setting error! Now using default TextAlign");
226             convertValue = txt::TextAlign::start;
227             break;
228     }
229     return convertValue;
230 }
231 #else
ConvertTxtTextAlign(TextAlign textAlign)232 Rosen::TextAlign ConvertTxtTextAlign(TextAlign textAlign)
233 {
234     Rosen::TextAlign convertValue;
235     switch (textAlign) {
236         case TextAlign::LEFT:
237             convertValue = Rosen::TextAlign::LEFT;
238             break;
239         case TextAlign::RIGHT:
240             convertValue = Rosen::TextAlign::RIGHT;
241             break;
242         case TextAlign::CENTER:
243             convertValue = Rosen::TextAlign::CENTER;
244             break;
245         case TextAlign::JUSTIFY:
246             convertValue = Rosen::TextAlign::JUSTIFY;
247             break;
248         case TextAlign::START:
249             convertValue = Rosen::TextAlign::START;
250             break;
251         case TextAlign::END:
252             convertValue = Rosen::TextAlign::END;
253             break;
254         default:
255             LOGW("TextAlign setting error! Now using default TextAlign");
256             convertValue = Rosen::TextAlign::START;
257             break;
258     }
259     return convertValue;
260 }
261 #endif
262 
263 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtTextDirection(TextDirection textDirection)264 txt::TextDirection ConvertTxtTextDirection(TextDirection textDirection)
265 #else
266 Rosen::TextDirection ConvertTxtTextDirection(TextDirection textDirection)
267 #endif
268 {
269 #ifndef USE_GRAPHIC_TEXT_GINE
270     txt::TextDirection convertValue;
271 #else
272     Rosen::TextDirection convertValue;
273 #endif
274     switch (textDirection) {
275         case TextDirection::RTL:
276 #ifndef USE_GRAPHIC_TEXT_GINE
277             convertValue = txt::TextDirection::rtl;
278 #else
279             convertValue = Rosen::TextDirection::RTL;
280 #endif
281             break;
282         case TextDirection::LTR:
283 #ifndef USE_GRAPHIC_TEXT_GINE
284             convertValue = txt::TextDirection::ltr;
285 #else
286             convertValue = Rosen::TextDirection::LTR;
287 #endif
288             break;
289         default:
290             LOGW("TextDirection setting error! Now using default TextDirection");
291 #ifndef USE_GRAPHIC_TEXT_GINE
292             convertValue = txt::TextDirection::ltr;
293 #else
294             convertValue = Rosen::TextDirection::LTR;
295 #endif
296             break;
297     }
298     return convertValue;
299 }
300 
ConvertSkColor(Color color)301 SkColor ConvertSkColor(Color color)
302 {
303     return color.GetValue();
304 }
305 
306 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtTextDecoration(TextDecoration textDecoration)307 txt::TextDecoration ConvertTxtTextDecoration(TextDecoration textDecoration)
308 {
309     txt::TextDecoration convertValue = txt::TextDecoration::kNone;
310     switch (textDecoration) {
311         case TextDecoration::NONE:
312             convertValue = txt::TextDecoration::kNone;
313             break;
314         case TextDecoration::UNDERLINE:
315             convertValue = txt::TextDecoration::kUnderline;
316             break;
317         case TextDecoration::OVERLINE:
318             convertValue = txt::TextDecoration::kOverline;
319             break;
320         case TextDecoration::LINE_THROUGH:
321             convertValue = txt::TextDecoration::kLineThrough;
322             break;
323         default:
324             LOGW("TextDecoration setting error! Now using default TextDecoration");
325             break;
326     }
327     return convertValue;
328 }
329 #else
ConvertTxtTextDecoration(TextDecoration textDecoration)330 Rosen::TextDecoration ConvertTxtTextDecoration(TextDecoration textDecoration)
331 {
332     Rosen::TextDecoration convertValue = Rosen::TextDecoration::NONE;
333     switch (textDecoration) {
334         case TextDecoration::NONE:
335             convertValue = Rosen::TextDecoration::NONE;
336             break;
337         case TextDecoration::UNDERLINE:
338             convertValue = Rosen::TextDecoration::UNDERLINE;
339             break;
340         case TextDecoration::OVERLINE:
341             convertValue = Rosen::TextDecoration::OVERLINE;
342             break;
343         case TextDecoration::LINE_THROUGH:
344             convertValue = Rosen::TextDecoration::LINE_THROUGH;
345             break;
346         default:
347             LOGW("TextDecoration setting error! Now using default TextDecoration");
348             break;
349     }
350     return convertValue;
351 }
352 #endif
353 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)354 txt::TextDecorationStyle ConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)
355 {
356     txt::TextDecorationStyle convertValue = txt::TextDecorationStyle::kSolid;
357     switch (textDecorationStyle) {
358         case TextDecorationStyle::SOLID:
359             convertValue = txt::TextDecorationStyle::kSolid;
360             break;
361         case TextDecorationStyle::DOUBLE:
362             convertValue = txt::TextDecorationStyle::kDouble;
363             break;
364         case TextDecorationStyle::DOTTED:
365             convertValue = txt::TextDecorationStyle::kDotted;
366             break;
367         case TextDecorationStyle::DASHED:
368             convertValue = txt::TextDecorationStyle::kDashed;
369             break;
370         case TextDecorationStyle::WAVY:
371             convertValue = txt::TextDecorationStyle::kWavy;
372             break;
373         default:
374             LOGW("TextDecorationStyle setting error! Now using default TextDecorationStyle");
375             break;
376     }
377     return convertValue;
378 }
379 #else
ConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)380 Rosen::TextDecorationStyle ConvertTxtTextDecorationStyle(TextDecorationStyle textDecorationStyle)
381 {
382     Rosen::TextDecorationStyle convertValue = Rosen::TextDecorationStyle::SOLID;
383     switch (textDecorationStyle) {
384         case TextDecorationStyle::SOLID:
385             convertValue = Rosen::TextDecorationStyle::SOLID;
386             break;
387         case TextDecorationStyle::DOUBLE:
388             convertValue = Rosen::TextDecorationStyle::DOUBLE;
389             break;
390         case TextDecorationStyle::DOTTED:
391             convertValue = Rosen::TextDecorationStyle::DOTTED;
392             break;
393         case TextDecorationStyle::DASHED:
394             convertValue = Rosen::TextDecorationStyle::DASHED;
395             break;
396         case TextDecorationStyle::WAVY:
397             convertValue = Rosen::TextDecorationStyle::WAVY;
398             break;
399         default:
400             LOGW("TextDecorationStyle setting error! Now using default TextDecorationStyle");
401             break;
402     }
403     return convertValue;
404 }
405 #endif
406 
407 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertTxtStyle(const TextStyle & textStyle,const WeakPtr<PipelineBase> & context,txt::TextStyle & txtStyle)408 void ConvertTxtStyle(const TextStyle& textStyle, const WeakPtr<PipelineBase>& context, txt::TextStyle& txtStyle)
409 {
410     txtStyle.color = ConvertSkColor(textStyle.GetTextColor());
411     txtStyle.font_weight = ConvertTxtFontWeight(textStyle.GetFontWeight());
412     // Font size must be px when transferring to txt::TextStyle
413     auto pipelineContext = context.Upgrade();
414     if (pipelineContext) {
415         txtStyle.font_size = pipelineContext->NormalizeToPx(textStyle.GetFontSize());
416         if (textStyle.IsAllowScale() || textStyle.GetFontSize().Unit() == DimensionUnit::FP) {
417             txtStyle.font_size =
418                 pipelineContext->NormalizeToPx(textStyle.GetFontSize() * pipelineContext->GetFontScale());
419         }
420     } else {
421         txtStyle.font_size = textStyle.GetFontSize().Value();
422     }
423     txtStyle.font_style = ConvertTxtFontStyle(textStyle.GetFontStyle());
424 
425     if (textStyle.GetWordSpacing().Unit() == DimensionUnit::PERCENT) {
426         txtStyle.word_spacing = textStyle.GetWordSpacing().Value() * txtStyle.font_size;
427     } else {
428         if (pipelineContext) {
429             txtStyle.word_spacing = pipelineContext->NormalizeToPx(textStyle.GetWordSpacing());
430         } else {
431             txtStyle.word_spacing = textStyle.GetWordSpacing().Value();
432         }
433     }
434     if (pipelineContext) {
435         txtStyle.letter_spacing = pipelineContext->NormalizeToPx(textStyle.GetLetterSpacing());
436     }
437     txtStyle.text_baseline = ConvertTxtTextBaseline(textStyle.GetTextBaseline());
438     txtStyle.decoration = ConvertTxtTextDecoration(textStyle.GetTextDecoration());
439     txtStyle.decoration_style = ConvertTxtTextDecorationStyle(textStyle.GetTextDecorationStyle());
440     txtStyle.decoration_color = ConvertSkColor(textStyle.GetTextDecorationColor());
441     txtStyle.font_families = textStyle.GetFontFamilies();
442     txtStyle.locale = Localization::GetInstance()->GetFontLocale();
443     txtStyle.half_leading = textStyle.GetHalfLeading();
444 
445     for (auto& spanShadow : textStyle.GetTextShadows()) {
446         txt::TextShadow txtShadow;
447         txtShadow.color = spanShadow.GetColor().GetValue();
448 #ifndef USE_ROSEN_DRAWING
449         txtShadow.offset.fX = static_cast<SkScalar>(spanShadow.GetOffset().GetX());
450         txtShadow.offset.fY = static_cast<SkScalar>(spanShadow.GetOffset().GetY());
451 #else
452         txtShadow.offset.SetX(static_cast<SkScalar>(spanShadow.GetOffset().GetX()));
453         txtShadow.offset.SetY(static_cast<SkScalar>(spanShadow.GetOffset().GetY()));
454 #endif
455         txtShadow.blur_sigma = spanShadow.GetBlurRadius();
456 
457         txtStyle.text_shadows.emplace_back(txtShadow);
458     }
459 
460     if (textStyle.GetLineHeight().Unit() == DimensionUnit::PERCENT) {
461         txtStyle.has_height_override = true;
462         txtStyle.height = textStyle.GetLineHeight().Value();
463     } else {
464         double fontSize = txtStyle.font_size;
465         double lineHeight = textStyle.GetLineHeight().Value();
466         if (pipelineContext) {
467             lineHeight = pipelineContext->NormalizeToPx(textStyle.GetLineHeight());
468         }
469         txtStyle.has_height_override = textStyle.HasHeightOverride();
470         if (!NearEqual(lineHeight, fontSize) && (lineHeight > 0.0) && (!NearZero(fontSize))) {
471             txtStyle.height = lineHeight / fontSize;
472         } else {
473             txtStyle.height = 1;
474             static const int32_t BEGIN_VERSION = 6;
475             auto isBeginVersion = pipelineContext && pipelineContext->GetMinPlatformVersion() >= BEGIN_VERSION;
476             if (NearZero(lineHeight) || (!isBeginVersion && NearEqual(lineHeight, fontSize))) {
477                 txtStyle.has_height_override = false;
478             }
479         }
480     }
481 
482     // set font variant
483     auto fontFeatures = textStyle.GetFontFeatures();
484     if (!fontFeatures.empty()) {
485         txt::FontFeatures features;
486         for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
487             features.SetFeature(iter->first, iter->second);
488         }
489         txtStyle.font_features = features;
490     }
491 }
492 
ConvertTxtStyle(const TextStyle & textStyle,txt::TextStyle & txtStyle)493 void ConvertTxtStyle(const TextStyle& textStyle, txt::TextStyle& txtStyle) {}
494 #else
NormalizeToPx(const Dimension & dimension)495 double NormalizeToPx(const Dimension& dimension)
496 {
497     if ((dimension.Unit() == DimensionUnit::VP) || (dimension.Unit() == DimensionUnit::FP)) {
498         return (dimension.Value() * SystemProperties::GetResolution());
499     }
500     return dimension.Value();
501 }
502 
ConvertTxtStyle(const TextStyle & textStyle,Rosen::TextStyle & txtStyle)503 void ConvertTxtStyle(const TextStyle& textStyle, Rosen::TextStyle& txtStyle)
504 {
505     txtStyle.color = ConvertSkColor(textStyle.GetTextColor());
506     txtStyle.fontWeight = ConvertTxtFontWeight(textStyle.GetFontWeight());
507 
508     txtStyle.fontSize = NormalizeToPx(textStyle.GetFontSize());
509 
510     txtStyle.fontStyle = ConvertTxtFontStyle(textStyle.GetFontStyle());
511 
512     if (textStyle.GetWordSpacing().Unit() == DimensionUnit::PERCENT) {
513         txtStyle.wordSpacing = textStyle.GetWordSpacing().Value() * txtStyle.fontSize;
514     } else {
515         txtStyle.wordSpacing = NormalizeToPx(textStyle.GetWordSpacing());
516     }
517 
518     txtStyle.letterSpacing = NormalizeToPx(textStyle.GetLetterSpacing());
519 
520     if (textStyle.isSymbolGlyph_) {
521         txtStyle.isSymbolGlyph = true;
522         const std::vector<Color>& symbolColor = textStyle.GetSymbolColorList();
523         std::vector<Rosen::Drawing::Color> symbolColors;
524         for (size_t i = 0; i < symbolColor.size(); i++) {
525             symbolColors.emplace_back(ConvertSkColor(symbolColor[i]));
526         }
527         txtStyle.symbol.SetRenderColor(symbolColors);
528         txtStyle.symbol.SetRenderMode(textStyle.GetRenderStrategy());
529         txtStyle.symbol.SetSymbolEffect(textStyle.GetEffectStrategy());
530     }
531     txtStyle.baseline = ConvertTxtTextBaseline(textStyle.GetTextBaseline());
532     txtStyle.decoration = ConvertTxtTextDecoration(textStyle.GetTextDecoration());
533     txtStyle.decorationColor = ConvertSkColor(textStyle.GetTextDecorationColor());
534     txtStyle.fontFamilies = textStyle.GetFontFamilies();
535     txtStyle.locale = Localization::GetInstance()->GetFontLocale();
536     txtStyle.halfLeading = textStyle.GetHalfLeading();
537 
538     for (auto& spanShadow : textStyle.GetTextShadows()) {
539         Rosen::TextShadow txtShadow;
540         txtShadow.color = spanShadow.GetColor().GetValue();
541         txtShadow.offset.SetX(spanShadow.GetOffset().GetX());
542         txtShadow.offset.SetY(spanShadow.GetOffset().GetY());
543         txtShadow.blurRadius = spanShadow.GetBlurRadius();
544         txtStyle.shadows.emplace_back(txtShadow);
545     }
546 
547     if (textStyle.GetLineHeight().Unit() == DimensionUnit::PERCENT) {
548         txtStyle.heightOnly = true;
549         txtStyle.heightScale = textStyle.GetLineHeight().Value();
550     } else {
551         double fontSize = txtStyle.fontSize;
552         double lineHeight = textStyle.GetLineHeight().Value();
553 
554         lineHeight = NormalizeToPx(textStyle.GetLineHeight());
555 
556         txtStyle.heightOnly = textStyle.HasHeightOverride();
557         if (!NearEqual(lineHeight, fontSize) && (lineHeight > 0.0) && (!NearZero(fontSize))) {
558             txtStyle.heightScale = lineHeight / fontSize;
559         } else {
560             txtStyle.heightScale = 1;
561             if (NearZero(lineHeight) || NearEqual(lineHeight, fontSize)) {
562                 txtStyle.heightOnly = false;
563             }
564         }
565     }
566 
567     // set font variant
568     auto fontFeatures = textStyle.GetFontFeatures();
569     if (!fontFeatures.empty()) {
570         Rosen::FontFeatures features;
571         for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
572             features.SetFeature(iter->first, iter->second);
573         }
574         txtStyle.fontFeatures = features;
575     }
576     auto textBackgroundStyle = textStyle.GetTextBackgroundStyle();
577     CHECK_NULL_VOID(textBackgroundStyle.has_value());
578     txtStyle.styleId = textBackgroundStyle->groupId;
579     if (textBackgroundStyle->backgroundColor.has_value()) {
580         txtStyle.backgroundRect.color = textBackgroundStyle->backgroundColor.value().GetValue();
581     }
582     auto radius = textBackgroundStyle->backgroundRadius;
583     CHECK_NULL_VOID(radius.has_value());
584     auto radiusConverter = [](const std::optional<Dimension>& radius) -> double {
585         CHECK_NULL_RETURN(radius.has_value(), 0);
586         return NormalizeToPx(radius.value());
587     };
588     txtStyle.backgroundRect.leftTopRadius = radiusConverter(radius->radiusTopLeft);
589     txtStyle.backgroundRect.rightTopRadius = radiusConverter(radius->radiusTopRight);
590     txtStyle.backgroundRect.leftBottomRadius = radiusConverter(radius->radiusBottomLeft);
591     txtStyle.backgroundRect.rightBottomRadius = radiusConverter(radius->radiusBottomRight);
592 }
593 
ConvertTxtStyle(const TextStyle & textStyle,const WeakPtr<PipelineBase> & context,Rosen::TextStyle & txtStyle)594 void ConvertTxtStyle(const TextStyle& textStyle, const WeakPtr<PipelineBase>& context, Rosen::TextStyle& txtStyle)
595 {
596     txtStyle.color = ConvertSkColor(textStyle.GetTextColor());
597     txtStyle.fontWeight = ConvertTxtFontWeight(textStyle.GetFontWeight());
598     // Font size must be px when transferring to Rosen::TextStyle
599     auto pipelineContext = context.Upgrade();
600     if (pipelineContext) {
601         txtStyle.fontSize = pipelineContext->NormalizeToPx(textStyle.GetFontSize());
602         if (textStyle.IsAllowScale() || textStyle.GetFontSize().Unit() == DimensionUnit::FP) {
603             txtStyle.fontSize =
604                 pipelineContext->NormalizeToPx(textStyle.GetFontSize() * pipelineContext->GetFontScale());
605         }
606     } else {
607         txtStyle.fontSize = textStyle.GetFontSize().Value();
608     }
609     txtStyle.fontStyle = ConvertTxtFontStyle(textStyle.GetFontStyle());
610 
611     if (textStyle.GetWordSpacing().Unit() == DimensionUnit::PERCENT) {
612         txtStyle.wordSpacing = textStyle.GetWordSpacing().Value() * txtStyle.fontSize;
613     } else {
614         if (pipelineContext) {
615             txtStyle.wordSpacing = pipelineContext->NormalizeToPx(textStyle.GetWordSpacing());
616         } else {
617             txtStyle.wordSpacing = textStyle.GetWordSpacing().Value();
618         }
619     }
620     if (pipelineContext) {
621         txtStyle.letterSpacing = pipelineContext->NormalizeToPx(textStyle.GetLetterSpacing());
622     }
623 
624     if (textStyle.isSymbolGlyph_) {
625         txtStyle.isSymbolGlyph = true;
626         const std::vector<Color>& symbolColor = textStyle.GetSymbolColorList();
627         std::vector<Rosen::Drawing::Color> symbolColors;
628         for (int i = 0; i < symbolColor.size(); i++) {
629             symbolColors.emplace_back(ConvertSkColor(symbolColor[i]));
630         }
631         txtStyle.symbol.SetRenderColor(symbolColors);
632         txtStyle.symbol.SetRenderMode(textStyle.GetRenderStrategy());
633         txtStyle.symbol.SetSymbolEffect(textStyle.GetEffectStrategy());
634     }
635     txtStyle.baseline = ConvertTxtTextBaseline(textStyle.GetTextBaseline());
636     txtStyle.decoration = ConvertTxtTextDecoration(textStyle.GetTextDecoration());
637     txtStyle.decorationColor = ConvertSkColor(textStyle.GetTextDecorationColor());
638     txtStyle.fontFamilies = textStyle.GetFontFamilies();
639     txtStyle.locale = Localization::GetInstance()->GetFontLocale();
640     txtStyle.halfLeading = textStyle.GetHalfLeading();
641 
642     for (auto& spanShadow : textStyle.GetTextShadows()) {
643         Rosen::TextShadow txtShadow;
644         txtShadow.color = spanShadow.GetColor().GetValue();
645         txtShadow.offset.SetX(spanShadow.GetOffset().GetX());
646         txtShadow.offset.SetY(spanShadow.GetOffset().GetY());
647         txtShadow.blurRadius = spanShadow.GetBlurRadius();
648         txtStyle.shadows.emplace_back(txtShadow);
649     }
650 
651     if (textStyle.GetLineHeight().Unit() == DimensionUnit::PERCENT) {
652         txtStyle.heightOnly = true;
653         txtStyle.heightScale = textStyle.GetLineHeight().Value();
654     } else {
655         double fontSize = txtStyle.fontSize;
656         double lineHeight = textStyle.GetLineHeight().Value();
657         if (pipelineContext) {
658             lineHeight = pipelineContext->NormalizeToPx(textStyle.GetLineHeight());
659         }
660         txtStyle.heightOnly = textStyle.HasHeightOverride();
661         if (!NearEqual(lineHeight, fontSize) && (lineHeight > 0.0) && (!NearZero(fontSize))) {
662             txtStyle.heightScale = lineHeight / fontSize;
663         } else {
664             txtStyle.heightScale = 1;
665             static const int32_t BEGIN_VERSION = 6;
666             auto isBeginVersion = pipelineContext && pipelineContext->GetMinPlatformVersion() >= BEGIN_VERSION;
667             if (NearZero(lineHeight) || (!isBeginVersion && NearEqual(lineHeight, fontSize))) {
668                 txtStyle.heightOnly = false;
669             }
670         }
671     }
672 
673     // set font variant
674     auto fontFeatures = textStyle.GetFontFeatures();
675     if (!fontFeatures.empty()) {
676         Rosen::FontFeatures features;
677         for (auto iter = fontFeatures.begin(); iter != fontFeatures.end(); ++iter) {
678             features.SetFeature(iter->first, iter->second);
679         }
680         txtStyle.fontFeatures = features;
681     }
682     auto textBackgroundStyle = textStyle.GetTextBackgroundStyle();
683     CHECK_NULL_VOID(textBackgroundStyle.has_value());
684     txtStyle.styleId = textBackgroundStyle->groupId;
685     if (textBackgroundStyle->backgroundColor.has_value()) {
686         txtStyle.backgroundRect.color = textBackgroundStyle->backgroundColor.value().GetValue();
687     }
688     auto radius = textBackgroundStyle->backgroundRadius;
689     CHECK_NULL_VOID(radius.has_value());
690     auto radiusConverter = [context = pipelineContext](const std::optional<Dimension>& radius) -> double {
691         CHECK_NULL_RETURN(radius.has_value(), 0);
692         CHECK_NULL_RETURN(context, radius->Value());
693         return context->NormalizeToPx(radius.value());
694     };
695     txtStyle.backgroundRect.leftTopRadius = radiusConverter(radius->radiusTopLeft);
696     txtStyle.backgroundRect.rightTopRadius = radiusConverter(radius->radiusTopRight);
697     txtStyle.backgroundRect.leftBottomRadius = radiusConverter(radius->radiusBottomLeft);
698     txtStyle.backgroundRect.rightBottomRadius = radiusConverter(radius->radiusBottomRight);
699 }
700 #endif
701 
702 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertSkRect(SkRect skRect)703 Rect ConvertSkRect(SkRect skRect)
704 {
705     Rect result;
706     result.SetLeft(skRect.fLeft);
707     result.SetTop(skRect.fTop);
708     result.SetWidth(skRect.width());
709     result.SetHeight(skRect.height());
710     return result;
711 }
712 #else
ConvertSkRect(const Rosen::Drawing::RectF & skRect)713 Rect ConvertSkRect(const Rosen::Drawing::RectF& skRect)
714 {
715     Rect result;
716     result.SetLeft(skRect.GetLeft());
717     result.SetTop(skRect.GetTop());
718     result.SetWidth(skRect.GetWidth());
719     result.SetHeight(skRect.GetHeight());
720     return result;
721 }
722 #endif
723 
724 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertPlaceholderAlignment(PlaceholderAlignment textDecoration)725 txt::PlaceholderAlignment ConvertPlaceholderAlignment(PlaceholderAlignment textDecoration)
726 {
727     txt::PlaceholderAlignment convertValue = txt::PlaceholderAlignment::kBaseline;
728     switch (textDecoration) {
729         case PlaceholderAlignment::BASELINE:
730             convertValue = txt::PlaceholderAlignment::kBaseline;
731             break;
732         case PlaceholderAlignment::ABOVEBASELINE:
733             convertValue = txt::PlaceholderAlignment::kAboveBaseline;
734             break;
735         case PlaceholderAlignment::BELOWBASELINE:
736             convertValue = txt::PlaceholderAlignment::kBelowBaseline;
737             break;
738         case PlaceholderAlignment::TOP:
739             convertValue = txt::PlaceholderAlignment::kTop;
740             break;
741         case PlaceholderAlignment::BOTTOM:
742             convertValue = txt::PlaceholderAlignment::kBottom;
743             break;
744         case PlaceholderAlignment::MIDDLE:
745             convertValue = txt::PlaceholderAlignment::kMiddle;
746             break;
747         default:
748             LOGW("PlaceholderAlignment setting error! Now using default PlaceholderAlignment");
749             break;
750     }
751     return convertValue;
752 }
753 #else
ConvertPlaceholderAlignment(PlaceholderAlignment textDecoration)754 Rosen::PlaceholderVerticalAlignment ConvertPlaceholderAlignment(PlaceholderAlignment textDecoration)
755 {
756     Rosen::PlaceholderVerticalAlignment convertValue = Rosen::PlaceholderVerticalAlignment::OFFSET_AT_BASELINE;
757     switch (textDecoration) {
758         case PlaceholderAlignment::BASELINE:
759             convertValue = Rosen::PlaceholderVerticalAlignment::OFFSET_AT_BASELINE;
760             break;
761         case PlaceholderAlignment::ABOVEBASELINE:
762             convertValue = Rosen::PlaceholderVerticalAlignment::ABOVE_BASELINE;
763             break;
764         case PlaceholderAlignment::BELOWBASELINE:
765             convertValue = Rosen::PlaceholderVerticalAlignment::BELOW_BASELINE;
766             break;
767         case PlaceholderAlignment::TOP:
768             convertValue = Rosen::PlaceholderVerticalAlignment::TOP_OF_ROW_BOX;
769             break;
770         case PlaceholderAlignment::BOTTOM:
771             convertValue = Rosen::PlaceholderVerticalAlignment::BOTTOM_OF_ROW_BOX;
772             break;
773         case PlaceholderAlignment::MIDDLE:
774             convertValue = Rosen::PlaceholderVerticalAlignment::CENTER_OF_ROW_BOX;
775             break;
776         default:
777             LOGW("PlaceholderAlignment setting error! Now using default PlaceholderAlignment");
778             break;
779     }
780     return convertValue;
781 }
782 #endif
783 
784 #ifndef USE_GRAPHIC_TEXT_GINE
ConvertPlaceholderRun(const PlaceholderRun & span,txt::PlaceholderRun & txtSpan)785 void ConvertPlaceholderRun(const PlaceholderRun& span, txt::PlaceholderRun& txtSpan)
786 #else
787 void ConvertPlaceholderRun(const PlaceholderRun& span, Rosen::PlaceholderSpan& txtSpan)
788 #endif
789 {
790     txtSpan.width = span.width;
791     txtSpan.height = span.height;
792     txtSpan.alignment = ConvertPlaceholderAlignment(span.alignment);
793     txtSpan.baseline = ConvertTxtTextBaseline(span.baseline);
794 #ifndef USE_GRAPHIC_TEXT_GINE
795     txtSpan.baseline_offset = span.baseline_offset;
796 #else
797     txtSpan.baselineOffset = span.baseline_offset;
798 #endif
799 }
800 
801 } // namespace OHOS::Ace::Constants
802