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_FUNC_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_FUNC_DECLARATION_H
18 
19 #include "core/components/declaration/svg/svg_fe_declaration.h"
20 
21 namespace OHOS::Ace {
22 
23 enum class FeFuncType {
24     DISCRETE,
25     GAMMA,
26     IDENTITY,
27     LINEAR,
28     TABLE,
29 };
30 
31 struct SvgFeFuncAttribute : SvgFeAttribute {
32     FeFuncType type = FeFuncType::IDENTITY;
33     std::vector<double> tableValues; // list of number between 0 and 1
34     double slope = 0.0;
35     double intercept = 0.0;
36     double amplitude = 1.0;
37     double exponent = 1.0;
38     double offset = 0.0;
39 };
40 
41 class SvgFeFuncDeclaration : public SvgFeDeclaration {
42     DECLARE_ACE_TYPE(SvgFeFuncDeclaration, SvgFeDeclaration);
43 
44 public:
45     SvgFeFuncDeclaration() = default;
46     ~SvgFeFuncDeclaration() override = default;
47     void InitializeStyle() override;
48 
SetAmplitude(double amplitude)49     void SetAmplitude(double amplitude)
50     {
51         auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
52         attribute.amplitude = amplitude;
53     }
54 
SetExponent(double exponent)55     void SetExponent(double exponent)
56     {
57         auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
58         attribute.exponent = exponent;
59     }
60 
SetIntercept(double intercept)61     void SetIntercept(double intercept)
62     {
63         auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
64         attribute.intercept = intercept;
65     }
66 
SetOffset(double offset)67     void SetOffset(double offset)
68     {
69         auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
70         attribute.offset = offset;
71     }
72 
SetSlope(double slope)73     void SetSlope(double slope)
74     {
75         auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
76         attribute.slope = slope;
77     }
78 
SetTableValues(const std::string & tableValues)79     void SetTableValues(const std::string& tableValues)
80     {
81         auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
82         StringUtils::StringSplitter(tableValues, ' ', attribute.tableValues);
83     }
84 
SetType(const std::string & type)85     void SetType(const std::string& type)
86     {
87         static const LinearMapNode<FeFuncType> FE_FUNC_TABLE[] = {
88             { "discrete", FeFuncType::DISCRETE },
89             { "gamma", FeFuncType::GAMMA },
90             { "identity", FeFuncType::IDENTITY },
91             { "linear", FeFuncType::LINEAR },
92             { "table", FeFuncType::TABLE },
93         };
94         int64_t inIndex = BinarySearchFindIndex(FE_FUNC_TABLE, ArraySize(FE_FUNC_TABLE), type.c_str());
95         if (inIndex != -1) {
96             auto& attribute = MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
97             attribute.type = FE_FUNC_TABLE[inIndex].value;
98         }
99     }
100 
GetType()101     FeFuncType GetType() const
102     {
103         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
104         return attribute.type;
105     }
106 
GetAmplitude()107     double GetAmplitude() const
108     {
109         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
110         return attribute.amplitude;
111     }
112 
GetExponent()113     double GetExponent() const
114     {
115         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
116         return attribute.exponent;
117     }
118 
GetIntercept()119     double GetIntercept() const
120     {
121         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
122         return attribute.intercept;
123     }
124 
GetOffset()125     double GetOffset() const
126     {
127         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
128         return attribute.offset;
129     }
130 
GetSlope()131     double GetSlope() const
132     {
133         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
134         return attribute.slope;
135     }
136 
GetTableValues()137     const std::vector<double>& GetTableValues() const
138     {
139         auto& attribute = static_cast<SvgFeFuncAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
140         return attribute.tableValues;
141     }
142 
143 protected:
144     void InitSpecialized() override;
145     bool SetSpecializedValue(const std::pair<std::string, std::string>& attr) override;
146 };
147 
148 } // namespace OHOS::Ace
149 
150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_FUNC_DECLARATION_H
151