2 * Copyright 2012 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"
14 static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint,
15 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0),
16 SkScalar phase = SkIntToScalar(0),
17 SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) {
20 const SkScalar intervals[] = {
25 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
26 canvas->drawLine(startX, startY, finalX, finalY, p);
29 // earlier bug stopped us from drawing very long single-segment dashes, because
30 // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is
31 // now fixes, so this giant dash should appear.
32 static void show_giant_dash(SkCanvas* canvas) {
35 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
38 static void show_zero_len_dash(SkCanvas* canvas) {
41 drawline(canvas, 2, 2, paint, SkIntToScalar(0));
42 paint.setStyle(SkPaint::kStroke_Style);
43 paint.setStrokeWidth(SkIntToScalar(2));
44 canvas->translate(0, SkIntToScalar(20));
45 drawline(canvas, 4, 4, paint, SkIntToScalar(0));
48 class DashingGM : public skiagm::GM {
54 SkString onShortName() {
55 return SkString("dashing");
58 SkISize onISize() { return SkISize::Make(640, 340); }
60 virtual void onDraw(SkCanvas* canvas) {
70 paint.setStyle(SkPaint::kStroke_Style);
72 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
73 canvas->translate(0, SK_ScalarHalf);
74 for (int width = 0; width <= 2; ++width) {
75 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
76 for (int aa = 0; aa <= 1; ++aa) {
77 int w = width * width * width;
78 paint.setAntiAlias(SkToBool(aa));
79 paint.setStrokeWidth(SkIntToScalar(w));
81 int scale = w ? w : 1;
83 drawline(canvas, gData[data].fOnInterval * scale,
84 gData[data].fOffInterval * scale,
86 canvas->translate(0, SkIntToScalar(20));
91 show_giant_dash(canvas);
92 canvas->translate(0, SkIntToScalar(20));
93 show_zero_len_dash(canvas);
94 canvas->translate(0, SkIntToScalar(20));
95 // Draw 0 on, 0 off dashed line
96 paint.setStrokeWidth(SkIntToScalar(8));
97 drawline(canvas, 0, 0, paint);
101 ///////////////////////////////////////////////////////////////////////////////
103 static void make_unit_star(SkPath* path, int n) {
104 SkScalar rad = -SK_ScalarPI / 2;
105 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
107 path->moveTo(0, -SK_Scalar1);
108 for (int i = 1; i < n; i++) {
110 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
111 path->lineTo(cosV, sinV);
116 static void make_path_line(SkPath* path, const SkRect& bounds) {
117 path->moveTo(bounds.left(), bounds.top());
118 path->lineTo(bounds.right(), bounds.bottom());
121 static void make_path_rect(SkPath* path, const SkRect& bounds) {
122 path->addRect(bounds);
125 static void make_path_oval(SkPath* path, const SkRect& bounds) {
126 path->addOval(bounds);
129 static void make_path_star(SkPath* path, const SkRect& bounds) {
130 make_unit_star(path, 5);
132 matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
133 path->transform(matrix);
136 class Dashing2GM : public skiagm::GM {
142 SkString onShortName() {
143 return SkString("dashing2");
146 SkISize onISize() { return SkISize::Make(640, 480); }
148 virtual void onDraw(SkCanvas* canvas) {
149 constexpr int gIntervals[] = {
150 3, // 3 dashes: each count [0] followed by intervals [1..count]
156 void (*gProc[])(SkPath*, const SkRect&) = {
157 make_path_line, make_path_rect, make_path_oval, make_path_star,
161 paint.setAntiAlias(true);
162 paint.setStyle(SkPaint::kStroke_Style);
163 paint.setStrokeWidth(SkIntToScalar(6));
165 SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
166 bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
167 SkScalar dx = bounds.width() * 4 / 3;
168 SkScalar dy = bounds.height() * 4 / 3;
170 const int* intervals = &gIntervals[1];
171 for (int y = 0; y < gIntervals[0]; ++y) {
172 SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough
173 int count = *intervals++;
174 for (int i = 0; i < count; ++i) {
175 vals[i] = SkIntToScalar(*intervals++);
177 SkScalar phase = vals[0] / 2;
178 paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase));
180 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
183 r.offset(x * dx, y * dy);
186 canvas->drawPath(path, paint);
192 //////////////////////////////////////////////////////////////////////////////
194 // Test out the on/off line dashing Chrome if fond of
195 class Dashing3GM : public skiagm::GM {
201 SkString onShortName() {
202 return SkString("dashing3");
205 SkISize onISize() { return SkISize::Make(640, 480); }
207 // Draw a 100x100 block of dashed lines. The horizontal ones are BW
208 // while the vertical ones are AA.
209 void drawDashedLines(SkCanvas* canvas,
216 p.setColor(SK_ColorBLACK);
217 p.setStyle(SkPaint::kStroke_Style);
218 p.setStrokeWidth(SkIntToScalar(strokeWidth));
221 p.setStrokeCap(SkPaint::kRound_Cap);
224 SkScalar intervals[2] = { dashLength, dashLength };
226 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
230 for (int y = 0; y < 100; y += 10*strokeWidth) {
231 pts[0].set(0, SkIntToScalar(y));
232 pts[1].set(lineLength, SkIntToScalar(y));
234 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
237 p.setAntiAlias(true);
239 for (int x = 0; x < 100; x += 14*strokeWidth) {
240 pts[0].set(SkIntToScalar(x), 0);
241 pts[1].set(SkIntToScalar(x), lineLength);
243 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
247 virtual void onDraw(SkCanvas* canvas) {
248 // 1on/1off 1x1 squares with phase of 0 - points fastpath
250 canvas->translate(2, 0);
251 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false);
254 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares)
256 canvas->translate(112, 0);
257 this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false);
260 // 1on/1off 1x1 squares with phase of 1 - points fastpath
262 canvas->translate(222, 0);
263 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
266 // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath
268 canvas->translate(332, 0);
269 this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false);
272 // 255on/255off 1x1 squares with phase of 0 - rects fast path
274 canvas->translate(446, 0);
275 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false);
278 // 1on/1off 3x3 squares with phase of 0 - points fast path
280 canvas->translate(2, 110);
281 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false);
284 // 1on/1off 3x3 squares with phase of 1.5 - rects fast path
286 canvas->translate(112, 110);
287 this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false);
290 // 1on/1off 1x1 circles with phase of 1 - no fast path yet
292 canvas->translate(2, 220);
293 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true);
296 // 1on/1off 3x3 circles with phase of 1 - no fast path yet
298 canvas->translate(112, 220);
299 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true);
302 // 1on/1off 1x1 squares with rotation - should break fast path
304 canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100);
306 canvas->translate(-50, -50);
308 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
311 // 3on/3off 3x1 rects - should use rect fast path regardless of phase
312 for (int phase = 0; phase <= 3; ++phase) {
314 canvas->translate(SkIntToScalar(phase*110+2),
316 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false);
323 //////////////////////////////////////////////////////////////////////////////
325 class Dashing4GM : public skiagm::GM {
331 SkString onShortName() {
332 return SkString("dashing4");
335 SkISize onISize() { return SkISize::Make(640, 950); }
337 virtual void onDraw(SkCanvas* canvas) {
344 { 0, 4 }, // test for zero length on interval
348 paint.setStyle(SkPaint::kStroke_Style);
350 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
351 canvas->translate(0, SK_ScalarHalf);
353 for (int width = 0; width <= 2; ++width) {
354 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
355 for (int aa = 0; aa <= 1; ++aa) {
356 for (int cap = 0; cap <= 1; ++cap) {
357 int w = width * width * width;
358 paint.setAntiAlias(SkToBool(aa));
359 paint.setStrokeWidth(SkIntToScalar(w));
361 SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap)
362 : paint.setStrokeCap(SkPaint::kRound_Cap);
364 int scale = w ? w : 1;
366 drawline(canvas, gData[data].fOnInterval * scale,
367 gData[data].fOffInterval * scale,
369 canvas->translate(0, SkIntToScalar(20));
375 for (int aa = 0; aa <= 1; ++aa) {
376 paint.setAntiAlias(SkToBool(aa));
377 paint.setStrokeWidth(8.f);
378 paint.setStrokeCap(SkPaint::kSquare_Cap);
379 // Single dash element that is cut off at start and end
380 drawline(canvas, 32, 16, paint, 20.f, 0, 5.f);
381 canvas->translate(0, SkIntToScalar(20));
383 // Two dash elements where each one is cut off at beginning and end respectively
384 drawline(canvas, 32, 16, paint, 56.f, 0, 5.f);
385 canvas->translate(0, SkIntToScalar(20));
387 // Many dash elements where first and last are cut off at beginning and end respectively
388 drawline(canvas, 32, 16, paint, 584.f, 0, 5.f);
389 canvas->translate(0, SkIntToScalar(20));
391 // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from
392 // a canvas rotation)
393 drawline(canvas, 32, 16, paint, 600.f, 30.f);
394 canvas->translate(0, SkIntToScalar(20));
396 // Case where only the off interval exists on the line. Thus nothing should be drawn
397 drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f);
398 canvas->translate(0, SkIntToScalar(20));
403 //////////////////////////////////////////////////////////////////////////////
405 class Dashing5GM : public skiagm::GM {
407 Dashing5GM(bool doAA) : fDoAA(doAA) {}
411 bool runAsBench() const override { return true; }
413 SkString onShortName() override {
415 return SkString("dashing5_aa");
417 return SkString("dashing5_bw");
421 SkISize onISize() override { return SkISize::Make(400, 200); }
423 void onDraw(SkCanvas* canvas) override {
424 constexpr int kOn = 4;
425 constexpr int kOff = 4;
426 constexpr int kIntervalLength = kOn + kOff;
428 constexpr SkColor gColors[kIntervalLength] = {
440 paint.setStyle(SkPaint::kStroke_Style);
442 paint.setAntiAlias(fDoAA);
446 SkASSERT(rot.rectStaysRect());
450 int sign; // used to toggle the direction of the lines
453 for (int x = 0; x < 200; x += 10) {
454 paint.setStrokeWidth(SkIntToScalar(phase+1));
455 paint.setColor(gColors[phase]);
456 sign = (x % 20) ? 1 : -1;
457 drawline(canvas, kOn, kOff, paint,
458 SkIntToScalar(x), -sign * SkIntToScalar(10003),
459 SkIntToScalar(phase),
460 SkIntToScalar(x), sign * SkIntToScalar(10003));
461 phase = (phase + 1) % kIntervalLength;
464 for (int y = -400; y < 0; y += 10) {
465 paint.setStrokeWidth(SkIntToScalar(phase+1));
466 paint.setColor(gColors[phase]);
467 sign = (y % 20) ? 1 : -1;
468 drawline(canvas, kOn, kOff, paint,
469 -sign * SkIntToScalar(10003), SkIntToScalar(y),
470 SkIntToScalar(phase),
471 sign * SkIntToScalar(10003), SkIntToScalar(y));
472 phase = (phase + 1) % kIntervalLength;
480 DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) {
482 for (int x = 32; x < 256; x += 16) {
483 for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) {
485 { 256 + (float) sin(a) * x,
486 256 + (float) cos(a) * x },
487 { 256 + (float) sin(a + 3.141592 / 3) * (x + 64),
488 256 + (float) cos(a + 3.141592 / 3) * (x + 64) }
490 lines.moveTo(pts[0]);
491 for (SkScalar i = 0; i < 1; i += 0.05f) {
492 lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i,
493 pts[0].fY * (1 - i) + pts[1].fY * i);
498 p.setAntiAlias(true);
499 p.setStyle(SkPaint::kStroke_Style);
501 const SkScalar intervals[] = { 1, 1 };
502 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
504 canvas->translate(50, 50);
505 canvas->drawPath(lines, p);
508 DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) {
510 p.setAntiAlias(true);
511 p.setStyle(SkPaint::kStroke_Style);
512 p.setStrokeWidth(80);
514 const SkScalar intervals[] = { 2, 2 };
515 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
516 canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p);
519 DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) {
521 p.setAntiAlias(true);
522 p.setStyle(SkPaint::kStroke_Style);
526 wavy.moveTo(-10000, 100);
527 for (SkScalar i = -10000; i < 10000; i += 20) {
528 wavy.quadTo(i + 5, 95, i + 10, 100);
529 wavy.quadTo(i + 15, 105, i + 20, 100);
531 canvas->drawPath(wavy, p);
534 DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) {
536 p.setAntiAlias(true);
537 p.setStyle(SkPaint::kStroke_Style);
538 p.setStrokeWidth(10);
539 p.setStrokeCap(SkPaint::kRound_Cap);
540 p.setStrokeJoin(SkPaint::kRound_Join);
542 p.setARGB(0xff, 0xbb, 0x00, 0x00);
543 sk_tool_utils::set_portable_typeface(&p);
544 const SkScalar intervals[] = { 12, 12 };
545 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
546 canvas->drawString("Sausages", 10, 90, p);
547 canvas->drawLine(8, 120, 456, 120, p);
550 //////////////////////////////////////////////////////////////////////////////
552 DEF_GM(return new DashingGM;)
553 DEF_GM(return new Dashing2GM;)
554 DEF_GM(return new Dashing3GM;)
555 DEF_GM(return new Dashing4GM;)
556 DEF_GM(return new Dashing5GM(true);)
557 DEF_GM(return new Dashing5GM(false);)