1 /*
2  * Copyright (c) 2021-2024 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 SKIA_PATH_H
17 #define SKIA_PATH_H
18 
19 #include <unordered_map>
20 
21 #include "include/core/SkPath.h"
22 #include "include/core/SkPathMeasure.h"
23 
24 #include "impl_interface/path_impl.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 class DRAWING_API SkiaPath : public PathImpl {
30 public:
31     static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER;
32 
SkiaPath()33     SkiaPath() noexcept {};
~SkiaPath()34     ~SkiaPath() override {};
35     SkiaPath(const SkiaPath& p) noexcept;
36     SkiaPath &operator=(const SkiaPath& p) noexcept;
37 
GetType()38     AdapterType GetType() const override
39     {
40         return AdapterType::SKIA_ADAPTER;
41     }
42 
43     PathImpl* Clone() override;
44 
45     bool InitWithSVGString(const std::string& str) override;
46     std::string ConvertToSVGString() const override;
47 
48     bool InitWithInterpolate(const Path& srcPath, const Path& endingPath, scalar weight) override;
49 
50     void MoveTo(scalar x, scalar y) override;
51     void LineTo(scalar x, scalar y) override;
52     void ArcTo(scalar pt1X, scalar pt1Y, scalar pt2X, scalar pt2Y, scalar startAngle, scalar sweepAngle) override;
53     void ArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar endX, scalar endY) override;
54     void ArcTo(scalar x1, scalar y1, scalar x2, scalar y2, scalar radius) override;
55     void CubicTo(
56         scalar ctrlPt1X, scalar ctrlPt1Y, scalar ctrlPt2X, scalar ctrlPt2Y, scalar endPtX, scalar endPtY) override;
57     void QuadTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY) override;
58     void ConicTo(scalar ctrlX, scalar ctrlY, scalar endX, scalar endY, scalar weight) override;
59 
60     void RMoveTo(scalar dx, scalar dy) override;
61     void RLineTo(scalar dx, scalar dy) override;
62     void RArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar dx, scalar dy) override;
63     void RCubicTo(scalar dx1, scalar dy1, scalar dx2, scalar dy2, scalar dx3, scalar dy3) override;
64     void RConicTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY, scalar weight) override;
65     void RQuadTo(scalar dx1, scalar dy1, scalar dx2, scalar dy2) override;
66 
67     void AddRect(scalar left, scalar top, scalar right, scalar bottom, PathDirection dir) override;
68     void AddRect(const Rect& rect, unsigned start, PathDirection dir) override;
69     void AddOval(scalar left, scalar top, scalar right, scalar bottom, PathDirection dir) override;
70     void AddOval(scalar left, scalar top, scalar right, scalar bottom, unsigned start, PathDirection dir) override;
71     void AddArc(scalar left, scalar top, scalar right, scalar bottom, scalar startAngle, scalar sweepAngle) override;
72     void AddPoly(const std::vector<Point>& points, int count, bool close) override;
73     void AddCircle(scalar x, scalar y, scalar radius, PathDirection dir) override;
74     void AddRoundRect(scalar left, scalar top, scalar right, scalar bottom, scalar xRadius, scalar yRadius,
75         PathDirection dir) override;
76     void AddRoundRect(const RoundRect& rrect, PathDirection dir) override;
77 
78     void AddPath(const Path& src, scalar dx, scalar dy, PathAddMode mode) override;
79     void AddPath(const Path& src, PathAddMode mode) override;
80     bool Contains(scalar x, scalar y) const override;
81     void AddPath(const Path& src, const Matrix& matrix, PathAddMode mode) override;
82     void ReverseAddPath(const Path& src) override;
83 
84     Rect GetBounds() const override;
85     void SetFillStyle(PathFillType fillstyle) override;
86 
87     bool Interpolate(const Path& ending, scalar weight, Path& out) override;
88     void Transform(const Matrix& matrix) override;
89     void TransformWithPerspectiveClip(const Matrix& matrix, Path* dst, bool applyPerspectiveClip) override;
90     void Offset(scalar dx, scalar dy) override;
91     void Offset(Path* dst, scalar dx, scalar dy) override;
92     bool OpWith(const Path& path1, const Path& path2, PathOp op) override;
93 
94     bool IsValid() const override;
95     void Reset() override;
96 
97     void Close() override;
98 
99     void SetPath(const SkPath& path);
100 
101     const SkPath& GetPath() const;
102 
103     SkPath& GetMutablePath();
104 
105     void PathMeasureUpdate(bool forceClosed);
106     scalar GetLength(bool forceClosed) override;
107     bool GetPositionAndTangent(scalar distance, Point& position, Point& tangent, bool forceClosed) override;
108     bool IsClosed(bool forceClosed) override;
109     bool GetMatrix(bool forceClosed, float distance, Matrix* matrix, PathMeasureMatrixFlags flag) override;
110 
111     std::shared_ptr<Data> Serialize() const override;
112     bool Deserialize(std::shared_ptr<Data> data) override;
113 private:
114     SkPath path_;
115     std::unique_ptr<SkPathMeasure> pathMeasure_ = nullptr;
116 
117     // Records if Path has changed, and if it is true, update pathmeasure if needed.
118     bool isChanged_ = true;
119     bool forceClosed_ = false;
120 };
121 } // namespace Drawing
122 } // namespace Rosen
123 } // namespace OHOS
124 #endif
125