1 /*
2  * Copyright (c) 2021-2022 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_SHARP_SHAPE_CONTAINER_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHARP_SHAPE_CONTAINER_COMPONENT_H
18 
19 #include "frameworks/core/components/common/properties/svg_paint_state.h"
20 #include "frameworks/core/pipeline/base/component_group.h"
21 
22 namespace OHOS::Ace {
23 
24 class ShapeViewBox final {
25 public:
26     ShapeViewBox() = default;
ShapeViewBox(const AnimatableDimension & x,const AnimatableDimension & y,const AnimatableDimension & width,const AnimatableDimension & height)27     ShapeViewBox(const AnimatableDimension& x, const AnimatableDimension& y,
28                  const AnimatableDimension& width, const AnimatableDimension& height)
29         : x_(x), y_(y), width_(width), height_(height) {}
30     ~ShapeViewBox() = default;
31 
SetContextAndCallback(const WeakPtr<PipelineContext> & context,const RenderNodeAnimationCallback & callback)32     void SetContextAndCallback(const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback)
33     {
34         x_.SetContextAndCallback(context, callback);
35         y_.SetContextAndCallback(context, callback);
36         width_.SetContextAndCallback(context, callback);
37         height_.SetContextAndCallback(context, callback);
38     }
39 
Left()40     const AnimatableDimension& Left() const
41     {
42         return x_;
43     }
44 
Top()45     const AnimatableDimension& Top() const
46     {
47         return y_;
48     }
49 
Width()50     const AnimatableDimension& Width() const
51     {
52         return width_;
53     }
54 
Height()55     const AnimatableDimension& Height() const
56     {
57         return height_;
58     }
59 
SetLeft(const AnimatableDimension & left)60     void SetLeft(const AnimatableDimension& left)
61     {
62         x_ = left;
63     }
64 
65     void SetLeft(const Dimension& left, const AnimationOption& option = AnimationOption())
66     {
67         x_ = AnimatableDimension(left, option);
68     }
69 
SetTop(const AnimatableDimension & top)70     void SetTop(const AnimatableDimension& top)
71     {
72         y_ = top;
73     }
74 
75     void SetTop(const Dimension& top, const AnimationOption& option = AnimationOption())
76     {
77         y_ = AnimatableDimension(top, option);
78     }
79 
SetWidth(const AnimatableDimension & width)80     void SetWidth(const AnimatableDimension& width)
81     {
82         width_ = width;
83     }
84 
85     void SetWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
86     {
87         width_ = AnimatableDimension(width, option);
88     }
89 
SetHeight(const AnimatableDimension & height)90     void SetHeight(const AnimatableDimension& height)
91     {
92         height_ = height;
93     }
94 
95     void SetHeight(const Dimension& height, const AnimationOption& option = AnimationOption())
96     {
97         height_ = AnimatableDimension(height, option);
98     }
99 
IsValid()100     bool IsValid() const
101     {
102         return width_.Value() > 0.0 && height_.Value() > 0.0;
103     }
104 private:
105     AnimatableDimension x_ = AnimatableDimension(-1.0);
106     AnimatableDimension y_ = AnimatableDimension(-1.0);
107     AnimatableDimension width_ = AnimatableDimension(-1.0);
108     AnimatableDimension height_ = AnimatableDimension(-1.0);
109 };
110 
111 class ACE_EXPORT ShapeContainerComponent : public ComponentGroup {
112     DECLARE_ACE_TYPE(ShapeContainerComponent, ComponentGroup);
113 
114 public:
ShapeContainerComponent(const std::list<RefPtr<Component>> & children)115     explicit ShapeContainerComponent(const std::list<RefPtr<Component>>& children) : ComponentGroup(children) {}
116     ~ShapeContainerComponent() override = default;
117     RefPtr<RenderNode> CreateRenderNode() override;
118     RefPtr<Element> CreateElement() override;
119     void InheritShapeStyle(const FillState& fillState, const StrokeState& strokeState, bool antiAlias);
120 
SetViewBox(const ShapeViewBox & viewBox)121     void SetViewBox(const ShapeViewBox& viewBox)
122     {
123         viewBox_ = viewBox;
124     }
125 
GetViewBox()126     ShapeViewBox GetViewBox() const
127     {
128         return viewBox_;
129     }
130 
GetFillState()131     FillState GetFillState() const
132     {
133         return fillState_;
134     }
135 
GetStrokeState()136     StrokeState GetStrokeState() const
137     {
138         return strokeState_;
139     }
140 
SetAntiAlias(bool antiAlias)141     void SetAntiAlias(bool antiAlias)
142     {
143         antiAlias_.first = true;
144         antiAlias_.second = antiAlias;
145     }
146 
SetBitmapMesh(const std::vector<double> & mesh,int32_t column,int32_t row)147     void SetBitmapMesh(const std::vector<double>& mesh, int32_t column, int32_t row)
148     {
149         mesh_ = mesh;
150         column_ = column;
151         row_ = row;
152     }
153 
GetMeshColumn()154     int32_t GetMeshColumn() const
155     {
156         return column_;
157     }
158 
GetMeshRow()159     int32_t GetMeshRow() const
160     {
161         return row_;
162     }
163 
GetMesh()164     const std::vector<double> GetMesh() const
165     {
166         return mesh_;
167     }
168 
GetAntiAlias()169     const std::pair<bool, bool>& GetAntiAlias() const
170     {
171         return antiAlias_;
172     }
173 
174     void SetStroke(const Color& color, const AnimationOption& option = AnimationOption())
175     {
176         strokeState_.SetColor(color, true, option);
177     }
178 
179     void SetFill(const Color& color, const AnimationOption& option = AnimationOption())
180     {
181         fillState_.SetColor(color, true, option);
182     }
183 
184     void SetStrokeDashOffset(const Dimension& dashOffset, const AnimationOption& option = AnimationOption())
185     {
186         strokeState_.SetStrokeDashOffset(dashOffset, true, option);
187     }
188 
SetStrokeLineCap(LineCapStyle lineCapStyle)189     void SetStrokeLineCap(LineCapStyle lineCapStyle)
190     {
191         strokeState_.SetLineCap(lineCapStyle);
192     }
193 
SetStrokeLineJoin(LineJoinStyle lineJoinStyle)194     void SetStrokeLineJoin(LineJoinStyle lineJoinStyle)
195     {
196         strokeState_.SetLineJoin(lineJoinStyle);
197     }
198 
SetStrokeMiterLimit(double miterLimit)199     void SetStrokeMiterLimit(double miterLimit)
200     {
201         strokeState_.SetMiterLimit(miterLimit);
202     }
203 
204     void SetStrokeOpacity(double opacity, const AnimationOption& option = AnimationOption())
205     {
206         strokeState_.SetOpacity(std::clamp(opacity, 0.0, 1.0), true, option);
207     }
208 
209     void SetFillOpacity(double opacity, const AnimationOption& option = AnimationOption())
210     {
211         fillState_.SetOpacity(std::clamp(opacity, 0.0, 1.0), true, option);
212     }
213 
214     void SetStrokeWidth(const Dimension& lineWidth, const AnimationOption& option = AnimationOption())
215     {
216         strokeState_.SetLineWidth(lineWidth, true, option);
217     }
218 
SetStrokeDashArray(const std::vector<Dimension> & segments)219     void SetStrokeDashArray(const std::vector<Dimension>& segments)
220     {
221         strokeState_.SetStrokeDashArray(segments);
222     }
223 
SetWidthFlag(bool flag)224     void SetWidthFlag(bool flag)
225     {
226         hasDefineWidth_ = flag;
227     }
228 
GetWidthFlag()229     bool GetWidthFlag()
230     {
231         return hasDefineWidth_;
232     }
233 
SetHeightFlag(bool flag)234     void SetHeightFlag(bool flag)
235     {
236         hasDefineHeight_ = flag;
237     }
238 
GetHeightFlag()239     bool GetHeightFlag()
240     {
241         return hasDefineHeight_;
242     }
243 
244 private:
245     ShapeViewBox viewBox_;
246     FillState fillState_;
247     StrokeState strokeState_;
248     std::pair<bool, bool> antiAlias_ = std::make_pair(false, true);
249     bool hasDefineWidth_ = false;
250     bool hasDefineHeight_ = false;
251     int32_t column_ = 0;
252     int32_t row_ = 0;
253     std::vector<double> mesh_;
254 };
255 
256 } // namespace OHOS::Ace
257 
258 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHARP_SHAPE_CONTAINER_COMPONENT_H
259