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 #include "c/drawing_point.h"
17 #include "c/drawing_path.h"
18
19 #include "draw/path.h"
20
21 using namespace OHOS;
22 using namespace Rosen;
23 using namespace Drawing;
24
CastToPath(OH_Drawing_Path * cPath)25 static Path* CastToPath(OH_Drawing_Path* cPath)
26 {
27 return reinterpret_cast<Path*>(cPath);
28 }
29
CastToMatrix(const OH_Drawing_Matrix * cMatrix)30 static const Matrix* CastToMatrix(const OH_Drawing_Matrix* cMatrix)
31 {
32 return reinterpret_cast<const Matrix*>(cMatrix);
33 }
34
CastToRect(const OH_Drawing_Rect & cRect)35 static const Rect& CastToRect(const OH_Drawing_Rect& cRect)
36 {
37 return reinterpret_cast<const Rect&>(cRect);
38 }
39
CastToRoundRect(const OH_Drawing_RoundRect & cRoundRect)40 static const RoundRect& CastToRoundRect(const OH_Drawing_RoundRect& cRoundRect)
41 {
42 return reinterpret_cast<const RoundRect&>(cRoundRect);
43 }
44
OH_Drawing_PathCreate()45 OH_Drawing_Path* OH_Drawing_PathCreate()
46 {
47 return (OH_Drawing_Path*)new Path;
48 }
49
OH_Drawing_PathCopy(OH_Drawing_Path * cPath)50 OH_Drawing_Path* OH_Drawing_PathCopy(OH_Drawing_Path* cPath)
51 {
52 Path* path = CastToPath(cPath);
53 if (path == nullptr) {
54 return nullptr;
55 }
56 return (OH_Drawing_Path*)new Path(*path);
57 }
58
OH_Drawing_PathDestroy(OH_Drawing_Path * cPath)59 void OH_Drawing_PathDestroy(OH_Drawing_Path* cPath)
60 {
61 delete CastToPath(cPath);
62 }
63
OH_Drawing_PathMoveTo(OH_Drawing_Path * cPath,float x,float y)64 void OH_Drawing_PathMoveTo(OH_Drawing_Path* cPath, float x, float y)
65 {
66 Path* path = CastToPath(cPath);
67 if (path == nullptr) {
68 return;
69 }
70 path->MoveTo(x, y);
71 }
72
OH_Drawing_PathLineTo(OH_Drawing_Path * cPath,float x,float y)73 void OH_Drawing_PathLineTo(OH_Drawing_Path* cPath, float x, float y)
74 {
75 Path* path = CastToPath(cPath);
76 if (path == nullptr) {
77 return;
78 }
79 path->LineTo(x, y);
80 }
81
OH_Drawing_PathArcTo(OH_Drawing_Path * cPath,float x1,float y1,float x2,float y2,float startDeg,float sweepDeg)82 void OH_Drawing_PathArcTo(
83 OH_Drawing_Path* cPath, float x1, float y1, float x2, float y2, float startDeg, float sweepDeg)
84 {
85 Path* path = CastToPath(cPath);
86 if (path == nullptr) {
87 return;
88 }
89 path->ArcTo(x1, y1, x2, y2, startDeg, sweepDeg);
90 }
91
OH_Drawing_PathQuadTo(OH_Drawing_Path * cPath,float ctrlX,float ctrlY,float endX,float endY)92 void OH_Drawing_PathQuadTo(OH_Drawing_Path* cPath, float ctrlX, float ctrlY, float endX, float endY)
93 {
94 Path* path = CastToPath(cPath);
95 if (path == nullptr) {
96 return;
97 }
98 path->QuadTo(ctrlX, ctrlY, endX, endY);
99 }
100
OH_Drawing_PathCubicTo(OH_Drawing_Path * cPath,float ctrlX1,float ctrlY1,float ctrlX2,float ctrlY2,float endX,float endY)101 void OH_Drawing_PathCubicTo(
102 OH_Drawing_Path* cPath, float ctrlX1, float ctrlY1, float ctrlX2, float ctrlY2, float endX, float endY)
103 {
104 Path* path = CastToPath(cPath);
105 if (path == nullptr) {
106 return;
107 }
108 path->CubicTo(ctrlX1, ctrlY1, ctrlX2, ctrlY2, endX, endY);
109 }
110
OH_Drawing_PathAddRect(OH_Drawing_Path * cPath,float left,float top,float right,float bottom,OH_Drawing_PathDirection dir)111 void OH_Drawing_PathAddRect(OH_Drawing_Path* cPath, float left,
112 float top, float right, float bottom, OH_Drawing_PathDirection dir)
113 {
114 Path* path = CastToPath(cPath);
115 if (path == nullptr) {
116 return;
117 }
118 path->AddRect(left, top, right, bottom, static_cast<PathDirection>(dir));
119 }
120
OH_Drawing_PathAddRoundRect(OH_Drawing_Path * cPath,const OH_Drawing_RoundRect * roundRect,OH_Drawing_PathDirection dir)121 void OH_Drawing_PathAddRoundRect(OH_Drawing_Path* cPath,
122 const OH_Drawing_RoundRect* roundRect, OH_Drawing_PathDirection dir)
123 {
124 if (roundRect == nullptr) {
125 return;
126 }
127 Path* path = CastToPath(cPath);
128 if (path == nullptr) {
129 return;
130 }
131 path->AddRoundRect(CastToRoundRect(*roundRect), static_cast<PathDirection>(dir));
132 }
133
OH_Drawing_PathAddArc(OH_Drawing_Path * cPath,const OH_Drawing_Rect * oval,float startAngle,float sweepAngle)134 void OH_Drawing_PathAddArc(OH_Drawing_Path* cPath,
135 const OH_Drawing_Rect* oval, float startAngle, float sweepAngle)
136 {
137 if (oval == nullptr) {
138 return;
139 }
140 Path* path = CastToPath(cPath);
141 if (path == nullptr) {
142 return;
143 }
144 path->AddArc(CastToRect(*oval), startAngle, sweepAngle);
145 }
146
OH_Drawing_PathAddPath(OH_Drawing_Path * cPath,const OH_Drawing_Path * src,const OH_Drawing_Matrix * matrix)147 void OH_Drawing_PathAddPath(OH_Drawing_Path* cPath,
148 const OH_Drawing_Path* src, const OH_Drawing_Matrix* matrix)
149 {
150 if (src == nullptr || matrix == nullptr) {
151 return;
152 }
153 Path* path = CastToPath(cPath);
154 if (path == nullptr) {
155 return;
156 }
157 Path* srcPath = CastToPath(const_cast<OH_Drawing_Path*>(src));
158 path->AddPath(*srcPath, *CastToMatrix(matrix));
159 }
160
OH_Drawing_PathContains(OH_Drawing_Path * cPath,float x,float y)161 bool OH_Drawing_PathContains(OH_Drawing_Path* cPath, float x, float y)
162 {
163 Path* path = CastToPath(cPath);
164 if (path == nullptr) {
165 return false ;
166 }
167 return path->Contains(x, y);
168 }
169
OH_Drawing_PathTransform(OH_Drawing_Path * cPath,const OH_Drawing_Matrix * matrix)170 void OH_Drawing_PathTransform(OH_Drawing_Path* cPath, const OH_Drawing_Matrix* matrix)
171 {
172 if (matrix == nullptr) {
173 return;
174 }
175 Path* path = CastToPath(cPath);
176 if (path == nullptr) {
177 return;
178 }
179 path->Transform(*CastToMatrix(matrix));
180 }
181
OH_Drawing_SetFillStyle(OH_Drawing_Path * cPath,OH_Drawing_PathFillType fillstyle)182 void OH_Drawing_SetFillStyle(OH_Drawing_Path* cPath, OH_Drawing_PathFillType fillstyle)
183 {
184 Path* path = CastToPath(cPath);
185 if (path == nullptr) {
186 return;
187 }
188 path->SetFillStyle(static_cast<PathFillType>(fillstyle));
189 }
190
OH_Drawing_PathClose(OH_Drawing_Path * cPath)191 void OH_Drawing_PathClose(OH_Drawing_Path* cPath)
192 {
193 Path* path = CastToPath(cPath);
194 if (path == nullptr) {
195 return;
196 }
197 path->Close();
198 }
199
OH_Drawing_PathReset(OH_Drawing_Path * cPath)200 void OH_Drawing_PathReset(OH_Drawing_Path* cPath)
201 {
202 Path* path = CastToPath(cPath);
203 if (path == nullptr) {
204 return;
205 }
206 path->Reset();
207 }
208