1 /*
2  * Copyright (c) 2024 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_BLEND_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_BLEND_DECLARATION_H
18 
19 #include "core/components/declaration/svg/svg_fe_declaration.h"
20 
21 namespace OHOS::Ace {
22 
23 enum class FeBlendMode {
24     NORMAL,
25     MULTIPLY,
26     SCREEN,
27     DARKEN,
28     LIGHTEN
29 };
30 
31 struct SvgFeBlendAttribute : SvgFeAttribute {
32     FeIn in2;
33     FeBlendMode blendMode = FeBlendMode::NORMAL;
34 };
35 
36 class SvgFeBlendDeclaration : public SvgFeDeclaration {
37     DECLARE_ACE_TYPE(SvgFeBlendDeclaration, SvgFeDeclaration);
38 
39 public:
40     SvgFeBlendDeclaration() = default;
41     ~SvgFeBlendDeclaration() override = default;
42     void InitializeStyle() override;
43 
SetIn2(const std::string & In2)44     void SetIn2(const std::string& In2)
45     {
46         static const LinearMapNode<FeInType> IN_TABLE[] = {
47             { "BackgroundAlpha", FeInType::BACKGROUND_ALPHA },
48             { "BackgroundImage", FeInType::BACKGROUND_IMAGE },
49             { "FillPaint", FeInType::FILL_PAINT },
50             { "SourceAlpha", FeInType::SOURCE_ALPHA },
51             { "SourceGraphic", FeInType::SOURCE_GRAPHIC },
52             { "StrokePaint", FeInType::STROKE_PAINT },
53         };
54         int64_t inIndex = BinarySearchFindIndex(IN_TABLE, ArraySize(IN_TABLE), In2.c_str());
55         auto& attribute = MaybeResetAttribute<SvgFeBlendAttribute>(AttributeTag::SPECIALIZED_ATTR);
56         if (inIndex != -1) {
57             attribute.in2.in = IN_TABLE[inIndex].value;
58         } else {
59             attribute.in2.id = In2;
60         }
61     }
62 
SetBlendMode(const std::string & blendMode)63     void SetBlendMode(const std::string& blendMode)
64     {
65         static const LinearMapNode<FeBlendMode> EDGE_MODE_TABLE[] = {
66             { "darken", FeBlendMode::DARKEN },
67             { "lighten", FeBlendMode::LIGHTEN },
68             { "multiply", FeBlendMode::MULTIPLY },
69             { "normal", FeBlendMode::NORMAL },
70             { "screen", FeBlendMode::SCREEN }
71         };
72         int64_t inIndex = BinarySearchFindIndex(EDGE_MODE_TABLE, ArraySize(EDGE_MODE_TABLE), blendMode.c_str());
73         if (inIndex != -1) {
74             auto& attribute = MaybeResetAttribute<SvgFeBlendAttribute>(AttributeTag::SPECIALIZED_ATTR);
75             attribute.blendMode = EDGE_MODE_TABLE[inIndex].value;
76         }
77     }
78 
GetBlendMode()79     const FeBlendMode& GetBlendMode() const
80     {
81         auto& attribute = static_cast<SvgFeBlendAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
82         return attribute.blendMode;
83     }
84 
GetIn2()85     const FeIn& GetIn2() const
86     {
87         auto& attribute = static_cast<SvgFeBlendAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
88         return attribute.in2;
89     }
90 
91 protected:
92     void InitSpecialized() override;
93     bool SetSpecializedValue(const std::pair<std::string, std::string>& attr) override;
94 };
95 
96 } // namespace OHOS::Ace
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_FE_BLEND_DECLARATION_H
99