• 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     for (const auto& child : GetChildList()) {
35         auto domSpan = AceType::DynamicCast<DOMSpan>(child);
36         if (!domSpan) {
37             LOGW("text only support span child");
38             continue;
39         }
40         auto spanComponent = AceType::DynamicCast<TextSpanComponent>(domSpan->GetSpecializedComponent());
41         if (!spanComponent) {
42             LOGW("text only support span child");
43             continue;
44         }
45 
46         // If span component has no developer-set styles, then set text styles to span
47         TextStyle spanStyle = spanComponent->GetTextStyle();
48         CheckAndSetSpanStyle(domSpan, spanStyle);
49         domSpan->SetTextStyle(spanStyle);
50         spanComponent->SetTextStyle(spanStyle);
51     }
52 }
53 
CheckAndSetSpanStyle(const RefPtr<DOMSpan> & dmoSpan,TextStyle & spanStyle)54 void DOMText::CheckAndSetSpanStyle(const RefPtr<DOMSpan>& dmoSpan, TextStyle& spanStyle)
55 {
56     auto textDeclaration = AceType::DynamicCast<TextDeclaration>(declaration_);
57     if (!textDeclaration) {
58         return;
59     }
60 
61     const auto& textStyle = textDeclaration->GetTextStyle();
62     if (!dmoSpan->HasSetFontSize()) {
63         LOGD("Set Text Font Size to Span");
64         spanStyle.SetFontSize(textStyle.GetFontSize());
65     }
66     if (!dmoSpan->HasSetFontStyle()) {
67         LOGD("Set Text Font Style to Span");
68         spanStyle.SetFontStyle(textStyle.GetFontStyle());
69     }
70     if (!dmoSpan->HasSetFontColor()) {
71         LOGD("Set Text Font Color to Span");
72         spanStyle.SetTextColor(textStyle.GetTextColor());
73     }
74     if (!dmoSpan->HasSetFontWeight()) {
75         LOGD("Set Text Font Weight to Span");
76         spanStyle.SetFontWeight(textStyle.GetFontWeight());
77     }
78     if (!dmoSpan->HasSetTextDecoration()) {
79         LOGD("Set Text Decoration to Span");
80         spanStyle.SetTextDecoration(textStyle.GetTextDecoration());
81     }
82     if (!dmoSpan->HasSetFontFamily()) {
83         LOGD("Set Text Font Family to Span");
84         spanStyle.SetFontFamilies(textStyle.GetFontFamilies());
85     }
86     if (!dmoSpan->HasSetAllowScale()) {
87         LOGD("Set Allow Scale to Span");
88         spanStyle.SetAllowScale(textStyle.IsAllowScale());
89     }
90     if (!dmoSpan->HasSetFontFeatures()) {
91         LOGD("Set Font Feature Settings to Span");
92         spanStyle.SetFontFeatures(textStyle.GetFontFeatures());
93     }
94     spanStyle.SetLetterSpacing(textStyle.GetLetterSpacing());
95     spanStyle.SetLineHeight(textStyle.GetLineHeight(), textStyle.HasHeightOverride());
96 }
97 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)98 void DOMText::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
99 {
100     ACE_DCHECK(child);
101     auto spanComponent = AceType::DynamicCast<TextSpanComponent>(child->GetSpecializedComponent());
102     if (!spanComponent) {
103         LOGW("text only support span child");
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     textChild_->SetDeclaration(textDeclaration);
143 
144     // set box align
145     if (!textDeclaration->IsMaxWidthLayout()) {
146         SetBoxAlignForText();
147     }
148 }
149 
SetBoxAlignForText()150 void DOMText::SetBoxAlignForText()
151 {
152     auto textDeclaration = AceType::DynamicCast<TextDeclaration>(declaration_);
153     if (!textDeclaration) {
154         return;
155     }
156 
157     switch (textDeclaration->GetTextStyle().GetTextAlign()) {
158         case TextAlign::LEFT:
159             SetAlignment(Alignment::CENTER_LEFT);
160             break;
161         case TextAlign::CENTER:
162             SetAlignment(Alignment::CENTER);
163             break;
164         case TextAlign::RIGHT:
165             SetAlignment(Alignment::CENTER_RIGHT);
166             break;
167         case TextAlign::START:
168             SetAlignment(IsRightToLeft() ? Alignment::CENTER_RIGHT : Alignment::CENTER_LEFT);
169             break;
170         case TextAlign::END:
171             SetAlignment(IsRightToLeft() ? Alignment::CENTER_LEFT : Alignment::CENTER_RIGHT);
172             break;
173         default:
174             break;
175     }
176 }
177 
178 } // namespace OHOS::Ace::Framework
179