• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "core/components_ng/pattern/text/text_styles.h"
17 
18 #include "core/components_ng/base/frame_node.h"
19 
20 namespace {
21 constexpr uint32_t RENDERINGSTRATEGY_MULTIPLE_COLOR = 1;
22 constexpr uint32_t RENDERINGSTRATEGY_MULTIPLE_OPACITY = 2;
23 constexpr uint32_t EFFECTSTRATEGY_SCALE = 1;
24 constexpr uint32_t EFFECTSTRATEGY_HIERARCHICAL = 2;
25 };
26 
27 namespace OHOS::Ace::NG {
28 #define UPDATE_TEXT_STYLE(group, name, func)             \
29     do {                                                 \
30         if ((group)->prop##name.has_value()) {           \
31             textStyle.func((group)->prop##name.value()); \
32         }                                                \
33     } while (false)
34 
CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle> & fontStyle,const std::unique_ptr<TextLineStyle> & textLineStyle,const RefPtr<TextTheme> & textTheme)35 TextStyle CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle>& fontStyle,
36     const std::unique_ptr<TextLineStyle>& textLineStyle, const RefPtr<TextTheme>& textTheme)
37 {
38     TextStyle textStyle = textTheme ? textTheme->GetTextStyle() : TextStyle();
39     if (fontStyle) {
40         UPDATE_TEXT_STYLE(fontStyle, FontSize, SetFontSize);
41         UPDATE_TEXT_STYLE(fontStyle, TextColor, SetTextColor);
42         UPDATE_TEXT_STYLE(fontStyle, TextShadow, SetTextShadows);
43         UPDATE_TEXT_STYLE(fontStyle, ItalicFontStyle, SetFontStyle);
44         UPDATE_TEXT_STYLE(fontStyle, FontWeight, SetFontWeight);
45         UPDATE_TEXT_STYLE(fontStyle, FontFamily, SetFontFamilies);
46         UPDATE_TEXT_STYLE(fontStyle, FontFeature, SetFontFeatures);
47         UPDATE_TEXT_STYLE(fontStyle, TextDecoration, SetTextDecoration);
48         UPDATE_TEXT_STYLE(fontStyle, TextDecorationColor, SetTextDecorationColor);
49         UPDATE_TEXT_STYLE(fontStyle, TextDecorationStyle, SetTextDecorationStyle);
50         UPDATE_TEXT_STYLE(fontStyle, TextCase, SetTextCase);
51         UPDATE_TEXT_STYLE(fontStyle, AdaptMinFontSize, SetAdaptMinFontSize);
52         UPDATE_TEXT_STYLE(fontStyle, AdaptMaxFontSize, SetAdaptMaxFontSize);
53         UPDATE_TEXT_STYLE(fontStyle, LetterSpacing, SetLetterSpacing);
54         UPDATE_TEXT_STYLE(fontStyle, SymbolColorList, SetSymbolColorList);
55         UPDATE_TEXT_STYLE(fontStyle, SymbolRenderingStrategy, SetRenderStrategy);
56         UPDATE_TEXT_STYLE(fontStyle, SymbolEffectStrategy, SetEffectStrategy);
57     }
58     if (textLineStyle) {
59         UPDATE_TEXT_STYLE(textLineStyle, LineHeight, SetLineHeight);
60         UPDATE_TEXT_STYLE(textLineStyle, TextBaseline, SetTextBaseline);
61         UPDATE_TEXT_STYLE(textLineStyle, BaselineOffset, SetBaselineOffset);
62         UPDATE_TEXT_STYLE(textLineStyle, TextOverflow, SetTextOverflow);
63         UPDATE_TEXT_STYLE(textLineStyle, TextAlign, SetTextAlign);
64         UPDATE_TEXT_STYLE(textLineStyle, MaxLines, SetMaxLines);
65         UPDATE_TEXT_STYLE(textLineStyle, TextIndent, SetTextIndent);
66         UPDATE_TEXT_STYLE(textLineStyle, WordBreak, SetWordBreak);
67         UPDATE_TEXT_STYLE(textLineStyle, EllipsisMode, SetEllipsisMode);
68     }
69     return textStyle;
70 }
71 
CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode,const std::unique_ptr<FontStyle> & fontStyle,const std::unique_ptr<TextLineStyle> & textLineStyle,const RefPtr<TextTheme> & textTheme)72 TextStyle CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode,
73     const std::unique_ptr<FontStyle>& fontStyle, const std::unique_ptr<TextLineStyle>& textLineStyle,
74     const RefPtr<TextTheme>& textTheme)
75 {
76     TextStyle textStyle = CreateTextStyleUsingTheme(fontStyle, textLineStyle, textTheme);
77     auto renderContext = frameNode->GetRenderContext();
78     if (renderContext->HasForegroundColor() || renderContext->HasForegroundColorStrategy()) {
79         textStyle.SetTextColor(Color::FOREGROUND);
80     }
81     return textStyle;
82 }
GetFontSizeInJson(const std::optional<Dimension> & value)83 std::string GetFontSizeInJson(const std::optional<Dimension>& value)
84 {
85     return value.value_or(TEXT_DEFAULT_FONT_SIZE).ToString();
86 }
GetFontStyleInJson(const std::optional<Ace::FontStyle> & value)87 std::string GetFontStyleInJson(const std::optional<Ace::FontStyle>& value)
88 {
89     return value.value_or(Ace::FontStyle::NORMAL) == Ace::FontStyle::NORMAL ? "FontStyle.Normal" : "FontStyle.Italic";
90 }
GetFontWeightInJson(const std::optional<FontWeight> & value)91 std::string GetFontWeightInJson(const std::optional<FontWeight>& value)
92 {
93     return V2::ConvertWrapFontWeightToStirng(value.value_or(FontWeight::NORMAL));
94 }
GetFontFamilyInJson(const std::optional<std::vector<std::string>> & value)95 std::string GetFontFamilyInJson(const std::optional<std::vector<std::string>>& value)
96 {
97     std::vector<std::string> fontFamilyVector = value.value_or<std::vector<std::string>>({ "HarmonyOS Sans" });
98     if (fontFamilyVector.empty()) {
99         fontFamilyVector = std::vector<std::string>({ "HarmonyOS Sans" });
100     }
101     std::string fontFamily = fontFamilyVector.at(0);
102     for (uint32_t i = 1; i < fontFamilyVector.size(); ++i) {
103         fontFamily += ',' + fontFamilyVector.at(i);
104     }
105     return fontFamily;
106 }
GetSymbolRenderingStrategyInJson(const std::optional<uint32_t> & value)107 std::string GetSymbolRenderingStrategyInJson(const std::optional<uint32_t>& value)
108 {
109     std::string text;
110     if (value == RENDERINGSTRATEGY_MULTIPLE_COLOR) {
111         text = "SymbolRenderingStrategy.MULTIPLE_COLOR";
112     } else if (value == RENDERINGSTRATEGY_MULTIPLE_OPACITY) {
113         text = "SymbolRenderingStrategy.MULTIPLE_OPACITY";
114     } else {
115         text = "SymbolRenderingStrategy.SINGLE";
116     }
117     return text;
118 }
GetSymbolEffectStrategyInJson(const std::optional<uint32_t> & value)119 std::string GetSymbolEffectStrategyInJson(const std::optional<uint32_t>& value)
120 {
121     std::string text;
122     if (value == EFFECTSTRATEGY_SCALE) {
123         text = "SymbolEffectStrategy.SCALE";
124     } else if (value == EFFECTSTRATEGY_HIERARCHICAL) {
125         text = "SymbolEffectStrategy.HIERARCHICAL";
126     } else {
127         text = "SymbolEffectStrategy.NONE";
128     }
129     return text;
130 }
131 } // namespace OHOS::Ace::NG
132