1 /*
2  * Copyright (c) 2021-2022 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/text/text_component.h"
17 
18 #include "core/components/text/render_text.h"
19 #include "core/components/text/text_element.h"
20 
21 namespace OHOS::Ace {
22 
TextComponent(const std::string & data)23 TextComponent::TextComponent(const std::string& data) : ComponentGroup(std::list<RefPtr<Component>>())
24 {
25     if (!declaration_) {
26         declaration_ = AceType::MakeRefPtr<TextDeclaration>();
27         declaration_->Init();
28     }
29     SetData(data);
30 }
31 
CreateRenderNode()32 RefPtr<RenderNode> TextComponent::CreateRenderNode()
33 {
34     return RenderText::Create();
35 }
36 
CreateElement()37 RefPtr<Element> TextComponent::CreateElement()
38 {
39     return AceType::MakeRefPtr<TextElement>();
40 }
41 
Compare(const RefPtr<Component> & component) const42 uint32_t TextComponent::Compare(const RefPtr<Component>& component) const
43 {
44     auto text = AceType::DynamicCast<TextComponent>(component);
45     if (!text) {
46         return static_cast<uint32_t>(UpdateRenderType::LAYOUT);
47     }
48     uint32_t updateType = static_cast<uint32_t>(UpdateRenderType::NONE);
49     TextStyle declarationStyle = declaration_->GetTextStyle();
50     TextStyle textStyle = text->GetTextStyle();
51     updateType |= static_cast<uint32_t>(declarationStyle.GetMaxLines() == textStyle.GetMaxLines() ?
52         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
53     updateType |= static_cast<uint32_t>(declarationStyle.GetLineHeight() == textStyle.GetLineHeight() ?
54         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
55     updateType |= static_cast<uint32_t>(declarationStyle.GetAdaptMinFontSize() == textStyle.GetAdaptMinFontSize() ?
56         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
57     updateType |= static_cast<uint32_t>(declarationStyle.GetAdaptMaxFontSize() == textStyle.GetAdaptMaxFontSize() ?
58         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
59     updateType |= static_cast<uint32_t>(declarationStyle.GetLetterSpacing() == textStyle.GetLetterSpacing() ?
60         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
61     updateType |= static_cast<uint32_t>(declarationStyle.GetBaselineOffset() == textStyle.GetBaselineOffset() ?
62         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
63     updateType |= static_cast<uint32_t>(declarationStyle.GetTextAlign() == textStyle.GetTextAlign() ?
64         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
65     updateType |= static_cast<uint32_t>(declarationStyle.GetTextOverflow() == textStyle.GetTextOverflow() ?
66         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
67     updateType |= static_cast<uint32_t>(textStyle.GetTextColor() == declarationStyle.GetTextColor() ?
68         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
69     updateType |= static_cast<uint32_t>(declarationStyle.GetFontFamilies() == textStyle.GetFontFamilies() ?
70         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
71     updateType |= static_cast<uint32_t>(declarationStyle.GetTextDecoration() == textStyle.GetTextDecoration() ?
72         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
73     updateType |= static_cast<uint32_t>(declarationStyle.GetTextDecorationStyle() ==
74         textStyle.GetTextDecorationStyle() ? UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
75     updateType |= static_cast<uint32_t>(declarationStyle.GetTextDecorationColor() ==
76         textStyle.GetTextDecorationColor() ? UpdateRenderType::NONE : UpdateRenderType::PAINT);
77     updateType |= static_cast<uint32_t>(declarationStyle.GetFontStyle() == textStyle.GetFontStyle() ?
78         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
79     updateType |= static_cast<uint32_t>(declarationStyle.GetFontWeight() == textStyle.GetFontWeight() ?
80         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
81     updateType |= static_cast<uint32_t>(declarationStyle.GetTextCase() == textStyle.GetTextCase() ?
82         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
83     return updateType;
84 }
85 
GetData() const86 const std::string& TextComponent::GetData() const
87 {
88     return declaration_->GetData();
89 }
90 
SetData(const std::string & data)91 void TextComponent::SetData(const std::string& data)
92 {
93     declaration_->SetData(data);
94 }
95 
GetTextStyle() const96 const TextStyle& TextComponent::GetTextStyle() const
97 {
98     return declaration_->GetTextStyle();
99 }
100 
SetTextStyle(const TextStyle & textStyle)101 void TextComponent::SetTextStyle(const TextStyle& textStyle)
102 {
103     declaration_->SetTextStyle(textStyle);
104 }
105 
GetAlignment() const106 const std::optional<TextAlign>& TextComponent::GetAlignment() const
107 {
108     return alignment_;
109 }
110 
SetAlignment(const TextAlign & alignment)111 void TextComponent::SetAlignment(const TextAlign& alignment)
112 {
113     alignment_ = alignment;
114 }
115 
GetFocusColor() const116 const Color& TextComponent::GetFocusColor() const
117 {
118     return declaration_->GetFocusColor();
119 }
120 
SetFocusColor(const Color & focusColor)121 void TextComponent::SetFocusColor(const Color& focusColor)
122 {
123     declaration_->SetFocusColor(focusColor);
124 }
125 
GetCopyOption() const126 const CopyOptions& TextComponent::GetCopyOption() const
127 {
128     return declaration_->GetCopyOption();
129 }
130 
SetCopyOption(const CopyOptions & copyOption)131 void TextComponent::SetCopyOption(const CopyOptions& copyOption)
132 {
133     declaration_->SetCopyOption(copyOption);
134 }
135 
GetMaxWidthLayout() const136 bool TextComponent::GetMaxWidthLayout() const
137 {
138     return declaration_->IsMaxWidthLayout();
139 }
140 
SetMaxWidthLayout(bool isMaxWidthLayout)141 void TextComponent::SetMaxWidthLayout(bool isMaxWidthLayout)
142 {
143     declaration_->SetIsMaxWidthLayout(isMaxWidthLayout);
144 }
145 
GetAutoMaxLines() const146 bool TextComponent::GetAutoMaxLines() const
147 {
148     return declaration_->GetAutoMaxLines();
149 }
150 
SetAutoMaxLines(bool autoMaxLines)151 void TextComponent::SetAutoMaxLines(bool autoMaxLines)
152 {
153     declaration_->SetAutoMaxLines(autoMaxLines);
154 }
155 
IsChanged() const156 bool TextComponent::IsChanged() const
157 {
158     return declaration_->IsChanged();
159 }
160 
SetIsChanged(bool isChanged)161 void TextComponent::SetIsChanged(bool isChanged)
162 {
163     declaration_->SetIsChanged(isChanged);
164 }
165 
SetOnClick(const EventMarker & onClick)166 void TextComponent::SetOnClick(const EventMarker& onClick)
167 {
168     declaration_->SetClickEvent(onClick);
169 }
170 
SetRemoteMessageEvent(const EventMarker & eventId)171 void TextComponent::SetRemoteMessageEvent(const EventMarker& eventId)
172 {
173     declaration_->SetRemoteMessageEvent(eventId);
174 }
175 
SetDeclaration(const RefPtr<TextDeclaration> & declaration)176 void TextComponent::SetDeclaration(const RefPtr<TextDeclaration>& declaration)
177 {
178     if (declaration) {
179         declaration_ = declaration;
180     }
181 }
182 
GetDeclaration() const183 const RefPtr<TextDeclaration>& TextComponent::GetDeclaration() const
184 {
185     return declaration_;
186 }
187 
GetDeclarationHeight() const188 Dimension TextComponent::GetDeclarationHeight() const
189 {
190     auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
191     if (sizeStyle.IsValid()) {
192         return sizeStyle.height;
193     }
194     return Dimension(-1.0);
195 }
196 
197 } // namespace OHOS::Ace
198