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