• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_BASE_GEOMETRY_SHAPE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_SHAPE_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/geometry/dimension_offset.h"
21 #include "base/memory/ace_type.h"
22 #include "core/components/common/properties/color.h"
23 #ifndef FUZZTEST
24 #include "core/components/common/properties/radius.h"
25 #else
26 #include "test/fuzztest/utilmost_fuzzer/radius.h"
27 #endif
28 
29 namespace OHOS::Ace {
30 
31 using ShapePoint = std::pair<Dimension, Dimension>;
32 using ShapePoints = std::vector<ShapePoint>;
33 
34 enum class ShapeType {
35     RECT = 0,
36     CIRCLE,
37     ELLIPSE,
38     LINE,
39     POLYGON,
40     POLYLINE,
41     PATH,
42 };
43 
44 enum class BasicShapeType { NONE, INSET, CIRCLE, ELLIPSE, POLYGON, PATH, RECT };
45 
46 class BasicShape : public AceType {
47     DECLARE_ACE_TYPE(BasicShape, AceType);
48 
49 public:
50     BasicShape() = default;
BasicShape(BasicShapeType basicShapeType)51     explicit BasicShape(BasicShapeType basicShapeType) : basicShapeType_(basicShapeType) {}
52     ~BasicShape() override = default;
53 
SetBasicShapeType(BasicShapeType basicShapeType)54     void SetBasicShapeType(BasicShapeType basicShapeType)
55     {
56         basicShapeType_ = basicShapeType;
57     }
58 
GetBasicShapeType()59     BasicShapeType GetBasicShapeType() const
60     {
61         return basicShapeType_;
62     }
63 
GetWidth()64     const Dimension& GetWidth() const
65     {
66         return width_;
67     }
68 
GetHeight()69     const Dimension& GetHeight() const
70     {
71         return height_;
72     }
73 
GetOffset()74     const DimensionOffset& GetOffset() const
75     {
76         return offset_;
77     }
78 
SetWidth(const Dimension & width)79     void SetWidth(const Dimension& width)
80     {
81         width_ = width;
82     }
83 
SetHeight(const Dimension & height)84     void SetHeight(const Dimension& height)
85     {
86         height_ = height;
87     }
88 
SetOffset(const DimensionOffset & offset)89     void SetOffset(const DimensionOffset& offset)
90     {
91         offset_ = offset;
92     }
93 
SetColor(const Color & color)94     void SetColor(const Color& color)
95     {
96         color_ = color;
97     }
98 
SetPosition(const DimensionOffset & position)99     void SetPosition(const DimensionOffset& position)
100     {
101         position_ = position;
102     }
103 
GetPosition()104     const DimensionOffset& GetPosition() const
105     {
106         return position_;
107     }
108 
GetColor()109     Color GetColor() const
110     {
111         return color_;
112     }
113 
114     bool operator==(const BasicShape& other) const
115     {
116         return (basicShapeType_ == other.GetBasicShapeType() && width_ == other.GetWidth() &&
117                 height_ == other.GetHeight() && offset_ == other.GetOffset() && color_ == other.GetColor());
118     }
119 
120     BasicShape& operator=(const BasicShape& other)
121     {
122         if (this != &other) {
123             basicShapeType_ = other.basicShapeType_;
124             width_ = other.width_;
125             height_ = other.height_;
126             offset_ = other.offset_;
127             color_ = other.color_;
128         }
129         return *this;
130     }
131 
132 protected:
133     BasicShapeType basicShapeType_ = BasicShapeType::NONE;
134     Dimension width_;
135     Dimension height_;
136     DimensionOffset offset_;
137     DimensionOffset position_;
138     Color color_;
139 };
140 
141 // inset(<top> <right> <bottom> <left> round <top-radius> <right-radius> <bottom-radius> <left-radius>)
142 class Inset : public BasicShape {
143     DECLARE_ACE_TYPE(Inset, BasicShape);
144 
145 public:
Inset()146     Inset() : BasicShape(BasicShapeType::INSET) {}
147     ~Inset() override = default;
148     bool SetLength(const std::vector<Dimension>& lengths);
149     void SetRadius(const std::vector<Dimension>& rounds, bool isX = true);
150 
GetTop()151     const Dimension& GetTop() const
152     {
153         return top_;
154     }
155 
GetRight()156     const Dimension& GetRight() const
157     {
158         return right_;
159     }
160 
GetBottom()161     const Dimension& GetBottom() const
162     {
163         return bottom_;
164     }
165 
GetLeft()166     const Dimension& GetLeft() const
167     {
168         return left_;
169     }
170 
GetTopLeftRadius()171     const Radius& GetTopLeftRadius() const
172     {
173         return topLeftRadius_;
174     }
175 
GetTopRightRadius()176     const Radius& GetTopRightRadius() const
177     {
178         return topRightRadius_;
179     }
180 
GetBottomRightRadius()181     const Radius& GetBottomRightRadius() const
182     {
183         return bottomRightRadius_;
184     }
185 
GetBottomLeftRadius()186     const Radius& GetBottomLeftRadius() const
187     {
188         return bottomLeftRadius_;
189     }
190 
SetTop(const Dimension & top)191     void SetTop(const Dimension& top)
192     {
193         top_ = top;
194     }
195 
SetRight(const Dimension & right)196     void SetRight(const Dimension& right)
197     {
198         right_ = right;
199     }
200 
SetBottom(const Dimension & bottom)201     void SetBottom(const Dimension& bottom)
202     {
203         bottom_ = bottom;
204     }
205 
SetLeft(const Dimension & left)206     void SetLeft(const Dimension& left)
207     {
208         left_ = left;
209     }
210 
211     void SetTopLeftRadius(const Dimension& topLeftRadius, bool isX = true)
212     {
213         if (!topLeftRadius.IsValid()) {
214             return;
215         }
216         if (isX) {
217             topLeftRadius_.SetX(topLeftRadius);
218         }
219         topLeftRadius_.SetY(topLeftRadius);
220     }
221 
222     void SetTopRightRadius(const Dimension& topRightRadius, bool isX = true)
223     {
224         if (!topRightRadius.IsValid()) {
225             return;
226         }
227         if (isX) {
228             topRightRadius_.SetX(topRightRadius);
229         }
230         topRightRadius_.SetY(topRightRadius);
231     }
232 
233     void SetBottomRightRadius(const Dimension& bottomRightRadius, bool isX = true)
234     {
235         if (!bottomRightRadius.IsValid()) {
236             return;
237         }
238         if (isX) {
239             bottomRightRadius_.SetX(bottomRightRadius);
240         }
241         bottomRightRadius_.SetY(bottomRightRadius);
242     }
243 
244     void SetBottomLeftRadius(const Dimension& bottomLeftRadius, bool isX = true)
245     {
246         if (!bottomLeftRadius.IsValid()) {
247             return;
248         }
249         if (isX) {
250             bottomLeftRadius_.SetX(bottomLeftRadius);
251         }
252         bottomLeftRadius_.SetY(bottomLeftRadius);
253     }
254 
255 private:
256     void SetLength(const Dimension& top, const Dimension& right, const Dimension& bottom, const Dimension& left);
257     void SetRadius(
258         const Dimension& top, const Dimension& right, const Dimension& bottom, const Dimension& left, bool isX);
259 
260     Dimension top_;
261     Dimension right_;
262     Dimension bottom_;
263     Dimension left_;
264     Radius topLeftRadius_;
265     Radius topRightRadius_;
266     Radius bottomRightRadius_;
267     Radius bottomLeftRadius_;
268 };
269 
270 // circle(radius at x-axis y-axis)
271 class Circle : public BasicShape {
272     DECLARE_ACE_TYPE(Circle, BasicShape);
273 
274 public:
Circle()275     Circle() : BasicShape(BasicShapeType::CIRCLE) {}
276     ~Circle() override = default;
277 
GetRadius()278     const Dimension& GetRadius() const
279     {
280         return radius_;
281     }
282 
GetAxisX()283     const Dimension& GetAxisX() const
284     {
285         return axisX_;
286     }
287 
GetAxisY()288     const Dimension& GetAxisY() const
289     {
290         return axisY_;
291     }
292 
SetRadius(const Dimension & radius)293     void SetRadius(const Dimension& radius)
294     {
295         if (radius.IsValid()) {
296             radius_ = radius;
297         }
298     }
299 
SetAxisX(const Dimension & axisX)300     void SetAxisX(const Dimension& axisX)
301     {
302         axisX_ = axisX;
303     }
304 
SetAxisY(const Dimension & axisY)305     void SetAxisY(const Dimension& axisY)
306     {
307         axisY_ = axisY;
308     }
309 
310     bool operator==(const Circle& other) const
311     {
312         return (radius_ == other.GetRadius() && axisX_ == other.GetAxisX() && axisY_ == other.GetAxisY());
313     }
314 
315 private:
316     Dimension radius_;
317     Dimension axisX_ = Dimension(0.5, DimensionUnit::PERCENT);
318     Dimension axisY_ = Dimension(0.5, DimensionUnit::PERCENT);
319 };
320 
321 // ellipse(x-rad y-rad at x-axis y-axis)
322 class Ellipse : public BasicShape {
323     DECLARE_ACE_TYPE(Ellipse, BasicShape);
324 
325 public:
Ellipse()326     Ellipse() : BasicShape(BasicShapeType::ELLIPSE) {}
327     ~Ellipse() override = default;
328 
GetRadiusX()329     const Dimension& GetRadiusX() const
330     {
331         return radiusX_;
332     }
333 
GetRadiusY()334     const Dimension& GetRadiusY() const
335     {
336         return radiusY_;
337     }
338 
GetAxisX()339     const Dimension& GetAxisX() const
340     {
341         return axisX_;
342     }
343 
GetAxisY()344     const Dimension& GetAxisY() const
345     {
346         return axisY_;
347     }
348 
SetRadiusX(const Dimension & radiusX)349     void SetRadiusX(const Dimension& radiusX)
350     {
351         if (radiusX.IsValid()) {
352             radiusX_ = radiusX;
353         }
354     }
355 
SetRadiusY(const Dimension & radiusY)356     void SetRadiusY(const Dimension& radiusY)
357     {
358         if (radiusY.IsValid()) {
359             radiusY_ = radiusY;
360         }
361     }
362 
SetAxisX(const Dimension & axisX)363     void SetAxisX(const Dimension& axisX)
364     {
365         axisX_ = axisX;
366     }
367 
SetAxisY(const Dimension & axisY)368     void SetAxisY(const Dimension& axisY)
369     {
370         axisY_ = axisY;
371     }
372 
373     bool operator==(const Ellipse& other) const
374     {
375         return (radiusX_ == other.GetRadiusX() && radiusY_ == other.GetRadiusY() && axisX_ == other.GetAxisX() &&
376                 axisY_ == other.GetAxisY());
377     }
378 
379 private:
380     Dimension radiusX_;
381     Dimension radiusY_;
382     Dimension axisX_ = Dimension(0.5, DimensionUnit::PERCENT);
383     Dimension axisY_ = Dimension(0.5, DimensionUnit::PERCENT);
384 };
385 
386 // polygon(x-axis y-axis, x-axis y-axis, … )
387 class Polygon : public BasicShape {
388     DECLARE_ACE_TYPE(Polygon, BasicShape);
389 
390 public:
Polygon()391     Polygon() : BasicShape(BasicShapeType::POLYGON) {}
392     ~Polygon() override = default;
393 
GetPoints()394     const std::vector<std::pair<Dimension, Dimension>>& GetPoints() const
395     {
396         return points_;
397     }
398 
PushPoint(const Dimension & x,const Dimension & y)399     void PushPoint(const Dimension& x, const Dimension& y)
400     {
401         points_.emplace_back(std::make_pair(x, y));
402     }
403 
IsValid()404     bool IsValid() const
405     {
406         return !points_.empty();
407     }
408 
409     bool operator==(const Polygon& other) const
410     {
411         return points_ == other.GetPoints();
412     }
413 
414 private:
415     std::vector<std::pair<Dimension, Dimension>> points_;
416 };
417 
418 // path('value')
419 class Path : public BasicShape {
420     DECLARE_ACE_TYPE(Path, BasicShape);
421 
422 public:
Path()423     Path() : BasicShape(BasicShapeType::PATH) {}
424     ~Path() override = default;
425 
GetValue()426     const std::string& GetValue() const
427     {
428         return value_;
429     }
430 
SetValue(const std::string & value)431     void SetValue(const std::string& value)
432     {
433         value_ = value;
434     }
435 
436     bool operator==(const Path& other) const
437     {
438         return value_ == other.GetValue();
439     }
440 
441 private:
442     std::string value_;
443 };
444 
445 class ShapeRect : public BasicShape {
446     DECLARE_ACE_TYPE(ShapeRect, BasicShape);
447 
448 public:
ShapeRect()449     ShapeRect() : BasicShape(BasicShapeType::RECT) {}
450     ~ShapeRect() override = default;
451 
SetTopLeftRadius(const Radius & topLeftRadius)452     void SetTopLeftRadius(const Radius& topLeftRadius)
453     {
454         topLeftRadius_ = topLeftRadius;
455     }
456 
SetTopRightRadius(const Radius & topRightRadius)457     void SetTopRightRadius(const Radius& topRightRadius)
458     {
459         topRightRadius_ = topRightRadius;
460     }
461 
SetBottomRightRadius(const Radius & bottomRightRadius)462     void SetBottomRightRadius(const Radius& bottomRightRadius)
463     {
464         bottomRightRadius_ = bottomRightRadius;
465     }
466 
SetBottomLeftRadius(const Radius & bottomLeftRadius)467     void SetBottomLeftRadius(const Radius& bottomLeftRadius)
468     {
469         bottomLeftRadius_ = bottomLeftRadius;
470     }
471 
GetTopLeftRadius()472     Radius GetTopLeftRadius() const
473     {
474         return topLeftRadius_;
475     }
476 
GetTopRightRadius()477     Radius GetTopRightRadius() const
478     {
479         return topRightRadius_;
480     }
481 
GetBottomRightRadius()482     Radius GetBottomRightRadius() const
483     {
484         return bottomRightRadius_;
485     }
486 
GetBottomLeftRadius()487     Radius GetBottomLeftRadius() const
488     {
489         return bottomLeftRadius_;
490     }
491 
492     void SetRadiusWidth(const Dimension& value, const AnimationOption& option = AnimationOption())
493     {
494         topLeftRadius_.SetX(value, option);
495         topRightRadius_.SetX(value, option);
496         bottomRightRadius_.SetX(value, option);
497         bottomLeftRadius_.SetX(value, option);
498     }
499 
500     void SetRadiusHeight(const Dimension& value, const AnimationOption& option = AnimationOption())
501     {
502         topLeftRadius_.SetY(value, option);
503         topRightRadius_.SetY(value, option);
504         bottomRightRadius_.SetY(value, option);
505         bottomLeftRadius_.SetY(value, option);
506     }
507 
508 private:
509     Radius topLeftRadius_ = Radius(-1.0);
510     Radius topRightRadius_ = Radius(-1.0);
511     Radius bottomRightRadius_ = Radius(-1.0);
512     Radius bottomLeftRadius_ = Radius(-1.0);
513 };
514 
515 } // namespace OHOS::Ace
516 
517 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_SHAPE_H