1 // Copyright 2023 Google LLC
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4 #include "modules/bentleyottmann/include/Contour.h"
5
6 #include "include/core/SkPath.h"
7 #include "include/core/SkPathBuilder.h"
8 #include "tests/Test.h"
9
10 using namespace contour;
11
DEF_TEST(CFC_Contours_Basic,r)12 DEF_TEST(CFC_Contours_Basic, r) {
13 {
14 // No contours basic
15 SkPath p;
16 Contours contours = Contours::Make(p);
17 REPORTER_ASSERT(r, contours.empty());
18 }
19 {
20 // Path with just a close.
21 SkPathBuilder b;
22 b.close();
23 SkPath p = b.detach();
24 Contours contours = Contours::Make(p);
25 REPORTER_ASSERT(r, contours.empty());
26 }
27 {
28 // Path with a bunch of moves.
29 SkPathBuilder b;
30 b.moveTo(10, 10);
31 b.moveTo(20, 20);
32 b.close();
33 SkPath p = b.detach();
34 Contours contours = Contours::Make(p);
35 REPORTER_ASSERT(r, contours.empty());
36 }
37 {
38 // LineTo, but no close.
39 SkPathBuilder b;
40 b.moveTo(10, 10);
41 b.moveTo(20, 20);
42 b.lineTo(30, 30);
43 SkPath p = b.detach();
44 Contours contours = Contours::Make(p);
45 REPORTER_ASSERT(r, contours.size() == 1);
46 Contour c = *contours.begin();
47 REPORTER_ASSERT(r, c.points.size() == 2);
48 REPORTER_ASSERT(r, c.points[0].x == 20 * Contours::kScaleFactor);
49 REPORTER_ASSERT(r, c.points[1].x == 30 * Contours::kScaleFactor);
50 REPORTER_ASSERT(r, c.bounds == SkIRect::MakeLTRB(20 * Contours::kScaleFactor,
51 20 * Contours::kScaleFactor,
52 30 * Contours::kScaleFactor,
53 30 * Contours::kScaleFactor));
54 }
55 {
56 // LineTo with close.
57 SkPathBuilder b;
58 b.moveTo(10, 10);
59 b.moveTo(20, 20);
60 b.lineTo(30, 30);
61 b.close();
62 SkPath p = b.detach();
63 Contours contours = Contours::Make(p);
64 REPORTER_ASSERT(r, contours.size() == 1);
65 Contour c = *contours.begin();
66 REPORTER_ASSERT(r, c.points.size() == 3);
67 REPORTER_ASSERT(r, c.points[0].x == 20 * Contours::kScaleFactor);
68 REPORTER_ASSERT(r, c.points[1].x == 30 * Contours::kScaleFactor);
69 // Extra point added by close.
70 REPORTER_ASSERT(r, c.points[2].x == 20 * Contours::kScaleFactor);
71 REPORTER_ASSERT(r, c.bounds == SkIRect::MakeLTRB(20 * Contours::kScaleFactor,
72 20 * Contours::kScaleFactor,
73 30 * Contours::kScaleFactor,
74 30 * Contours::kScaleFactor));
75 }
76 {
77 // LineTo with close and extra moves.
78 SkPathBuilder b;
79 b.moveTo(10, 10);
80 b.moveTo(20, 20);
81 b.lineTo(30, 30);
82 b.close();
83 b.moveTo(100, 100);
84 SkPath p = b.detach();
85 Contours contours = Contours::Make(p);
86 REPORTER_ASSERT(r, contours.size() == 1);
87 Contour c = *contours.begin();
88 REPORTER_ASSERT(r, c.points.size() == 3);
89 REPORTER_ASSERT(r, c.points[0].x == 20 * Contours::kScaleFactor);
90 REPORTER_ASSERT(r, c.points[1].x == 30 * Contours::kScaleFactor);
91 // Extra point added by close.
92 REPORTER_ASSERT(r, c.points[2].x == 20 * Contours::kScaleFactor);
93 REPORTER_ASSERT(r, c.bounds == SkIRect::MakeLTRB(20 * Contours::kScaleFactor,
94 20 * Contours::kScaleFactor,
95 30 * Contours::kScaleFactor,
96 30 * Contours::kScaleFactor));
97 }
98 }
99