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_COMPOSITE_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_COMPOSITE_DECLARATION_H
18 
19 #include "core/components/declaration/svg/svg_fe_declaration.h"
20 
21 namespace OHOS::Ace {
22 
23 enum class FeOperatorType {
24     FE_ARITHMETIC,
25     FE_ATOP,
26     FE_IN,
27     FE_LIGHTER,
28     FE_OUT,
29     FE_OVER,
30     FE_XOR
31 };
32 
33 struct SvgFeCompositeAttribute : SvgFeAttribute {
34     FeIn in2;
35     FeOperatorType operatorType = FeOperatorType::FE_OVER;
36     double k1 = 0.0;
37     double k2 = 0.0;
38     double k3 = 0.0;
39     double k4 = 0.0;
40 };
41 
42 class SvgFeCompositeDeclaration : public SvgFeDeclaration {
43     DECLARE_ACE_TYPE(SvgFeCompositeDeclaration, SvgFeDeclaration);
44 
45 public:
46     SvgFeCompositeDeclaration() = default;
47     ~SvgFeCompositeDeclaration() override = default;
48     void InitializeStyle() override;
49 
SetK1(double k1)50     void SetK1(double k1)
51     {
52         auto& attribute = MaybeResetAttribute<SvgFeCompositeAttribute>(AttributeTag::SPECIALIZED_ATTR);
53         attribute.k1 = k1;
54     }
55 
SetK2(double k2)56     void SetK2(double k2)
57     {
58         auto& attribute = MaybeResetAttribute<SvgFeCompositeAttribute>(AttributeTag::SPECIALIZED_ATTR);
59         attribute.k2 = k2;
60     }
61 
SetK3(double k3)62     void SetK3(double k3)
63     {
64         auto& attribute = MaybeResetAttribute<SvgFeCompositeAttribute>(AttributeTag::SPECIALIZED_ATTR);
65         attribute.k3 = k3;
66     }
67 
SetK4(double k4)68     void SetK4(double k4)
69     {
70         auto& attribute = MaybeResetAttribute<SvgFeCompositeAttribute>(AttributeTag::SPECIALIZED_ATTR);
71         attribute.k4 = k4;
72     }
73 
SetIn2(const std::string & In2)74     void SetIn2(const std::string& In2)
75     {
76         static const LinearMapNode<FeInType> IN_TABLE[] = {
77             { "BackgroundAlpha", FeInType::BACKGROUND_ALPHA },
78             { "BackgroundImage", FeInType::BACKGROUND_IMAGE },
79             { "FillPaint", FeInType::FILL_PAINT },
80             { "SourceAlpha", FeInType::SOURCE_ALPHA },
81             { "SourceGraphic", FeInType::SOURCE_GRAPHIC },
82             { "StrokePaint", FeInType::STROKE_PAINT },
83         };
84         int64_t inIndex = BinarySearchFindIndex(IN_TABLE, ArraySize(IN_TABLE), In2.c_str());
85         auto& attribute = MaybeResetAttribute<SvgFeCompositeAttribute>(AttributeTag::SPECIALIZED_ATTR);
86         if (inIndex != -1) {
87             attribute.in2.in = IN_TABLE[inIndex].value;
88         } else {
89             attribute.in2.id = In2;
90         }
91     }
92 
SetOperatorType(const std::string & type)93     void SetOperatorType(const std::string& type)
94     {
95         static const LinearMapNode<FeOperatorType> FE_OPERATOR_TABLE[] = {
96             { "arithmetic", FeOperatorType::FE_ARITHMETIC },
97             { "atop", FeOperatorType::FE_ATOP },
98             { "in", FeOperatorType::FE_IN },
99             { "lighter", FeOperatorType::FE_LIGHTER },
100             { "out", FeOperatorType::FE_OUT },
101             { "over", FeOperatorType::FE_OVER },
102             { "xor", FeOperatorType::FE_XOR },
103         };
104         int64_t inIndex = BinarySearchFindIndex(FE_OPERATOR_TABLE, ArraySize(FE_OPERATOR_TABLE), type.c_str());
105         if (inIndex != -1) {
106             auto& attribute = MaybeResetAttribute<SvgFeCompositeAttribute>(AttributeTag::SPECIALIZED_ATTR);
107             attribute.operatorType = FE_OPERATOR_TABLE[inIndex].value;
108         }
109     }
110 
GetK1()111     double GetK1() const
112     {
113         auto& attribute = static_cast<SvgFeCompositeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
114         return attribute.k1;
115     }
116 
GetK2()117     double GetK2() const
118     {
119         auto& attribute = static_cast<SvgFeCompositeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
120         return attribute.k2;
121     }
122 
GetK3()123     double GetK3() const
124     {
125         auto& attribute = static_cast<SvgFeCompositeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
126         return attribute.k3;
127     }
128 
GetK4()129     double GetK4() const
130     {
131         auto& attribute = static_cast<SvgFeCompositeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
132         return attribute.k4;
133     }
134 
GetIn2()135     const FeIn& GetIn2() const
136     {
137         auto& attribute = static_cast<SvgFeCompositeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
138         return attribute.in2;
139     }
140 
GetOperatorType()141     const FeOperatorType& GetOperatorType() const
142     {
143         auto& attribute = static_cast<SvgFeCompositeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
144         return attribute.operatorType;
145     }
146 
147 protected:
148     void InitSpecialized() override;
149     bool SetSpecializedValue(const std::pair<std::string, std::string>& attr) override;
150 };
151 
152 } // namespace OHOS::Ace
153 
154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_COMPOSITE_DECLARATION_H
155