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