• 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/SkPath.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     SkPath svgArc;
76 
77     for (int angle = 0; angle <= 45; angle += 45) {
78        for (int oHeight = 2; oHeight >= 1; --oHeight) {
79             SkScalar ovalHeight = oval.height() / oHeight;
80             svgArc.moveTo(oval.fLeft, oval.fTop);
81             svgArc.arcTo(oval.width() / 2, ovalHeight, SkIntToScalar(angle), SkPath::kSmall_ArcSize,
82                     SkPathDirection::kCW, oval.right(), oval.bottom());
83             canvas->drawPath(svgArc, paint);
84             svgArc.reset();
85 
86             svgArc.moveTo(oval.fLeft + 100, oval.fTop + 100);
87             svgArc.arcTo(oval.width() / 2, ovalHeight, SkIntToScalar(angle), SkPath::kLarge_ArcSize,
88                     SkPathDirection::kCCW, oval.right(), oval.bottom() + 100);
89             canvas->drawPath(svgArc, paint);
90             oval.offset(50, 0);
91             svgArc.reset();
92 
93         }
94     }
95 
96     paint.setStrokeWidth(5);
97     const SkColor purple = 0xFF800080;
98     const SkColor darkgreen = 0xFF008000;
99     const SkColor colors[] = { SK_ColorRED, darkgreen, purple, SK_ColorBLUE };
100     const char* arcstrs[] = {
101         "M250,400  A120,80 0 0,0 250,500",
102         "M250,400  A120,80 0 1,1 250,500",
103         "M250,400  A120,80 0 1,0 250,500",
104         "M250,400  A120,80 0 0,1 250,500"
105     };
106     int cIndex = 0;
107     for (const char* arcstr : arcstrs) {
108         SkParsePath::FromSVGString(arcstr, &svgArc);
109         paint.setColor(colors[cIndex++]);
110         canvas->drawPath(svgArc, paint);
111     }
112 
113     // test that zero length arcs still draw round cap
114     paint.setStrokeCap(SkPaint::kRound_Cap);
115     SkPath path;
116     path.moveTo(100, 100);
117     path.arcTo(0, 0, 0, SkPath::kLarge_ArcSize, SkPathDirection::kCW, 200, 200);
118     canvas->drawPath(path, paint);
119 
120     path.reset();
121     path.moveTo(200, 100);
122     path.arcTo(80, 80, 0, SkPath::kLarge_ArcSize, SkPathDirection::kCW, 200, 100);
123     canvas->drawPath(path, paint);
124 }
125 
126 enum {
127     kParsePathTestDimension = 500
128 };
129 
DEF_SIMPLE_GM(parsedpaths,canvas,kParsePathTestDimension,kParsePathTestDimension)130 DEF_SIMPLE_GM(parsedpaths, canvas, kParsePathTestDimension, kParsePathTestDimension) {
131     SkString str;
132     FILE* file;
133     if (GENERATE_SVG_REFERENCE) {
134         file = sk_fopen("svgout.htm", kWrite_SkFILE_Flag);
135         str.printf("<svg width=\"%d\" height=\"%d\">\n", kParsePathTestDimension,
136                 kParsePathTestDimension);
137         sk_fwrite(str.c_str(), str.size(), file);
138     }
139     SkRandom rand;
140     SkPaint paint;
141     paint.setAntiAlias(true);
142     for (int xStart = 0; xStart < kParsePathTestDimension; xStart +=  100) {
143         canvas->save();
144         for (int yStart = 0; yStart < kParsePathTestDimension; yStart += 100) {
145             if (GENERATE_SVG_REFERENCE) {
146                 str.printf("<g transform='translate(%d,%d) scale(%d,%d)'>\n", xStart, yStart,
147                     1, 1);
148                 sk_fwrite(str.c_str(), str.size(), file);
149                 str.printf("<clipPath id='clip_%d_%d'>\n", xStart, yStart);
150                 sk_fwrite(str.c_str(), str.size(), file);
151                 str.printf("<rect width='100' height='100' x='0' y='0'></rect>\n");
152                 sk_fwrite(str.c_str(), str.size(), file);
153                 str.printf("</clipPath>\n");
154                 sk_fwrite(str.c_str(), str.size(), file);
155             }
156             int count = 3;
157             do {
158                 SkPath path;
159                 SkString spec;
160                 uint32_t y = rand.nextRangeU(30, 70);
161                 uint32_t x = rand.nextRangeU(30, 70);
162                 spec.printf("M %d,%d\n", x, y);
163                 uint32_t count = rand.nextRangeU(0, 10);
164                 for (uint32_t i = 0; i < count; ++i) {
165                     spec.append(MakeRandomParsePathPiece(&rand));
166                 }
167                 SkAssertResult(SkParsePath::FromSVGString(spec.c_str(), &path));
168                 paint.setColor(rand.nextU());
169                 canvas->save();
170                 canvas->clipRect(SkRect::MakeIWH(100, 100));
171                 canvas->drawPath(path, paint);
172                 canvas->restore();
173                 if (GENERATE_SVG_REFERENCE) {
174                     str.printf("<path d='\n");
175                     sk_fwrite(str.c_str(), str.size(), file);
176                     sk_fwrite(spec.c_str(), spec.size(), file);
177                     str.printf("\n' fill='#%06x' fill-opacity='%g'", paint.getColor() & 0xFFFFFF,
178                             paint.getAlpha() / 255.f);
179                     sk_fwrite(str.c_str(), str.size(), file);
180                     str.printf(" clip-path='url(#clip_%d_%d)'/>\n", xStart, yStart);
181                     sk_fwrite(str.c_str(), str.size(), file);
182                 }
183             } while (--count > 0);
184             if (GENERATE_SVG_REFERENCE) {
185                 str.printf("</g>\n");
186                 sk_fwrite(str.c_str(), str.size(), file);
187             }
188             canvas->translate(0, 100);
189         }
190         canvas->restore();
191         canvas->translate(100, 0);
192     }
193     if (GENERATE_SVG_REFERENCE) {
194         const char trailer[] = "</svg>\n";
195         sk_fwrite(trailer, sizeof(trailer) - 1, file);
196         sk_fclose(file);
197     }
198 }
199 
200 DEF_SIMPLE_GM(bug593049, canvas, 300, 300) {
201     canvas->translate(111, 0);
202 
203     SkPath p;
204     p.moveTo(-43.44464063610148f, 79.43535936389853f);
205     const SkScalar yOffset = 122.88f;
206     const SkScalar radius = 61.44f;
207     SkRect oval = SkRect::MakeXYWH(-radius, yOffset - radius, 2 * radius, 2 * radius);
208     p.arcTo(oval, 1.25f * 180, .5f * 180, false);
209 
210     SkPaint paint;
211     paint.setStyle(SkPaint::kStroke_Style);
212     paint.setStrokeCap(SkPaint::kRound_Cap);
213     paint.setStrokeWidth(15.36f);
214 
215     canvas->drawPath(p, paint);
216 }
217 
218 DEF_SIMPLE_GM(bug583299, canvas, 300, 300) {
219   const char* d="M60,60 A50,50 0 0 0 160,60 A50,50 0 0 0 60,60z";
220   SkPaint p;
221   p.setStyle(SkPaint::kStroke_Style);
222   p.setStrokeWidth(100);
223   p.setAntiAlias(true);
224   p.setColor(0xFF008200);
225   p.setStrokeCap(SkPaint::kSquare_Cap);
226   SkPath path;
227   SkParsePath::FromSVGString(d, &path);
228   SkPathMeasure meas(path, false);
229   SkScalar length = meas.getLength();
230   SkScalar intervals[] = {0, length };
231   int intervalCount = (int) SK_ARRAY_COUNT(intervals);
232   p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
233   canvas->drawPath(path, p);
234 }
235