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/common/properties/text_style.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/pattern/symbol/constants.h"
21
22 namespace {
23 constexpr uint32_t RENDERINGSTRATEGY_MULTIPLE_COLOR = 1;
24 constexpr uint32_t RENDERINGSTRATEGY_MULTIPLE_OPACITY = 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 defined(IOS_PLATFORM) || defined(ANDROID_PLATFORM)
40 const std::vector<std::string> defaultFontFamily = { "sans-serif" };
41 textStyle.SetFontFamilies(defaultFontFamily);
42 #endif
43 if (fontStyle) {
44 UPDATE_TEXT_STYLE(fontStyle, FontSize, SetFontSize);
45 UPDATE_TEXT_STYLE(fontStyle, TextColor, SetTextColor);
46 UPDATE_TEXT_STYLE(fontStyle, TextShadow, SetTextShadows);
47 UPDATE_TEXT_STYLE(fontStyle, ItalicFontStyle, SetFontStyle);
48 UPDATE_TEXT_STYLE(fontStyle, FontWeight, SetFontWeight);
49 UPDATE_TEXT_STYLE(fontStyle, FontFamily, SetFontFamilies);
50 UPDATE_TEXT_STYLE(fontStyle, FontFeature, SetFontFeatures);
51 UPDATE_TEXT_STYLE(fontStyle, TextDecoration, SetTextDecoration);
52 UPDATE_TEXT_STYLE(fontStyle, TextDecorationColor, SetTextDecorationColor);
53 UPDATE_TEXT_STYLE(fontStyle, TextDecorationStyle, SetTextDecorationStyle);
54 UPDATE_TEXT_STYLE(fontStyle, TextCase, SetTextCase);
55 UPDATE_TEXT_STYLE(fontStyle, AdaptMinFontSize, SetAdaptMinFontSize);
56 UPDATE_TEXT_STYLE(fontStyle, AdaptMaxFontSize, SetAdaptMaxFontSize);
57 UPDATE_TEXT_STYLE(fontStyle, LetterSpacing, SetLetterSpacing);
58 UPDATE_TEXT_STYLE(fontStyle, SymbolColorList, SetSymbolColorList);
59 UPDATE_TEXT_STYLE(fontStyle, SymbolRenderingStrategy, SetRenderStrategy);
60 UPDATE_TEXT_STYLE(fontStyle, SymbolEffectStrategy, SetEffectStrategy);
61 UPDATE_TEXT_STYLE(fontStyle, SymbolEffectOptions, SetSymbolEffectOptions);
62 UPDATE_TEXT_STYLE(fontStyle, MinFontScale, SetMinFontScale);
63 UPDATE_TEXT_STYLE(fontStyle, MaxFontScale, SetMaxFontScale);
64 UPDATE_TEXT_STYLE(fontStyle, VariableFontWeight, SetVariableFontWeight);
65 UPDATE_TEXT_STYLE(fontStyle, EnableVariableFontWeight, SetEnableVariableFontWeight);
66 }
67 if (textLineStyle) {
68 UPDATE_TEXT_STYLE(textLineStyle, LineHeight, SetLineHeight);
69 UPDATE_TEXT_STYLE(textLineStyle, TextBaseline, SetTextBaseline);
70 UPDATE_TEXT_STYLE(textLineStyle, BaselineOffset, SetBaselineOffset);
71 UPDATE_TEXT_STYLE(textLineStyle, TextOverflow, SetTextOverflow);
72 UPDATE_TEXT_STYLE(textLineStyle, TextAlign, SetTextAlign);
73 UPDATE_TEXT_STYLE(textLineStyle, MaxLines, SetMaxLines);
74 UPDATE_TEXT_STYLE(textLineStyle, TextIndent, SetTextIndent);
75 UPDATE_TEXT_STYLE(textLineStyle, WordBreak, SetWordBreak);
76 UPDATE_TEXT_STYLE(textLineStyle, EllipsisMode, SetEllipsisMode);
77 UPDATE_TEXT_STYLE(textLineStyle, LineSpacing, SetLineSpacing);
78 UPDATE_TEXT_STYLE(textLineStyle, LineBreakStrategy, SetLineBreakStrategy);
79 UPDATE_TEXT_STYLE(textLineStyle, AllowScale, SetAllowScale);
80 }
81 return textStyle;
82 }
83
UseSelfStyle(const std::unique_ptr<FontStyle> & fontStyle,const std::unique_ptr<TextLineStyle> & textLineStyle,TextStyle & textStyle)84 void UseSelfStyle(const std::unique_ptr<FontStyle>& fontStyle,
85 const std::unique_ptr<TextLineStyle>& textLineStyle, TextStyle& textStyle)
86 {
87 if (fontStyle) {
88 UPDATE_TEXT_STYLE(fontStyle, FontSize, SetFontSize);
89 UPDATE_TEXT_STYLE(fontStyle, TextColor, SetTextColor);
90 UPDATE_TEXT_STYLE(fontStyle, TextShadow, SetTextShadows);
91 UPDATE_TEXT_STYLE(fontStyle, ItalicFontStyle, SetFontStyle);
92 UPDATE_TEXT_STYLE(fontStyle, FontWeight, SetFontWeight);
93 UPDATE_TEXT_STYLE(fontStyle, FontFamily, SetFontFamilies);
94 UPDATE_TEXT_STYLE(fontStyle, FontFeature, SetFontFeatures);
95 UPDATE_TEXT_STYLE(fontStyle, TextDecoration, SetTextDecoration);
96 UPDATE_TEXT_STYLE(fontStyle, TextDecorationColor, SetTextDecorationColor);
97 UPDATE_TEXT_STYLE(fontStyle, TextDecorationStyle, SetTextDecorationStyle);
98 UPDATE_TEXT_STYLE(fontStyle, TextCase, SetTextCase);
99 UPDATE_TEXT_STYLE(fontStyle, AdaptMinFontSize, SetAdaptMinFontSize);
100 UPDATE_TEXT_STYLE(fontStyle, AdaptMaxFontSize, SetAdaptMaxFontSize);
101 UPDATE_TEXT_STYLE(fontStyle, LetterSpacing, SetLetterSpacing);
102 UPDATE_TEXT_STYLE(fontStyle, SymbolColorList, SetSymbolColorList);
103 UPDATE_TEXT_STYLE(fontStyle, SymbolRenderingStrategy, SetRenderStrategy);
104 UPDATE_TEXT_STYLE(fontStyle, SymbolEffectStrategy, SetEffectStrategy);
105 UPDATE_TEXT_STYLE(fontStyle, SymbolEffectOptions, SetSymbolEffectOptions);
106 UPDATE_TEXT_STYLE(fontStyle, MinFontScale, SetMinFontScale);
107 UPDATE_TEXT_STYLE(fontStyle, MaxFontScale, SetMaxFontScale);
108 UPDATE_TEXT_STYLE(fontStyle, VariableFontWeight, SetVariableFontWeight);
109 UPDATE_TEXT_STYLE(fontStyle, EnableVariableFontWeight, SetEnableVariableFontWeight);
110 }
111 if (textLineStyle) {
112 UPDATE_TEXT_STYLE(textLineStyle, LineHeight, SetLineHeight);
113 UPDATE_TEXT_STYLE(textLineStyle, TextBaseline, SetTextBaseline);
114 UPDATE_TEXT_STYLE(textLineStyle, BaselineOffset, SetBaselineOffset);
115 UPDATE_TEXT_STYLE(textLineStyle, TextOverflow, SetTextOverflow);
116 UPDATE_TEXT_STYLE(textLineStyle, TextAlign, SetTextAlign);
117 UPDATE_TEXT_STYLE(textLineStyle, MaxLines, SetMaxLines);
118 UPDATE_TEXT_STYLE(textLineStyle, TextIndent, SetTextIndent);
119 UPDATE_TEXT_STYLE(textLineStyle, WordBreak, SetWordBreak);
120 UPDATE_TEXT_STYLE(textLineStyle, EllipsisMode, SetEllipsisMode);
121 UPDATE_TEXT_STYLE(textLineStyle, LineSpacing, SetLineSpacing);
122 }
123 }
124
CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode,const std::unique_ptr<FontStyle> & fontStyle,const std::unique_ptr<TextLineStyle> & textLineStyle,const RefPtr<TextTheme> & textTheme)125 TextStyle CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode,
126 const std::unique_ptr<FontStyle>& fontStyle, const std::unique_ptr<TextLineStyle>& textLineStyle,
127 const RefPtr<TextTheme>& textTheme)
128 {
129 TextStyle textStyle = CreateTextStyleUsingTheme(fontStyle, textLineStyle, textTheme);
130 auto renderContext = frameNode->GetRenderContext();
131 if (renderContext->HasForegroundColor() || renderContext->HasForegroundColorStrategy()) {
132 textStyle.SetTextColor(Color::FOREGROUND);
133 }
134 return textStyle;
135 }
GetFontSizeInJson(const std::optional<Dimension> & value)136 std::string GetFontSizeInJson(const std::optional<Dimension>& value)
137 {
138 return value.value_or(TEXT_DEFAULT_FONT_SIZE).ToString();
139 }
GetFontStyleInJson(const std::optional<Ace::FontStyle> & value)140 std::string GetFontStyleInJson(const std::optional<Ace::FontStyle>& value)
141 {
142 return value.value_or(Ace::FontStyle::NORMAL) == Ace::FontStyle::NORMAL ? "FontStyle.Normal" : "FontStyle.Italic";
143 }
GetFontWeightInJson(const std::optional<FontWeight> & value)144 std::string GetFontWeightInJson(const std::optional<FontWeight>& value)
145 {
146 return V2::ConvertWrapFontWeightToStirng(value.value_or(FontWeight::NORMAL));
147 }
GetFontFamilyInJson(const std::optional<std::vector<std::string>> & value)148 std::string GetFontFamilyInJson(const std::optional<std::vector<std::string>>& value)
149 {
150 std::vector<std::string> fontFamilyVector = value.value_or<std::vector<std::string>>({ "HarmonyOS Sans" });
151 if (fontFamilyVector.empty()) {
152 fontFamilyVector = std::vector<std::string>({ "HarmonyOS Sans" });
153 }
154 std::string fontFamily = fontFamilyVector.at(0);
155 for (uint32_t i = 1; i < fontFamilyVector.size(); ++i) {
156 fontFamily += ',' + fontFamilyVector.at(i);
157 }
158 return fontFamily;
159 }
GetSymbolRenderingStrategyInJson(const std::optional<uint32_t> & value)160 std::string GetSymbolRenderingStrategyInJson(const std::optional<uint32_t>& value)
161 {
162 std::string text;
163 if (value == RENDERINGSTRATEGY_MULTIPLE_COLOR) {
164 text = "SymbolRenderingStrategy.MULTIPLE_COLOR";
165 } else if (value == RENDERINGSTRATEGY_MULTIPLE_OPACITY) {
166 text = "SymbolRenderingStrategy.MULTIPLE_OPACITY";
167 } else {
168 text = "SymbolRenderingStrategy.SINGLE";
169 }
170 return text;
171 }
GetSymbolEffectStrategyInJson(const std::optional<uint32_t> & value)172 std::string GetSymbolEffectStrategyInJson(const std::optional<uint32_t>& value)
173 {
174 std::string text;
175 SymbolEffectType type = static_cast<SymbolEffectType>(value.value_or(0));
176 if (type == SymbolEffectType::SCALE) {
177 text = "SymbolEffectStrategy.SCALE";
178 } else if (type == SymbolEffectType::HIERARCHICAL) {
179 text = "SymbolEffectStrategy.HIERARCHICAL";
180 } else {
181 text = "SymbolEffectStrategy.NONE";
182 }
183 return text;
184 }
185
GetLineBreakStrategyInJson(const std::optional<Ace::LineBreakStrategy> & value)186 std::string GetLineBreakStrategyInJson(const std::optional<Ace::LineBreakStrategy>& value)
187 {
188 std::string text;
189 if (value == LineBreakStrategy::HIGH_QUALITY) {
190 text = "HIGH_QUALITY";
191 } else if (value == LineBreakStrategy::BALANCED) {
192 text = "BALANCED";
193 } else {
194 text = "GREEDY";
195 }
196 return text;
197 }
198
GetSymbolEffectOptionsInJson(const std::optional<SymbolEffectOptions> & value)199 std::string GetSymbolEffectOptionsInJson(const std::optional<SymbolEffectOptions>& value)
200 {
201 std::string text = "";
202 if (value.has_value()) {
203 text = value.value().ToString();
204 }
205 return text;
206 }
207
UpdateColorByResourceId()208 void FontStyle::UpdateColorByResourceId()
209 {
210 if (propTextColor) {
211 propTextColor->UpdateColorByResourceId();
212 }
213 if (propTextDecorationColor) {
214 propTextDecorationColor->UpdateColorByResourceId();
215 }
216 if (propTextShadow) {
217 auto& shadows = propTextShadow.value();
218 std::for_each(shadows.begin(), shadows.end(), [](Shadow& sd) { sd.UpdateColorByResourceId(); });
219 }
220 if (propSymbolColorList) {
221 auto& colors = propSymbolColorList.value();
222 std::for_each(colors.begin(), colors.end(), [](Color& cl) { cl.UpdateColorByResourceId(); });
223 }
224 }
225 } // namespace OHOS::Ace::NG
226