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_DECLARATION_SVG_SVG_BASE_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_BASE_DECLARATION_H 18 19 #include "core/components/common/properties/svg_paint_state.h" 20 #include "core/components/declaration/common/declaration.h" 21 #include "frameworks/bridge/common/dom/dom_type.h" 22 23 namespace OHOS::Ace { 24 25 const char VALUE_NONE[] = "none"; 26 27 struct SvgBaseAttribute : Attribute { 28 bool hasOpacity = false; 29 double opacity = 1.0; 30 FillState fillState; 31 StrokeState strokeState; 32 SvgTextStyle textStyle; 33 std::string transform; 34 std::string transformOrigin; 35 std::string filterId; 36 std::string maskId; 37 std::string href; 38 std::string id; 39 ClipState clipState; 40 InheritFromUseSvgBaseAttribute41 void InheritFromUse(const SvgBaseAttribute& parent) 42 { 43 if (!hasOpacity) { 44 if (parent.hasOpacity) { 45 fillState.SetOpacity(parent.opacity); 46 opacity = parent.opacity; 47 } else { 48 opacity = 1.0; // default opacity is 1.0 49 } 50 } 51 fillState.Inherit(parent.fillState); 52 strokeState.Inherit(parent.strokeState); 53 clipState.Inherit(parent.clipState); 54 } 55 InheritSvgBaseAttribute56 void Inherit(const SvgBaseAttribute& parent) 57 { 58 if (!hasOpacity) { 59 if (parent.hasOpacity) { 60 opacity = parent.opacity; 61 } else { 62 opacity = 1.0; // default opacity is 1.0 63 } 64 } 65 fillState.Inherit(parent.fillState); 66 strokeState.Inherit(parent.strokeState); 67 clipState.Inherit(parent.clipState); 68 } 69 }; 70 71 class SvgBaseDeclaration : public Declaration { 72 DECLARE_ACE_TYPE(SvgBaseDeclaration, Declaration); 73 74 public: 75 SvgBaseDeclaration() = default; 76 ~SvgBaseDeclaration() override = default; 77 GetFillState()78 const FillState& GetFillState() const 79 { 80 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 81 return attribute.fillState; 82 } 83 GetStrokeState()84 const StrokeState& GetStrokeState() const 85 { 86 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 87 return attribute.strokeState; 88 } 89 GetSvgTextStyle()90 const SvgTextStyle& GetSvgTextStyle() const 91 { 92 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 93 return attribute.textStyle; 94 } 95 GetTransform()96 const std::string& GetTransform() const 97 { 98 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 99 return attribute.transform; 100 } 101 GetMaskId()102 const std::string& GetMaskId() const 103 { 104 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 105 return attribute.maskId; 106 } 107 GetFilterId()108 const std::string& GetFilterId() const 109 { 110 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 111 return attribute.filterId; 112 } 113 GetTransformOrigin()114 const std::string& GetTransformOrigin() const 115 { 116 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 117 return attribute.transformOrigin; 118 } 119 HasOpacity()120 bool HasOpacity() const 121 { 122 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 123 return attribute.hasOpacity; 124 } 125 GetOpacity()126 double GetOpacity() const 127 { 128 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 129 return attribute.opacity; 130 } 131 132 void Inherit(const RefPtr<Declaration>& parent); 133 SetGradient(const Gradient & gradient)134 void SetGradient(const Gradient& gradient) 135 { 136 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 137 attribute.fillState.SetGradient(gradient); 138 } 139 GetClipPathHref()140 const std::string& GetClipPathHref() const 141 { 142 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 143 return attribute.clipState.GetHref(); 144 } 145 SetHref(const std::string & href)146 void SetHref(const std::string& href) 147 { 148 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 149 attribute.href = href; 150 } 151 GetHref()152 const std::string& GetHref() const 153 { 154 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 155 return attribute.href; 156 } 157 GetClipState()158 const ClipState& GetClipState() const 159 { 160 auto& attribute = static_cast<SvgBaseAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 161 return attribute.clipState; 162 } 163 GetId()164 const std::string& GetId() 165 { 166 auto& commonAttr = MaybeResetAttribute<CommonAttribute>(AttributeTag::COMMON_ATTR); 167 return commonAttr.id; 168 } 169 170 LineCapStyle GetLineCapStyle(const std::string& val) const; 171 LineJoinStyle GetLineJoinStyle(const std::string& val) const; 172 Color GetColor(const std::string& value) const; 173 174 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 175 176 void ReplaceAttributes(const SvgBaseAttribute& attr); 177 178 protected: 179 void InitSpecialized() override; 180 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 181 bool SetPresentationAttr(const std::pair<std::string, std::string>& attr); 182 }; 183 184 } // namespace OHOS::Ace 185 186 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_BASE_DECLARATION_H 187