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_RENDER_SVG_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_RENDER_SVG_H 18 19 #include "frameworks/core/components/svg/render_svg_base.h" 20 #include "frameworks/core/components/svg/svg_component.h" 21 22 namespace OHOS::Ace { 23 24 class RenderSvg : public RenderSvgBase { 25 DECLARE_ACE_TYPE(RenderSvg, RenderSvgBase); 26 27 public: 28 static RefPtr<RenderNode> Create(); 29 ~RenderSvg() override; 30 31 void Update(const RefPtr<Component>& component) override; 32 void PerformLayout() override; 33 bool PrepareSelfAnimation(const RefPtr<SvgAnimate>& svgAnimate) override; 34 GetViewBox()35 const Rect& GetViewBox() const 36 { 37 return viewBox_; 38 } 39 GetX()40 const Dimension& GetX() const 41 { 42 return x_; 43 } 44 GetY()45 const Dimension& GetY() const 46 { 47 return y_; 48 } 49 GetWidth()50 const Dimension& GetWidth() const 51 { 52 return width_; 53 } 54 GetHeight()55 const Dimension& GetHeight() const 56 { 57 return height_; 58 } 59 IsRoot()60 bool IsRoot() const 61 { 62 return isRoot_; 63 } 64 AddHrefNode(const std::string & id,const RefPtr<RenderSvgBase> & mask)65 void AddHrefNode(const std::string& id, const RefPtr<RenderSvgBase>& mask) 66 { 67 if (id.empty() || mask == nullptr) { 68 return; 69 } 70 svgHrefNodes_[id] = mask; 71 } 72 GetHrefNode(const std::string & id)73 RefPtr<RenderSvgBase> GetHrefNode(const std::string& id) 74 { 75 if (svgHrefNodes_.find(id) == svgHrefNodes_.end()) { 76 return nullptr; 77 } 78 return svgHrefNodes_[id].Upgrade(); 79 } 80 AddHrefComponent(const std::string & id,const RefPtr<Component> & component)81 void AddHrefComponent(const std::string& id, const RefPtr<Component>& component) 82 { 83 if (id.empty() || component == nullptr) { 84 return; 85 } 86 svgHrefComponents_[id] = component; 87 } 88 GetHrefComponent(const std::string & id)89 RefPtr<Component> GetHrefComponent(const std::string& id) 90 { 91 if (svgHrefComponents_.find(id) == svgHrefComponents_.end()) { 92 return nullptr; 93 } 94 return svgHrefComponents_[id].Upgrade(); 95 } 96 AddHrefDeclaration(const std::string & id,const RefPtr<SvgBaseDeclaration> & declaration)97 void AddHrefDeclaration(const std::string& id, const RefPtr<SvgBaseDeclaration>& declaration) 98 { 99 if (id.empty() || declaration == nullptr) { 100 return; 101 } 102 svgHrefDeclarations_[id] = declaration; 103 } 104 GetHrefDeclaration(const std::string & id)105 RefPtr<SvgBaseDeclaration> GetHrefDeclaration(const std::string& id) 106 { 107 if (svgHrefDeclarations_.find(id) == svgHrefDeclarations_.end()) { 108 return nullptr; 109 } 110 return svgHrefDeclarations_[id].Upgrade(); 111 } 112 MarkIsFixSize(bool isFixSize)113 void MarkIsFixSize(bool isFixSize) 114 { 115 isFixSize_ = isFixSize; 116 } 117 SetRootOpacity(uint8_t alpha)118 void SetRootOpacity(uint8_t alpha) 119 { 120 rootOpacity_ = alpha; 121 } 122 SetRootRotate(double rotate)123 void SetRootRotate(double rotate) 124 { 125 rootRotate_ = rotate; 126 } 127 128 protected: 129 Rect viewBox_; 130 bool isRoot_ = false; 131 uint8_t rootOpacity_ = 255; 132 double rootRotate_ = 0.0; 133 134 private: 135 void PrepareAnimations(); 136 void AddSvgAnimations(const RefPtr<SvgComponent>& svgComponent); 137 bool SetProperty(const std::string& attrName, const Dimension& value); 138 bool GetProperty(const std::string& attrName, Dimension& dimension) const; 139 void SetOpacityCallback(); 140 bool OpacityAnimation(const RefPtr<SvgAnimate>& svgAnimate); 141 void UpdateTransform(); 142 143 Dimension x_; 144 Dimension y_; 145 Dimension width_ = Dimension(-1.0); 146 Dimension height_ = Dimension(-1.0); 147 bool autoMirror_ = false; 148 std::vector<RefPtr<SvgAnimate>> svgAnimates_; 149 bool hasUpdated_ = false; 150 std::function<void(double)> opacityCallback_; 151 std::map<std::string, WeakPtr<RenderSvgBase>> svgHrefNodes_; 152 std::map<std::string, WeakPtr<Component>> svgHrefComponents_; 153 std::map<std::string, WeakPtr<SvgBaseDeclaration>> svgHrefDeclarations_; 154 bool isFixSize_ = false; 155 }; 156 157 } // namespace OHOS::Ace 158 159 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_RENDER_SVG_H 160