• 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 "frameworks/bridge/common/dom/dom_text.h"
17 
18 #include "core/common/ace_application_info.h"
19 #include "core/components/declaration/text/text_declaration.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21 
22 namespace OHOS::Ace::Framework {
23 
DOMText(NodeId nodeId,const std::string & nodeName)24 DOMText::DOMText(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
25 {
26     textChild_ = AceType::MakeRefPtr<TextComponent>("");
27 }
28 
ResetInitializedStyle()29 void DOMText::ResetInitializedStyle()
30 {
31     if (declaration_) {
32         declaration_->InitializeStyle();
33     }
34     CheckAndSetAllSpanStyle();
35 }
36 
CheckAndSetAllSpanStyle()37 void DOMText::CheckAndSetAllSpanStyle()
38 {
39     for (const auto& child : GetChildList()) {
40         auto domSpan = AceType::DynamicCast<DOMSpan>(child);
41         if (!domSpan) {
42             continue;
43         }
44         auto spanComponent = AceType::DynamicCast<TextSpanComponent>(domSpan->GetSpecializedComponent());
45         if (!spanComponent) {
46             continue;
47         }
48 
49         // If span component has no developer-set styles, then set text styles to span
50         TextStyle spanStyle = spanComponent->GetTextStyle();
51         CheckAndSetSpanStyle(domSpan, spanStyle);
52         domSpan->SetTextStyle(spanStyle);
53         spanComponent->SetTextStyle(spanStyle);
54     }
55 }
56 
CheckAndSetSpanStyle(const RefPtr<DOMSpan> & dmoSpan,TextStyle & spanStyle)57 void DOMText::CheckAndSetSpanStyle(const RefPtr<DOMSpan>& dmoSpan, TextStyle& spanStyle)
58 {
59     auto textDeclaration = AceType::DynamicCast<TextDeclaration>(declaration_);
60     if (!textDeclaration) {
61         return;
62     }
63 
64     const auto& textStyle = textDeclaration->GetTextStyle();
65     if (!dmoSpan->HasSetFontSize()) {
66         spanStyle.SetFontSize(textStyle.GetFontSize());
67     }
68     if (!dmoSpan->HasSetFontStyle()) {
69         spanStyle.SetFontStyle(textStyle.GetFontStyle());
70     }
71     if (!dmoSpan->HasSetFontColor()) {
72         spanStyle.SetTextColor(textStyle.GetTextColor());
73     }
74     if (!dmoSpan->HasSetFontWeight()) {
75         spanStyle.SetFontWeight(textStyle.GetFontWeight());
76     }
77     if (!dmoSpan->HasSetTextDecoration()) {
78         spanStyle.SetTextDecoration(textStyle.GetTextDecoration());
79     }
80     if (!dmoSpan->HasSetTextDecorationColor()) {
81         spanStyle.SetTextDecorationColor(textStyle.GetTextDecorationColor());
82     }
83     if (!dmoSpan->HasSetTextDecorationStyle()) {
84         spanStyle.SetTextDecorationStyle(textStyle.GetTextDecorationStyle());
85     }
86     if (!dmoSpan->HasSetFontFamily()) {
87         spanStyle.SetFontFamilies(textStyle.GetFontFamilies());
88     }
89     if (!dmoSpan->HasSetAllowScale()) {
90         spanStyle.SetAllowScale(textStyle.IsAllowScale());
91     }
92     if (!dmoSpan->HasSetFontFeatures()) {
93         spanStyle.SetFontFeatures(textStyle.GetFontFeatures());
94     }
95     spanStyle.SetLetterSpacing(textStyle.GetLetterSpacing());
96     spanStyle.SetLineHeight(textStyle.GetLineHeight(), textStyle.HasHeightOverride());
97 }
98 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)99 void DOMText::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
100 {
101     ACE_DCHECK(child);
102     auto spanComponent = AceType::DynamicCast<TextSpanComponent>(child->GetSpecializedComponent());
103     if (!spanComponent) {
104         return;
105     }
106 
107     // If span component has no developer-set styles, then set text styles to span
108     TextStyle spanStyle = spanComponent->GetTextStyle();
109     auto domSpan = AceType::DynamicCast<DOMSpan>(child);
110     ACE_DCHECK(domSpan);
111     CheckAndSetSpanStyle(domSpan, spanStyle);
112     domSpan->SetTextStyle(spanStyle);
113     spanComponent->SetTextStyle(spanStyle);
114     textChild_->InsertChild(slot, child->GetRootComponent());
115 }
116 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)117 void DOMText::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
118 {
119     ACE_DCHECK(child);
120     textChild_->RemoveChild(child->GetRootComponent());
121 }
122 
PrepareSpecializedComponent()123 void DOMText::PrepareSpecializedComponent()
124 {
125     textChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
126 
127     auto textDeclaration = AceType::DynamicCast<TextDeclaration>(declaration_);
128     if (!textDeclaration) {
129         return;
130     }
131     bool isCard = AceApplicationInfo::GetInstance().GetIsCardType();
132     if (isCard) {
133         auto& style = textDeclaration->MaybeResetStyle<TextSpecializedStyle>(StyleTag::SPECIALIZED_STYLE);
134         if (style.IsValid()) {
135             style.textStyle.SetAllowScale(false);
136             if (style.textStyle.GetFontSize().Unit() == DimensionUnit::FP) {
137                 style.textStyle.SetAllowScale(true);
138             }
139         }
140     }
141     textDeclaration->SetIsMaxWidthLayout(boxComponent_->GetWidthDimension().IsValid());
142     CheckAndSetAllSpanStyle();
143     textChild_->SetDeclaration(textDeclaration);
144 
145     // set box align
146     if (!textDeclaration->IsMaxWidthLayout()) {
147         SetBoxAlignForText();
148     }
149 }
150 
SetBoxAlignForText()151 void DOMText::SetBoxAlignForText()
152 {
153     auto textDeclaration = AceType::DynamicCast<TextDeclaration>(declaration_);
154     if (!textDeclaration) {
155         return;
156     }
157 
158     switch (textDeclaration->GetTextStyle().GetTextAlign()) {
159         case TextAlign::LEFT:
160             SetAlignment(Alignment::CENTER_LEFT);
161             break;
162         case TextAlign::CENTER:
163             SetAlignment(Alignment::CENTER);
164             break;
165         case TextAlign::RIGHT:
166             SetAlignment(Alignment::CENTER_RIGHT);
167             break;
168         case TextAlign::START:
169             SetAlignment(IsRightToLeft() ? Alignment::CENTER_RIGHT : Alignment::CENTER_LEFT);
170             break;
171         case TextAlign::END:
172             SetAlignment(IsRightToLeft() ? Alignment::CENTER_LEFT : Alignment::CENTER_RIGHT);
173             break;
174         default:
175             break;
176     }
177 }
178 
179 } // namespace OHOS::Ace::Framework
180