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 "gm.h"
9 #include "sk_tool_utils.h"
10 #include "SkPath.h"
11 #include "SkRandom.h"
12 #include "SkDashPathEffect.h"
13 #include "SkParsePath.h"
14
15 #define W 400
16 #define H 400
17 #define N 50
18
19 constexpr SkScalar SW = SkIntToScalar(W);
20 constexpr SkScalar SH = SkIntToScalar(H);
21
rnd_rect(SkRect * r,SkPaint * paint,SkRandom & rand)22 static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) {
23 SkScalar x = rand.nextUScalar1() * W;
24 SkScalar y = rand.nextUScalar1() * H;
25 SkScalar w = rand.nextUScalar1() * (W >> 2);
26 SkScalar h = rand.nextUScalar1() * (H >> 2);
27 SkScalar hoffset = rand.nextSScalar1();
28 SkScalar woffset = rand.nextSScalar1();
29
30 r->set(x, y, x + w, y + h);
31 r->offset(-w/2 + woffset, -h/2 + hoffset);
32
33 paint->setColor(rand.nextU());
34 paint->setAlpha(0xFF);
35 }
36
37
38 class StrokesGM : public skiagm::GM {
39 public:
StrokesGM()40 StrokesGM() {}
41
42 protected:
43
onShortName()44 SkString onShortName() override {
45 return SkString("strokes_round");
46 }
47
onISize()48 SkISize onISize() override {
49 return SkISize::Make(W, H*2);
50 }
51
onDraw(SkCanvas * canvas)52 void onDraw(SkCanvas* canvas) override {
53 SkPaint paint;
54 paint.setStyle(SkPaint::kStroke_Style);
55 paint.setStrokeWidth(SkIntToScalar(9)/2);
56
57 for (int y = 0; y < 2; y++) {
58 paint.setAntiAlias(!!y);
59 SkAutoCanvasRestore acr(canvas, true);
60 canvas->translate(0, SH * y);
61 canvas->clipRect(SkRect::MakeLTRB(
62 SkIntToScalar(2), SkIntToScalar(2)
63 , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
64 ));
65
66 SkRandom rand;
67 for (int i = 0; i < N; i++) {
68 SkRect r;
69 rnd_rect(&r, &paint, rand);
70 canvas->drawOval(r, paint);
71 rnd_rect(&r, &paint, rand);
72 canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint);
73 rnd_rect(&r, &paint, rand);
74 }
75 }
76 }
77
78 private:
79 typedef skiagm::GM INHERITED;
80 };
81
82 /* See
83 https://code.google.com/p/chromium/issues/detail?id=422974 and
84 http://jsfiddle.net/1xnku3sg/2/
85 */
86 class ZeroLenStrokesGM : public skiagm::GM {
87 SkPath fMoveHfPath, fMoveZfPath, fDashedfPath, fRefPath[4];
88 SkPath fCubicPath, fQuadPath, fLinePath;
89 protected:
onOnceBeforeDraw()90 void onOnceBeforeDraw() override {
91
92 SkAssertResult(SkParsePath::FromSVGString("M0,0h0M10,0h0M20,0h0", &fMoveHfPath));
93 SkAssertResult(SkParsePath::FromSVGString("M0,0zM10,0zM20,0z", &fMoveZfPath));
94 SkAssertResult(SkParsePath::FromSVGString("M0,0h25", &fDashedfPath));
95 SkAssertResult(SkParsePath::FromSVGString("M 0 0 C 0 0 0 0 0 0", &fCubicPath));
96 SkAssertResult(SkParsePath::FromSVGString("M 0 0 Q 0 0 0 0", &fQuadPath));
97 SkAssertResult(SkParsePath::FromSVGString("M 0 0 L 0 0", &fLinePath));
98
99 for (int i = 0; i < 3; ++i) {
100 fRefPath[0].addCircle(i * 10.f, 0, 5);
101 fRefPath[1].addCircle(i * 10.f, 0, 10);
102 fRefPath[2].addRect(i * 10.f - 4, -2, i * 10.f + 4, 6);
103 fRefPath[3].addRect(i * 10.f - 10, -10, i * 10.f + 10, 10);
104 }
105 }
106
onShortName()107 SkString onShortName() override {
108 return SkString("zeroPath");
109 }
110
onISize()111 SkISize onISize() override {
112 return SkISize::Make(W, H*2);
113 }
114
onDraw(SkCanvas * canvas)115 void onDraw(SkCanvas* canvas) override {
116 SkPaint fillPaint, strokePaint, dashPaint;
117 fillPaint.setAntiAlias(true);
118 strokePaint = fillPaint;
119 strokePaint.setStyle(SkPaint::kStroke_Style);
120 for (int i = 0; i < 2; ++i) {
121 fillPaint.setAlpha(255);
122 strokePaint.setAlpha(255);
123 strokePaint.setStrokeWidth(i ? 8.f : 10.f);
124 strokePaint.setStrokeCap(i ? SkPaint::kSquare_Cap : SkPaint::kRound_Cap);
125 canvas->save();
126 canvas->translate(10 + i * 100.f, 10);
127 canvas->drawPath(fMoveHfPath, strokePaint);
128 canvas->translate(0, 20);
129 canvas->drawPath(fMoveZfPath, strokePaint);
130 dashPaint = strokePaint;
131 const SkScalar intervals[] = { 0, 10 };
132 dashPaint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
133 SkPath fillPath;
134 dashPaint.getFillPath(fDashedfPath, &fillPath);
135 canvas->translate(0, 20);
136 canvas->drawPath(fDashedfPath, dashPaint);
137 canvas->translate(0, 20);
138 canvas->drawPath(fRefPath[i * 2], fillPaint);
139 strokePaint.setStrokeWidth(20);
140 strokePaint.setAlpha(127);
141 canvas->translate(0, 50);
142 canvas->drawPath(fMoveHfPath, strokePaint);
143 canvas->translate(0, 30);
144 canvas->drawPath(fMoveZfPath, strokePaint);
145 canvas->translate(0, 30);
146 fillPaint.setAlpha(127);
147 canvas->drawPath(fRefPath[1 + i * 2], fillPaint);
148 canvas->translate(0, 30);
149 canvas->drawPath(fCubicPath, strokePaint);
150 canvas->translate(0, 30);
151 canvas->drawPath(fQuadPath, strokePaint);
152 canvas->translate(0, 30);
153 canvas->drawPath(fLinePath, strokePaint);
154 canvas->restore();
155 }
156 }
157
158 private:
159 typedef skiagm::GM INHERITED;
160 };
161
162 class TeenyStrokesGM : public skiagm::GM {
163
onShortName()164 SkString onShortName() override {
165 return SkString("teenyStrokes");
166 }
167
onISize()168 SkISize onISize() override {
169 return SkISize::Make(W, H*2);
170 }
171
line(SkScalar scale,SkCanvas * canvas,SkColor color)172 static void line(SkScalar scale, SkCanvas* canvas, SkColor color) {
173 SkPaint p;
174 p.setAntiAlias(true);
175 p.setStyle(SkPaint::kStroke_Style);
176 p.setColor(color);
177 canvas->translate(50, 0);
178 canvas->save();
179 p.setStrokeWidth(scale * 5);
180 canvas->scale(1 / scale, 1 / scale);
181 canvas->drawLine(20 * scale, 20 * scale, 20 * scale, 100 * scale, p);
182 canvas->drawLine(20 * scale, 20 * scale, 100 * scale, 100 * scale, p);
183 canvas->restore();
184 }
185
onDraw(SkCanvas * canvas)186 void onDraw(SkCanvas* canvas) override {
187 line(0.00005f, canvas, SK_ColorBLACK);
188 line(0.000045f, canvas, SK_ColorRED);
189 line(0.0000035f, canvas, SK_ColorGREEN);
190 line(0.000003f, canvas, SK_ColorBLUE);
191 line(0.000002f, canvas, SK_ColorBLACK);
192 }
193 private:
194 typedef skiagm::GM INHERITED;
195 };
196
197 DEF_SIMPLE_GM(CubicStroke, canvas, 384, 384) {
198 SkPaint p;
199 p.setAntiAlias(true);
200 p.setStyle(SkPaint::kStroke_Style);
201 p.setStrokeWidth(1.0720f);
202 SkPath path;
203 path.moveTo(-6000,-6000);
204 path.cubicTo(-3500,5500,-500,5500,2500,-6500);
205 canvas->drawPath(path, p);
206 p.setStrokeWidth(1.0721f);
207 canvas->translate(10, 10);
208 canvas->drawPath(path, p);
209 p.setStrokeWidth(1.0722f);
210 canvas->translate(10, 10);
211 canvas->drawPath(path, p);
212 }
213
214 DEF_SIMPLE_GM(zerolinestroke, canvas, 90, 120) {
215 SkPaint paint;
216 paint.setStyle(SkPaint::kStroke_Style);
217 paint.setStrokeWidth(20);
218 paint.setAntiAlias(true);
219 paint.setStrokeCap(SkPaint::kRound_Cap);
220
221 SkPath path;
222 path.moveTo(30, 90);
223 path.lineTo(30, 90);
224 path.lineTo(60, 90);
225 path.lineTo(60, 90);
226 canvas->drawPath(path, paint);
227
228 path.reset();
229 path.moveTo(30, 30);
230 path.lineTo(60, 30);
231 canvas->drawPath(path, paint);
232
233 path.reset();
234 path.moveTo(30, 60);
235 path.lineTo(30, 60);
236 path.lineTo(60, 60);
237 canvas->drawPath(path, paint);
238 }
239
240 DEF_SIMPLE_GM(quadcap, canvas, 200, 200) {
241 SkPaint p;
242 p.setAntiAlias(true);
243 p.setStyle(SkPaint::kStroke_Style);
244 p.setStrokeWidth(0);
245 SkPath path;
246 SkPoint pts[] = {{105.738571f,13.126318f},
247 {105.738571f,13.126318f},
248 {123.753784f,1.f}};
249 SkVector tangent = pts[1] - pts[2];
250 tangent.normalize();
251 SkPoint pts2[3];
252 memcpy(pts2, pts, sizeof(pts));
253 const SkScalar capOutset = SK_ScalarPI / 8;
254 pts2[0].fX += tangent.fX * capOutset;
255 pts2[0].fY += tangent.fY * capOutset;
256 pts2[1].fX += tangent.fX * capOutset;
257 pts2[1].fY += tangent.fY * capOutset;
258 pts2[2].fX += -tangent.fX * capOutset;
259 pts2[2].fY += -tangent.fY * capOutset;
260 path.moveTo(pts2[0]);
261 path.quadTo(pts2[1], pts2[2]);
262 canvas->drawPath(path, p);
263
264 path.reset();
265 path.moveTo(pts[0]);
266 path.quadTo(pts[1], pts[2]);
267 p.setStrokeCap(SkPaint::kRound_Cap);
268 canvas->translate(30, 0);
269 canvas->drawPath(path, p);
270 }
271
272 class Strokes2GM : public skiagm::GM {
273 SkPath fPath;
274 protected:
onOnceBeforeDraw()275 void onOnceBeforeDraw() override {
276 SkRandom rand;
277 fPath.moveTo(0, 0);
278 for (int i = 0; i < 13; i++) {
279 SkScalar x = rand.nextUScalar1() * (W >> 1);
280 SkScalar y = rand.nextUScalar1() * (H >> 1);
281 fPath.lineTo(x, y);
282 }
283 }
284
285
onShortName()286 SkString onShortName() override {
287 return SkString("strokes_poly");
288 }
289
onISize()290 SkISize onISize() override {
291 return SkISize::Make(W, H*2);
292 }
293
onDraw(SkCanvas * canvas)294 void onDraw(SkCanvas* canvas) override {
295 canvas->drawColor(SK_ColorWHITE);
296
297 SkPaint paint;
298 paint.setStyle(SkPaint::kStroke_Style);
299 paint.setStrokeWidth(SkIntToScalar(9)/2);
300
301 for (int y = 0; y < 2; y++) {
302 paint.setAntiAlias(!!y);
303 SkAutoCanvasRestore acr(canvas, true);
304 canvas->translate(0, SH * y);
305 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
306 SkIntToScalar(2),
307 SW - SkIntToScalar(2),
308 SH - SkIntToScalar(2)));
309
310 SkRandom rand;
311 for (int i = 0; i < N/2; i++) {
312 SkRect r;
313 rnd_rect(&r, &paint, rand);
314 canvas->rotate(SkIntToScalar(15), SW/2, SH/2);
315 canvas->drawPath(fPath, paint);
316 }
317 }
318 }
319
320 private:
321 typedef skiagm::GM INHERITED;
322 };
323
324 //////////////////////////////////////////////////////////////////////////////
325
inset(const SkRect & r)326 static SkRect inset(const SkRect& r) {
327 SkRect rr(r);
328 rr.inset(r.width()/10, r.height()/10);
329 return rr;
330 }
331
332 class Strokes3GM : public skiagm::GM {
make0(SkPath * path,const SkRect & bounds,SkString * title)333 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
334 path->addRect(bounds, SkPath::kCW_Direction);
335 path->addRect(inset(bounds), SkPath::kCW_Direction);
336 title->set("CW CW");
337 }
338
make1(SkPath * path,const SkRect & bounds,SkString * title)339 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
340 path->addRect(bounds, SkPath::kCW_Direction);
341 path->addRect(inset(bounds), SkPath::kCCW_Direction);
342 title->set("CW CCW");
343 }
344
make2(SkPath * path,const SkRect & bounds,SkString * title)345 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
346 path->addOval(bounds, SkPath::kCW_Direction);
347 path->addOval(inset(bounds), SkPath::kCW_Direction);
348 title->set("CW CW");
349 }
350
make3(SkPath * path,const SkRect & bounds,SkString * title)351 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
352 path->addOval(bounds, SkPath::kCW_Direction);
353 path->addOval(inset(bounds), SkPath::kCCW_Direction);
354 title->set("CW CCW");
355 }
356
make4(SkPath * path,const SkRect & bounds,SkString * title)357 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
358 path->addRect(bounds, SkPath::kCW_Direction);
359 SkRect r = bounds;
360 r.inset(bounds.width() / 10, -bounds.height() / 10);
361 path->addOval(r, SkPath::kCW_Direction);
362 title->set("CW CW");
363 }
364
make5(SkPath * path,const SkRect & bounds,SkString * title)365 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
366 path->addRect(bounds, SkPath::kCW_Direction);
367 SkRect r = bounds;
368 r.inset(bounds.width() / 10, -bounds.height() / 10);
369 path->addOval(r, SkPath::kCCW_Direction);
370 title->set("CW CCW");
371 }
372
373 public:
Strokes3GM()374 Strokes3GM() {}
375
376 protected:
377
onShortName()378 SkString onShortName() override {
379 return SkString("strokes3");
380 }
381
onISize()382 SkISize onISize() override {
383 return SkISize::Make(1500, 1500);
384 }
385
onDraw(SkCanvas * canvas)386 void onDraw(SkCanvas* canvas) override {
387 SkPaint origPaint;
388 origPaint.setAntiAlias(true);
389 origPaint.setStyle(SkPaint::kStroke_Style);
390 SkPaint fillPaint(origPaint);
391 fillPaint.setColor(SK_ColorRED);
392 SkPaint strokePaint(origPaint);
393 strokePaint.setColor(sk_tool_utils::color_to_565(0xFF4444FF));
394
395 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
396 make0, make1, make2, make3, make4, make5
397 };
398
399 canvas->translate(SkIntToScalar(20), SkIntToScalar(80));
400
401 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
402 SkScalar dx = bounds.width() * 4/3;
403 SkScalar dy = bounds.height() * 5;
404
405 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
406 SkPath orig;
407 SkString str;
408 procs[i](&orig, bounds, &str);
409
410 canvas->save();
411 for (int j = 0; j < 13; ++j) {
412 strokePaint.setStrokeWidth(SK_Scalar1 * j * j);
413 canvas->drawPath(orig, strokePaint);
414 canvas->drawPath(orig, origPaint);
415 SkPath fill;
416 strokePaint.getFillPath(orig, &fill);
417 canvas->drawPath(fill, fillPaint);
418 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
419 }
420 canvas->restore();
421 canvas->translate(0, dy);
422 }
423 }
424
425 private:
426 typedef skiagm::GM INHERITED;
427 };
428
429 class Strokes4GM : public skiagm::GM {
430 public:
Strokes4GM()431 Strokes4GM() {}
432
433 protected:
434
onShortName()435 SkString onShortName() override {
436 return SkString("strokes_zoomed");
437 }
438
onISize()439 SkISize onISize() override {
440 return SkISize::Make(W, H*2);
441 }
442
onDraw(SkCanvas * canvas)443 void onDraw(SkCanvas* canvas) override {
444 SkPaint paint;
445 paint.setStyle(SkPaint::kStroke_Style);
446 paint.setStrokeWidth(0.055f);
447
448 canvas->scale(1000, 1000);
449 canvas->drawCircle(0, 2, 1.97f, paint);
450 }
451
452 private:
453 typedef skiagm::GM INHERITED;
454 };
455
456 // Test stroking for curves that produce degenerate tangents when t is 0 or 1 (see bug 4191)
457 class Strokes5GM : public skiagm::GM {
458 public:
Strokes5GM()459 Strokes5GM() {}
460
461 protected:
462
onShortName()463 SkString onShortName() override {
464 return SkString("zero_control_stroke");
465 }
466
onISize()467 SkISize onISize() override {
468 return SkISize::Make(W, H*2);
469 }
470
onDraw(SkCanvas * canvas)471 void onDraw(SkCanvas* canvas) override {
472 SkPaint p;
473 p.setColor(SK_ColorRED);
474 p.setAntiAlias(true);
475 p.setStyle(SkPaint::kStroke_Style);
476 p.setStrokeWidth(40);
477 p.setStrokeCap(SkPaint::kButt_Cap);
478
479 SkPath path;
480 path.moveTo(157.474f,111.753f);
481 path.cubicTo(128.5f,111.5f,35.5f,29.5f,35.5f,29.5f);
482 canvas->drawPath(path, p);
483 path.reset();
484 path.moveTo(250, 50);
485 path.quadTo(280, 80, 280, 80);
486 canvas->drawPath(path, p);
487 path.reset();
488 path.moveTo(150, 50);
489 path.conicTo(180, 80, 180, 80, 0.707f);
490 canvas->drawPath(path, p);
491
492 path.reset();
493 path.moveTo(157.474f,311.753f);
494 path.cubicTo(157.474f,311.753f,85.5f,229.5f,35.5f,229.5f);
495 canvas->drawPath(path, p);
496 path.reset();
497 path.moveTo(280, 250);
498 path.quadTo(280, 250, 310, 280);
499 canvas->drawPath(path, p);
500 path.reset();
501 path.moveTo(180, 250);
502 path.conicTo(180, 250, 210, 280, 0.707f);
503 canvas->drawPath(path, p);
504 }
505
506 private:
507 typedef skiagm::GM INHERITED;
508 };
509
510
511 //////////////////////////////////////////////////////////////////////////////
512
513 DEF_GM( return new StrokesGM; )
514 DEF_GM( return new Strokes2GM; )
515 DEF_GM( return new Strokes3GM; )
516 DEF_GM( return new Strokes4GM; )
517 DEF_GM( return new Strokes5GM; )
518
519 DEF_GM( return new ZeroLenStrokesGM; )
520 DEF_GM( return new TeenyStrokesGM; )
521