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_PARSE_SVG_NODE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_PARSE_SVG_NODE_H 18 19 #include <vector> 20 #ifndef USE_ROSEN_DRAWING 21 #include "include/core/SkPath.h" 22 #else 23 #include "core/components_ng/render/drawing.h" 24 #endif 25 26 #include "base/memory/ace_type.h" 27 #include "core/components/declaration/svg/svg_base_declaration.h" 28 #include "core/pipeline/base/render_node.h" 29 #include "frameworks/core/components/box/box_component.h" 30 #include "frameworks/core/components/svg/parse/svg_context.h" 31 32 namespace OHOS::Ace { 33 34 enum class SvgLengthType { 35 HORIZONTAL, 36 VERTICAL, 37 OTHER, 38 }; 39 40 class SvgContext; 41 42 class SvgNode : public AceType { 43 DECLARE_ACE_TYPE(SvgNode, AceType); 44 45 public: 46 SvgNode() = default; 47 ~SvgNode() override = default; 48 AppendChild(const RefPtr<SvgNode> & svgNode)49 virtual void AppendChild(const RefPtr<SvgNode>& svgNode) {} SetAttr(const std::string & name,const std::string & value)50 virtual void SetAttr(const std::string& name, const std::string& value) {} Update(RefPtr<RenderNode> & node)51 virtual void Update(RefPtr<RenderNode>& node) {} Inherit(const RefPtr<SvgBaseDeclaration> & parent)52 virtual void Inherit(const RefPtr<SvgBaseDeclaration>& parent) {} 53 54 virtual RefPtr<RenderNode> CreateRender( 55 const LayoutParam& layoutParam, const RefPtr<SvgBaseDeclaration>& parent, bool useBox = true) 56 { 57 return nullptr; 58 } 59 60 #ifndef USE_ROSEN_DRAWING AsPath(const Size & viewPort)61 virtual SkPath AsPath(const Size& viewPort) const 62 { 63 return SkPath(); 64 } 65 #else AsPath(const Size & viewPort)66 virtual RSPath AsPath(const Size& viewPort) const 67 { 68 return RSPath(); 69 } 70 #endif 71 GetComponent()72 virtual RefPtr<Component> GetComponent() const 73 { 74 return nullptr; 75 } 76 SetNodeId(const std::string & value)77 void SetNodeId(const std::string& value) 78 { 79 nodeId_ = value; 80 } 81 SetContext(const WeakPtr<PipelineContext> & context,const WeakPtr<SvgContext> & svgContext)82 void SetContext(const WeakPtr<PipelineContext>& context, const WeakPtr<SvgContext>& svgContext) 83 { 84 context_ = context; 85 svgContext_ = svgContext; 86 } 87 SetText(const std::string & text)88 void SetText(const std::string& text) 89 { 90 text_ = text; 91 } 92 93 protected: 94 double ConvertDimensionToPx(const Dimension& value, const Size& viewPort, SvgLengthType type) const; 95 std::optional<Gradient> GetGradient(const std::string& href); 96 RefPtr<BoxComponent> CreateBoxComponent(const LayoutParam& layoutParam, const std::string& clipPathHref); 97 98 WeakPtr<PipelineContext> context_; 99 WeakPtr<SvgContext> svgContext_; 100 std::vector<RefPtr<SvgNode>> children_; 101 std::string nodeId_; 102 std::string text_; 103 104 private: 105 std::string AsClipPathCommands(const Size& viewPort, const std::string& href) const; 106 }; 107 108 } // namespace OHOS::Ace 109 110 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_PARSE_SVG_NODE_H