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