• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkPathMeasure.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/effects/SkDashPathEffect.h"
20 #include "include/utils/SkParsePath.h"
21 #include "include/utils/SkRandom.h"
22 #include "src/core/SkOSFile.h"
23 #include "tools/random_parse_path.h"
24 
25 #include <stdio.h>
26 
27 /* The test below generates a reference image using SVG. To compare the result for correctness,
28    enable the define below and then view the generated SVG in a browser.
29  */
30 static constexpr bool GENERATE_SVG_REFERENCE = false;
31 
32 /*
33 The arcto test below should draw the same as this SVG:
34 (Note that Skia's arcTo Direction parameter value is opposite SVG's sweep value, e.g. 0 / 1)
35 
36 <svg width="500" height="600">
37 <path d="M 50,100 A50,50,   0,0,1, 150,200" style="stroke:#660000; fill:none; stroke-width:2" />
38 <path d="M100,100 A50,100,  0,0,1, 200,200" style="stroke:#660000; fill:none; stroke-width:2" />
39 <path d="M150,100 A50,50,  45,0,1, 250,200" style="stroke:#660000; fill:none; stroke-width:2" />
40 <path d="M200,100 A50,100, 45,0,1, 300,200" style="stroke:#660000; fill:none; stroke-width:2" />
41 
42 <path d="M150,200 A50,50,   0,1,0, 150,300" style="stroke:#660000; fill:none; stroke-width:2" />
43 <path d="M200,200 A50,100,  0,1,0, 200,300" style="stroke:#660000; fill:none; stroke-width:2" />
44 <path d="M250,200 A50,50,  45,1,0, 250,300" style="stroke:#660000; fill:none; stroke-width:2" />
45 <path d="M300,200 A50,100, 45,1,0, 300,300" style="stroke:#660000; fill:none; stroke-width:2" />
46 
47 <path d="M250,400  A120,80 0 0,0 250,500"
48     fill="none" stroke="red" stroke-width="5" />
49 
50 <path d="M250,400  A120,80 0 1,1 250,500"
51     fill="none" stroke="green" stroke-width="5"/>
52 
53 <path d="M250,400  A120,80 0 1,0 250,500"
54     fill="none" stroke="purple" stroke-width="5"/>
55 
56 <path d="M250,400  A120,80 0 0,1 250,500"
57     fill="none" stroke="blue" stroke-width="5"/>
58 
59 <path d="M100,100  A  0, 0 0 0,1 200,200"
60     fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/>
61 
62 <path d="M200,100  A 80,80 0 0,1 200,100"
63     fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/>
64 </svg>
65  */
66 
67 DEF_SIMPLE_GM(arcto, canvas, 500, 600) {
68     SkPaint paint;
69     paint.setAntiAlias(true);
70     paint.setStyle(SkPaint::kStroke_Style);
71     paint.setStrokeWidth(2);
72     paint.setColor(0xFF660000);
73 //    canvas->scale(2, 2);  // for testing on retina
74     SkRect oval = SkRect::MakeXYWH(100, 100, 100, 100);
75 
76     for (SkScalar angle = 0; angle <= 45; angle += 45) {
77         for (int oHeight = 2; oHeight >= 1; --oHeight) {
78             SkPathBuilder svgArc;
79             SkScalar ovalHeight = oval.height() / oHeight;
80             svgArc.moveTo(oval.fLeft, oval.fTop);
81             svgArc.arcTo({oval.width() / 2, ovalHeight}, angle, SkPathBuilder::kSmall_ArcSize,
82                          SkPathDirection::kCW, {oval.right(), oval.bottom()});
83             canvas->drawPath(svgArc.detach(), paint);
84 
85             svgArc.moveTo(oval.fLeft + 100, oval.fTop + 100);
86             svgArc.arcTo({oval.width() / 2, ovalHeight}, angle, SkPathBuilder::kLarge_ArcSize,
87                          SkPathDirection::kCCW, {oval.right(), oval.bottom() + 100});
88             canvas->drawPath(svgArc.detach(), paint);
89             oval.offset(50, 0);
90 
91         }
92     }
93 
94     paint.setStrokeWidth(5);
95     const SkColor purple = 0xFF800080;
96     const SkColor darkgreen = 0xFF008000;
97     const SkColor colors[] = { SK_ColorRED, darkgreen, purple, SK_ColorBLUE };
98     const char* arcstrs[] = {
99         "M250,400  A120,80 0 0,0 250,500",
100         "M250,400  A120,80 0 1,1 250,500",
101         "M250,400  A120,80 0 1,0 250,500",
102         "M250,400  A120,80 0 0,1 250,500"
103     };
104     int cIndex = 0;
105     for (const char* arcstr : arcstrs) {
106         SkPath path;
107         SkParsePath::FromSVGString(arcstr, &path);
108         paint.setColor(colors[cIndex++]);
109         canvas->drawPath(path, paint);
110     }
111 
112     // test that zero length arcs still draw round cap
113     paint.setStrokeCap(SkPaint::kRound_Cap);
114     SkPathBuilder path;
115     path.moveTo(100, 100)
116         .arcTo({0, 0}, 0, SkPathBuilder::kLarge_ArcSize, SkPathDirection::kCW, {200, 200});
117     canvas->drawPath(path.detach(), paint);
118 
119     path.moveTo(200, 100)
120         .arcTo({80, 80}, 0, SkPathBuilder::kLarge_ArcSize, SkPathDirection::kCW, {200, 100});
121     canvas->drawPath(path.detach(), paint);
122 }
123 
124 enum {
125     kParsePathTestDimension = 500
126 };
127 
DEF_SIMPLE_GM(parsedpaths,canvas,kParsePathTestDimension,kParsePathTestDimension)128 DEF_SIMPLE_GM(parsedpaths, canvas, kParsePathTestDimension, kParsePathTestDimension) {
129     SkString str;
130     FILE* file;
131     if (GENERATE_SVG_REFERENCE) {
132         file = sk_fopen("svgout.htm", kWrite_SkFILE_Flag);
133         str.printf("<svg width=\"%d\" height=\"%d\">\n", kParsePathTestDimension,
134                 kParsePathTestDimension);
135         sk_fwrite(str.c_str(), str.size(), file);
136     }
137     SkRandom rand;
138     SkPaint paint;
139     paint.setAntiAlias(true);
140     for (int xStart = 0; xStart < kParsePathTestDimension; xStart +=  100) {
141         canvas->save();
142         for (int yStart = 0; yStart < kParsePathTestDimension; yStart += 100) {
143             if (GENERATE_SVG_REFERENCE) {
144                 str.printf("<g transform='translate(%d,%d) scale(%d,%d)'>\n", xStart, yStart,
145                     1, 1);
146                 sk_fwrite(str.c_str(), str.size(), file);
147                 str.printf("<clipPath id='clip_%d_%d'>\n", xStart, yStart);
148                 sk_fwrite(str.c_str(), str.size(), file);
149                 str.printf("<rect width='100' height='100' x='0' y='0'></rect>\n");
150                 sk_fwrite(str.c_str(), str.size(), file);
151                 str.printf("</clipPath>\n");
152                 sk_fwrite(str.c_str(), str.size(), file);
153             }
154             int count = 3;
155             do {
156                 SkPath path;
157                 SkString spec;
158                 uint32_t y = rand.nextRangeU(30, 70);
159                 uint32_t x = rand.nextRangeU(30, 70);
160                 spec.printf("M %d,%d\n", x, y);
161                 for (uint32_t i = rand.nextRangeU(0, 10); i--; ) {
162                     spec.append(MakeRandomParsePathPiece(&rand));
163                 }
164                 SkAssertResult(SkParsePath::FromSVGString(spec.c_str(), &path));
165                 paint.setColor(rand.nextU());
166                 canvas->save();
167                 canvas->clipRect(SkRect::MakeIWH(100, 100));
168                 canvas->drawPath(path, paint);
169                 canvas->restore();
170                 if (GENERATE_SVG_REFERENCE) {
171                     str.printf("<path d='\n");
172                     sk_fwrite(str.c_str(), str.size(), file);
173                     sk_fwrite(spec.c_str(), spec.size(), file);
174                     str.printf("\n' fill='#%06x' fill-opacity='%g'", paint.getColor() & 0xFFFFFF,
175                             paint.getAlpha() / 255.f);
176                     sk_fwrite(str.c_str(), str.size(), file);
177                     str.printf(" clip-path='url(#clip_%d_%d)'/>\n", xStart, yStart);
178                     sk_fwrite(str.c_str(), str.size(), file);
179                 }
180             } while (--count > 0);
181             if (GENERATE_SVG_REFERENCE) {
182                 str.printf("</g>\n");
183                 sk_fwrite(str.c_str(), str.size(), file);
184             }
185             canvas->translate(0, 100);
186         }
187         canvas->restore();
188         canvas->translate(100, 0);
189     }
190     if (GENERATE_SVG_REFERENCE) {
191         const char trailer[] = "</svg>\n";
192         sk_fwrite(trailer, sizeof(trailer) - 1, file);
193         sk_fclose(file);
194     }
195 }
196 
197 DEF_SIMPLE_GM(bug593049, canvas, 300, 300) {
198     canvas->translate(111, 0);
199 
200     SkPathBuilder p;
201     p.moveTo(-43.44464063610148f, 79.43535936389853f);
202     const SkScalar yOffset = 122.88f;
203     const SkScalar radius = 61.44f;
204     SkRect oval = SkRect::MakeXYWH(-radius, yOffset - radius, 2 * radius, 2 * radius);
205     p.arcTo(oval, 1.25f * 180, .5f * 180, false);
206 
207     SkPaint paint;
208     paint.setStyle(SkPaint::kStroke_Style);
209     paint.setStrokeCap(SkPaint::kRound_Cap);
210     paint.setStrokeWidth(15.36f);
211 
212     canvas->drawPath(p.detach(), paint);
213 }
214 
215 DEF_SIMPLE_GM(bug583299, canvas, 300, 300) {
216   const char* d="M60,60 A50,50 0 0 0 160,60 A50,50 0 0 0 60,60z";
217   SkPaint p;
218   p.setStyle(SkPaint::kStroke_Style);
219   p.setStrokeWidth(100);
220   p.setAntiAlias(true);
221   p.setColor(0xFF008200);
222   p.setStrokeCap(SkPaint::kSquare_Cap);
223   SkPath path;
224   SkParsePath::FromSVGString(d, &path);
225   SkPathMeasure meas(path, false);
226   SkScalar length = meas.getLength();
227   SkScalar intervals[] = {0, length };
228   int intervalCount = (int) SK_ARRAY_COUNT(intervals);
229   p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
230   canvas->drawPath(path, p);
231 }
232