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_CORE_COMPONENTS_SVG_PARSE_SVG_DEFS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_PARSE_SVG_DEFS_H
18 
19 #ifndef USE_ROSEN_DRAWING
20 #include "include/pathops/SkPathOps.h"
21 #endif
22 
23 #include "frameworks/core/components/svg/parse/svg_mask.h"
24 #include "frameworks/core/components/svg/parse/svg_pattern.h"
25 #include "frameworks/core/components/svg/parse/svg_filter.h"
26 
27 namespace OHOS::Ace {
28 
29 class SvgDefs : public SvgNode {
30     DECLARE_ACE_TYPE(SvgDefs, SvgNode);
31 
32 public:
33     SvgDefs() = default;
34     ~SvgDefs() override = default;
35 
Create()36     static RefPtr<SvgNode> Create()
37     {
38         return AceType::MakeRefPtr<SvgDefs>();
39     }
40 
41     RefPtr<RenderNode> CreateRender(
42         const LayoutParam& layoutParam, const RefPtr<SvgBaseDeclaration>& parent, bool useBox = true) override
43     {
44         auto svgContext = svgContext_.Upgrade();
45         if (svgContext == nullptr) {
46             return nullptr;
47         }
48         auto root = svgContext->GetSvgRoot();
49         if (root == nullptr) {
50             return nullptr;
51         }
52         for (auto& child : children_) {
53             if (AceType::InstanceOf<SvgMask>(child) ||
54                 AceType::InstanceOf<SvgPattern>(child) ||
55                 AceType::InstanceOf<SvgFilter>(child)) {
56                 auto childRender = child->CreateRender(layoutParam, nullptr, useBox);
57                 if (childRender) {
58                     root->AddChild(childRender, root->GetChildren().size());
59                     child->Update(childRender);
60                 }
61             }
62         }
63         return nullptr;
64     }
65 
AppendChild(const RefPtr<SvgNode> & child)66     void AppendChild(const RefPtr<SvgNode>& child) override
67     {
68         children_.emplace_back(child);
69     }
70 
71 #ifndef USE_ROSEN_DRAWING
AsPath(const Size & viewPort)72     SkPath AsPath(const Size& viewPort) const override
73     {
74         SkPath path;
75         for (auto child : children_) {
76             const SkPath childPath = child->AsPath(viewPort);
77             Op(path, childPath, kUnion_SkPathOp, &path);
78         }
79         return path;
80     }
81 #else
AsPath(const Size & viewPort)82     RSPath AsPath(const Size& viewPort) const override
83     {
84         RSPath path;
85         for (auto child : children_) {
86             RSPath childPath = child->AsPath(viewPort);
87             path.Op(path, childPath, RSPathOp::UNION);
88         }
89         return path;
90     }
91 #endif
92 };
93 
94 } // namespace OHOS::Ace
95 
96 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_SVG_DEFS_H