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_GAUSSIANBLUR_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_GAUSSIANBLUR_DECLARATION_H
18 
19 #include <vector>
20 
21 #include "base/utils/utils.h"
22 #include "core/components/declaration/svg/svg_fe_declaration.h"
23 
24 namespace OHOS::Ace {
25 enum class FeEdgeMode {
26     EDGE_DUPLICATE,
27     EDGE_WRAP,
28     EDGE_NONE
29 };
30 
31 struct SvgFeGaussianBlurAttribute : SvgFeAttribute {
32     double stdDeviationX = 0.0;
33     double stdDeviationY = 0.0;
34     FeEdgeMode edgeMode = FeEdgeMode::EDGE_DUPLICATE;
35 };
36 
37 class SvgFeGaussianBlurDeclaration : public SvgFeDeclaration {
38     DECLARE_ACE_TYPE(SvgFeGaussianBlurDeclaration, SvgFeDeclaration);
39 
40 public:
41     SvgFeGaussianBlurDeclaration() = default;
42     ~SvgFeGaussianBlurDeclaration() override = default;
43     void InitializeStyle() override;
44 
SetStdDeviation(const std::vector<float> & stdDeviation)45     void SetStdDeviation(const std::vector<float>& stdDeviation)
46     {
47         if (stdDeviation.empty() || stdDeviation.size() > 2) {
48             return;
49         }
50         auto& attribute = MaybeResetAttribute<SvgFeGaussianBlurAttribute>(AttributeTag::SPECIALIZED_ATTR);
51         attribute.stdDeviationX = stdDeviation[0];
52         attribute.stdDeviationY = stdDeviation.size() > 1 ? stdDeviation[1] : stdDeviation[0];
53     }
54 
SetEdgeMode(const std::string & edgeMode)55     void SetEdgeMode(const std::string& edgeMode)
56     {
57         static const LinearMapNode<FeEdgeMode> EDGE_MODE_TABLE[] = {
58             { "duplicate", FeEdgeMode::EDGE_DUPLICATE },
59             { "none", FeEdgeMode::EDGE_NONE },
60             { "wrap", FeEdgeMode::EDGE_WRAP },
61         };
62         int64_t inIndex = BinarySearchFindIndex(EDGE_MODE_TABLE, ArraySize(EDGE_MODE_TABLE), edgeMode.c_str());
63         if (inIndex != -1) {
64             auto& attribute = MaybeResetAttribute<SvgFeGaussianBlurAttribute>(AttributeTag::SPECIALIZED_ATTR);
65             attribute.edgeMode = EDGE_MODE_TABLE[inIndex].value;
66         }
67     }
68 
GetStdDeviationX()69     double GetStdDeviationX() const
70     {
71         auto& attribute = static_cast<SvgFeGaussianBlurAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
72         return attribute.stdDeviationX;
73     }
74 
GetStdDeviationY()75     double GetStdDeviationY() const
76     {
77         auto& attribute = static_cast<SvgFeGaussianBlurAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
78         return attribute.stdDeviationY;
79     }
80 
GetEdgeMode()81     const FeEdgeMode& GetEdgeMode() const
82     {
83         auto& attribute = static_cast<SvgFeGaussianBlurAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
84         return attribute.edgeMode;
85     }
86 
87 protected:
88     void InitSpecialized() override;
89     bool SetSpecializedValue(const std::pair<std::string, std::string>& attr) override;
90 };
91 
92 } // namespace OHOS::Ace
93 
94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_GAUSSIANBLUR_DECLARATION_H
95