• 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_span.h"
17 
18 #include "core/common/ace_application_info.h"
19 #include "core/components/declaration/span/span_declaration.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21 
22 namespace OHOS::Ace::Framework {
23 
DOMSpan(NodeId nodeId,const std::string & nodeName)24 DOMSpan::DOMSpan(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
25 {
26     textSpanChild_ = AceType::MakeRefPtr<TextSpanComponent>("");
27     // span has no box component.
28     boxComponent_.Reset();
29 }
30 
ResetInitializedStyle()31 void DOMSpan::ResetInitializedStyle()
32 {
33     for (const auto& child : GetChildList()) {
34         auto domSpan = AceType::DynamicCast<DOMSpan>(child);
35         if (!domSpan) {
36             LOGW("span only support span child");
37             continue;
38         }
39         auto spanComponent = AceType::DynamicCast<TextSpanComponent>(domSpan->GetSpecializedComponent());
40         if (!spanComponent) {
41             LOGW("span only support span child");
42             continue;
43         }
44 
45         // If span component has no developer-set styles, then set text styles to span
46         TextStyle spanStyle = spanComponent->GetTextStyle();
47         CheckAndSetCurrentSpanStyle(domSpan, spanStyle, textSpanChild_->GetTextStyle());
48         domSpan->SetTextStyle(spanStyle);
49         spanComponent->SetTextStyle(spanStyle);
50     }
51 }
52 
CheckAndSetCurrentSpanStyle(const RefPtr<DOMSpan> & domSpan,TextStyle & currentStyle,const TextStyle & parentStyle)53 void DOMSpan::CheckAndSetCurrentSpanStyle(
54     const RefPtr<DOMSpan>& domSpan, TextStyle& currentStyle, const TextStyle& parentStyle)
55 {
56     if (!domSpan->HasSetFontColor()) {
57         currentStyle.SetTextColor(parentStyle.GetTextColor());
58     }
59     if (!domSpan->HasSetFontSize()) {
60         currentStyle.SetFontSize(parentStyle.GetFontSize());
61     }
62     if (!domSpan->HasSetFontStyle()) {
63         currentStyle.SetFontStyle(parentStyle.GetFontStyle());
64     }
65     if (!domSpan->HasSetFontWeight()) {
66         currentStyle.SetFontWeight(parentStyle.GetFontWeight());
67     }
68     if (!domSpan->HasSetTextDecoration()) {
69         currentStyle.SetTextDecoration(parentStyle.GetTextDecoration());
70     }
71     if (!domSpan->HasSetFontFamily()) {
72         currentStyle.SetFontFamilies(parentStyle.GetFontFamilies());
73     }
74     if (!domSpan->HasSetAllowScale()) {
75         currentStyle.SetAllowScale(parentStyle.IsAllowScale());
76     }
77     if (!domSpan->HasSetFontFeatures()) {
78         currentStyle.SetFontFeatures(parentStyle.GetFontFeatures());
79     }
80     currentStyle.SetLetterSpacing(parentStyle.GetLetterSpacing());
81     currentStyle.SetLineHeight(parentStyle.GetLineHeight(), parentStyle.HasHeightOverride());
82 }
83 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)84 void DOMSpan::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
85 {
86     if (!child) {
87         return;
88     }
89 
90     if (child->GetTag() == DOM_NODE_TAG_SPAN) {
91         // Get current span's parent (also span) styles, if no user setting styles
92         // on this span, use its parent styles.
93         TextStyle parentSpanStyle = textSpanChild_->GetTextStyle();
94         auto spanComponent = AceType::DynamicCast<TextSpanComponent>(child->GetSpecializedComponent());
95         if (!spanComponent) {
96             LOGE("DOMSpan: span is null");
97             return;
98         }
99 
100         // Get current span's styles to compare with its parent span.
101         TextStyle currentSpanStyle = spanComponent->GetTextStyle();
102         auto domSpan = AceType::DynamicCast<DOMSpan>(child);
103         CheckAndSetCurrentSpanStyle(domSpan, currentSpanStyle, parentSpanStyle);
104         domSpan->SetTextStyle(currentSpanStyle);
105         spanComponent->SetTextStyle(currentSpanStyle);
106         textSpanChild_->InsertChild(slot, child->GetRootComponent());
107     }
108 }
109 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)110 void DOMSpan::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
111 {
112     if (!child) {
113         return;
114     }
115 
116     textSpanChild_->RemoveChild(child->GetRootComponent());
117 }
118 
PrepareSpecializedComponent()119 void DOMSpan::PrepareSpecializedComponent()
120 {
121     auto spanDeclaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
122     if (!spanDeclaration) {
123         return;
124     }
125     auto& specializedStyle = declaration_->MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
126     if (specializedStyle.IsValid()) {
127         bool isCard = AceApplicationInfo::GetInstance().GetIsCardType();
128         if (isCard) {
129             specializedStyle.spanStyle.SetAllowScale(false);
130             if (specializedStyle.spanStyle.GetFontSize().Unit() == DimensionUnit::FP) {
131                 specializedStyle.spanStyle.SetAllowScale(true);
132             }
133         }
134     }
135 
136     textSpanChild_->SetDeclaration(spanDeclaration);
137 }
138 
139 } // namespace OHOS::Ace::Framework
140