• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PARSE_SVG_CONTEXT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_PARSE_SVG_CONTEXT_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "base/geometry/dimension.h"
25 #include "base/geometry/rect.h"
26 #include "base/memory/ace_type.h"
27 #include "base/utils/noncopyable.h"
28 #include "core/animation/animator.h"
29 #include "core/components_ng/render/canvas_image.h"
30 #include "core/components_ng/svg/base/svg_length_scale_rule.h"
31 
32 namespace OHOS::Ace::NG {
33 using AttrMap = std::unordered_map<std::string, std::string>;
34 using ClassStyleMap = std::unordered_map<std::string, AttrMap>;
35 using FuncNormalizeToPx = std::function<double(const Dimension&)>;
36 using FuncAnimateFlush = std::function<void()>;
37 
38 class SvgDumpInfo {
39 public:
SvgDumpInfo(Size contentSize,std::string drawTime)40     SvgDumpInfo(Size contentSize, std::string drawTime) : contentSize_(contentSize), drawTime_(drawTime) {}
41     SvgDumpInfo() = default;
42     ~SvgDumpInfo() = default;
SetSvgDrawPathInfoDump(const std::string & pathInfo)43     void SetSvgDrawPathInfoDump(const std::string& pathInfo)
44     {
45         svgDrawPathInfo_ = pathInfo;
46     }
ToString()47     std::string ToString()
48     {
49         return std::string("contentSize: ")
50             .append(contentSize_.ToString())
51             .append(", drawTime: ")
52             .append(drawTime_)
53             .append(", svgDrawPathInfo: ")
54             .append(svgDrawPathInfo_);
55     }
56 
57 private:
58     Size contentSize_;
59     std::string drawTime_;
60     std::string svgDrawPathInfo_;
61 };
62 
63 class SvgNode;
64 
65 class SvgContext : public AceType {
66     DECLARE_ACE_TYPE(SvgContext, AceType);
67 
68 public:
69     SvgContext() = default;
~SvgContext()70     ~SvgContext() override
71     {
72         idMapper_.clear();
73     }
74 
Push(const std::string & value,const RefPtr<SvgNode> & svgNode)75     void Push(const std::string& value, const RefPtr<SvgNode>& svgNode)
76     {
77         idMapper_.emplace(value, svgNode);
78     }
79 
80     RefPtr<SvgNode> GetSvgNodeById(const std::string& id) const;
81 
82     void PushStyle(const std::string& styleName, const std::pair<std::string, std::string>& attrPair);
83 
84     const AttrMap& GetAttrMap(const std::string& key) const;
85 
86     void AddAnimator(int32_t key, const RefPtr<Animator>& animator);
87 
88     void RemoveAnimator(int32_t key);
89 
90     void ControlAnimators(bool play);
91 
92     void SetFuncNormalizeToPx(const FuncNormalizeToPx& funcNormalizeToPx);
93 
94     double NormalizeToPx(const Dimension& value);
95 
96     void SetFuncAnimateFlush(FuncAnimateFlush&& funcAnimateFlush, const WeakPtr<CanvasImage>& imagePtr);
97 
98     void AnimateFlush();
99 
SetRootViewBox(const Rect & viewBox)100     void SetRootViewBox(const Rect& viewBox)
101     {
102         rootViewBox_ = viewBox;
103     }
104 
GetRootViewBox()105     const Rect& GetRootViewBox() const
106     {
107         return rootViewBox_;
108     }
109 
SetViewPort(const Size & viewPort)110     void SetViewPort(const Size& viewPort)
111     {
112         viewPort_ = viewPort;
113     }
114 
GetViewPort()115     const Size& GetViewPort() const
116     {
117         return viewPort_;
118     }
119     void SetOnAnimationFinished(const std::function<void()>& onFinishCallback);
120     void OnAnimationFinished();
121     void CreateDumpInfo(SvgDumpInfo dumpInfo);
122     void SetSvgDrawPathInfoDump(const std::string& pathInfo);
SetContentSize(Size & contentSize)123     void SetContentSize(Size& contentSize)
124     {
125         contentSize_ = contentSize;
126     }
GetContentSize()127     const Size& GetContentSize()
128     {
129         return contentSize_;
130     }
GetHasRecordedPath()131     bool GetHasRecordedPath() const
132     {
133         return hasRecordedPath_;
134     }
SetGetHasRecordedPath(bool hasRecordedPath)135     void SetGetHasRecordedPath(bool hasRecordedPath)
136     {
137         hasRecordedPath_ = hasRecordedPath;
138     }
139     std::string GetDumpInfo();
140     std::string GetCurrentTimeString();
SetFillColor(std::optional<Color> & fillColor)141     void SetFillColor(std::optional<Color>& fillColor)
142     {
143         fillColor_ = fillColor;
144     }
GetFillColor()145     std::optional<Color>& GetFillColor()
146     {
147         return fillColor_;
148     }
149     Rect GetBoundingRect(RefPtr<SvgNode>& boxNode, SvgLengthScaleRule& boxMeasureRule);
150 private:
151     std::unordered_map<std::string, WeakPtr<SvgNode>> idMapper_;
152     // weak references to animators in svgDom
153     std::unordered_map<int32_t, WeakPtr<Animator>> animators_;
154     ClassStyleMap styleMap_;
155     FuncNormalizeToPx funcNormalizeToPx_ = nullptr;
156     // svg dom shared by multiple images
157     std::map<WeakPtr<CanvasImage>, FuncAnimateFlush> animateCallbacks_;
158     Rect rootViewBox_;
159     Size viewPort_;
160     std::list<std::function<void()>> onFinishCallbacks_;
161     Size contentSize_;
162     bool hasRecordedPath_ = false;
163     SvgDumpInfo dumpInfo_;
164     std::optional<Color> fillColor_;
165     ACE_DISALLOW_COPY_AND_MOVE(SvgContext);
166 };
167 } // namespace OHOS::Ace::NG
168 
169 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_PARSE_SVG_CONTEXT_H