• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 PATH_H
17 #define PATH_H
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "common/rs_macros.h"
23 #include "drawing/engine_adapter/impl_interface/path_impl.h"
24 #include "utils/drawing_macros.h"
25 #include "utils/matrix.h"
26 #include "utils/point.h"
27 #include "utils/rect.h"
28 
29 #ifdef WINDOWS_PLATFORM
30 #ifdef DIFFERENCE
31 #undef DIFFERENCE
32 #endif
33 #ifdef WINDING
34 #undef WINDING
35 #endif
36 #endif
37 
38 namespace OHOS {
39 namespace Rosen {
40 namespace Drawing {
41 enum class PathDirection {
42     CW_DIRECTION,
43     CCW_DIRECTION,
44 };
45 
46 enum class PathFillType {
47     WINDING,
48     EVENTODD,
49     INVERSE_WINDING,
50     INVERSE_EVENTODD,
51 };
52 
53 enum class PathOp {
54     DIFFERENCE,
55     INTERSECT,
56     UNION,
57     XOR,
58     REVERSE_DIFFERENCE,
59 };
60 
61 #ifndef USE_ROSEN_DRAWING
62 class RS_EXPORT Path {
63 #else
64 class DRAWING_API Path {
65 #endif
66 public:
67     Path() noexcept;
68     Path(const Path& p) noexcept;
69     Path &operator=(const Path& p) noexcept;
70     virtual ~Path();
71 
GetDrawingType()72     virtual DrawingType GetDrawingType() const
73     {
74         return DrawingType::COMMON;
75     }
76 
77     /*
78      * @brief       Parses the SVG format string that describes the drawing path, and sets the Path.
79      * @param str   A string in SVG format that describes the drawing path.
80      */
81     virtual bool BuildFromSVGString(const std::string& str);
82 
83     /*
84      * @brief   Parses into a string in SVG format that describes the Path.
85      */
86     std::string ConvertToSVGString() const;
87     virtual void MoveTo(scalar x, scalar y);
88     virtual void LineTo(scalar x, scalar y);
89     virtual void ArcTo(scalar pt1X, scalar pt1Y, scalar pt2X, scalar pt2Y, scalar startAngle, scalar sweepAngle);
90     virtual void ArcTo(const Point& pt1, const Point& pt2, scalar startAngle, scalar sweepAngle);
91     virtual void ArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar endX, scalar endY);
92     virtual void ArcTo(scalar x1, scalar y1, scalar x2, scalar y2, scalar radius);
93     virtual void CubicTo(
94         scalar ctrlPt1X, scalar ctrlPt1Y, scalar ctrlPt2X, scalar ctrlPt2Y, scalar endPtX, scalar endPtY);
95     virtual void CubicTo(const Point& ctrlPt1, const Point& ctrlPt2, const Point& endPt);
96     virtual void QuadTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY);
97     virtual void QuadTo(const Point& ctrlPt, const Point endPt);
98 
99     virtual void RMoveTo(scalar dx, scalar dy);
100     virtual void RLineTo(scalar dx, scalar dy);
101     virtual void RArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar dx, scalar dy);
102     virtual void RCubicTo(scalar dx1, scalar dy1, scalar dx2, scalar dy2, scalar dx3, scalar dy3);
103     virtual void RQuadTo(scalar dx1, scalar dy1, scalar dx2, scalar dy2);
104 
105     virtual void AddRect(const Rect& rect, PathDirection dir = PathDirection::CW_DIRECTION);
106     virtual void AddRect(
107         scalar left, scalar top, scalar right, scalar bottom, PathDirection dir = PathDirection::CW_DIRECTION);
108 
109     virtual void AddOval(const Rect& oval, PathDirection dir = PathDirection::CW_DIRECTION);
110     virtual void AddArc(const Rect& oval, scalar startAngle, scalar sweepAngle);
111     virtual void AddPoly(const std::vector<Point>& points, int count, bool close);
112     virtual void AddCircle(scalar x, scalar y, scalar radius, PathDirection dir = PathDirection::CW_DIRECTION);
113     virtual void AddRoundRect(
114         const Rect& rect, scalar xRadius, scalar yRadius, PathDirection dir = PathDirection::CW_DIRECTION);
115 
116     /*
117      * @brief             Adds the circle rectangle to the Path.
118      * @param roundRect   The boundary and radius of a roundRect.
119      * @param dir         Direction of rotation.
120      */
121     virtual void AddRoundRect(const RoundRect& roundRect, PathDirection dir = PathDirection::CW_DIRECTION);
122 
123     virtual void AddPath(const Path& src, scalar dx, scalar dy);
124     virtual void AddPath(const Path& src);
125     virtual void AddPath(const Path& src, const Matrix& matrix);
126     virtual bool Contains(scalar x, scalar y) const;
127 
128     /*
129      * @brief       Adds the src from back forward to the Path.
130      * @param src   To add Path.
131      */
132     virtual void ReverseAddPath(const Path& src);
133 
134     Rect GetBounds() const;
135     virtual void SetFillStyle(PathFillType fillstyle);
136 
137     bool Interpolate(const Path& ending, scalar weight, Path& out);
138 
139     /*
140      * @brief         Two equal number of point set path objects are weighted interpolated, and the sets Path.
141      * @param src     The number of point sets of the src Path.
142      * @param ending  The number of point sets of the ending Path.
143      * @param weight  The weight value is between 0 and 1.
144      */
145     virtual bool BuildFromInterpolate(const Path& src, const Path& ending, scalar weight);
146     virtual void Transform(const Matrix& matrix);
147     virtual void Offset(scalar dx, scalar dy);
148     virtual bool Op(const Path& path1, Path& path2, PathOp op);
149 
150     /*
151      * @brief   Checks whether the Path is valid.
152      */
153     bool IsValid() const;
154     virtual void Reset();
155 
156     virtual void Close();
157 
158     /*
159      * @brief               Gets the length of the current path object.
160      * @param forceClosed   Whether to close the Path.
161      */
162     scalar GetLength(bool forceClosed) const;
163 
164     /*
165      * @brief              Gets the position and tangent of the distance from the starting position of the Path.
166      * @param distance     The distance from the start of the Path, should be greater than 0 and less than 'GetLength()'
167      * @param position     Sets to the position of distance from the starting position of the Path.
168      * @param tangent      Sets to the tangent of distance from the starting position of the Path.
169      * @param forceClosed  Whether to close the Path.
170      */
171     bool GetPositionAndTangent(scalar distance, Point& position, Point& tangent, bool forceClosed) const;
172 
173     std::shared_ptr<Data> Serialize() const;
174     bool Deserialize(std::shared_ptr<Data> data);
175 
176     template<typename T>
GetImpl()177     T* GetImpl() const
178     {
179         return impl_->DowncastingTo<T>();
180     }
181 
182 private:
183     std::shared_ptr<PathImpl> impl_;
184 };
185 } // namespace Drawing
186 } // namespace Rosen
187 } // namespace OHOS
188 #endif
189