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_RECT_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_RECT_DECLARATION_H
18 
19 #include "core/components/declaration/svg/svg_base_declaration.h"
20 
21 namespace OHOS::Ace {
22 
23 struct SvgRectAttribute : SvgBaseAttribute {
24     Dimension x;
25     Dimension y;
26     Dimension rx = -1.0_px;
27     Dimension ry = -1.0_px;
28     Dimension width;
29     Dimension height;
30 };
31 
32 class SvgRectDeclaration : public SvgBaseDeclaration {
33     DECLARE_ACE_TYPE(SvgRectDeclaration, SvgBaseDeclaration);
34 
35 public:
36     SvgRectDeclaration() = default;
37     ~SvgRectDeclaration() override = default;
38     void InitializeStyle() override;
39     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
40     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
41 
GetX()42     const Dimension& GetX() const
43     {
44         auto& attribute = static_cast<SvgRectAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
45         return attribute.x;
46     }
47 
GetY()48     const Dimension& GetY() const
49     {
50         auto& attribute = static_cast<SvgRectAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
51         return attribute.y;
52     }
53 
GetRx()54     const Dimension& GetRx() const
55     {
56         auto& attribute = static_cast<SvgRectAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
57         return attribute.rx;
58     }
59 
GetRy()60     const Dimension& GetRy() const
61     {
62         auto& attribute = static_cast<SvgRectAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
63         return attribute.ry;
64     }
65 
GetWidth()66     const Dimension& GetWidth() const
67     {
68         auto& attribute = static_cast<SvgRectAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
69         return attribute.width;
70     }
71 
GetHeight()72     const Dimension& GetHeight() const
73     {
74         auto& attribute = static_cast<SvgRectAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
75         return attribute.height;
76     }
77 
SetX(const Dimension & x)78     void SetX(const Dimension& x)
79     {
80         auto& attribute = MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
81         attribute.x = x;
82     }
83 
SetY(const Dimension & y)84     void SetY(const Dimension& y)
85     {
86         auto& attribute = MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
87         attribute.y = y;
88     }
89 
SetRx(const Dimension & rx)90     void SetRx(const Dimension& rx)
91     {
92         auto& attribute = MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
93         if (GreatOrEqual(rx.Value(), 0.0)) {
94             attribute.rx = rx;
95         }
96     }
97 
SetRy(const Dimension & ry)98     void SetRy(const Dimension& ry)
99     {
100         auto& attribute = MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
101         if (GreatOrEqual(ry.Value(), 0.0)) {
102             attribute.ry = ry;
103         }
104     }
105 
SetWidth(const Dimension & width)106     void SetWidth(const Dimension& width)
107     {
108         auto& attribute = MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
109         if (GreatOrEqual(width.Value(), 0.0)) {
110             attribute.width = width;
111         }
112     }
113 
SetHeight(const Dimension & height)114     void SetHeight(const Dimension& height)
115     {
116         auto& attribute = MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
117         if (GreatOrEqual(height.Value(), 0.0)) {
118             attribute.height = height;
119         }
120     }
121 
122 protected:
123     void InitSpecialized() override;
124 
125 private:
126     bool SetSpecializedValue(const std::pair<std::string, std::string>& attr);
127 };
128 
129 } // namespace OHOS::Ace
130 
131 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SVG_SVG_RECT_DECLARATION_H
132