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_FE_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_DECLARATION_H 18 19 #include "core/components/declaration/svg/svg_base_declaration.h" 20 21 namespace OHOS::Ace { 22 23 enum class ColorInterpolationType { 24 LINEAR_RGB, 25 SRGB, 26 AUTO 27 }; 28 29 enum class FeInType { 30 SOURCE_GRAPHIC, 31 SOURCE_ALPHA, 32 BACKGROUND_IMAGE, 33 BACKGROUND_ALPHA, 34 FILL_PAINT, 35 STROKE_PAINT, 36 PRIMITIVE 37 }; 38 39 struct SvgFeAttribute : SvgBaseAttribute { 40 Dimension x = Dimension(0.0, DimensionUnit::PERCENT); 41 Dimension y = Dimension(0.0, DimensionUnit::PERCENT); 42 Dimension height = Dimension(1.0, DimensionUnit::PERCENT); 43 Dimension width = Dimension(1.0, DimensionUnit::PERCENT); 44 std::string result; 45 FeInType in = FeInType::PRIMITIVE; 46 ColorInterpolationType colorInterpolationType = ColorInterpolationType::LINEAR_RGB; 47 }; 48 49 class SvgFeDeclaration : public SvgBaseDeclaration { 50 DECLARE_ACE_TYPE(SvgFeDeclaration, SvgBaseDeclaration); 51 52 public: 53 SvgFeDeclaration() = default; 54 ~SvgFeDeclaration() override = default; 55 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 56 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 57 SetX(const Dimension & x)58 void SetX(const Dimension& x) 59 { 60 auto& attribute = MaybeResetAttribute<SvgFeAttribute>(AttributeTag::SPECIALIZED_ATTR); 61 attribute.x = x; 62 } 63 GetX()64 Dimension& GetX() const 65 { 66 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 67 return attribute.x; 68 } 69 SetY(const Dimension & y)70 void SetY(const Dimension& y) 71 { 72 auto& attribute = MaybeResetAttribute<SvgFeAttribute>(AttributeTag::SPECIALIZED_ATTR); 73 attribute.y = y; 74 } 75 GetY()76 Dimension& GetY() const 77 { 78 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 79 return attribute.y; 80 } 81 SetHeight(const Dimension & height)82 void SetHeight(const Dimension& height) 83 { 84 auto& attribute = MaybeResetAttribute<SvgFeAttribute>(AttributeTag::SPECIALIZED_ATTR); 85 attribute.height = height; 86 } 87 GetHeight()88 Dimension& GetHeight() const 89 { 90 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 91 return attribute.height; 92 } 93 SetWidth(const Dimension & width)94 void SetWidth(const Dimension& width) 95 { 96 auto& attribute = MaybeResetAttribute<SvgFeAttribute>(AttributeTag::SPECIALIZED_ATTR); 97 attribute.width = width; 98 } 99 GetWidth()100 Dimension& GetWidth() const 101 { 102 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 103 return attribute.width; 104 } 105 SetResult(const std::string & result)106 void SetResult(const std::string& result) 107 { 108 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 109 attribute.result = result; 110 } 111 GetResult()112 const std::string& GetResult() const 113 { 114 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 115 return attribute.result; 116 } 117 SetIn(const std::string & in)118 void SetIn(const std::string& in) 119 { 120 static const LinearMapNode<FeInType> IN_TABLE[] = { 121 { "BackgroundAlpha", FeInType::BACKGROUND_ALPHA }, 122 { "BackgroundImage", FeInType::BACKGROUND_IMAGE }, 123 { "FillPaint", FeInType::FILL_PAINT }, 124 { "SourceAlpha", FeInType::SOURCE_ALPHA }, 125 { "SourceGraphic", FeInType::SOURCE_GRAPHIC }, 126 { "StrokePaint", FeInType::STROKE_PAINT }, 127 }; 128 int64_t inIndex = BinarySearchFindIndex(IN_TABLE, ArraySize(IN_TABLE), in.c_str()); 129 if (inIndex != -1) { 130 auto& attribute = MaybeResetAttribute<SvgFeAttribute>(AttributeTag::SPECIALIZED_ATTR); 131 attribute.in = IN_TABLE[inIndex].value; 132 } 133 } 134 GetIn()135 const FeInType& GetIn() const 136 { 137 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 138 return attribute.in; 139 } 140 SetColorInterpolationType(const std::string & colorInterpolationType)141 void SetColorInterpolationType(const std::string& colorInterpolationType) 142 { 143 static const LinearMapNode<ColorInterpolationType> COLOR_INTERPOLATION_TYPE_TABLE[] = { 144 { "auto", ColorInterpolationType::AUTO }, 145 { "linearRGB", ColorInterpolationType::LINEAR_RGB }, 146 { "sRGB", ColorInterpolationType::SRGB }, 147 }; 148 int64_t inIndex = BinarySearchFindIndex( 149 COLOR_INTERPOLATION_TYPE_TABLE, ArraySize(COLOR_INTERPOLATION_TYPE_TABLE), colorInterpolationType.c_str()); 150 if (inIndex != -1) { 151 auto& attribute = MaybeResetAttribute<SvgFeAttribute>(AttributeTag::SPECIALIZED_ATTR); 152 attribute.colorInterpolationType = COLOR_INTERPOLATION_TYPE_TABLE[inIndex].value; 153 } 154 } 155 GetColorInterpolationType()156 const ColorInterpolationType& GetColorInterpolationType() const 157 { 158 auto& attribute = static_cast<SvgFeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 159 return attribute.colorInterpolationType; 160 } 161 162 protected: 163 virtual bool SetSpecializedValue(const std::pair<std::string, std::string>& attr); 164 }; 165 166 } // namespace OHOS::Ace 167 168 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_DECLARATION_H 169