xref: /aosp_15_r20/external/skia/tests/ParsePathTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkPath.h"
9 #include "include/core/SkRect.h"
10 #include "include/core/SkScalar.h"
11 #include "include/core/SkString.h"
12 #include "include/utils/SkParsePath.h"
13 #include "tests/Test.h"
14 
15 #include <array>
16 #include <cstddef>
17 
test_to_from(skiatest::Reporter * reporter,const SkPath & path)18 static void test_to_from(skiatest::Reporter* reporter, const SkPath& path) {
19     SkString str = SkParsePath::ToSVGString(path);
20 
21     SkPath path2;
22     bool success = SkParsePath::FromSVGString(str.c_str(), &path2);
23     REPORTER_ASSERT(reporter, success);
24 
25     SkString str2 = SkParsePath::ToSVGString(path2);
26     REPORTER_ASSERT(reporter, str == str2);
27 #if 0 // closed paths are not equal, the iter explicitly gives the closing
28       // edge, even if it is not in the path.
29     REPORTER_ASSERT(reporter, path == path2);
30     if (path != path2) {
31         SkDebugf("str1=%s\nstr2=%s\n", str.c_str(), str2.c_str());
32     }
33 #endif
34 }
35 
36 static struct {
37     const char* fStr;
38     const SkRect fBounds;
39 } gRec[] = {
40     { "M1,1 l-2.58-2.828-3.82-0.113, 1.9-3.3223-1.08-3.6702, 3.75,0.7744,3.16-2.1551,"
41        "0.42,3.8008,3.02,2.3384-3.48,1.574-1.29,3.601z",
42         { -5.39999962f, -10.3142f, 5.77000046f, 1.f } },
43     { "", { 0, 0, 0, 0 } },
44     { "M0,0L10,10", { 0, 0, SkIntToScalar(10), SkIntToScalar(10) } },
45     { "M-5.5,-0.5 Q 0 0 6,6.50",
46         { -5.5f, -0.5f,
47           6, 6.5f } }
48 };
49 
DEF_TEST(ParsePath,reporter)50 DEF_TEST(ParsePath, reporter) {
51     for (size_t i = 0; i < std::size(gRec); i++) {
52         SkPath  path;
53         bool success = SkParsePath::FromSVGString(gRec[i].fStr, &path);
54         REPORTER_ASSERT(reporter, success);
55         const SkRect& expectedBounds = gRec[i].fBounds;
56         const SkRect& pathBounds = path.getBounds();
57         REPORTER_ASSERT(reporter, expectedBounds == pathBounds);
58 
59         test_to_from(reporter, path);
60     }
61 
62     SkRect r;
63     r.setLTRB(0, 0, 10, 10.5f);
64     SkPath p;
65     p.addRect(r);
66     test_to_from(reporter, p);
67     p.addOval(r);
68     test_to_from(reporter, p);
69     p.addRoundRect(r, 4, 4.5f);
70     test_to_from(reporter, p);
71 }
72 
testInvalidPath(skiatest::Reporter * reporter,const std::string & name,const std::string & input)73 static void testInvalidPath(skiatest::Reporter* reporter, const std::string& name,
74                             const std::string& input) {
75     skiatest::ReporterContext subtest(reporter, name);
76     SkPath path;
77     bool success = SkParsePath::FromSVGString(input.c_str(), &path);
78     REPORTER_ASSERT(reporter, !success);
79     // We should not modify the input path on a failure.
80     REPORTER_ASSERT(reporter, path.isEmpty());
81 }
82 
DEF_TEST(ParsePath_InvalidDoesNotCrash,r)83 DEF_TEST(ParsePath_InvalidDoesNotCrash, r) {
84     testInvalidPath(r, "empty move", "M");
85     testInvalidPath(r, "partial move", "M 5");
86     testInvalidPath(r, "partial vertical line", "V"); // oss-fuzz:68723
87     testInvalidPath(r, "partial horizontal line", "H");
88     testInvalidPath(r, "partial cubic", "C 1 2");
89     testInvalidPath(r, "partial continued cubic", "S 6 7");
90     testInvalidPath(r, "partial quad", "Q 3 4 5");
91     testInvalidPath(r, "partial continued quad", "T");
92     testInvalidPath(r, "partial arc", "A 1 2 3 4 5 6");
93     testInvalidPath(r, "partial ~", "~ 7 6 5");
94 }
95 
DEF_TEST(ParsePathOptionalCommand,r)96 DEF_TEST(ParsePathOptionalCommand, r) {
97     struct {
98         const char* fStr;
99         int         fVerbs;
100         int         fPoints;
101     } gTests[] = {
102         { "", 0, 0 },
103 
104         { "H100 200 ", 3, 3 },
105         { "H-100-200", 3, 3 },
106         { "H+100+200", 3, 3 },
107         { "H.10.20"  , 3, 3 },
108         { "H-.10-.20", 3, 3 },
109         { "H+.10+.20", 3, 3 },
110 
111         { "L100 100 200 200" , 3, 3 },
112         { "L-100-100-200-200", 3, 3 },
113         { "L+100+100+200+200", 3, 3 },
114         { "L.10.10.20.20"    , 3, 3 },
115         { "L-.10-.10-.20-.20", 3, 3 },
116         { "L+.10+.10+.20+.20", 3, 3 },
117 
118         { "C100 100 200 200 300 300 400 400 500 500 600 600" , 3, 7 },
119         { "C100-100-200-200-300-300-400-400-500-500-600-600" , 3, 7 },
120         { "C100+100+200+200+300+300+400+400+500+500+600+600" , 3, 7 },
121         { "C.10.10.20.20.30.30.40.40.50.50.60.60"            , 3, 7 },
122         { "C-.10-.10-.20-.20-.30-.30-.40-.40-.50-.50-.60-.60", 3, 7 },
123         { "C+.10+.10+.20+.20+.30+.30+.40+.40+.50+.50+.60+.60", 3, 7 },
124 
125         { "c-1.49.71-2.12 2.5-1.4 4 .71 1.49 2.5 2.12 4 1.4z", 4, 7 },
126     };
127 
128     SkPath path;
129     for (size_t i = 0; i < std::size(gTests); ++i) {
130         REPORTER_ASSERT(r, SkParsePath::FromSVGString(gTests[i].fStr, &path));
131         REPORTER_ASSERT(r, path.countVerbs() == gTests[i].fVerbs);
132         REPORTER_ASSERT(r, path.countPoints() == gTests[i].fPoints);
133     }
134 }
135 
DEF_TEST(ParsePathArcFlags,r)136 DEF_TEST(ParsePathArcFlags, r) {
137     const char* arcs = "M10 10a2.143 2.143 0 100-4.285 2.143 2.143 0 000 4.286";
138     SkPath path;
139     REPORTER_ASSERT(r, SkParsePath::FromSVGString(arcs, &path));
140     // Arcs decompose to two conics.
141     REPORTER_ASSERT(r, path.countVerbs() == 5);
142     // One for move, 2x per conic.
143     REPORTER_ASSERT(r, path.countPoints() == 9);
144 }
145