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