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