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