• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_TEXT_STYLE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_TEXT_STYLE_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "base/geometry/dimension.h"
24 #include "base/utils/linear_map.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components/common/properties/color.h"
27 #include "core/components/common/properties/shadow.h"
28 #include "core/components_ng/base/inspector_filter.h"
29 #include "core/components_ng/property/border_property.h"
30 #include "core/pipeline/base/render_component.h"
31 #include "frameworks/core/components_ng/pattern/symbol/symbol_effect_options.h"
32 
33 namespace OHOS::Ace {
34 // The normal weight is W400, the larger the number after W, the thicker the font will be.
35 // BOLD is equal to W700 and NORMAL is equal to W400, lighter is W100, BOLDER is W900.
36 enum class FontWeight {
37     W100 = 0,
38     W200,
39     W300,
40     W400,
41     W500,
42     W600,
43     W700,
44     W800,
45     W900,
46     BOLD,
47     NORMAL,
48     BOLDER,
49     LIGHTER,
50     MEDIUM,
51     REGULAR,
52 };
53 
54 constexpr uint32_t DEFAULT_MAX_FONT_FAMILY_LENGTH = Infinity<uint32_t>();
55 
56 enum class FontStyle {
57     NORMAL,
58     ITALIC,
59     NONE
60 };
61 
62 namespace StringUtils {
ToString(const FontStyle & fontStyle)63 inline std::string ToString(const FontStyle& fontStyle)
64 {
65     static const LinearEnumMapNode<FontStyle, std::string> table[] = {
66         { FontStyle::NORMAL, "NORMAL" },
67         { FontStyle::ITALIC, "ITALIC" },
68     };
69     auto iter = BinarySearchFindIndex(table, ArraySize(table), fontStyle);
70     return iter != -1 ? table[iter].value : "";
71 }
72 
ToStringNDK(const FontStyle & fontStyle)73 inline std::string ToStringNDK(const FontStyle& fontStyle)
74 {
75     static const LinearEnumMapNode<FontStyle, std::string> table[] = {
76         { FontStyle::NORMAL, "normal" },
77         { FontStyle::ITALIC, "italic" },
78     };
79     auto iter = BinarySearchFindIndex(table, ArraySize(table), fontStyle);
80     return iter != -1 ? table[iter].value : "";
81 }
82 } // namespace StringUtils
83 
84 enum class TextBaseline {
85     ALPHABETIC,
86     IDEOGRAPHIC,
87     TOP,
88     BOTTOM,
89     MIDDLE,
90     HANGING,
91 };
92 
93 namespace StringUtils {
ToString(const TextBaseline & textBaseline)94 inline std::string ToString(const TextBaseline& textBaseline)
95 {
96     static const LinearEnumMapNode<TextBaseline, std::string> table[] = {
97         { TextBaseline::ALPHABETIC, "ALPHABETIC" },
98         { TextBaseline::IDEOGRAPHIC, "IDEOGRAPHIC" },
99         { TextBaseline::TOP, "TOP" },
100         { TextBaseline::BOTTOM, "BOTTOM" },
101         { TextBaseline::MIDDLE, "MIDDLE" },
102         { TextBaseline::HANGING, "HANGING" },
103     };
104     auto iter = BinarySearchFindIndex(table, ArraySize(table), textBaseline);
105     return iter != -1 ? table[iter].value : "";
106 }
107 } // namespace StringUtils
108 
109 enum class TextCase {
110     NORMAL = 0,
111     LOWERCASE,
112     UPPERCASE,
113 };
114 
115 namespace StringUtils {
ToString(const TextCase & textCase)116 inline std::string ToString(const TextCase& textCase)
117 {
118     static const LinearEnumMapNode<TextCase, std::string> table[] = {
119         { TextCase::NORMAL, "NORMAL" },
120         { TextCase::LOWERCASE, "LOWERCASE" },
121         { TextCase::UPPERCASE, "UPPERCASE" },
122     };
123     auto iter = BinarySearchFindIndex(table, ArraySize(table), textCase);
124     return iter != -1 ? table[iter].value : "";
125 }
126 } // namespace StringUtils
127 
128 enum class EllipsisMode {
129     HEAD,
130     MIDDLE,
131     TAIL,
132 };
133 
134 namespace StringUtils {
ToString(const EllipsisMode & ellipsisMode)135 inline std::string ToString(const EllipsisMode& ellipsisMode)
136 {
137     static const LinearEnumMapNode<EllipsisMode, std::string> table[] = {
138         { EllipsisMode::HEAD, "HEAD" },
139         { EllipsisMode::MIDDLE, "MIDDLE" },
140         { EllipsisMode::TAIL, "TAIL" },
141     };
142     auto iter = BinarySearchFindIndex(table, ArraySize(table), ellipsisMode);
143     return iter != -1 ? table[iter].value : "";
144 }
145 } // namespace StringUtils
146 
147 enum class WordBreak { NORMAL = 0, BREAK_ALL, BREAK_WORD, HYPHENATION };
148 extern const std::vector<WordBreak> WORD_BREAK_TYPES;
149 extern const std::vector<LineBreakStrategy> LINE_BREAK_STRATEGY_TYPES;
150 namespace StringUtils {
ToString(const WordBreak & wordBreak)151 inline std::string ToString(const WordBreak& wordBreak)
152 {
153     static const LinearEnumMapNode<WordBreak, std::string> table[] = {
154         { WordBreak::NORMAL, "NORMAL" },
155         { WordBreak::BREAK_ALL, "BREAK_ALL" },
156         { WordBreak::BREAK_WORD, "BREAK_WORD" },
157         { WordBreak::HYPHENATION, "HYPHENATION" },
158     };
159     auto iter = BinarySearchFindIndex(table, ArraySize(table), wordBreak);
160     return iter != -1 ? table[iter].value : "";
161 }
162 } // namespace StringUtils
163 
164 /// Where to vertically align the placeholder relative to the surrounding text.
165 enum class PlaceholderAlignment {
166     /// Match the baseline of the placeholder with the baseline.
167     BASELINE,
168 
169     /// Align the bottom edge of the placeholder with the baseline such that the
170     /// placeholder sits on top of the baseline.
171     ABOVEBASELINE,
172 
173     /// Align the top edge of the placeholder with the baseline specified in
174     /// such that the placeholder hangs below the baseline.
175     BELOWBASELINE,
176 
177     /// Align the top edge of the placeholder with the top edge of the font.
178     /// When the placeholder is very tall, the extra space will hang from
179     /// the top and extend through the bottom of the line.
180     TOP,
181 
182     /// Align the bottom edge of the placeholder with the top edge of the font.
183     /// When the placeholder is very tall, the extra space will rise from
184     /// the bottom and extend through the top of the line.
185     BOTTOM,
186 
187     /// Align the middle of the placeholder with the middle of the text. When the
188     /// placeholder is very tall, the extra space will grow equally from
189     /// the top and bottom of the line.
190     MIDDLE,
191 };
192 
193 namespace StringUtils {
ToString(const PlaceholderAlignment & placeholderAlignment)194 inline std::string ToString(const PlaceholderAlignment& placeholderAlignment)
195 {
196     static const LinearEnumMapNode<PlaceholderAlignment, std::string> table[] = {
197         { PlaceholderAlignment::BASELINE, "BASELINE" },
198         { PlaceholderAlignment::ABOVEBASELINE, "ABOVEBASELINE" },
199         { PlaceholderAlignment::BELOWBASELINE, "BELOWBASELINE" },
200         { PlaceholderAlignment::TOP, "TOP" },
201         { PlaceholderAlignment::BOTTOM, "BOTTOM" },
202         { PlaceholderAlignment::MIDDLE, "MIDDLE" },
203     };
204     auto iter = BinarySearchFindIndex(table, ArraySize(table), placeholderAlignment);
205     return iter != -1 ? table[iter].value : "";
206 }
207 } // namespace StringUtils
208 
209 struct TextSizeGroup {
210     Dimension fontSize = 14.0_px;
211     uint32_t maxLines = INT32_MAX;
212     TextOverflow textOverflow = TextOverflow::CLIP;
213 };
214 
215 /// Placeholder properties
216 struct PlaceholderRun {
217     /// Placeholder's width
218     float width = 0.0f;
219 
220     /// Placeholder's height
221     float height = 0.0f;
222 
223     /// Vertically alignment the placeholder relative to the surrounding text.
224     PlaceholderAlignment alignment = PlaceholderAlignment::BOTTOM;
225 
226     /// The placeholder with the baseline styles
227     TextBaseline baseline = TextBaseline::ALPHABETIC;
228 
229     /// The baseline offset
230     float baseline_offset = 0.0f;
231 };
232 
233 struct TextBackgroundStyle {
234     std::optional<Color> backgroundColor;
235     std::optional<NG::BorderRadiusProperty> backgroundRadius;
236     int32_t groupId = 0;
237     bool needCompareGroupId = true;
238 
UpdateColorByResourceIdTextBackgroundStyle239     void UpdateColorByResourceId()
240     {
241         CHECK_NULL_VOID(backgroundColor);
242         backgroundColor->UpdateColorByResourceId();
243     }
244 
245     static void ToJsonValue(std::unique_ptr<JsonValue>& json, const std::optional<TextBackgroundStyle>& style,
246         const NG::InspectorFilter& filter);
247 
248     bool operator==(const TextBackgroundStyle& value) const
249     {
250         // Only compare groupId if both styles require it.
251         bool bothNeedCompareGroupId = needCompareGroupId && value.needCompareGroupId;
252         return backgroundColor == value.backgroundColor && backgroundRadius == value.backgroundRadius &&
253                (!bothNeedCompareGroupId || groupId == value.groupId);
254     }
255 };
256 
257 class ACE_EXPORT TextStyle final {
258 public:
259     TextStyle() = default;
260     TextStyle(const std::vector<std::string>& fontFamilies, double fontSize, FontWeight fontWeight, FontStyle fontStyle,
261         const Color& textColor);
TextStyle(double fontSize)262     TextStyle(double fontSize) : fontSize_(fontSize) {}
TextStyle(const Color & textColor)263     TextStyle(const Color& textColor) : textColor_(textColor) {}
264     ~TextStyle() = default;
265 
266     bool operator==(const TextStyle& rhs) const;
267     bool operator!=(const TextStyle& rhs) const;
268 
GetTextBaseline()269     TextBaseline GetTextBaseline() const
270     {
271         return textBaseline_;
272     }
273 
GetBaselineOffset()274     const Dimension& GetBaselineOffset() const
275     {
276         return baselineOffset_;
277     }
278 
SetBaselineOffset(const Dimension & baselineOffset)279     void SetBaselineOffset(const Dimension& baselineOffset)
280     {
281         baselineOffset_ = baselineOffset;
282     }
283 
SetTextBaseline(TextBaseline baseline)284     void SetTextBaseline(TextBaseline baseline)
285     {
286         textBaseline_ = baseline;
287     }
288 
ResetTextBaseline()289     void ResetTextBaseline()
290     {
291         baselineOffset_.Reset();
292     }
293 
SetTextDecoration(TextDecoration textDecoration)294     void SetTextDecoration(TextDecoration textDecoration)
295     {
296         textDecoration_ = textDecoration;
297     }
298 
SetTextDecorationStyle(TextDecorationStyle textDecorationStyle)299     void SetTextDecorationStyle(TextDecorationStyle textDecorationStyle)
300     {
301         textDecorationStyle_ = textDecorationStyle;
302     }
303 
GetFontStyle()304     FontStyle GetFontStyle() const
305     {
306         return fontStyle_;
307     }
308 
SetFontStyle(FontStyle fontStyle)309     void SetFontStyle(FontStyle fontStyle)
310     {
311         fontStyle_ = fontStyle;
312     }
313 
GetFontSize()314     const Dimension& GetFontSize() const
315     {
316         return fontSize_;
317     }
318 
GetWhiteSpace()319     WhiteSpace GetWhiteSpace() const
320     {
321         return whiteSpace_;
322     }
323 
SetWhiteSpace(WhiteSpace whiteSpace)324     void SetWhiteSpace(WhiteSpace whiteSpace)
325     {
326         whiteSpace_ = whiteSpace;
327     }
328 
SetFontSize(const Dimension & fontSize)329     void SetFontSize(const Dimension& fontSize)
330     {
331         fontSize_ = fontSize;
332     }
333 
SetMaxFontScale(float maxFontScale)334     void SetMaxFontScale(float maxFontScale)
335     {
336         maxFontScale_ = maxFontScale;
337     }
338 
SetMinFontScale(float minFontScale)339     void SetMinFontScale(float minFontScale)
340     {
341         minFontScale_ = minFontScale;
342     }
343 
GetMaxFontScale()344     std::optional<float> GetMaxFontScale() const
345     {
346         return maxFontScale_;
347     }
348 
GetMinFontScale()349     std::optional<float> GetMinFontScale() const
350     {
351         return minFontScale_;
352     }
353 
GetFontWeight()354     FontWeight GetFontWeight() const
355     {
356         return fontWeight_;
357     }
358 
SetFontWeight(FontWeight fontWeight)359     void SetFontWeight(FontWeight fontWeight)
360     {
361         fontWeight_ = fontWeight;
362     }
363 
GetVariableFontWeight()364     int32_t GetVariableFontWeight() const
365     {
366         return variableFontWeight_;
367     }
368 
SetVariableFontWeight(int32_t variableFontWeight)369     void SetVariableFontWeight(int32_t variableFontWeight)
370     {
371         variableFontWeight_ = variableFontWeight;
372     }
373 
GetEnableVariableFontWeight()374     bool GetEnableVariableFontWeight() const
375     {
376         return enableVariableFontWeight_;
377     }
378 
SetEnableVariableFontWeight(bool enableVariableFontWeight)379     void SetEnableVariableFontWeight(bool enableVariableFontWeight)
380     {
381         enableVariableFontWeight_ = enableVariableFontWeight;
382     }
GetTextColor()383     const Color GetTextColor() const
384     {
385         return textColor_;
386     }
387 
SetTextColor(const Color & textColor)388     void SetTextColor(const Color& textColor)
389     {
390         textColor_ = textColor;
391     }
392 
GetTextDecoration()393     TextDecoration GetTextDecoration() const
394     {
395         return textDecoration_;
396     }
397 
GetTextDecorationStyle()398     TextDecorationStyle GetTextDecorationStyle() const
399     {
400         return textDecorationStyle_;
401     }
402 
GetWordSpacing()403     const Dimension& GetWordSpacing() const
404     {
405         return wordSpacing_;
406     }
407 
SetWordSpacing(const Dimension & wordSpacing)408     void SetWordSpacing(const Dimension& wordSpacing)
409     {
410         wordSpacing_ = wordSpacing;
411     }
412 
GetTextDecorationColor()413     const Color GetTextDecorationColor() const
414     {
415         return textDecorationColor_;
416     }
417 
SetTextDecorationColor(const Color & textDecorationColor)418     void SetTextDecorationColor(const Color& textDecorationColor)
419     {
420         textDecorationColor_ = textDecorationColor;
421     }
422 
GetFontFamilies()423     const std::vector<std::string>& GetFontFamilies() const
424     {
425         return fontFamilies_;
426     }
427 
SetFontFamilies(const std::vector<std::string> & fontFamilies)428     void SetFontFamilies(const std::vector<std::string>& fontFamilies)
429     {
430         fontFamilies_ = fontFamilies;
431     }
432 
GetTextIndent()433     Dimension GetTextIndent() const
434     {
435         return textIndent_;
436     }
437 
SetTextIndent(const Dimension & textIndent)438     void SetTextIndent(const Dimension& textIndent)
439     {
440         textIndent_ = textIndent;
441     }
442 
GetFontFeatures()443     const std::list<std::pair<std::string, int32_t>>& GetFontFeatures() const
444     {
445         return fontFeatures_;
446     }
447 
SetFontFeatures(const std::list<std::pair<std::string,int32_t>> & fontFeatures)448     void SetFontFeatures(const std::list<std::pair<std::string, int32_t>>& fontFeatures)
449     {
450         fontFeatures_ = fontFeatures;
451     }
452 
GetLineHeight()453     const Dimension& GetLineHeight() const
454     {
455         return lineHeight_;
456     }
457 
458     void SetLineHeight(const Dimension& lineHeight, bool hasHeightOverride = true)
459     {
460         lineHeight_ = lineHeight;
461         hasHeightOverride_ = hasHeightOverride;
462     }
463 
GetLineSpacing()464     const Dimension& GetLineSpacing() const
465     {
466         return lineSpacing_;
467     }
468 
SetLineSpacing(const Dimension & lineSpacing)469     void SetLineSpacing(const Dimension& lineSpacing)
470     {
471         lineSpacing_ = lineSpacing;
472     }
473 
GetParagraphSpacing()474     const Dimension& GetParagraphSpacing() const
475     {
476         return paragraphSpacing_;
477     }
478 
SetParagraphSpacing(const Dimension & paragraphSpacing)479     void SetParagraphSpacing(const Dimension& paragraphSpacing)
480     {
481         paragraphSpacing_ = paragraphSpacing;
482     }
483 
HasHeightOverride()484     bool HasHeightOverride() const
485     {
486         return hasHeightOverride_;
487     }
488 
GetLetterSpacing()489     const Dimension& GetLetterSpacing() const
490     {
491         return letterSpacing_;
492     }
493 
SetLetterSpacing(const Dimension & letterSpacing)494     void SetLetterSpacing(const Dimension& letterSpacing)
495     {
496         letterSpacing_ = letterSpacing;
497     }
498 
GetAdaptTextSize()499     bool GetAdaptTextSize() const
500     {
501         return adaptTextSize_;
502     }
503 
504     void SetAdaptTextSize(
505         const Dimension& maxFontSize, const Dimension& minFontSize, const Dimension& fontSizeStep = 1.0_px);
506 
GetAdaptHeight()507     bool GetAdaptHeight() const
508     {
509         return adaptHeight_;
510     }
511 
SetAdaptHeight(bool adaptHeight)512     void SetAdaptHeight(bool adaptHeight)
513     {
514         adaptHeight_ = adaptHeight;
515     }
516 
DisableAdaptTextSize()517     void DisableAdaptTextSize()
518     {
519         adaptTextSize_ = false;
520     }
521 
GetMaxLines()522     uint32_t GetMaxLines() const
523     {
524         return maxLines_;
525     }
526 
SetMaxLines(uint32_t maxLines)527     void SetMaxLines(uint32_t maxLines)
528     {
529         maxLines_ = maxLines;
530     }
531 
SetPreferFontSizes(const std::vector<Dimension> & preferFontSizes)532     void SetPreferFontSizes(const std::vector<Dimension>& preferFontSizes)
533     {
534         preferFontSizes_ = preferFontSizes;
535         adaptTextSize_ = true;
536     }
537 
GetPreferFontSizes()538     const std::vector<Dimension>& GetPreferFontSizes() const
539     {
540         return preferFontSizes_;
541     }
542 
543     // Must use with SetAdaptMinFontSize and SetAdaptMaxFontSize.
SetAdaptFontSizeStep(const Dimension & adaptTextSizeStep)544     void SetAdaptFontSizeStep(const Dimension& adaptTextSizeStep)
545     {
546         adaptFontSizeStep_ = adaptTextSizeStep;
547     }
548     // Must use with SetAdaptMaxFontSize.
SetAdaptMinFontSize(const Dimension & adaptMinFontSize)549     void SetAdaptMinFontSize(const Dimension& adaptMinFontSize)
550     {
551         adaptMinFontSize_ = adaptMinFontSize;
552         adaptTextSize_ = true;
553     }
554     // Must use with SetAdaptMinFontSize.
SetAdaptMaxFontSize(const Dimension & adaptMaxFontSize)555     void SetAdaptMaxFontSize(const Dimension& adaptMaxFontSize)
556     {
557         adaptMaxFontSize_ = adaptMaxFontSize;
558         adaptTextSize_ = true;
559     }
560 
GetAdaptFontSizeStep()561     const Dimension& GetAdaptFontSizeStep() const
562     {
563         return adaptFontSizeStep_;
564     }
565 
GetAdaptMinFontSize()566     const Dimension& GetAdaptMinFontSize() const
567     {
568         return adaptMinFontSize_;
569     }
570 
GetAdaptMaxFontSize()571     const Dimension& GetAdaptMaxFontSize() const
572     {
573         return adaptMaxFontSize_;
574     }
575 
GetPreferTextSizeGroups()576     const std::vector<TextSizeGroup>& GetPreferTextSizeGroups() const
577     {
578         return preferTextSizeGroups_;
579     }
SetPreferTextSizeGroups(const std::vector<TextSizeGroup> & preferTextSizeGroups)580     void SetPreferTextSizeGroups(const std::vector<TextSizeGroup>& preferTextSizeGroups)
581     {
582         preferTextSizeGroups_ = preferTextSizeGroups;
583         adaptTextSize_ = true;
584     }
585 
IsAllowScale()586     bool IsAllowScale() const
587     {
588         return allowScale_;
589     }
590 
SetAllowScale(bool allowScale)591     void SetAllowScale(bool allowScale)
592     {
593         allowScale_ = allowScale;
594     }
595 
GetTextOverflow()596     TextOverflow GetTextOverflow() const
597     {
598         return textOverflow_;
599     }
SetTextOverflow(TextOverflow textOverflow)600     void SetTextOverflow(TextOverflow textOverflow)
601     {
602         textOverflow_ = textOverflow;
603     }
GetTextAlign()604     TextAlign GetTextAlign() const
605     {
606         return textAlign_;
607     }
SetTextAlign(TextAlign textAlign)608     void SetTextAlign(TextAlign textAlign)
609     {
610         textAlign_ = textAlign;
611     }
SetTextVerticalAlign(VerticalAlign verticalAlign)612     void SetTextVerticalAlign(VerticalAlign verticalAlign)
613     {
614         verticalAlign_ = verticalAlign;
615     }
616 
GetTextVerticalAlign()617     VerticalAlign GetTextVerticalAlign() const
618     {
619         return verticalAlign_;
620     }
621 
GetWordBreak()622     WordBreak GetWordBreak() const
623     {
624         return wordBreak_;
625     }
626 
SetWordBreak(WordBreak wordBreak)627     void SetWordBreak(WordBreak wordBreak)
628     {
629         wordBreak_ = wordBreak;
630     }
631 
GetTextCase()632     TextCase GetTextCase() const
633     {
634         return textCase_;
635     }
636 
SetTextCase(TextCase textCase)637     void SetTextCase(TextCase textCase)
638     {
639         textCase_ = textCase;
640     }
641 
GetTextShadows()642     const std::vector<Shadow>& GetTextShadows() const
643     {
644         return textShadows_;
645     }
646 
SetTextShadows(const std::vector<Shadow> & textShadows)647     void SetTextShadows(const std::vector<Shadow>& textShadows)
648     {
649         textShadows_ = textShadows;
650     }
651 
SetShadow(const Shadow & shadow)652     void SetShadow(const Shadow& shadow)
653     {
654         textShadows_.emplace_back(shadow);
655     }
656 
GetHalfLeading()657     bool GetHalfLeading() const
658     {
659         return halfLeading_;
660     }
661 
SetHalfLeading(bool halfLeading)662     void SetHalfLeading(bool halfLeading)
663     {
664         halfLeading_ = halfLeading;
665     }
666 
SetEllipsisMode(EllipsisMode modal)667     void SetEllipsisMode(EllipsisMode modal)
668     {
669         ellipsisMode_ = modal;
670     }
671 
GetEllipsisMode()672     EllipsisMode GetEllipsisMode() const
673     {
674         return ellipsisMode_;
675     }
676 
SetHeightScale(double heightScale)677     void SetHeightScale(double heightScale)
678     {
679         heightScale_ = heightScale;
680     }
681 
GetHeightScale()682     double GetHeightScale() const
683     {
684         return heightScale_;
685     }
686 
SetHeightOnly(bool heightOnly)687     void SetHeightOnly(bool heightOnly)
688     {
689         heightOnly_ = heightOnly;
690     }
691 
GetHeightOnly()692     bool GetHeightOnly() const
693     {
694         return heightOnly_;
695     }
696 
SetEllipsis(std::u16string ellipsis)697     void SetEllipsis(std::u16string ellipsis)
698     {
699         ellipsis_ = ellipsis;
700     }
701 
GetEllipsis()702     std::u16string GetEllipsis() const
703     {
704         return ellipsis_;
705     }
706 
SetLocale(std::string locale)707     void SetLocale(std::string locale)
708     {
709         locale_ = locale;
710     }
711 
GetLocale()712     std::string GetLocale() const
713     {
714         return locale_;
715     }
716 
SetTextBackgroundStyle(const std::optional<TextBackgroundStyle> & style)717     void SetTextBackgroundStyle(const std::optional<TextBackgroundStyle>& style)
718     {
719         textBackgroundStyle_ = style;
720     }
721 
GetTextBackgroundStyle()722     const std::optional<TextBackgroundStyle>& GetTextBackgroundStyle() const
723     {
724         return textBackgroundStyle_;
725     }
726 
727     bool isSymbolGlyph_ = false;
728 
SetRenderColors(std::vector<Color> & renderColors)729     void SetRenderColors(std::vector<Color>& renderColors)
730     {
731         renderColors_ = renderColors ;
732     }
733 
SetRenderStrategy(int32_t renderStrategy)734     void SetRenderStrategy(int32_t renderStrategy)
735     {
736         renderStrategy_ = renderStrategy;
737     }
738 
SetEffectStrategy(int32_t effectStrategy)739     void SetEffectStrategy(int32_t effectStrategy)
740     {
741         effectStrategy_ = effectStrategy;
742     }
743 
SetSymbolColorList(const std::vector<Color> & renderColors)744     void SetSymbolColorList(const std::vector<Color>& renderColors)
745     {
746         renderColors_ = renderColors;
747     }
748 
SetSymbolEffectOptions(const std::optional<NG::SymbolEffectOptions> & symbolEffectOptions)749     void SetSymbolEffectOptions(const std::optional<NG::SymbolEffectOptions>& symbolEffectOptions)
750     {
751         symbolEffectOptions_ = symbolEffectOptions;
752     }
753 
GetRenderColors()754     std::vector<Color> GetRenderColors()
755     {
756         return renderColors_;
757     }
758 
GetRenderStrategy()759     int32_t GetRenderStrategy() const
760     {
761         return renderStrategy_;
762     }
763 
GetSymbolColorList()764     const std::vector<Color>& GetSymbolColorList() const
765     {
766         return renderColors_;
767     }
768 
GetEffectStrategy()769     int32_t GetEffectStrategy() const
770     {
771         return effectStrategy_;
772     }
773 
GetLineBreakStrategy()774     LineBreakStrategy GetLineBreakStrategy() const
775     {
776         return lineBreakStrategy_;
777     }
778 
SetLineBreakStrategy(const LineBreakStrategy breakStrategy)779     void SetLineBreakStrategy(const LineBreakStrategy breakStrategy)
780     {
781         lineBreakStrategy_ = breakStrategy;
782     }
783 
GetSymbolType()784     SymbolType GetSymbolType() const
785     {
786         return symbolType_;
787     }
788 
SetSymbolType(const SymbolType symbolType)789     void SetSymbolType(const SymbolType symbolType)
790     {
791         symbolType_ = symbolType;
792     }
793 
GetSymbolEffectOptions()794     const std::optional<NG::SymbolEffectOptions> GetSymbolEffectOptions() const
795     {
796         return symbolEffectOptions_;
797     }
798 
799     std::string ToString() const;
800     void UpdateColorByResourceId();
801 
802 private:
803     std::vector<std::string> fontFamilies_;
804     std::list<std::pair<std::string, int32_t>> fontFeatures_;
805     std::vector<Dimension> preferFontSizes_;
806     std::vector<TextSizeGroup> preferTextSizeGroups_;
807     std::vector<Shadow> textShadows_;
808     // use 14px for normal font size.
809     Dimension fontSize_ { 14, DimensionUnit::PX };
810     Dimension adaptMinFontSize_;
811     Dimension adaptMaxFontSize_;
812     Dimension adaptFontSizeStep_;
813     Dimension lineHeight_;
814     Dimension baselineOffset_;
815     Dimension wordSpacing_;
816     Dimension textIndent_ { 0.0f, DimensionUnit::PX };
817     Dimension letterSpacing_;
818     Dimension lineSpacing_;
819     Dimension paragraphSpacing_ { 0.0f, DimensionUnit::PX };
820     FontWeight fontWeight_ { FontWeight::NORMAL };
821     FontStyle fontStyle_ { FontStyle::NORMAL };
822     TextBaseline textBaseline_ { TextBaseline::ALPHABETIC };
823     TextOverflow textOverflow_ { TextOverflow::CLIP };
824     VerticalAlign verticalAlign_ { VerticalAlign::NONE };
825     TextAlign textAlign_ { TextAlign::START };
826     TextDecorationStyle textDecorationStyle_ { TextDecorationStyle::SOLID };
827     TextDecoration textDecoration_ { TextDecoration::NONE };
828     WhiteSpace whiteSpace_ { WhiteSpace::PRE };
829     WordBreak wordBreak_ { WordBreak::BREAK_WORD };
830     TextCase textCase_ { TextCase::NORMAL };
831     EllipsisMode ellipsisMode_ = EllipsisMode::TAIL;
832     LineBreakStrategy lineBreakStrategy_ { LineBreakStrategy::GREEDY };
833     Color textColor_ { Color::BLACK };
834     Color textDecorationColor_ { Color::BLACK };
835     uint32_t maxLines_ = UINT32_MAX;
836     int32_t variableFontWeight_ = 0;
837     bool hasHeightOverride_ = false;
838     bool adaptTextSize_ = false;
839     bool adaptHeight_ = false; // whether adjust text size with height.
840     bool allowScale_ = true;
841     bool halfLeading_ = false;
842     bool enableVariableFontWeight_ = false;
843     std::optional<TextBackgroundStyle> textBackgroundStyle_;
844     std::optional<float> minFontScale_;
845     std::optional<float> maxFontScale_;
846 
847     // for Symbol
848     std::vector<Color> renderColors_;
849     int32_t renderStrategy_ = 0;
850     int32_t effectStrategy_ = 0;
851     std::optional<NG::SymbolEffectOptions> symbolEffectOptions_;
852     double heightScale_ = 1.0;
853     bool heightOnly_ = false;
854     std::u16string ellipsis_;
855     std::string locale_;
856     SymbolType symbolType_ = SymbolType::SYSTEM;
857 };
858 
859 namespace StringUtils {
860 
861 inline std::pair<bool, FontWeight> ParseFontWeight(const std::string& weight,
862     FontWeight defaultFontWeight = FontWeight::NORMAL)
863 {
864     static const LinearMapNode<FontWeight> fontWeightTable[] = {
865         { "100", FontWeight::W100 },
866         { "200", FontWeight::W200 },
867         { "300", FontWeight::W300 },
868         { "400", FontWeight::W400 },
869         { "500", FontWeight::W500 },
870         { "600", FontWeight::W600 },
871         { "700", FontWeight::W700 },
872         { "800", FontWeight::W800 },
873         { "900", FontWeight::W900 },
874         { "bold", FontWeight::BOLD },
875         { "bolder", FontWeight::BOLDER },
876         { "lighter", FontWeight::LIGHTER },
877         { "medium", FontWeight::MEDIUM },
878         { "normal", FontWeight::NORMAL },
879         { "regular", FontWeight::REGULAR },
880     };
881     auto weightIter = BinarySearchFindIndex(fontWeightTable, ArraySize(fontWeightTable), weight.c_str());
882     return weightIter != -1 ? std::make_pair(true, fontWeightTable[weightIter].value)
883                             : std::make_pair(false, defaultFontWeight);
884 }
885 
886 inline FontWeight StringToFontWeight(const std::string& weight, FontWeight defaultFontWeight = FontWeight::NORMAL)
887 {
888     return ParseFontWeight(weight, defaultFontWeight).second;
889 }
890 
StringToWordBreak(const std::string & wordBreak)891 inline WordBreak StringToWordBreak(const std::string& wordBreak)
892 {
893     static const LinearMapNode<WordBreak> wordBreakTable[] = {
894         { "hyphenation", WordBreak::HYPHENATION },
895         { "break-all", WordBreak::BREAK_ALL },
896         { "break-word", WordBreak::BREAK_WORD },
897         { "normal", WordBreak::NORMAL },
898     };
899     auto wordBreakIter = BinarySearchFindIndex(wordBreakTable, ArraySize(wordBreakTable), wordBreak.c_str());
900     return wordBreakIter != -1 ? wordBreakTable[wordBreakIter].value : WordBreak::BREAK_WORD;
901 }
902 
FontWeightToString(const FontWeight & fontWeight)903 inline std::string FontWeightToString(const FontWeight& fontWeight)
904 {
905     static const std::unordered_map<FontWeight, std::string> fontWeightTable = {
906         { FontWeight::W100, "100" }, { FontWeight::W200, "200" }, { FontWeight::W300, "300" },
907         { FontWeight::W400, "400" }, { FontWeight::W500, "500" }, { FontWeight::W600, "600" },
908         { FontWeight::W700, "700" }, { FontWeight::W800, "800" }, { FontWeight::W900, "900" },
909         { FontWeight::BOLD, "bold" }, { FontWeight::BOLDER, "bolder" }, { FontWeight::LIGHTER, "lighter" },
910         { FontWeight::MEDIUM, "medium" }, { FontWeight::NORMAL, "normal" }, { FontWeight::REGULAR, "regular" },
911     };
912     auto weightIter = fontWeightTable.find(fontWeight);
913     return weightIter != fontWeightTable.end() ? weightIter->second : "";
914 }
915 
ToString(const FontWeight & fontWeight)916 inline std::string ToString(const FontWeight& fontWeight)
917 {
918     return FontWeightToString(fontWeight);
919 }
920 
SymbolColorListToString(const std::vector<Color> & colorList)921 inline std::string SymbolColorListToString(const std::vector<Color>& colorList)
922 {
923     std::string symbolColorList = "";
924     if (!colorList.empty()) {
925         symbolColorList = colorList[0].ColorToString();
926         for (uint32_t i = 1; i < colorList.size(); ++i) {
927             symbolColorList += ", " + colorList[i].ColorToString();
928         }
929     }
930     return symbolColorList;
931 }
932 } // namespace StringUtils
933 } // namespace OHOS::Ace
934 
935 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_TEXT_STYLE_H
936