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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_SPAN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_SPAN_H
18 
19 #include "core/components/declaration/span/span_declaration.h"
20 #include "core/components/text_span/text_span_component.h"
21 #include "frameworks/bridge/common/dom/dom_node.h"
22 #include "frameworks/bridge/common/dom/dom_type.h"
23 
24 namespace OHOS::Ace::Framework {
25 
26 class DOMSpan final : public DOMNode {
27     DECLARE_ACE_TYPE(DOMSpan, DOMNode);
28 
29 public:
30     DOMSpan(NodeId nodeId, const std::string& nodeName);
31     ~DOMSpan() override = default;
32 
HasSetFontStyle()33     bool HasSetFontStyle() const
34     {
35         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
36         return declaration ? declaration->HasSetFontStyle() : false;
37     }
38 
HasSetFontColor()39     bool HasSetFontColor() const
40     {
41         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
42         return declaration ? declaration->HasSetFontColor() : false;
43     }
44 
HasSetFontWeight()45     bool HasSetFontWeight() const
46     {
47         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
48         return declaration ? declaration->HasSetFontWeight() : false;
49     }
50 
HasSetFontSize()51     bool HasSetFontSize() const
52     {
53         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
54         return declaration ? declaration->HasSetFontSize() : false;
55     }
56 
HasSetFontFamily()57     bool HasSetFontFamily() const
58     {
59         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
60         return declaration ? declaration->HasSetFontFamily() : false;
61     }
62 
HasSetTextDecoration()63     bool HasSetTextDecoration() const
64     {
65         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
66         return declaration ? declaration->HasSetTextDecoration() : false;
67     }
68 
HasSetTextDecorationColor()69     bool HasSetTextDecorationColor() const
70     {
71         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
72         return declaration ? declaration->HasSetTextDecorationColor() : false;
73     }
74 
HasSetTextDecorationStyle()75     bool HasSetTextDecorationStyle() const
76     {
77         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
78         return declaration ? declaration->HasSetTextDecorationStyle() : false;
79     }
80 
HasSetAllowScale()81     bool HasSetAllowScale() const
82     {
83         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
84         return declaration ? declaration->HasSetAllowScale() : false;
85     }
86 
HasSetFontFeatures()87     bool HasSetFontFeatures() const
88     {
89         auto declaration = AceType::DynamicCast<SpanDeclaration>(declaration_);
90         return declaration ? declaration->HasSetFontFeatures() : false;
91     }
92 
SetTextStyle(const TextStyle & spanStyle)93     void SetTextStyle(const TextStyle& spanStyle)
94     {
95         if (declaration_) {
96             auto& specializedStyle = declaration_->MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
97             if (specializedStyle.IsValid()) {
98                 specializedStyle.spanStyle = spanStyle;
99             }
100         }
101     }
102 
GetSpecializedComponent()103     RefPtr<Component> GetSpecializedComponent() override
104     {
105         return textSpanChild_;
106     }
107 
108 protected:
109     void PrepareSpecializedComponent() override;
110     void OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot) override;
111     void OnChildNodeRemoved(const RefPtr<DOMNode>& child) override;
112     void ResetInitializedStyle() override;
113 
114 private:
115     void CheckAndSetCurrentSpanStyle(
116         const RefPtr<DOMSpan>& domSpan, TextStyle& currentStyle, const TextStyle& parentStyle);
117 
118     std::list<RefPtr<DOMNode>> children_;
119     RefPtr<TextSpanComponent> textSpanChild_;
120 };
121 
122 } // namespace OHOS::Ace::Framework
123 
124 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_SPAN_H
125