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 31 namespace OHOS::Ace::NG { 32 using AttrMap = std::unordered_map<std::string, std::string>; 33 using ClassStyleMap = std::unordered_map<std::string, AttrMap>; 34 using FuncNormalizeToPx = std::function<double(const Dimension&)>; 35 using FuncAnimateFlush = std::function<void()>; 36 37 class SvgDumpInfo { 38 public: SvgDumpInfo(Size contentSize,std::string drawTime)39 SvgDumpInfo(Size contentSize, std::string drawTime) : contentSize_(contentSize), drawTime_(drawTime) {} 40 SvgDumpInfo() = default; 41 ~SvgDumpInfo() = default; ToString()42 std::string ToString() 43 { 44 return std::string("contentSize: ").append(contentSize_.ToString()).append(", drawTime: ").append(drawTime_); 45 } 46 private: 47 Size contentSize_; 48 std::string drawTime_; 49 }; 50 51 class SvgNode; 52 53 class SvgContext : public AceType { 54 DECLARE_ACE_TYPE(SvgContext, AceType); 55 56 public: 57 SvgContext() = default; ~SvgContext()58 ~SvgContext() override 59 { 60 idMapper_.clear(); 61 } 62 Push(const std::string & value,const RefPtr<SvgNode> & svgNode)63 void Push(const std::string& value, const RefPtr<SvgNode>& svgNode) 64 { 65 idMapper_.emplace(value, svgNode); 66 } 67 68 RefPtr<SvgNode> GetSvgNodeById(const std::string& id) const; 69 70 void PushStyle(const std::string& styleName, const std::pair<std::string, std::string>& attrPair); 71 72 const AttrMap& GetAttrMap(const std::string& key) const; 73 74 void AddAnimator(int32_t key, const RefPtr<Animator>& animator); 75 76 void RemoveAnimator(int32_t key); 77 78 void ControlAnimators(bool play); 79 80 void SetFuncNormalizeToPx(const FuncNormalizeToPx& funcNormalizeToPx); 81 82 double NormalizeToPx(const Dimension& value); 83 84 void SetFuncAnimateFlush(FuncAnimateFlush&& funcAnimateFlush, const WeakPtr<CanvasImage>& imagePtr); 85 86 void AnimateFlush(); 87 SetRootViewBox(const Rect & viewBox)88 void SetRootViewBox(const Rect& viewBox) 89 { 90 rootViewBox_ = viewBox; 91 } 92 GetRootViewBox()93 const Rect& GetRootViewBox() const 94 { 95 return rootViewBox_; 96 } 97 SetViewPort(const Size & viewPort)98 void SetViewPort(const Size& viewPort) 99 { 100 viewPort_ = viewPort; 101 } 102 GetViewPort()103 const Size& GetViewPort() const 104 { 105 return viewPort_; 106 } 107 108 void CreateDumpInfo(SvgDumpInfo dumpInfo); SetContentSize(Size & contentSize)109 void SetContentSize(Size& contentSize) 110 { 111 contentSize_ = contentSize; 112 } GetContentSize()113 const Size& GetContentSize() 114 { 115 return contentSize_; 116 } 117 SvgDumpInfo& GetDumpInfo(); 118 std::string GetCurrentTimeString(); 119 void SetOnAnimationFinished(const std::function<void()>& onFinishCallback); 120 void OnAnimationFinished(); 121 private: 122 std::unordered_map<std::string, WeakPtr<SvgNode>> idMapper_; 123 // weak references to animators in svgDom 124 std::unordered_map<int32_t, WeakPtr<Animator>> animators_; 125 ClassStyleMap styleMap_; 126 FuncNormalizeToPx funcNormalizeToPx_ = nullptr; 127 // svg dom shared by multiple images 128 std::map<WeakPtr<CanvasImage>, FuncAnimateFlush> animateCallbacks_; 129 Rect rootViewBox_; 130 Size viewPort_; 131 Size contentSize_; 132 SvgDumpInfo dumpInfo_; 133 std::function<void()> onFinishCallback_; 134 ACE_DISALLOW_COPY_AND_MOVE(SvgContext); 135 }; 136 } // namespace OHOS::Ace::NG 137 138 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_PARSE_SVG_CONTEXT_H