1 /*
2  * Copyright (c) 2022 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_NG_SVG_SVG_DOM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_DOM_H
18 
19 #include <memory>
20 
21 #include "src/xml/SkDOM.h"
22 #include "src/xml/SkXMLParser.h"
23 #include "src/xml/SkXMLWriter.h"
24 
25 #include "base/memory/ace_type.h"
26 #include "core/components_ng/image_provider/svg_dom_base.h"
27 #include "core/components_ng/render/canvas_image.h"
28 #include "core/components_ng/svg/parse/svg_node.h"
29 #include "core/components_ng/svg/parse/svg_style.h"
30 #include "core/components_ng/svg/svg_context.h"
31 #include "core/image/image_source_info.h"
32 
33 namespace OHOS::Ace::NG {
34 class SvgDom : public SvgDomBase {
35     DECLARE_ACE_TYPE(SvgDom, SvgDomBase);
36 
37 public:
38     SvgDom();
39     ~SvgDom() override;
40 
41     static RefPtr<SvgDom> CreateSvgDom(SkStream& svgStream, const ImageSourceInfo& src);
42 
43     void SetFuncNormalizeToPx(FuncNormalizeToPx&& funcNormalizeToPx);
44     void SetAnimationCallback(std::function<void()>&& funcAnimateFlush, const WeakPtr<CanvasImage>& imagePtr) override;
45     void SetAnimationOnFinishCallback(const std::function<void()>& onFinishCallback) override;
46     void ControlAnimation(bool play) override;
47     bool IsStatic() override;
48 
49     bool ParseSvg(SkStream& svgStream);
50 
51     void SetFillColor(const std::optional<Color>& color) override;
52     void SetSmoothEdge(float value) override;
53     void SetColorFilter(const std::optional<ImageColorFilter>& colorFilter) override;
54 
55     void DrawImage(
56         RSCanvas& canvas, const ImageFit& imageFit, const Size& layout) override;
57 
58     SizeF GetContainerSize() const override;
SetContainerSize(const SizeF & containerSize)59     void SetContainerSize(const SizeF& containerSize) override {}
60 
61     void PushAnimatorOnFinishCallback(const RefPtr<SvgNode>& root, std::function<void()> onFinishCallback);
62     std::string GetDumpInfo() override;
63 
64 protected:
65     void FitImage(RSCanvas& canvas, const ImageFit& imageFit, const Size& layout);
66     void FitViewPort(const Size& layout);
67 
68 private:
69     RefPtr<SvgNode> TranslateSvgNode(const SkDOM& dom, const SkDOM::Node* xmlNode, const RefPtr<SvgNode>& parent);
70     void ParseAttrs(const SkDOM& xmlDom, const SkDOM::Node* xmlNode, const RefPtr<SvgNode>& svgNode);
71     void SetAttrValue(const std::string& name, const std::string& value, const RefPtr<SvgNode>& svgNode);
72     void ParseIdAttr(const WeakPtr<SvgNode>& weakSvgNode, const std::string& value);
73     void ParseFillAttr(const WeakPtr<SvgNode>& weakSvgNode, const std::string& value);
74     void ParseClassAttr(const WeakPtr<SvgNode>& weakSvgNode, const std::string& value);
75     void ParseStyleAttr(const WeakPtr<SvgNode>& weakSvgNode, const std::string& value);
76     void SyncRSNode(const RefPtr<RenderNode>& renderNode);
77     void InitStyles();
78     RefPtr<SvgContext> svgContext_;
79     RefPtr<SvgNode> root_;
80     Rect viewBox_;
81     PushAttr attrCallback_;
82     std::optional<Color> fillColor_;
83     std::string path_;
84     float smoothEdge_ = 0.0f;
85     std::optional<ImageColorFilter> colorFilter_;
86     std::atomic<bool> isStatic_{ true };
87     bool isStyleInited_ = false;
88 };
89 } // namespace OHOS::Ace::NG
90 
91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_DOM_H
92