2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
12 #include "SkDashPathEffect.h"
13 #include "SkParsePath.h"
19 constexpr SkScalar SW = SkIntToScalar(W);
20 constexpr SkScalar SH = SkIntToScalar(H);
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();
30 r->set(x, y, x + w, y + h);
31 r->offset(-w/2 + woffset, -h/2 + hoffset);
33 paint->setColor(rand.nextU());
34 paint->setAlpha(0xFF);
38 class StrokesGM : public skiagm::GM {
44 SkString onShortName() override {
45 return SkString("strokes_round");
48 SkISize onISize() override {
49 return SkISize::Make(W, H*2);
52 void onDraw(SkCanvas* canvas) override {
54 paint.setStyle(SkPaint::kStroke_Style);
55 paint.setStrokeWidth(SkIntToScalar(9)/2);
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)
67 for (int i = 0; i < N; i++) {
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);
79 typedef skiagm::GM INHERITED;
83 https://code.google.com/p/chromium/issues/detail?id=422974 and
84 http://jsfiddle.net/1xnku3sg/2/
86 class ZeroLenStrokesGM : public skiagm::GM {
87 SkPath fMoveHfPath, fMoveZfPath, fDashedfPath, fRefPath[4];
88 SkPath fCubicPath, fQuadPath, fLinePath;
90 void onOnceBeforeDraw() override {
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));
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);
107 SkString onShortName() override {
108 return SkString("zeroPath");
111 SkISize onISize() override {
112 return SkISize::Make(W, H*2);
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);
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));
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);
159 typedef skiagm::GM INHERITED;
162 class TeenyStrokesGM : public skiagm::GM {
164 SkString onShortName() override {
165 return SkString("teenyStrokes");
168 SkISize onISize() override {
169 return SkISize::Make(W, H*2);
172 static void line(SkScalar scale, SkCanvas* canvas, SkColor color) {
174 p.setAntiAlias(true);
175 p.setStyle(SkPaint::kStroke_Style);
177 canvas->translate(50, 0);
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);
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);
194 typedef skiagm::GM INHERITED;
197 DEF_SIMPLE_GM(CubicStroke, canvas, 384, 384) {
199 p.setAntiAlias(true);
200 p.setStyle(SkPaint::kStroke_Style);
201 p.setStrokeWidth(1.0720f);
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);
214 DEF_SIMPLE_GM(zerolinestroke, canvas, 90, 120) {
216 paint.setStyle(SkPaint::kStroke_Style);
217 paint.setStrokeWidth(20);
218 paint.setAntiAlias(true);
219 paint.setStrokeCap(SkPaint::kRound_Cap);
226 canvas->drawPath(path, paint);
231 canvas->drawPath(path, paint);
237 canvas->drawPath(path, paint);
240 DEF_SIMPLE_GM(quadcap, canvas, 200, 200) {
242 p.setAntiAlias(true);
243 p.setStyle(SkPaint::kStroke_Style);
246 SkPoint pts[] = {{105.738571f,13.126318f},
247 {105.738571f,13.126318f},
249 SkVector tangent = pts[1] - pts[2];
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);
266 path.quadTo(pts[1], pts[2]);
267 p.setStrokeCap(SkPaint::kRound_Cap);
268 canvas->translate(30, 0);
269 canvas->drawPath(path, p);
272 class Strokes2GM : public skiagm::GM {
275 void onOnceBeforeDraw() override {
278 for (int i = 0; i < 13; i++) {
279 SkScalar x = rand.nextUScalar1() * (W >> 1);
280 SkScalar y = rand.nextUScalar1() * (H >> 1);
286 SkString onShortName() override {
287 return SkString("strokes_poly");
290 SkISize onISize() override {
291 return SkISize::Make(W, H*2);
294 void onDraw(SkCanvas* canvas) override {
295 canvas->drawColor(SK_ColorWHITE);
298 paint.setStyle(SkPaint::kStroke_Style);
299 paint.setStrokeWidth(SkIntToScalar(9)/2);
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),
307 SW - SkIntToScalar(2),
308 SH - SkIntToScalar(2)));
311 for (int i = 0; i < N/2; i++) {
313 rnd_rect(&r, &paint, rand);
314 canvas->rotate(SkIntToScalar(15), SW/2, SH/2);
315 canvas->drawPath(fPath, paint);
321 typedef skiagm::GM INHERITED;
324 //////////////////////////////////////////////////////////////////////////////
326 static SkRect inset(const SkRect& r) {
328 rr.inset(r.width()/10, r.height()/10);
332 class Strokes3GM : public skiagm::GM {
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);
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");
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);
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");
357 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
358 path->addRect(bounds, SkPath::kCW_Direction);
360 r.inset(bounds.width() / 10, -bounds.height() / 10);
361 path->addOval(r, SkPath::kCW_Direction);
365 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
366 path->addRect(bounds, SkPath::kCW_Direction);
368 r.inset(bounds.width() / 10, -bounds.height() / 10);
369 path->addOval(r, SkPath::kCCW_Direction);
370 title->set("CW CCW");
378 SkString onShortName() override {
379 return SkString("strokes3");
382 SkISize onISize() override {
383 return SkISize::Make(1500, 1500);
386 void onDraw(SkCanvas* canvas) override {
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));
395 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
396 make0, make1, make2, make3, make4, make5
399 canvas->translate(SkIntToScalar(20), SkIntToScalar(80));
401 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
402 SkScalar dx = bounds.width() * 4/3;
403 SkScalar dy = bounds.height() * 5;
405 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
408 procs[i](&orig, bounds, &str);
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);
416 strokePaint.getFillPath(orig, &fill);
417 canvas->drawPath(fill, fillPaint);
418 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
421 canvas->translate(0, dy);
426 typedef skiagm::GM INHERITED;
429 class Strokes4GM : public skiagm::GM {
435 SkString onShortName() override {
436 return SkString("strokes_zoomed");
439 SkISize onISize() override {
440 return SkISize::Make(W, H*2);
443 void onDraw(SkCanvas* canvas) override {
445 paint.setStyle(SkPaint::kStroke_Style);
446 paint.setStrokeWidth(0.055f);
448 canvas->scale(1000, 1000);
449 canvas->drawCircle(0, 2, 1.97f, paint);
453 typedef skiagm::GM INHERITED;
456 // Test stroking for curves that produce degenerate tangents when t is 0 or 1 (see bug 4191)
457 class Strokes5GM : public skiagm::GM {
463 SkString onShortName() override {
464 return SkString("zero_control_stroke");
467 SkISize onISize() override {
468 return SkISize::Make(W, H*2);
471 void onDraw(SkCanvas* canvas) override {
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);
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);
484 path.moveTo(250, 50);
485 path.quadTo(280, 80, 280, 80);
486 canvas->drawPath(path, p);
488 path.moveTo(150, 50);
489 path.conicTo(180, 80, 180, 80, 0.707f);
490 canvas->drawPath(path, p);
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);
497 path.moveTo(280, 250);
498 path.quadTo(280, 250, 310, 280);
499 canvas->drawPath(path, p);
501 path.moveTo(180, 250);
502 path.conicTo(180, 250, 210, 280, 0.707f);
503 canvas->drawPath(path, p);
507 typedef skiagm::GM INHERITED;
511 //////////////////////////////////////////////////////////////////////////////
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; )
519 DEF_GM( return new ZeroLenStrokesGM; )
520 DEF_GM( return new TeenyStrokesGM; )