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_svg.h"
17 #include "frameworks/bridge/common/dom/dom_svg_animate.h"
18 
19 namespace OHOS::Ace::Framework {
20 
DOMSvg(NodeId nodeId,const std::string & nodeName)21 DOMSvg::DOMSvg(NodeId nodeId, const std::string& nodeName) : DOMSvgBase(nodeId, nodeName)
22 {
23     transformComponent_ = AceType::MakeRefPtr<TransformComponent>();
24     if (declaration_) {
25         declaration_->SetHasDisplayStyle(true);
26         auto& overflowStyle = declaration_->MaybeResetStyle<CommonOverflowStyle>(StyleTag::COMMON_OVERFLOW_STYLE);
27         if (overflowStyle.IsValid()) {
28             overflowStyle.overflow = Overflow::CLIP;
29         }
30     }
31 }
32 
GetSpecializedComponent()33 RefPtr<Component> DOMSvg::GetSpecializedComponent()
34 {
35     return svgComponent_;
36 }
37 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)38 void DOMSvg::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
39 {
40     if (!child) {
41         return;
42     }
43     if (AceType::InstanceOf<SvgAnimateComponent>(child->GetSpecializedComponent())) {
44         svgComponent_->InsertChild(slot, child->GetSpecializedComponent());
45     } else {
46         svgComponent_->InsertChild(slot, child->GetRootComponent());
47     }
48 }
49 
OnMounted(const RefPtr<DOMNode> & parentNode)50 void DOMSvg::OnMounted(const RefPtr<DOMNode>& parentNode)
51 {
52     auto declaration = AceType::DynamicCast<SvgDeclaration>(declaration_);
53     if (!declaration) {
54         return;
55     }
56     auto svgNode = AceType::DynamicCast<DOMSvg>(parentNode);
57     if (svgNode) {
58         declaration->Inherit(svgNode->GetDeclaration());
59         svgComponent_->MarkIsRoot(false);
60     } else {
61         svgComponent_->MarkIsRoot(true);
62     }
63     auto box = GetBoxComponent();
64     if (box) {
65         box->SetOverflow(Overflow::FORCE_CLIP);
66     }
67 }
68 
PrepareSpecializedComponent()69 void DOMSvg::PrepareSpecializedComponent()
70 {
71     if (!svgComponent_) {
72         svgComponent_ = AceType::MakeRefPtr<SvgComponent>();
73     }
74     auto declaration = AceType::DynamicCast<SvgDeclaration>(declaration_);
75     if (declaration) {
76         svgComponent_->SetDeclaration(declaration);
77     }
78 }
79 
80 } // namespace OHOS::Ace::Framework
81