• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
GetFocusColor() const104 const Color& TextComponent::GetFocusColor() const
105 {
106     return declaration_->GetFocusColor();
107 }
108 
SetFocusColor(const Color & focusColor)109 void TextComponent::SetFocusColor(const Color& focusColor)
110 {
111     declaration_->SetFocusColor(focusColor);
112 }
113 
GetMaxWidthLayout() const114 bool TextComponent::GetMaxWidthLayout() const
115 {
116     return declaration_->IsMaxWidthLayout();
117 }
118 
SetMaxWidthLayout(bool isMaxWidthLayout)119 void TextComponent::SetMaxWidthLayout(bool isMaxWidthLayout)
120 {
121     declaration_->SetIsMaxWidthLayout(isMaxWidthLayout);
122 }
123 
GetAutoMaxLines() const124 bool TextComponent::GetAutoMaxLines() const
125 {
126     return declaration_->GetAutoMaxLines();
127 }
128 
SetAutoMaxLines(bool autoMaxLines)129 void TextComponent::SetAutoMaxLines(bool autoMaxLines)
130 {
131     declaration_->SetAutoMaxLines(autoMaxLines);
132 }
133 
IsChanged() const134 bool TextComponent::IsChanged() const
135 {
136     return declaration_->IsChanged();
137 }
138 
SetIsChanged(bool isChanged)139 void TextComponent::SetIsChanged(bool isChanged)
140 {
141     declaration_->SetIsChanged(isChanged);
142 }
143 
SetOnClick(const EventMarker & onClick)144 void TextComponent::SetOnClick(const EventMarker& onClick)
145 {
146     declaration_->SetClickEvent(onClick);
147 }
148 
SetRemoteMessageEvent(const EventMarker & eventId)149 void TextComponent::SetRemoteMessageEvent(const EventMarker& eventId)
150 {
151     declaration_->SetRemoteMessageEvent(eventId);
152 }
153 
SetDeclaration(const RefPtr<TextDeclaration> & declaration)154 void TextComponent::SetDeclaration(const RefPtr<TextDeclaration>& declaration)
155 {
156     if (declaration) {
157         declaration_ = declaration;
158     }
159 }
160 
GetDeclaration() const161 const RefPtr<TextDeclaration>& TextComponent::GetDeclaration() const
162 {
163     return declaration_;
164 }
165 
GetDeclarationHeight() const166 Dimension TextComponent::GetDeclarationHeight() const
167 {
168     auto& sizeStyle = static_cast<CommonSizeStyle&>(declaration_->GetStyle(StyleTag::COMMON_SIZE_STYLE));
169     if (sizeStyle.IsValid()) {
170         return sizeStyle.height;
171     }
172     return Dimension(-1.0);
173 }
174 
175 } // namespace OHOS::Ace
176