1 /*
2 * Copyright (c) 2022 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 "path_test.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace Drawing {
21 constexpr static float MARGINE_SCALE_SIZE = 10.0f;
TestDrawStar(Canvas & canvas,uint32_t width,uint32_t height)22 void PathTest::TestDrawStar(Canvas& canvas, uint32_t width, uint32_t height)
23 {
24 LOGI("+++++++ TestDrawStar");
25 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
26 Rect rect(margin, margin, width - margin, height - margin);
27 int len = rect.GetWidth() - rect.GetWidth() / 5.0f; // set len size
28 // half of width
29 Point a(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + rect.GetHeight() / 2.0f - len * std::cos(18.0f));
30 Point c;
31 Point d;
32
33 // five star angle is 36
34 d.SetX(a.GetX() - len * std::sin(18.0f));
35 d.SetY(a.GetY() + len * std::cos(18.0f));
36
37 c.SetX(a.GetX() + len * std::sin(18.0f));
38 c.SetY(d.GetY());
39
40 Point b;
41 b.SetX(a.GetX() + (len / 2.0)); // half of len, for point position calculation
42 b.SetY(a.GetY() + std::sqrt((c.GetX() - d.GetX()) * (c.GetX() - d.GetX()) + (len / 2.0) * (len / 2.0)));
43
44 Point e;
45 e.SetX(a.GetX() - (len / 2.0)); // half of len, for point position calculation
46 e.SetY(b.GetY());
47
48 Path path;
49 path.MoveTo(a.GetX(), a.GetY());
50 path.LineTo(b.GetX(), b.GetY());
51 path.LineTo(c.GetX(), c.GetY());
52 path.LineTo(d.GetX(), d.GetY());
53 path.LineTo(e.GetX(), e.GetY());
54 path.Close();
55
56 Pen pen;
57 pen.SetAntiAlias(true);
58 pen.SetColor(Drawing::Color::COLOR_RED);
59 pen.SetWidth(10); // The thickness of the pen is 10
60 canvas.AttachPen(pen);
61
62 Brush brush;
63 brush.SetColor(Drawing::Color::COLOR_BLUE);
64 canvas.AttachBrush(brush);
65
66 canvas.AttachPen(pen).AttachBrush(brush).DrawPath(path);
67 }
68
PathTestCase()69 std::vector<PathTest::TestFunc> PathTest::PathTestCase()
70 {
71 std::vector<TestFunc> testFuncVec;
72 testFuncVec.push_back(TestDrawStar);
73 testFuncVec.push_back(TestMoveTo);
74 testFuncVec.push_back(TestLineTo);
75 testFuncVec.push_back(TestArcTo);
76 testFuncVec.push_back(TestCubicTo);
77 testFuncVec.push_back(TestQuadTo);
78
79 testFuncVec.push_back(TestAddRect);
80 testFuncVec.push_back(TestAddOval);
81 testFuncVec.push_back(TestAddArc);
82 testFuncVec.push_back(TestAddPoly);
83 testFuncVec.push_back(TestAddCircle);
84 testFuncVec.push_back(TestAddRoundRect);
85 testFuncVec.push_back(TestAddPath);
86
87 testFuncVec.push_back(TestFillStyle);
88 testFuncVec.push_back(TestFillStyle2);
89 testFuncVec.push_back(TestFillStyle3);
90 testFuncVec.push_back(TestFillStyle4);
91
92 testFuncVec.push_back(TestOffset);
93 testFuncVec.push_back(TestTransform);
94 testFuncVec.push_back(TestOp);
95 testFuncVec.push_back(TestOp2);
96 testFuncVec.push_back(TestOp3);
97 testFuncVec.push_back(TestOp4);
98 testFuncVec.push_back(TestOp5);
99 testFuncVec.push_back(TestClose);
100 return testFuncVec;
101 }
102
TestMoveTo(Canvas & canvas,uint32_t width,uint32_t height)103 void PathTest::TestMoveTo(Canvas& canvas, uint32_t width, uint32_t height)
104 {
105 LOGI("+++++++ TestMoveTo");
106 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
107 Rect rect(margin, margin, width - margin, height - margin);
108 Path path;
109 path.AddRect(rect);
110 path.MoveTo(rect.GetLeft(), rect.GetTop());
111 path.LineTo(rect.GetRight(), rect.GetBottom());
112 path.MoveTo(rect.GetRight(), rect.GetTop());
113 path.LineTo(rect.GetLeft(), rect.GetBottom());
114 // half of width
115 path.MoveTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop());
116 // half of width
117 path.LineTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom());
118 path.MoveTo(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 2.0f);
119 path.LineTo(rect.GetRight(), rect.GetTop() + rect.GetHeight() / 2.0f);
120
121 Pen pen;
122 pen.SetAntiAlias(true);
123 pen.SetColor(Drawing::Color::COLOR_BLACK);
124 canvas.AttachPen(pen).DrawPath(path);
125 }
126
TestLineTo(Canvas & canvas,uint32_t width,uint32_t height)127 void PathTest::TestLineTo(Canvas& canvas, uint32_t width, uint32_t height)
128 {
129 LOGI("+++++++ TestLineTo");
130 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
131 Rect rect(margin, margin, width - margin, height - margin);
132 std::vector<Point> linePoints;
133 // a quarter of width
134 linePoints.emplace_back(Point(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop()));
135 // a quarter of width
136 linePoints.emplace_back(Point(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetBottom()));
137 // a quarter of width
138 linePoints.emplace_back(Point(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetTop()));
139 // a quarter of width
140 linePoints.emplace_back(Point(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom()));
141 linePoints.emplace_back(Point(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 4.0f));
142 linePoints.emplace_back(Point(rect.GetRight(), rect.GetTop() + rect.GetHeight() / 4.0f));
143 linePoints.emplace_back(Point(rect.GetLeft(), rect.GetBottom() - rect.GetHeight() / 4.0f));
144 linePoints.emplace_back(Point(rect.GetRight(), rect.GetBottom() - rect.GetHeight() / 4.0f));
145
146 Path path;
147 // two point one line
148 for (size_t i = 0; i < linePoints.size(); i += 2) {
149 path.MoveTo(linePoints.at(i).GetX(), linePoints.at(i).GetY());
150 path.LineTo(linePoints.at(i + 1).GetX(), linePoints.at(i + 1).GetY());
151 }
152
153 Pen pen;
154 pen.SetAntiAlias(true);
155 pen.SetColor(Drawing::Color::COLOR_BLACK);
156 canvas.AttachPen(pen).DrawPath(path);
157 }
158
TestArcTo(Canvas & canvas,uint32_t width,uint32_t height)159 void PathTest::TestArcTo(Canvas& canvas, uint32_t width, uint32_t height)
160 {
161 LOGI("+++++++ TestArcTo");
162 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
163 Rect rect(margin, margin, width - margin, height - margin);
164 Path path;
165
166 // half of width
167 path.MoveTo(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 2.0f);
168 // start angle is -90, sweep angle is 270
169 path.ArcTo(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom(), -90, 270);
170
171 Pen pen;
172 pen.SetAntiAlias(true);
173 pen.SetColor(Drawing::Color::COLOR_BLACK);
174 canvas.AttachPen(pen).DrawRect(rect);
175
176 pen.SetColor(Drawing::Color::COLOR_RED);
177 canvas.AttachPen(pen).DrawPath(path);
178 }
179
TestCubicTo(Canvas & canvas,uint32_t width,uint32_t height)180 void PathTest::TestCubicTo(Canvas& canvas, uint32_t width, uint32_t height)
181 {
182 LOGI("+++++++ TestCubicTo");
183 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
184 Rect rect(margin, margin, width - margin, height - margin);
185 Path path;
186 path.MoveTo(rect.GetLeft(), rect.GetTop());
187 path.CubicTo(rect.GetLeft(), rect.GetBottom(), rect.GetRight(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
188
189 Path path2;
190 path2.MoveTo(rect.GetLeft(), rect.GetTop());
191 path2.LineTo(rect.GetLeft(), rect.GetBottom());
192 path2.LineTo(rect.GetRight(), rect.GetTop());
193 path2.LineTo(rect.GetRight(), rect.GetBottom());
194
195 Pen pen;
196 pen.SetAntiAlias(true);
197 pen.SetColor(Drawing::Color::COLOR_BLACK);
198 canvas.AttachPen(pen).DrawPath(path2);
199
200 pen.SetColor(Drawing::Color::COLOR_RED);
201 canvas.AttachPen(pen).DrawPath(path);
202 }
203
TestQuadTo(Canvas & canvas,uint32_t width,uint32_t height)204 void PathTest::TestQuadTo(Canvas& canvas, uint32_t width, uint32_t height)
205 {
206 LOGI("+++++++ TestQuadTo");
207 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
208 Rect rect(margin, margin, width - margin, height - margin);
209 Path path;
210 path.MoveTo(rect.GetLeft(), rect.GetTop());
211 // half of width
212 path.QuadTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom(), rect.GetRight(), rect.GetTop());
213
214 Path path2;
215 path2.MoveTo(rect.GetLeft(), rect.GetTop());
216 // half of width
217 path2.LineTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom());
218 path2.LineTo(rect.GetRight(), rect.GetTop());
219
220 Pen pen;
221 pen.SetAntiAlias(true);
222 pen.SetColor(Drawing::Color::COLOR_BLACK);
223 canvas.AttachPen(pen).DrawPath(path2);
224
225 pen.SetColor(Drawing::Color::COLOR_RED);
226 canvas.AttachPen(pen).DrawPath(path);
227 }
228
TestAddRect(Canvas & canvas,uint32_t width,uint32_t height)229 void PathTest::TestAddRect(Canvas& canvas, uint32_t width, uint32_t height)
230 {
231 LOGI("+++++++ TestAddRect");
232 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
233 Rect rect(margin, margin, width - margin, height - margin);
234 Path path;
235 path.AddRect(rect);
236
237 Pen pen;
238 pen.SetAntiAlias(true);
239 pen.SetColor(Drawing::Color::COLOR_BLACK);
240 pen.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
241 scalar intervals[] = { 5, 20.0f }; // solid line length is 5, dotted line length is 20.0
242 // intervals[] count is 2
243 pen.SetPathEffect(PathEffect::CreateDashPathEffect(intervals, 2, 0));
244 canvas.AttachPen(pen).DrawPath(path);
245
246 // a quarter of height
247 Rect rect2(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f,
248 rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
249 Path path2;
250 path2.AddRect(rect2);
251
252 Brush brush;
253 brush.SetAntiAlias(true);
254 brush.SetColor(Drawing::Color::COLOR_RED);
255 canvas.DetachPen().AttachBrush(brush).DrawPath(path2);
256 }
257
TestAddOval(Canvas & canvas,uint32_t width,uint32_t height)258 void PathTest::TestAddOval(Canvas& canvas, uint32_t width, uint32_t height)
259 {
260 LOGI("+++++++ TestAddOval");
261 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
262 Rect rect(margin, margin, width - margin, height - margin);
263 Path path;
264 path.AddOval(rect);
265
266 Pen pen;
267 pen.SetAntiAlias(true);
268 pen.SetColor(Drawing::Color::COLOR_BLACK);
269 scalar intervals[] = { 5, 20.0f }; // solid line length is 5, dotted line length is 20.0
270 // intervals[] count is 2
271 pen.SetPathEffect(PathEffect::CreateDashPathEffect(intervals, 2, 0));
272 canvas.AttachPen(pen).DrawPath(path);
273
274 // a quarter of height
275 Rect rect2(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f,
276 rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
277 Path path2;
278 path2.AddOval(rect2);
279
280 Brush brush;
281 brush.SetAntiAlias(true);
282 brush.SetColor(Drawing::Color::COLOR_RED);
283 canvas.DetachPen().AttachBrush(brush).DrawPath(path2);
284 }
285
TestAddArc(Canvas & canvas,uint32_t width,uint32_t height)286 void PathTest::TestAddArc(Canvas& canvas, uint32_t width, uint32_t height)
287 {
288 LOGI("+++++++ TestAddArc");
289 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
290 Rect rect(margin, margin, width - margin, height - margin);
291 Path path;
292 // start angle is -180, sweep angle is 180
293 path.AddArc(rect, -180, 180);
294
295 Pen pen;
296 pen.SetAntiAlias(true);
297 pen.SetColor(Drawing::Color::COLOR_BLACK);
298 canvas.AttachPen(pen).DrawPath(path);
299
300 Path path2;
301 // start angle is 0, sweep angle is 180
302 path2.AddArc(rect, 0, 180);
303
304 Brush brush;
305 brush.SetAntiAlias(true);
306 brush.SetColor(Drawing::Color::COLOR_RED);
307 canvas.DetachPen().AttachBrush(brush).DrawPath(path2);
308 }
309
TestAddPoly(Canvas & canvas,uint32_t width,uint32_t height)310 void PathTest::TestAddPoly(Canvas& canvas, uint32_t width, uint32_t height)
311 {
312 LOGI("+++++++ TestAddPoly");
313 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
314 Rect rect(margin, margin, width - margin, height - margin);
315 Path path;
316 // half of width
317 std::vector<Point> points = { Point(rect.GetLeft(), rect.GetTop()),
318 Point(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom()), Point(rect.GetRight(), rect.GetTop()) };
319 // points array count is 3
320 path.AddPoly(points, 3, true);
321
322 Brush brush;
323 brush.SetAntiAlias(true);
324 brush.SetColor(Drawing::Color::COLOR_RED);
325 canvas.DetachPen().AttachBrush(brush).DrawPath(path);
326 }
327
TestAddCircle(Canvas & canvas,uint32_t width,uint32_t height)328 void PathTest::TestAddCircle(Canvas& canvas, uint32_t width, uint32_t height)
329 {
330 LOGI("+++++++ TestAddCircle");
331 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
332 Rect rect(margin, margin, width - margin, height - margin);
333 // start radius is 10, radius length is increased by 20 at a time, not more than half the width
334 for (size_t i = 10; i < rect.GetWidth() / 2.0f; i += 20) {
335 Path path;
336 // half of width and height
337 path.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + rect.GetHeight() / 2.0f, i);
338 Pen pen;
339 pen.SetAntiAlias(true);
340 pen.SetColor(Drawing::Color::COLOR_BLACK);
341 pen.SetWidth(10); // The thickness of the pen is 10
342 canvas.AttachPen(pen).DrawPath(path);
343 }
344 }
345
TestAddRoundRect(Canvas & canvas,uint32_t width,uint32_t height)346 void PathTest::TestAddRoundRect(Canvas& canvas, uint32_t width, uint32_t height)
347 {
348 LOGI("+++++++ TestAddRoundRect");
349 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
350 Rect rect(margin, margin, width - margin, height - margin);
351 Path path;
352 // the corner radius is 30.0
353 path.AddRoundRect(rect, 30.0f, 30.0f);
354
355 Pen pen;
356 pen.SetAntiAlias(true);
357 pen.SetColor(Drawing::Color::COLOR_BLACK);
358 canvas.AttachPen(pen).DrawPath(path);
359
360 // a quarter of height
361 Rect rect2(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f,
362 rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
363 Path path2;
364 // the corner radius is 30.0
365 path2.AddRoundRect(rect2, 30.0f, 30.0f);
366
367 Brush brush;
368 brush.SetAntiAlias(true);
369 brush.SetColor(Drawing::Color::COLOR_RED);
370 canvas.DetachPen().AttachBrush(brush).DrawPath(path2);
371 }
372
TestAddPath(Canvas & canvas,uint32_t width,uint32_t height)373 void PathTest::TestAddPath(Canvas& canvas, uint32_t width, uint32_t height)
374 {
375 LOGI("+++++++ TestAddPath");
376 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
377 Rect rect(margin, margin, width - margin, height - margin);
378 Path path;
379 path.AddRect(rect);
380
381 // a quarter of height
382 Rect rect2(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f,
383 rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
384 Path path2;
385 // the corner radius is 30.0
386 path2.AddRoundRect(rect2, 30.0f, 30.0f);
387
388 Path destPath;
389 destPath.AddPath(path);
390 destPath.AddPath(path2);
391
392 Pen pen;
393 pen.SetAntiAlias(true);
394 pen.SetColor(Drawing::Color::COLOR_BLACK);
395 canvas.AttachPen(pen).DrawPath(destPath);
396 }
397
TestFillStyle(Canvas & canvas,uint32_t width,uint32_t height)398 void PathTest::TestFillStyle(Canvas& canvas, uint32_t width, uint32_t height)
399 {
400 LOGI("+++++++ TestFillStyle_EVENTODD");
401 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
402 Rect rect(margin, margin, width - margin, height - margin);
403 // a quarter of height
404 Rect rect1(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom() - rect.GetHeight() / 4.0f);
405 Rect rect2(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 4.0f, rect.GetRight(), rect.GetBottom());
406 Path path;
407 path.AddRect(rect1);
408 path.AddRect(rect2);
409 path.SetFillStyle(PathFillType::EVENTODD);
410
411 Pen pen;
412 pen.SetAntiAlias(true);
413 pen.SetColor(Drawing::Color::COLOR_BLACK);
414
415 Brush brush;
416 brush.SetAntiAlias(true);
417 brush.SetColor(Drawing::Color::COLOR_RED);
418 canvas.AttachPen(pen).AttachBrush(brush).DrawPath(path);
419 }
420
TestFillStyle2(Canvas & canvas,uint32_t width,uint32_t height)421 void PathTest::TestFillStyle2(Canvas& canvas, uint32_t width, uint32_t height)
422 {
423 LOGI("+++++++ TestFillStyle_INVERSE_EVENTODD");
424 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
425 Rect rect(margin, margin, width - margin, height - margin);
426 // a quarter of height
427 Rect rect1(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom() - rect.GetHeight() / 4.0f);
428 Rect rect2(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 4.0f, rect.GetRight(), rect.GetBottom());
429 Path path;
430 path.AddRect(rect1);
431 path.AddRect(rect2);
432 path.SetFillStyle(PathFillType::INVERSE_EVENTODD);
433
434 Pen pen;
435 pen.SetAntiAlias(true);
436 pen.SetColor(Drawing::Color::COLOR_BLACK);
437
438 Brush brush;
439 brush.SetAntiAlias(true);
440 brush.SetColor(Drawing::Color::COLOR_RED);
441 canvas.AttachPen(pen).AttachBrush(brush).DrawPath(path);
442 }
443
TestFillStyle3(Canvas & canvas,uint32_t width,uint32_t height)444 void PathTest::TestFillStyle3(Canvas& canvas, uint32_t width, uint32_t height)
445 {
446 LOGI("+++++++ TestFillStyle_WINDING");
447 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
448 Rect rect(margin, margin, width - margin, height - margin);
449 // a quarter of height
450 Rect rect1(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom() - rect.GetHeight() / 4.0f);
451 Rect rect2(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 4.0f, rect.GetRight(), rect.GetBottom());
452 Path path;
453 path.AddRect(rect1);
454 path.AddRect(rect2);
455 path.SetFillStyle(PathFillType::WINDING);
456
457 Pen pen;
458 pen.SetAntiAlias(true);
459 pen.SetColor(Drawing::Color::COLOR_BLACK);
460
461 Brush brush;
462 brush.SetAntiAlias(true);
463 brush.SetColor(Drawing::Color::COLOR_RED);
464 canvas.AttachPen(pen).AttachBrush(brush).DrawPath(path);
465 }
466
TestFillStyle4(Canvas & canvas,uint32_t width,uint32_t height)467 void PathTest::TestFillStyle4(Canvas& canvas, uint32_t width, uint32_t height)
468 {
469 LOGI("+++++++ TestFillStyle_INVERSE_WINDING");
470 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
471 Rect rect(margin, margin, width - margin, height - margin);
472 // a quarter of height
473 Rect rect1(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom() - rect.GetHeight() / 4.0f);
474 Rect rect2(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 4.0f, rect.GetRight(), rect.GetBottom());
475 Path path;
476 path.AddRect(rect1);
477 path.AddRect(rect2);
478 path.SetFillStyle(PathFillType::INVERSE_WINDING);
479
480 Pen pen;
481 pen.SetAntiAlias(true);
482 pen.SetColor(Drawing::Color::COLOR_BLACK);
483
484 Brush brush;
485 brush.SetAntiAlias(true);
486 brush.SetColor(Drawing::Color::COLOR_RED);
487 canvas.AttachPen(pen).AttachBrush(brush).DrawPath(path);
488 }
489
TestOffset(Canvas & canvas,uint32_t width,uint32_t height)490 void PathTest::TestOffset(Canvas& canvas, uint32_t width, uint32_t height)
491 {
492 LOGI("+++++++ TestOffset");
493 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
494 Rect rect(margin, margin, width - margin, height - margin);
495 // a quarter of width and height
496 Rect rect2(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop(), rect.GetRight() - rect.GetWidth() / 4.0f,
497 rect.GetBottom() - rect.GetHeight() / 4.0f);
498 Path path;
499 path.AddRect(rect2);
500 // a quarter of height
501 path.Offset(0, rect.GetHeight() / 4.0f);
502
503 Pen pen;
504 pen.SetAntiAlias(true);
505 pen.SetColor(Drawing::Color::COLOR_BLACK);
506 canvas.AttachPen(pen).DrawRect(rect2);
507 pen.SetColor(Drawing::Color::COLOR_RED);
508 canvas.AttachPen(pen).DrawPath(path);
509 }
510
TestTransform(Canvas & canvas,uint32_t width,uint32_t height)511 void PathTest::TestTransform(Canvas& canvas, uint32_t width, uint32_t height)
512 {
513 LOGI("+++++++ TestTransform");
514 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
515 Rect rect(margin, margin, width - margin, height - margin);
516 Path path;
517 // half of height
518 path.MoveTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + rect.GetHeight() / 2.0f);
519 // half of height
520 path.LineTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + (rect.GetHeight() - rect.GetHeight()) / 2.0f);
521 // half of height
522 path.LineTo(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 2.0f);
523 Matrix m;
524 // rotate angle is 36
525 m.Rotate(36, rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + rect.GetHeight() / 2.0f);
526 // transform 10 times
527 for (size_t i = 0; i < 10; i++) {
528 path.Transform(m);
529 Pen pen;
530 pen.SetAntiAlias(true);
531 pen.SetColor(Drawing::Color::COLOR_BLACK);
532 canvas.AttachPen(pen).DrawPath(path);
533 }
534 }
535
TestOp(Canvas & canvas,uint32_t width,uint32_t height)536 void PathTest::TestOp(Canvas& canvas, uint32_t width, uint32_t height)
537 {
538 LOGI("+++++++ TestOp_DIFFERENCE");
539 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
540 Rect rect(margin, margin, width - margin, height - margin);
541 // a third of height
542 const uint32_t radius = rect.GetHeight() / 3.0f;
543 Path path1;
544 // half of width
545 path1.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + radius, radius);
546 Path path2;
547 // half of width
548 path2.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom() - radius, radius);
549 Path path;
550
551 if (path.Op(path1, path2, PathOp::DIFFERENCE)) {
552 Pen pen;
553 pen.SetAntiAlias(true);
554 pen.SetWidth(10); // The thickness of the pen is 10
555 pen.SetColor(Drawing::Color::COLOR_BLACK);
556
557 Brush brush;
558 brush.SetAntiAlias(true);
559 brush.SetColor(Drawing::Color::COLOR_RED);
560 canvas.AttachBrush(brush).AttachPen(pen).DrawPath(path);
561 }
562 }
563
TestOp2(Canvas & canvas,uint32_t width,uint32_t height)564 void PathTest::TestOp2(Canvas& canvas, uint32_t width, uint32_t height)
565 {
566 LOGI("+++++++ TestOp_INTERSECT");
567 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
568 Rect rect(margin, margin, width - margin, height - margin);
569 // a third of height
570 const uint32_t radius = rect.GetHeight() / 3.0f;
571 Path path1;
572 // half of width
573 path1.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + radius, radius);
574 Path path2;
575 // half of width
576 path2.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom() - radius, radius);
577 Path path;
578
579 if (path.Op(path1, path2, PathOp::INTERSECT)) {
580 Pen pen;
581 pen.SetAntiAlias(true);
582 pen.SetWidth(10); // The thickness of the pen is 10
583 pen.SetColor(Drawing::Color::COLOR_BLACK);
584
585 Brush brush;
586 brush.SetAntiAlias(true);
587 brush.SetColor(Drawing::Color::COLOR_RED);
588 canvas.AttachBrush(brush).AttachPen(pen).DrawPath(path);
589 }
590 }
591
TestOp3(Canvas & canvas,uint32_t width,uint32_t height)592 void PathTest::TestOp3(Canvas& canvas, uint32_t width, uint32_t height)
593 {
594 LOGI("+++++++ TestOp_REVERSE_DIFFERENCE");
595 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
596 Rect rect(margin, margin, width - margin, height - margin);
597 // a third of height
598 const uint32_t radius = rect.GetHeight() / 3.0f;
599 Path path1;
600 // half of width
601 path1.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + radius, radius);
602 Path path2;
603 // half of width
604 path2.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom() - radius, radius);
605 Path path;
606
607 if (path.Op(path1, path2, PathOp::REVERSE_DIFFERENCE)) {
608 Pen pen;
609 pen.SetAntiAlias(true);
610 pen.SetWidth(10); // The thickness of the pen is 10
611 pen.SetColor(Drawing::Color::COLOR_BLACK);
612
613 Brush brush;
614 brush.SetAntiAlias(true);
615 brush.SetColor(Drawing::Color::COLOR_RED);
616 canvas.AttachBrush(brush).AttachPen(pen).DrawPath(path);
617 }
618 }
619
TestOp4(Canvas & canvas,uint32_t width,uint32_t height)620 void PathTest::TestOp4(Canvas& canvas, uint32_t width, uint32_t height)
621 {
622 LOGI("+++++++ TestOp_UNION");
623 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
624 Rect rect(margin, margin, width - margin, height - margin);
625 // a third of height
626 const uint32_t radius = rect.GetHeight() / 3.0f;
627 Path path1;
628 // half of width
629 path1.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + radius, radius);
630 Path path2;
631 // half of width
632 path2.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom() - radius, radius);
633 Path path;
634
635 if (path.Op(path1, path2, PathOp::UNION)) {
636 Pen pen;
637 pen.SetAntiAlias(true);
638 pen.SetWidth(10); // The thickness of the pen is 10
639 pen.SetColor(Drawing::Color::COLOR_BLACK);
640
641 Brush brush;
642 brush.SetAntiAlias(true);
643 brush.SetColor(Drawing::Color::COLOR_RED);
644 canvas.AttachBrush(brush).AttachPen(pen).DrawPath(path);
645 }
646 }
647
TestOp5(Canvas & canvas,uint32_t width,uint32_t height)648 void PathTest::TestOp5(Canvas& canvas, uint32_t width, uint32_t height)
649 {
650 LOGI("+++++++ TestOp_XOR");
651 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
652 Rect rect(margin, margin, width - margin, height - margin);
653 // a third of height
654 const uint32_t radius = rect.GetHeight() / 3.0f;
655 Path path1;
656 // half of width
657 path1.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + radius, radius);
658 Path path2;
659 // half of width
660 path2.AddCircle(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetBottom() - radius, radius);
661 Path path;
662
663 if (path.Op(path1, path2, PathOp::XOR)) {
664 Pen pen;
665 pen.SetAntiAlias(true);
666 pen.SetWidth(10); // The thickness of the pen is 10
667 pen.SetColor(Drawing::Color::COLOR_BLACK);
668
669 Brush brush;
670 brush.SetAntiAlias(true);
671 brush.SetColor(Drawing::Color::COLOR_RED);
672 canvas.AttachBrush(brush).AttachPen(pen).DrawPath(path);
673 }
674 }
675
TestClose(Canvas & canvas,uint32_t width,uint32_t height)676 void PathTest::TestClose(Canvas& canvas, uint32_t width, uint32_t height)
677 {
678 LOGI("+++++++ TestClose");
679 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
680 Rect rect(margin, margin, width - margin, height - margin);
681 Path path1;
682 // half of width
683 path1.MoveTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop());
684 path1.LineTo(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 2.0f);
685 path1.LineTo(rect.GetRight(), rect.GetTop() + rect.GetHeight() / 2.0f);
686 Path path2;
687 // half of width and height
688 path2.MoveTo(rect.GetLeft() + rect.GetWidth() / 2.0f, rect.GetTop() + rect.GetHeight() / 2.0f);
689 path2.LineTo(rect.GetLeft(), rect.GetBottom());
690 path2.LineTo(rect.GetRight(), rect.GetBottom());
691 path2.Close();
692
693 Pen pen;
694 pen.SetAntiAlias(true);
695 pen.SetColor(Drawing::Color::COLOR_BLACK);
696 canvas.AttachPen(pen).DrawPath(path1);
697 pen.SetColor(Drawing::Color::COLOR_RED);
698 canvas.AttachPen(pen).DrawPath(path2);
699 }
700 } // namespace Drawing
701 } // namespace Rosen
702 } // namespace OHOS