• 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 OHOS::Ace::NG {
21 #define UPDATE_TEXT_STYLE(group, name, func)             \
22     do {                                                 \
23         if ((group)->prop##name.has_value()) {           \
24             textStyle.func((group)->prop##name.value()); \
25         }                                                \
26     } while (false)
27 
CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle> & fontStyle,const std::unique_ptr<TextLineStyle> & textLineStyle,const RefPtr<TextTheme> & textTheme)28 TextStyle CreateTextStyleUsingTheme(const std::unique_ptr<FontStyle>& fontStyle,
29     const std::unique_ptr<TextLineStyle>& textLineStyle, const RefPtr<TextTheme>& textTheme)
30 {
31     TextStyle textStyle = textTheme ? textTheme->GetTextStyle() : TextStyle();
32     if (fontStyle) {
33         UPDATE_TEXT_STYLE(fontStyle, FontSize, SetFontSize);
34         UPDATE_TEXT_STYLE(fontStyle, TextColor, SetTextColor);
35         UPDATE_TEXT_STYLE(fontStyle, TextShadow, SetShadow);
36         UPDATE_TEXT_STYLE(fontStyle, ItalicFontStyle, SetFontStyle);
37         UPDATE_TEXT_STYLE(fontStyle, FontWeight, SetFontWeight);
38         UPDATE_TEXT_STYLE(fontStyle, FontFamily, SetFontFamilies);
39         UPDATE_TEXT_STYLE(fontStyle, TextDecoration, SetTextDecoration);
40         UPDATE_TEXT_STYLE(fontStyle, TextDecorationColor, SetTextDecorationColor);
41         UPDATE_TEXT_STYLE(fontStyle, TextCase, SetTextCase);
42         UPDATE_TEXT_STYLE(fontStyle, AdaptMinFontSize, SetAdaptMinFontSize);
43         UPDATE_TEXT_STYLE(fontStyle, AdaptMaxFontSize, SetAdaptMaxFontSize);
44         UPDATE_TEXT_STYLE(fontStyle, LetterSpacing, SetLetterSpacing);
45     }
46     if (textLineStyle) {
47         UPDATE_TEXT_STYLE(textLineStyle, LineHeight, SetLineHeight);
48         UPDATE_TEXT_STYLE(textLineStyle, TextBaseline, SetTextBaseline);
49         UPDATE_TEXT_STYLE(textLineStyle, BaselineOffset, SetBaselineOffset);
50         UPDATE_TEXT_STYLE(textLineStyle, TextOverflow, SetTextOverflow);
51         UPDATE_TEXT_STYLE(textLineStyle, TextAlign, SetTextAlign);
52         UPDATE_TEXT_STYLE(textLineStyle, MaxLines, SetMaxLines);
53         UPDATE_TEXT_STYLE(textLineStyle, TextIndent, SetTextIndent);
54     }
55     return textStyle;
56 }
57 
CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode,const std::unique_ptr<FontStyle> & fontStyle,const std::unique_ptr<TextLineStyle> & textLineStyle,const RefPtr<TextTheme> & textTheme)58 TextStyle CreateTextStyleUsingThemeWithText(const RefPtr<FrameNode> frameNode,
59     const std::unique_ptr<FontStyle>& fontStyle, const std::unique_ptr<TextLineStyle>& textLineStyle,
60     const RefPtr<TextTheme>& textTheme)
61 {
62     TextStyle textStyle = CreateTextStyleUsingTheme(fontStyle, textLineStyle, textTheme);
63     auto renderContext = frameNode->GetRenderContext();
64     if (renderContext->HasForegroundColor() || renderContext->HasForegroundColorStrategy()) {
65         textStyle.SetTextColor(Color::FOREGROUND);
66     }
67     return textStyle;
68 }
GetFontSizeInJson(const std::optional<Dimension> & value)69 std::string GetFontSizeInJson(const std::optional<Dimension>& value)
70 {
71     return value.value_or(TEXT_DEFAULT_FONT_SIZE).ToString();
72 }
GetFontStyleInJson(const std::optional<Ace::FontStyle> & value)73 std::string GetFontStyleInJson(const std::optional<Ace::FontStyle>& value)
74 {
75     return value.value_or(Ace::FontStyle::NORMAL) == Ace::FontStyle::NORMAL ? "FontStyle.Normal" : "FontStyle.Italic";
76 }
GetFontWeightInJson(const std::optional<FontWeight> & value)77 std::string GetFontWeightInJson(const std::optional<FontWeight>& value)
78 {
79     return V2::ConvertWrapFontWeightToStirng(value.value_or(FontWeight::NORMAL));
80 }
GetFontFamilyInJson(const std::optional<std::vector<std::string>> & value)81 std::string GetFontFamilyInJson(const std::optional<std::vector<std::string>>& value)
82 {
83     std::vector<std::string> fontFamilyVector = value.value_or<std::vector<std::string>>({ "HarmonyOS Sans" });
84     if (fontFamilyVector.empty()) {
85         fontFamilyVector = std::vector<std::string>({ "HarmonyOS Sans" });
86     }
87     std::string fontFamily = fontFamilyVector.at(0);
88     for (uint32_t i = 1; i < fontFamilyVector.size(); ++i) {
89         fontFamily += ',' + fontFamilyVector.at(i);
90     }
91     return fontFamily;
92 }
93 } // namespace OHOS::Ace::NG
94