• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.GetTextDecorationColor() ==
74         textStyle.GetTextDecorationColor() ? UpdateRenderType::NONE : UpdateRenderType::PAINT);
75     updateType |= static_cast<uint32_t>(declarationStyle.GetFontStyle() == textStyle.GetFontStyle() ?
76         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
77     updateType |= static_cast<uint32_t>(declarationStyle.GetFontWeight() == textStyle.GetFontWeight() ?
78         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
79     updateType |= static_cast<uint32_t>(declarationStyle.GetTextCase() == textStyle.GetTextCase() ?
80         UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
81     return updateType;
82 }
83 
GetData() const84 const std::string& TextComponent::GetData() const
85 {
86     return declaration_->GetData();
87 }
88 
SetData(const std::string & data)89 void TextComponent::SetData(const std::string& data)
90 {
91     declaration_->SetData(data);
92 }
93 
GetTextStyle() const94 const TextStyle& TextComponent::GetTextStyle() const
95 {
96     return declaration_->GetTextStyle();
97 }
98 
SetTextStyle(const TextStyle & textStyle)99 void TextComponent::SetTextStyle(const TextStyle& textStyle)
100 {
101     declaration_->SetTextStyle(textStyle);
102 }
103 
GetAlignment() const104 const std::optional<TextAlign>& TextComponent::GetAlignment() const
105 {
106     return alignment_;
107 }
108 
SetAlignment(const TextAlign & alignment)109 void TextComponent::SetAlignment(const TextAlign& alignment)
110 {
111     alignment_ = alignment;
112 }
113 
GetFocusColor() const114 const Color& TextComponent::GetFocusColor() const
115 {
116     return declaration_->GetFocusColor();
117 }
118 
SetFocusColor(const Color & focusColor)119 void TextComponent::SetFocusColor(const Color& focusColor)
120 {
121     declaration_->SetFocusColor(focusColor);
122 }
123 
GetCopyOption() const124 const CopyOptions& TextComponent::GetCopyOption() const
125 {
126     return declaration_->GetCopyOption();
127 }
128 
SetCopyOption(const CopyOptions & copyOption)129 void TextComponent::SetCopyOption(const CopyOptions& copyOption)
130 {
131     declaration_->SetCopyOption(copyOption);
132 }
133 
GetMaxWidthLayout() const134 bool TextComponent::GetMaxWidthLayout() const
135 {
136     return declaration_->IsMaxWidthLayout();
137 }
138 
SetMaxWidthLayout(bool isMaxWidthLayout)139 void TextComponent::SetMaxWidthLayout(bool isMaxWidthLayout)
140 {
141     declaration_->SetIsMaxWidthLayout(isMaxWidthLayout);
142 }
143 
GetAutoMaxLines() const144 bool TextComponent::GetAutoMaxLines() const
145 {
146     return declaration_->GetAutoMaxLines();
147 }
148 
SetAutoMaxLines(bool autoMaxLines)149 void TextComponent::SetAutoMaxLines(bool autoMaxLines)
150 {
151     declaration_->SetAutoMaxLines(autoMaxLines);
152 }
153 
IsChanged() const154 bool TextComponent::IsChanged() const
155 {
156     return declaration_->IsChanged();
157 }
158 
SetIsChanged(bool isChanged)159 void TextComponent::SetIsChanged(bool isChanged)
160 {
161     declaration_->SetIsChanged(isChanged);
162 }
163 
SetOnClick(const EventMarker & onClick)164 void TextComponent::SetOnClick(const EventMarker& onClick)
165 {
166     declaration_->SetClickEvent(onClick);
167 }
168 
SetRemoteMessageEvent(const EventMarker & eventId)169 void TextComponent::SetRemoteMessageEvent(const EventMarker& eventId)
170 {
171     declaration_->SetRemoteMessageEvent(eventId);
172 }
173 
SetDeclaration(const RefPtr<TextDeclaration> & declaration)174 void TextComponent::SetDeclaration(const RefPtr<TextDeclaration>& declaration)
175 {
176     if (declaration) {
177         declaration_ = declaration;
178     }
179 }
180 
GetDeclaration() const181 const RefPtr<TextDeclaration>& TextComponent::GetDeclaration() const
182 {
183     return declaration_;
184 }
185 
GetDeclarationHeight() const186 Dimension TextComponent::GetDeclarationHeight() const
187 {
188     auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
189     if (sizeStyle.IsValid()) {
190         return sizeStyle.height;
191     }
192     return Dimension(-1.0);
193 }
194 
195 } // namespace OHOS::Ace
196