1 /*
2 * Copyright 2012 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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPathBuilder.h"
15 #include "include/core/SkPathEffect.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "include/effects/SkDashPathEffect.h"
24 #include "tools/ToolUtils.h"
25
26 #include <math.h>
27 #include <initializer_list>
28
drawline(SkCanvas * canvas,int on,int off,const SkPaint & paint,SkScalar finalX=SkIntToScalar (600),SkScalar finalY=SkIntToScalar (0),SkScalar phase=SkIntToScalar (0),SkScalar startX=SkIntToScalar (0),SkScalar startY=SkIntToScalar (0))29 static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint,
30 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0),
31 SkScalar phase = SkIntToScalar(0),
32 SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) {
33 SkPaint p(paint);
34
35 const SkScalar intervals[] = {
36 SkIntToScalar(on),
37 SkIntToScalar(off),
38 };
39
40 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
41 canvas->drawLine(startX, startY, finalX, finalY, p);
42 }
43
44 // earlier bug stopped us from drawing very long single-segment dashes, because
45 // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is
46 // now fixes, so this giant dash should appear.
show_giant_dash(SkCanvas * canvas)47 static void show_giant_dash(SkCanvas* canvas) {
48 SkPaint paint;
49
50 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
51 }
52
show_zero_len_dash(SkCanvas * canvas)53 static void show_zero_len_dash(SkCanvas* canvas) {
54 SkPaint paint;
55
56 drawline(canvas, 2, 2, paint, SkIntToScalar(0));
57 paint.setStroke(true);
58 paint.setStrokeWidth(SkIntToScalar(2));
59 canvas->translate(0, SkIntToScalar(20));
60 drawline(canvas, 4, 4, paint, SkIntToScalar(0));
61 }
62
63 class DashingGM : public skiagm::GM {
onShortName()64 SkString onShortName() override { return SkString("dashing"); }
65
onISize()66 SkISize onISize() override { return {640, 340}; }
67
onDraw(SkCanvas * canvas)68 void onDraw(SkCanvas* canvas) override {
69 struct Intervals {
70 int fOnInterval;
71 int fOffInterval;
72 };
73
74 SkPaint paint;
75 paint.setStroke(true);
76
77 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
78 canvas->translate(0, SK_ScalarHalf);
79 for (int width = 0; width <= 2; ++width) {
80 for (const Intervals& data : {Intervals{1, 1},
81 Intervals{4, 1}}) {
82 for (bool aa : {false, true}) {
83 int w = width * width * width;
84 paint.setAntiAlias(aa);
85 paint.setStrokeWidth(SkIntToScalar(w));
86
87 int scale = w ? w : 1;
88
89 drawline(canvas, data.fOnInterval * scale, data.fOffInterval * scale,
90 paint);
91 canvas->translate(0, SkIntToScalar(20));
92 }
93 }
94 }
95
96 show_giant_dash(canvas);
97 canvas->translate(0, SkIntToScalar(20));
98 show_zero_len_dash(canvas);
99 canvas->translate(0, SkIntToScalar(20));
100 // Draw 0 on, 0 off dashed line
101 paint.setStrokeWidth(SkIntToScalar(8));
102 drawline(canvas, 0, 0, paint);
103 }
104 };
105
106 ///////////////////////////////////////////////////////////////////////////////
107
make_unit_star(int n)108 static SkPath make_unit_star(int n) {
109 SkScalar rad = -SK_ScalarPI / 2;
110 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
111
112 SkPathBuilder b;
113 b.moveTo(0, -SK_Scalar1);
114 for (int i = 1; i < n; i++) {
115 rad += drad;
116 b.lineTo(SkScalarCos(rad), SkScalarSin(rad));
117 }
118 return b.close().detach();
119 }
120
make_path_line(const SkRect & bounds)121 static SkPath make_path_line(const SkRect& bounds) {
122 return SkPathBuilder().moveTo(bounds.left(), bounds.top())
123 .lineTo(bounds.right(), bounds.bottom())
124 .detach();
125 }
126
make_path_rect(const SkRect & bounds)127 static SkPath make_path_rect(const SkRect& bounds) {
128 return SkPath::Rect(bounds);
129 }
130
make_path_oval(const SkRect & bounds)131 static SkPath make_path_oval(const SkRect& bounds) {
132 return SkPath::Oval(bounds);
133 }
134
make_path_star(const SkRect & bounds)135 static SkPath make_path_star(const SkRect& bounds) {
136 SkPath path = make_unit_star(5);
137 SkMatrix matrix = SkMatrix::RectToRect(path.getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
138 return path.makeTransform(matrix);
139 }
140
141 class Dashing2GM : public skiagm::GM {
onShortName()142 SkString onShortName() override { return SkString("dashing2"); }
143
onISize()144 SkISize onISize() override { return {640, 480}; }
145
onDraw(SkCanvas * canvas)146 void onDraw(SkCanvas* canvas) override {
147 constexpr int gIntervals[] = {
148 3, // 3 dashes: each count [0] followed by intervals [1..count]
149 2, 10, 10,
150 4, 20, 5, 5, 5,
151 2, 2, 2
152 };
153
154 SkPath (*gProc[])(const SkRect&) = {
155 make_path_line, make_path_rect, make_path_oval, make_path_star,
156 };
157
158 SkPaint paint;
159 paint.setAntiAlias(true);
160 paint.setStroke(true);
161 paint.setStrokeWidth(SkIntToScalar(6));
162
163 SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
164 bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
165 SkScalar dx = bounds.width() * 4 / 3;
166 SkScalar dy = bounds.height() * 4 / 3;
167
168 const int* intervals = &gIntervals[1];
169 for (int y = 0; y < gIntervals[0]; ++y) {
170 SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough
171 int count = *intervals++;
172 for (int i = 0; i < count; ++i) {
173 vals[i] = SkIntToScalar(*intervals++);
174 }
175 SkScalar phase = vals[0] / 2;
176 paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase));
177
178 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
179 SkPath path;
180 SkRect r = bounds;
181 r.offset(x * dx, y * dy);
182 canvas->drawPath(gProc[x](r), paint);
183 }
184 }
185 }
186 };
187
188 //////////////////////////////////////////////////////////////////////////////
189
190 // Test out the on/off line dashing Chrome if fond of
191 class Dashing3GM : public skiagm::GM {
onShortName()192 SkString onShortName() override { return SkString("dashing3"); }
193
onISize()194 SkISize onISize() override { return {640, 480}; }
195
196 // Draw a 100x100 block of dashed lines. The horizontal ones are BW
197 // while the vertical ones are AA.
drawDashedLines(SkCanvas * canvas,SkScalar lineLength,SkScalar phase,SkScalar dashLength,int strokeWidth,bool circles)198 void drawDashedLines(SkCanvas* canvas,
199 SkScalar lineLength,
200 SkScalar phase,
201 SkScalar dashLength,
202 int strokeWidth,
203 bool circles) {
204 SkPaint p;
205 p.setColor(SK_ColorBLACK);
206 p.setStroke(true);
207 p.setStrokeWidth(SkIntToScalar(strokeWidth));
208
209 if (circles) {
210 p.setStrokeCap(SkPaint::kRound_Cap);
211 }
212
213 SkScalar intervals[2] = { dashLength, dashLength };
214
215 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
216
217 SkPoint pts[2];
218
219 for (int y = 0; y < 100; y += 10*strokeWidth) {
220 pts[0].set(0, SkIntToScalar(y));
221 pts[1].set(lineLength, SkIntToScalar(y));
222
223 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
224 }
225
226 p.setAntiAlias(true);
227
228 for (int x = 0; x < 100; x += 14*strokeWidth) {
229 pts[0].set(SkIntToScalar(x), 0);
230 pts[1].set(SkIntToScalar(x), lineLength);
231
232 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
233 }
234 }
235
onDraw(SkCanvas * canvas)236 void onDraw(SkCanvas* canvas) override {
237 // 1on/1off 1x1 squares with phase of 0 - points fastpath
238 canvas->save();
239 canvas->translate(2, 0);
240 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false);
241 canvas->restore();
242
243 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares)
244 canvas->save();
245 canvas->translate(112, 0);
246 this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false);
247 canvas->restore();
248
249 // 1on/1off 1x1 squares with phase of 1 - points fastpath
250 canvas->save();
251 canvas->translate(222, 0);
252 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
253 canvas->restore();
254
255 // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath
256 canvas->save();
257 canvas->translate(332, 0);
258 this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false);
259 canvas->restore();
260
261 // 255on/255off 1x1 squares with phase of 0 - rects fast path
262 canvas->save();
263 canvas->translate(446, 0);
264 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false);
265 canvas->restore();
266
267 // 1on/1off 3x3 squares with phase of 0 - points fast path
268 canvas->save();
269 canvas->translate(2, 110);
270 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false);
271 canvas->restore();
272
273 // 1on/1off 3x3 squares with phase of 1.5 - rects fast path
274 canvas->save();
275 canvas->translate(112, 110);
276 this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false);
277 canvas->restore();
278
279 // 1on/1off 1x1 circles with phase of 1 - no fast path yet
280 canvas->save();
281 canvas->translate(2, 220);
282 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true);
283 canvas->restore();
284
285 // 1on/1off 3x3 circles with phase of 1 - no fast path yet
286 canvas->save();
287 canvas->translate(112, 220);
288 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true);
289 canvas->restore();
290
291 // 1on/1off 1x1 squares with rotation - should break fast path
292 canvas->save();
293 canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100);
294 canvas->rotate(45);
295 canvas->translate(-50, -50);
296
297 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
298 canvas->restore();
299
300 // 3on/3off 3x1 rects - should use rect fast path regardless of phase
301 for (int phase = 0; phase <= 3; ++phase) {
302 canvas->save();
303 canvas->translate(SkIntToScalar(phase*110+2),
304 SkIntToScalar(330));
305 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false);
306 canvas->restore();
307 }
308 }
309
310 };
311
312 //////////////////////////////////////////////////////////////////////////////
313
314 class Dashing4GM : public skiagm::GM {
onShortName()315 SkString onShortName() override { return SkString("dashing4"); }
316
onISize()317 SkISize onISize() override { return {640, 1100}; }
318
onDraw(SkCanvas * canvas)319 void onDraw(SkCanvas* canvas) override {
320 struct Intervals {
321 int fOnInterval;
322 int fOffInterval;
323 };
324
325 SkPaint paint;
326 paint.setStroke(true);
327
328 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
329 canvas->translate(SK_ScalarHalf, SK_ScalarHalf);
330
331 for (int width = 0; width <= 2; ++width) {
332 for (const Intervals& data : {Intervals{1, 1},
333 Intervals{4, 2},
334 Intervals{0, 4}}) { // test for zero length on interval.
335 // zero length intervals should draw
336 // a line of squares or circles
337 for (bool aa : {false, true}) {
338 for (auto cap : {SkPaint::kRound_Cap, SkPaint::kSquare_Cap}) {
339 int w = width * width * width;
340 paint.setAntiAlias(aa);
341 paint.setStrokeWidth(SkIntToScalar(w));
342 paint.setStrokeCap(cap);
343
344 int scale = w ? w : 1;
345
346 drawline(canvas, data.fOnInterval * scale, data.fOffInterval * scale,
347 paint);
348 canvas->translate(0, SkIntToScalar(20));
349 }
350 }
351 }
352 }
353
354 for (int aa = 0; aa <= 1; ++aa) {
355 paint.setAntiAlias(SkToBool(aa));
356 paint.setStrokeWidth(8.f);
357 paint.setStrokeCap(SkPaint::kSquare_Cap);
358 // Single dash element that is cut off at start and end
359 drawline(canvas, 32, 16, paint, 20.f, 0, 5.f);
360 canvas->translate(0, SkIntToScalar(20));
361
362 // Two dash elements where each one is cut off at beginning and end respectively
363 drawline(canvas, 32, 16, paint, 56.f, 0, 5.f);
364 canvas->translate(0, SkIntToScalar(20));
365
366 // Many dash elements where first and last are cut off at beginning and end respectively
367 drawline(canvas, 32, 16, paint, 584.f, 0, 5.f);
368 canvas->translate(0, SkIntToScalar(20));
369
370 // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from
371 // a canvas rotation)
372 drawline(canvas, 32, 16, paint, 600.f, 30.f);
373 canvas->translate(0, SkIntToScalar(20));
374
375 // Case where only the off interval exists on the line. Thus nothing should be drawn
376 drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f);
377 canvas->translate(0, SkIntToScalar(20));
378 }
379
380 // Test overlapping circles.
381 canvas->translate(SkIntToScalar(5), SkIntToScalar(20));
382 paint.setAntiAlias(true);
383 paint.setStrokeCap(SkPaint::kRound_Cap);
384 paint.setColor(0x44000000);
385 paint.setStrokeWidth(40);
386 drawline(canvas, 0, 30, paint);
387
388 canvas->translate(0, SkIntToScalar(50));
389 paint.setStrokeCap(SkPaint::kSquare_Cap);
390 drawline(canvas, 0, 30, paint);
391
392 // Test we draw the cap when the line length is zero.
393 canvas->translate(0, SkIntToScalar(50));
394 paint.setStrokeCap(SkPaint::kRound_Cap);
395 paint.setColor(0xFF000000);
396 paint.setStrokeWidth(11);
397 drawline(canvas, 0, 30, paint, 0);
398
399 canvas->translate(SkIntToScalar(100), 0);
400 drawline(canvas, 1, 30, paint, 0);
401 }
402 };
403
404 //////////////////////////////////////////////////////////////////////////////
405
406 class Dashing5GM : public skiagm::GM {
407 public:
Dashing5GM(bool doAA)408 Dashing5GM(bool doAA) : fDoAA(doAA) {}
409
410 private:
runAsBench() const411 bool runAsBench() const override { return true; }
412
onShortName()413 SkString onShortName() override { return SkString(fDoAA ? "dashing5_aa" : "dashing5_bw"); }
414
onISize()415 SkISize onISize() override { return {400, 200}; }
416
onDraw(SkCanvas * canvas)417 void onDraw(SkCanvas* canvas) override {
418 constexpr int kOn = 4;
419 constexpr int kOff = 4;
420 constexpr int kIntervalLength = kOn + kOff;
421
422 constexpr SkColor gColors[kIntervalLength] = {
423 SK_ColorRED,
424 SK_ColorGREEN,
425 SK_ColorBLUE,
426 SK_ColorCYAN,
427 SK_ColorMAGENTA,
428 SK_ColorYELLOW,
429 SK_ColorGRAY,
430 SK_ColorDKGRAY
431 };
432
433 SkPaint paint;
434 paint.setStroke(true);
435
436 paint.setAntiAlias(fDoAA);
437
438 SkMatrix rot;
439 rot.setRotate(90);
440 SkASSERT(rot.rectStaysRect());
441
442 canvas->concat(rot);
443
444 int sign; // used to toggle the direction of the lines
445 int phase = 0;
446
447 for (int x = 0; x < 200; x += 10) {
448 paint.setStrokeWidth(SkIntToScalar(phase+1));
449 paint.setColor(gColors[phase]);
450 sign = (x % 20) ? 1 : -1;
451 drawline(canvas, kOn, kOff, paint,
452 SkIntToScalar(x), -sign * SkIntToScalar(10003),
453 SkIntToScalar(phase),
454 SkIntToScalar(x), sign * SkIntToScalar(10003));
455 phase = (phase + 1) % kIntervalLength;
456 }
457
458 for (int y = -400; y < 0; y += 10) {
459 paint.setStrokeWidth(SkIntToScalar(phase+1));
460 paint.setColor(gColors[phase]);
461 sign = (y % 20) ? 1 : -1;
462 drawline(canvas, kOn, kOff, paint,
463 -sign * SkIntToScalar(10003), SkIntToScalar(y),
464 SkIntToScalar(phase),
465 sign * SkIntToScalar(10003), SkIntToScalar(y));
466 phase = (phase + 1) % kIntervalLength;
467 }
468 }
469
470 private:
471 bool fDoAA;
472 };
473
474 DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) {
475 SkPath lines;
476 for (int x = 32; x < 256; x += 16) {
477 for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) {
478 SkPoint pts[2] = {
479 { 256 + (float) sin(a) * x,
480 256 + (float) cos(a) * x },
481 { 256 + (float) sin(a + 3.141592 / 3) * (x + 64),
482 256 + (float) cos(a + 3.141592 / 3) * (x + 64) }
483 };
484 lines.moveTo(pts[0]);
485 for (SkScalar i = 0; i < 1; i += 0.05f) {
486 lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i,
487 pts[0].fY * (1 - i) + pts[1].fY * i);
488 }
489 }
490 }
491 SkPaint p;
492 p.setAntiAlias(true);
493 p.setStroke(true);
494 p.setStrokeWidth(1);
495 const SkScalar intervals[] = { 1, 1 };
496 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
497
498 canvas->translate(50, 50);
499 canvas->drawPath(lines, p);
500 }
501
502 DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) {
503 SkPaint p;
504 p.setAntiAlias(true);
505 p.setStroke(true);
506 p.setStrokeWidth(80);
507
508 const SkScalar intervals[] = { 2, 2 };
509 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
510 canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p);
511 }
512
513 DEF_SIMPLE_GM(dashbigrects, canvas, 256, 256) {
514 SkRandom rand;
515
516 constexpr int kHalfStrokeWidth = 8;
517 constexpr int kOnOffInterval = 2*kHalfStrokeWidth;
518
519 canvas->clear(SkColors::kBlack);
520
521 SkPaint p;
522 p.setAntiAlias(true);
523 p.setStroke(true);
524 p.setStrokeWidth(2*kHalfStrokeWidth);
525 p.setStrokeCap(SkPaint::kButt_Cap);
526
527 constexpr SkScalar intervals[] = { kOnOffInterval, kOnOffInterval };
528 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
529
530 constexpr float gWidthHeights[] = {
531 1000000000.0f * kOnOffInterval + kOnOffInterval/2.0f,
532 1000000.0f * kOnOffInterval + kOnOffInterval/2.0f,
533 1000.0f * kOnOffInterval + kOnOffInterval/2.0f,
534 100.0f * kOnOffInterval + kOnOffInterval/2.0f,
535 10.0f * kOnOffInterval + kOnOffInterval/2.0f,
536 9.0f * kOnOffInterval + kOnOffInterval/2.0f,
537 8.0f * kOnOffInterval + kOnOffInterval/2.0f,
538 7.0f * kOnOffInterval + kOnOffInterval/2.0f,
539 6.0f * kOnOffInterval + kOnOffInterval/2.0f,
540 5.0f * kOnOffInterval + kOnOffInterval/2.0f,
541 4.0f * kOnOffInterval + kOnOffInterval/2.0f,
542 };
543
544 for (size_t i = 0; i < SK_ARRAY_COUNT(gWidthHeights); ++i) {
545 p.setColor(ToolUtils::color_to_565(rand.nextU() | (0xFF << 24)));
546
547 int offset = 2 * i * kHalfStrokeWidth + kHalfStrokeWidth;
548 canvas->drawRect(SkRect::MakeXYWH(offset, offset, gWidthHeights[i], gWidthHeights[i]), p);
549 }
550 }
551
552 DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) {
553 SkPaint p;
554 p.setAntiAlias(true);
555 p.setStroke(true);
556 p.setStrokeWidth(2);
557
558 SkPath wavy;
559 wavy.moveTo(-10000, 100);
560 for (SkScalar i = -10000; i < 10000; i += 20) {
561 wavy.quadTo(i + 5, 95, i + 10, 100);
562 wavy.quadTo(i + 15, 105, i + 20, 100);
563 }
564 canvas->drawPath(wavy, p);
565 }
566
567 DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) {
568 SkPaint p;
569 p.setAntiAlias(true);
570 p.setStroke(true);
571 p.setStrokeWidth(10);
572 p.setStrokeCap(SkPaint::kRound_Cap);
573 p.setStrokeJoin(SkPaint::kRound_Join);
574 p.setARGB(0xff, 0xbb, 0x00, 0x00);
575
576 SkFont font(ToolUtils::create_portable_typeface(), 100);
577
578 const SkScalar intervals[] = { 12, 12 };
579 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
580 canvas->drawString("Sausages", 10, 90, font, p);
581 canvas->drawLine(8, 120, 456, 120, p);
582 }
583
584 DEF_SIMPLE_GM(dash_line_zero_off_interval, canvas, 160, 330) {
585 static constexpr SkScalar kIntervals[] = {5.f, 0.f, 2.f, 0.f};
586 SkPaint dashPaint;
587 dashPaint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f));
588 SkASSERT(dashPaint.getPathEffect());
589 dashPaint.setStroke(true);
590 dashPaint.setStrokeWidth(20.f);
591 static constexpr struct {
592 SkPoint fA, fB;
593 } kLines[] = {{{0.5f, 0.5f}, {30.5f, 0.5f}}, // horizontal
594 {{0.5f, 0.5f}, {0.5f, 30.5f}}, // vertical
595 {{0.5f, 0.5f}, {0.5f, 0.5f}}, // point
596 {{0.5f, 0.5f}, {25.5f, 25.5f}}}; // diagonal
597 SkScalar pad = 5.f + dashPaint.getStrokeWidth();
598 canvas->translate(pad / 2.f, pad / 2.f);
599 canvas->save();
600 SkScalar h = 0.f;
601 for (const auto& line : kLines) {
602 h = std::max(h, SkScalarAbs(line.fA.fY - line.fB.fY));
603 }
604 for (const auto& line : kLines) {
605 SkScalar w = SkScalarAbs(line.fA.fX - line.fB.fX);
606 for (auto cap : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) {
607 dashPaint.setStrokeCap(cap);
608 for (auto aa : {false, true}) {
609 dashPaint.setAntiAlias(aa);
610 canvas->drawLine(line.fA, line.fB, dashPaint);
611 canvas->translate(0.f, pad + h);
612 }
613 }
614 canvas->restore();
615 canvas->translate(pad + w, 0.f);
616 canvas->save();
617 }
618 }
619
620 DEF_SIMPLE_GM(thin_aa_dash_lines, canvas, 330, 110) {
621 SkPaint paint;
622 static constexpr SkScalar kScale = 100.f;
623 static constexpr SkScalar kIntervals[] = {10/kScale, 5/kScale};
624 paint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f));
625 paint.setAntiAlias(true);
626 paint.setStrokeWidth(0.25f/kScale);
627 // substep moves the subpixel offset every iteration.
628 static constexpr SkScalar kSubstep = 0.05f/kScale;
629 // We will draw a grid of horiz/vertical lines that pass through each other's off intervals.
630 static constexpr SkScalar kStep = kIntervals[0] + kIntervals[1];
631 canvas->scale(kScale, kScale);
632 canvas->translate(kIntervals[1], kIntervals[1]);
633 for (auto c : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) {
634 paint.setStrokeCap(c);
635 for (SkScalar x = -.5f*kIntervals[1]; x < 105/kScale; x += (kStep + kSubstep)) {
636 canvas->drawLine({x, 0}, {x, 100/kScale}, paint);
637 canvas->drawLine({0, x}, {100/kScale, x}, paint);
638 }
639 canvas->translate(110/kScale, 0);
640 }
641 }
642
643 DEF_SIMPLE_GM(path_effect_empty_result, canvas, 100, 100) {
644 SkPaint p;
645 p.setStroke(true);
646 p.setStrokeWidth(1);
647
648 SkPath path;
649 float r = 70;
650 float l = 70;
651 float t = 70;
652 float b = 70;
653 path.moveTo(l, t);
654 path.lineTo(r, t);
655 path.lineTo(r, b);
656 path.lineTo(l, b);
657 path.close();
658
659 float dashes[] = {2.f, 2.f};
660 p.setPathEffect(SkDashPathEffect::Make(dashes, 2, 0.f));
661
662 canvas->drawPath(path, p);
663 }
664
665 //////////////////////////////////////////////////////////////////////////////
666
667 DEF_GM(return new DashingGM;)
668 DEF_GM(return new Dashing2GM;)
669 DEF_GM(return new Dashing3GM;)
670 DEF_GM(return new Dashing4GM;)
671 DEF_GM(return new Dashing5GM(true);)
672 DEF_GM(return new Dashing5GM(false);)
673