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 continue;
37 }
38 auto spanComponent = AceType::DynamicCast<TextSpanComponent>(domSpan->GetSpecializedComponent());
39 if (!spanComponent) {
40 continue;
41 }
42
43 // If span component has no developer-set styles, then set text styles to span
44 TextStyle spanStyle = spanComponent->GetTextStyle();
45 CheckAndSetCurrentSpanStyle(domSpan, spanStyle, textSpanChild_->GetTextStyle());
46 domSpan->SetTextStyle(spanStyle);
47 spanComponent->SetTextStyle(spanStyle);
48 }
49 }
50
CheckAndSetCurrentSpanStyle(const RefPtr<DOMSpan> & domSpan,TextStyle & currentStyle,const TextStyle & parentStyle)51 void DOMSpan::CheckAndSetCurrentSpanStyle(
52 const RefPtr<DOMSpan>& domSpan, TextStyle& currentStyle, const TextStyle& parentStyle)
53 {
54 if (!domSpan->HasSetFontColor()) {
55 currentStyle.SetTextColor(parentStyle.GetTextColor());
56 }
57 if (!domSpan->HasSetFontSize()) {
58 currentStyle.SetFontSize(parentStyle.GetFontSize());
59 }
60 if (!domSpan->HasSetFontStyle()) {
61 currentStyle.SetFontStyle(parentStyle.GetFontStyle());
62 }
63 if (!domSpan->HasSetFontWeight()) {
64 currentStyle.SetFontWeight(parentStyle.GetFontWeight());
65 }
66 if (!domSpan->HasSetTextDecoration()) {
67 currentStyle.SetTextDecoration(parentStyle.GetTextDecoration());
68 }
69 if (!domSpan->HasSetTextDecorationColor()) {
70 currentStyle.SetTextDecorationColor(parentStyle.GetTextDecorationColor());
71 }
72 if (!domSpan->HasSetTextDecorationStyle()) {
73 currentStyle.SetTextDecorationStyle(parentStyle.GetTextDecorationStyle());
74 }
75 if (!domSpan->HasSetFontFamily()) {
76 currentStyle.SetFontFamilies(parentStyle.GetFontFamilies());
77 }
78 if (!domSpan->HasSetAllowScale()) {
79 currentStyle.SetAllowScale(parentStyle.IsAllowScale());
80 }
81 if (!domSpan->HasSetFontFeatures()) {
82 currentStyle.SetFontFeatures(parentStyle.GetFontFeatures());
83 }
84 currentStyle.SetLetterSpacing(parentStyle.GetLetterSpacing());
85 currentStyle.SetLineHeight(parentStyle.GetLineHeight(), parentStyle.HasHeightOverride());
86 }
87
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)88 void DOMSpan::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
89 {
90 if (!child) {
91 return;
92 }
93
94 if (child->GetTag() == DOM_NODE_TAG_SPAN) {
95 // Get current span's parent (also span) styles, if no user setting styles
96 // on this span, use its parent styles.
97 TextStyle parentSpanStyle = textSpanChild_->GetTextStyle();
98 auto spanComponent = AceType::DynamicCast<TextSpanComponent>(child->GetSpecializedComponent());
99 if (!spanComponent) {
100 return;
101 }
102
103 // Get current span's styles to compare with its parent span.
104 TextStyle currentSpanStyle = spanComponent->GetTextStyle();
105 auto domSpan = AceType::DynamicCast<DOMSpan>(child);
106 CheckAndSetCurrentSpanStyle(domSpan, currentSpanStyle, parentSpanStyle);
107 domSpan->SetTextStyle(currentSpanStyle);
108 spanComponent->SetTextStyle(currentSpanStyle);
109 textSpanChild_->InsertChild(slot, child->GetRootComponent());
110 }
111 }
112
OnChildNodeRemoved(const RefPtr<DOMNode> & child)113 void DOMSpan::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
114 {
115 if (!child) {
116 return;
117 }
118
119 textSpanChild_->RemoveChild(child->GetRootComponent());
120 }
121
PrepareSpecializedComponent()122 void DOMSpan::PrepareSpecializedComponent()
123 {
124 auto spanDeclaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
125 if (!spanDeclaration) {
126 return;
127 }
128 auto& specializedStyle = declaration_->MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
129 if (specializedStyle.IsValid()) {
130 bool isCard = AceApplicationInfo::GetInstance().GetIsCardType();
131 if (isCard) {
132 specializedStyle.spanStyle.SetAllowScale(false);
133 if (specializedStyle.spanStyle.GetFontSize().Unit() == DimensionUnit::FP) {
134 specializedStyle.spanStyle.SetAllowScale(true);
135 }
136 }
137 }
138
139 textSpanChild_->SetDeclaration(spanDeclaration);
140 }
141
142 } // namespace OHOS::Ace::Framework
143