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