blob: ed08f1049f48c90655bea109e58d336008dfe74d [file] [log] [blame]
reed@google.com35a81df2012-05-04 21:49:27 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPaint.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPath.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/effects/SkDashPathEffect.h"
24#include "tools/ToolUtils.h"
reed@google.com35a81df2012-05-04 21:49:27 +000025
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include <math.h>
27#include <initializer_list>
28
reed@google.comde1837b2012-05-21 16:47:43 +000029static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000030 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0),
halcanary9d524f22016-03-29 09:03:52 -070031 SkScalar phase = SkIntToScalar(0),
robertphillips9f2251c2014-11-04 13:33:50 -080032 SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) {
reed@google.com35a81df2012-05-04 21:49:27 +000033 SkPaint p(paint);
34
35 const SkScalar intervals[] = {
36 SkIntToScalar(on),
37 SkIntToScalar(off),
38 };
39
reeda4393342016-03-18 11:22:57 -070040 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
robertphillips9f2251c2014-11-04 13:33:50 -080041 canvas->drawLine(startX, startY, finalX, finalY, p);
reed@google.comde1837b2012-05-21 16:47:43 +000042}
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.
47static void show_giant_dash(SkCanvas* canvas) {
48 SkPaint paint;
49
50 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
reed@google.com35a81df2012-05-04 21:49:27 +000051}
52
egdaniel21402e32014-11-05 05:02:27 -080053static void show_zero_len_dash(SkCanvas* canvas) {
54 SkPaint paint;
55
56 drawline(canvas, 2, 2, paint, SkIntToScalar(0));
57 paint.setStyle(SkPaint::kStroke_Style);
58 paint.setStrokeWidth(SkIntToScalar(2));
59 canvas->translate(0, SkIntToScalar(20));
60 drawline(canvas, 4, 4, paint, SkIntToScalar(0));
61}
62
reed@google.com21384df2012-05-18 17:59:08 +000063class DashingGM : public skiagm::GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -040064 SkString onShortName() override { return SkString("dashing"); }
reed@google.com35a81df2012-05-04 21:49:27 +000065
Hal Canaryfa3305a2019-07-18 12:36:54 -040066 SkISize onISize() override { return {640, 340}; }
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000067
Hal Canaryfa3305a2019-07-18 12:36:54 -040068 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070069 constexpr struct {
reed@google.com35a81df2012-05-04 21:49:27 +000070 int fOnInterval;
71 int fOffInterval;
72 } gData[] = {
73 { 1, 1 },
74 { 4, 1 },
75 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@google.com35a81df2012-05-04 21:49:27 +000077 SkPaint paint;
78 paint.setStyle(SkPaint::kStroke_Style);
79
80 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
81 canvas->translate(0, SK_ScalarHalf);
reed@google.com35a81df2012-05-04 21:49:27 +000082 for (int width = 0; width <= 2; ++width) {
83 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
84 for (int aa = 0; aa <= 1; ++aa) {
85 int w = width * width * width;
86 paint.setAntiAlias(SkToBool(aa));
87 paint.setStrokeWidth(SkIntToScalar(w));
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@google.com35a81df2012-05-04 21:49:27 +000089 int scale = w ? w : 1;
90
91 drawline(canvas, gData[data].fOnInterval * scale,
92 gData[data].fOffInterval * scale,
93 paint);
94 canvas->translate(0, SkIntToScalar(20));
95 }
96 }
97 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@google.comde1837b2012-05-21 16:47:43 +000099 show_giant_dash(canvas);
egdaniel21402e32014-11-05 05:02:27 -0800100 canvas->translate(0, SkIntToScalar(20));
101 show_zero_len_dash(canvas);
egdanielc00389e2015-10-05 08:11:49 -0700102 canvas->translate(0, SkIntToScalar(20));
103 // Draw 0 on, 0 off dashed line
104 paint.setStrokeWidth(SkIntToScalar(8));
105 drawline(canvas, 0, 0, paint);
reed@google.com35a81df2012-05-04 21:49:27 +0000106 }
reed@google.com21384df2012-05-18 17:59:08 +0000107};
reed@google.com35a81df2012-05-04 21:49:27 +0000108
reed@google.com21384df2012-05-18 17:59:08 +0000109///////////////////////////////////////////////////////////////////////////////
110
111static void make_unit_star(SkPath* path, int n) {
112 SkScalar rad = -SK_ScalarPI / 2;
113 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@google.com21384df2012-05-18 17:59:08 +0000115 path->moveTo(0, -SK_Scalar1);
116 for (int i = 1; i < n; i++) {
117 rad += drad;
Brian Osman4428f2c2019-04-02 10:59:28 -0400118 path->lineTo(SkScalarCos(rad), SkScalarSin(rad));
reed@google.com21384df2012-05-18 17:59:08 +0000119 }
120 path->close();
121}
122
123static void make_path_line(SkPath* path, const SkRect& bounds) {
124 path->moveTo(bounds.left(), bounds.top());
125 path->lineTo(bounds.right(), bounds.bottom());
126}
127
128static void make_path_rect(SkPath* path, const SkRect& bounds) {
129 path->addRect(bounds);
130}
131
132static void make_path_oval(SkPath* path, const SkRect& bounds) {
133 path->addOval(bounds);
134}
135
136static void make_path_star(SkPath* path, const SkRect& bounds) {
137 make_unit_star(path, 5);
138 SkMatrix matrix;
139 matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
140 path->transform(matrix);
141}
142
143class Dashing2GM : public skiagm::GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400144 SkString onShortName() override { return SkString("dashing2"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
Hal Canaryfa3305a2019-07-18 12:36:54 -0400146 SkISize onISize() override { return {640, 480}; }
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000147
Hal Canaryfa3305a2019-07-18 12:36:54 -0400148 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700149 constexpr int gIntervals[] = {
reed@google.com21384df2012-05-18 17:59:08 +0000150 3, // 3 dashes: each count [0] followed by intervals [1..count]
151 2, 10, 10,
152 4, 20, 5, 5, 5,
153 2, 2, 2
154 };
155
156 void (*gProc[])(SkPath*, const SkRect&) = {
157 make_path_line, make_path_rect, make_path_oval, make_path_star,
158 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@google.com21384df2012-05-18 17:59:08 +0000160 SkPaint paint;
161 paint.setAntiAlias(true);
162 paint.setStyle(SkPaint::kStroke_Style);
163 paint.setStrokeWidth(SkIntToScalar(6));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@google.com21384df2012-05-18 17:59:08 +0000165 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;
169
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++);
176 }
177 SkScalar phase = vals[0] / 2;
reeda4393342016-03-18 11:22:57 -0700178 paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@google.com21384df2012-05-18 17:59:08 +0000180 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
181 SkPath path;
182 SkRect r = bounds;
183 r.offset(x * dx, y * dy);
184 gProc[x](&path, r);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
reed@google.com21384df2012-05-18 17:59:08 +0000186 canvas->drawPath(path, paint);
187 }
188 }
189 }
reed@google.com35a81df2012-05-04 21:49:27 +0000190};
191
192//////////////////////////////////////////////////////////////////////////////
193
robertphillips@google.com629ab542012-11-28 17:18:11 +0000194// Test out the on/off line dashing Chrome if fond of
195class Dashing3GM : public skiagm::GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400196 SkString onShortName() override { return SkString("dashing3"); }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000197
Hal Canaryfa3305a2019-07-18 12:36:54 -0400198 SkISize onISize() override { return {640, 480}; }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000199
200 // Draw a 100x100 block of dashed lines. The horizontal ones are BW
201 // while the vertical ones are AA.
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000202 void drawDashedLines(SkCanvas* canvas,
203 SkScalar lineLength,
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000204 SkScalar phase,
205 SkScalar dashLength,
206 int strokeWidth,
207 bool circles) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000208 SkPaint p;
209 p.setColor(SK_ColorBLACK);
210 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000211 p.setStrokeWidth(SkIntToScalar(strokeWidth));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000212
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000213 if (circles) {
214 p.setStrokeCap(SkPaint::kRound_Cap);
215 }
216
217 SkScalar intervals[2] = { dashLength, dashLength };
robertphillips@google.com629ab542012-11-28 17:18:11 +0000218
reeda4393342016-03-18 11:22:57 -0700219 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000220
221 SkPoint pts[2];
222
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000223 for (int y = 0; y < 100; y += 10*strokeWidth) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000224 pts[0].set(0, SkIntToScalar(y));
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000225 pts[1].set(lineLength, SkIntToScalar(y));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000226
227 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
228 }
229
230 p.setAntiAlias(true);
231
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000232 for (int x = 0; x < 100; x += 14*strokeWidth) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000233 pts[0].set(SkIntToScalar(x), 0);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000234 pts[1].set(SkIntToScalar(x), lineLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000235
236 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
237 }
238 }
239
Hal Canaryfa3305a2019-07-18 12:36:54 -0400240 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000241 // 1on/1off 1x1 squares with phase of 0 - points fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000242 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000243 canvas->translate(2, 0);
244 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000245 canvas->restore();
246
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000247 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares)
robertphillips@google.com629ab542012-11-28 17:18:11 +0000248 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000249 canvas->translate(112, 0);
250 this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000251 canvas->restore();
252
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000253 // 1on/1off 1x1 squares with phase of 1 - points fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000254 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000255 canvas->translate(222, 0);
256 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000257 canvas->restore();
258
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000259 // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000260 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000261 canvas->translate(332, 0);
reed@google.com140d7282013-01-07 20:25:04 +0000262 this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000263 canvas->restore();
264
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000265 // 255on/255off 1x1 squares with phase of 0 - rects fast path
266 canvas->save();
267 canvas->translate(446, 0);
268 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false);
269 canvas->restore();
270
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000271 // 1on/1off 3x3 squares with phase of 0 - points fast path
272 canvas->save();
273 canvas->translate(2, 110);
274 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false);
275 canvas->restore();
276
277 // 1on/1off 3x3 squares with phase of 1.5 - rects fast path
278 canvas->save();
279 canvas->translate(112, 110);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000280 this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000281 canvas->restore();
282
283 // 1on/1off 1x1 circles with phase of 1 - no fast path yet
284 canvas->save();
285 canvas->translate(2, 220);
286 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true);
287 canvas->restore();
288
289 // 1on/1off 3x3 circles with phase of 1 - no fast path yet
290 canvas->save();
291 canvas->translate(112, 220);
292 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true);
293 canvas->restore();
294
295 // 1on/1off 1x1 squares with rotation - should break fast path
296 canvas->save();
297 canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000298 canvas->rotate(45);
299 canvas->translate(-50, -50);
300
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000301 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000302 canvas->restore();
303
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000304 // 3on/3off 3x1 rects - should use rect fast path regardless of phase
305 for (int phase = 0; phase <= 3; ++phase) {
306 canvas->save();
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000307 canvas->translate(SkIntToScalar(phase*110+2),
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000308 SkIntToScalar(330));
309 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false);
310 canvas->restore();
311 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000312 }
313
314};
315
316//////////////////////////////////////////////////////////////////////////////
317
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000318class Dashing4GM : public skiagm::GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400319 SkString onShortName() override { return SkString("dashing4"); }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000320
Hal Canaryfa3305a2019-07-18 12:36:54 -0400321 SkISize onISize() override { return {640, 1100}; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000322
Hal Canaryfa3305a2019-07-18 12:36:54 -0400323 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700324 constexpr struct {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000325 int fOnInterval;
326 int fOffInterval;
327 } gData[] = {
328 { 1, 1 },
329 { 4, 2 },
330 { 0, 4 }, // test for zero length on interval
331 };
332
333 SkPaint paint;
334 paint.setStyle(SkPaint::kStroke_Style);
335
336 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
Greg Daniel95581bb2017-06-30 10:36:38 -0400337 canvas->translate(SK_ScalarHalf, SK_ScalarHalf);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000338
339 for (int width = 0; width <= 2; ++width) {
340 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
341 for (int aa = 0; aa <= 1; ++aa) {
342 for (int cap = 0; cap <= 1; ++cap) {
343 int w = width * width * width;
344 paint.setAntiAlias(SkToBool(aa));
345 paint.setStrokeWidth(SkIntToScalar(w));
346
347 SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap)
348 : paint.setStrokeCap(SkPaint::kRound_Cap);
349
350 int scale = w ? w : 1;
351
352 drawline(canvas, gData[data].fOnInterval * scale,
353 gData[data].fOffInterval * scale,
354 paint);
355 canvas->translate(0, SkIntToScalar(20));
356 }
357 }
358 }
359 }
360
361 for (int aa = 0; aa <= 1; ++aa) {
362 paint.setAntiAlias(SkToBool(aa));
363 paint.setStrokeWidth(8.f);
364 paint.setStrokeCap(SkPaint::kSquare_Cap);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000365 // Single dash element that is cut off at start and end
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000366 drawline(canvas, 32, 16, paint, 20.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000367 canvas->translate(0, SkIntToScalar(20));
368
369 // Two dash elements where each one is cut off at beginning and end respectively
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000370 drawline(canvas, 32, 16, paint, 56.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000371 canvas->translate(0, SkIntToScalar(20));
372
373 // Many dash elements where first and last are cut off at beginning and end respectively
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000374 drawline(canvas, 32, 16, paint, 584.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000375 canvas->translate(0, SkIntToScalar(20));
376
377 // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from
378 // a canvas rotation)
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000379 drawline(canvas, 32, 16, paint, 600.f, 30.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000380 canvas->translate(0, SkIntToScalar(20));
381
382 // Case where only the off interval exists on the line. Thus nothing should be drawn
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000383 drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000384 canvas->translate(0, SkIntToScalar(20));
385 }
Greg Daniel5fb30562017-06-29 12:27:48 -0400386
387 // Test overlapping circles.
388 canvas->translate(SkIntToScalar(5), SkIntToScalar(20));
389 paint.setAntiAlias(true);
390 paint.setStrokeCap(SkPaint::kRound_Cap);
391 paint.setColor(0x44000000);
392 paint.setStrokeWidth(40);
393 drawline(canvas, 0, 30, paint);
394
395 canvas->translate(0, SkIntToScalar(50));
396 paint.setStrokeCap(SkPaint::kSquare_Cap);
397 drawline(canvas, 0, 30, paint);
Greg Danielc96f9b52017-12-07 15:00:06 -0500398
399 // Test we draw the cap when the line length is zero.
400 canvas->translate(0, SkIntToScalar(50));
401 paint.setStrokeCap(SkPaint::kRound_Cap);
402 paint.setColor(0xFF000000);
403 paint.setStrokeWidth(11);
404 drawline(canvas, 0, 30, paint, 0);
405
406 canvas->translate(SkIntToScalar(100), 0);
407 drawline(canvas, 1, 30, paint, 0);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000408 }
409};
410
411//////////////////////////////////////////////////////////////////////////////
412
robertphillips9f2251c2014-11-04 13:33:50 -0800413class Dashing5GM : public skiagm::GM {
414public:
415 Dashing5GM(bool doAA) : fDoAA(doAA) {}
reed@google.com35a81df2012-05-04 21:49:27 +0000416
Hal Canaryfa3305a2019-07-18 12:36:54 -0400417private:
mtklein36352bf2015-03-25 18:17:31 -0700418 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800419
Hal Canaryfa3305a2019-07-18 12:36:54 -0400420 SkString onShortName() override { return SkString(fDoAA ? "dashing5_aa" : "dashing5_bw"); }
robertphillips9f2251c2014-11-04 13:33:50 -0800421
Hal Canaryfa3305a2019-07-18 12:36:54 -0400422 SkISize onISize() override { return {400, 200}; }
robertphillips9f2251c2014-11-04 13:33:50 -0800423
mtklein36352bf2015-03-25 18:17:31 -0700424 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700425 constexpr int kOn = 4;
426 constexpr int kOff = 4;
427 constexpr int kIntervalLength = kOn + kOff;
robertphillips9f2251c2014-11-04 13:33:50 -0800428
mtkleindbfd7ab2016-09-01 11:24:54 -0700429 constexpr SkColor gColors[kIntervalLength] = {
robertphillips9f2251c2014-11-04 13:33:50 -0800430 SK_ColorRED,
431 SK_ColorGREEN,
432 SK_ColorBLUE,
433 SK_ColorCYAN,
434 SK_ColorMAGENTA,
435 SK_ColorYELLOW,
436 SK_ColorGRAY,
437 SK_ColorDKGRAY
438 };
439
440 SkPaint paint;
441 paint.setStyle(SkPaint::kStroke_Style);
442
443 paint.setAntiAlias(fDoAA);
444
445 SkMatrix rot;
446 rot.setRotate(90);
447 SkASSERT(rot.rectStaysRect());
448
449 canvas->concat(rot);
450
451 int sign; // used to toggle the direction of the lines
452 int phase = 0;
453
454 for (int x = 0; x < 200; x += 10) {
455 paint.setStrokeWidth(SkIntToScalar(phase+1));
456 paint.setColor(gColors[phase]);
457 sign = (x % 20) ? 1 : -1;
halcanary9d524f22016-03-29 09:03:52 -0700458 drawline(canvas, kOn, kOff, paint,
459 SkIntToScalar(x), -sign * SkIntToScalar(10003),
robertphillips9f2251c2014-11-04 13:33:50 -0800460 SkIntToScalar(phase),
461 SkIntToScalar(x), sign * SkIntToScalar(10003));
462 phase = (phase + 1) % kIntervalLength;
463 }
464
465 for (int y = -400; y < 0; y += 10) {
466 paint.setStrokeWidth(SkIntToScalar(phase+1));
467 paint.setColor(gColors[phase]);
468 sign = (y % 20) ? 1 : -1;
halcanary9d524f22016-03-29 09:03:52 -0700469 drawline(canvas, kOn, kOff, paint,
470 -sign * SkIntToScalar(10003), SkIntToScalar(y),
robertphillips9f2251c2014-11-04 13:33:50 -0800471 SkIntToScalar(phase),
472 sign * SkIntToScalar(10003), SkIntToScalar(y));
473 phase = (phase + 1) % kIntervalLength;
474 }
475 }
476
477private:
478 bool fDoAA;
479};
480
reed6dc14aa2016-04-11 07:46:38 -0700481DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) {
caryclarkf97aa742015-12-18 07:03:13 -0800482 SkPath lines;
483 for (int x = 32; x < 256; x += 16) {
484 for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) {
485 SkPoint pts[2] = {
486 { 256 + (float) sin(a) * x,
487 256 + (float) cos(a) * x },
488 { 256 + (float) sin(a + 3.141592 / 3) * (x + 64),
489 256 + (float) cos(a + 3.141592 / 3) * (x + 64) }
490 };
491 lines.moveTo(pts[0]);
492 for (SkScalar i = 0; i < 1; i += 0.05f) {
493 lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i,
494 pts[0].fY * (1 - i) + pts[1].fY * i);
495 }
496 }
497 }
498 SkPaint p;
499 p.setAntiAlias(true);
500 p.setStyle(SkPaint::kStroke_Style);
501 p.setStrokeWidth(1);
502 const SkScalar intervals[] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -0700503 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
Ben Wagner63fd7602017-10-09 15:45:33 -0400504
reed6dc14aa2016-04-11 07:46:38 -0700505 canvas->translate(50, 50);
caryclarkf97aa742015-12-18 07:03:13 -0800506 canvas->drawPath(lines, p);
507}
508
caryclark70e6d602016-01-30 10:11:21 -0800509DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) {
510 SkPaint p;
511 p.setAntiAlias(true);
512 p.setStyle(SkPaint::kStroke_Style);
513 p.setStrokeWidth(80);
514
515 const SkScalar intervals[] = { 2, 2 };
reeda4393342016-03-18 11:22:57 -0700516 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
caryclark70e6d602016-01-30 10:11:21 -0800517 canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p);
518}
519
520DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) {
521 SkPaint p;
522 p.setAntiAlias(true);
523 p.setStyle(SkPaint::kStroke_Style);
524 p.setStrokeWidth(2);
525
526 SkPath wavy;
527 wavy.moveTo(-10000, 100);
528 for (SkScalar i = -10000; i < 10000; i += 20) {
529 wavy.quadTo(i + 5, 95, i + 10, 100);
530 wavy.quadTo(i + 15, 105, i + 20, 100);
531 }
532 canvas->drawPath(wavy, p);
533}
534
caryclarkd7ea92f2016-03-16 07:34:02 -0700535DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) {
536 SkPaint p;
537 p.setAntiAlias(true);
538 p.setStyle(SkPaint::kStroke_Style);
539 p.setStrokeWidth(10);
540 p.setStrokeCap(SkPaint::kRound_Cap);
caryclark1aaadbd2016-03-17 07:01:49 -0700541 p.setStrokeJoin(SkPaint::kRound_Join);
caryclarkd7ea92f2016-03-16 07:34:02 -0700542 p.setARGB(0xff, 0xbb, 0x00, 0x00);
Mike Reed4de2f1f2019-01-05 16:35:13 -0500543
Mike Kleinea3f0142019-03-20 11:12:10 -0500544 SkFont font(ToolUtils::create_portable_typeface(), 100);
Mike Reed4de2f1f2019-01-05 16:35:13 -0500545
caryclarkd7ea92f2016-03-16 07:34:02 -0700546 const SkScalar intervals[] = { 12, 12 };
reeda4393342016-03-18 11:22:57 -0700547 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
Mike Reed4de2f1f2019-01-05 16:35:13 -0500548 canvas->drawString("Sausages", 10, 90, font, p);
caryclarkd7ea92f2016-03-16 07:34:02 -0700549 canvas->drawLine(8, 120, 456, 120, p);
550}
551
Brian Salomon72f78c32017-12-21 11:56:42 -0500552DEF_SIMPLE_GM(dash_line_zero_off_interval, canvas, 160, 330) {
553 static constexpr SkScalar kIntervals[] = {5.f, 0.f, 2.f, 0.f};
554 SkPaint dashPaint;
555 dashPaint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f));
556 SkASSERT(dashPaint.getPathEffect());
557 dashPaint.setStyle(SkPaint::kStroke_Style);
558 dashPaint.setStrokeWidth(20.f);
559 static constexpr struct {
560 SkPoint fA, fB;
561 } kLines[] = {{{0.5f, 0.5f}, {30.5f, 0.5f}}, // horizontal
562 {{0.5f, 0.5f}, {0.5f, 30.5f}}, // vertical
563 {{0.5f, 0.5f}, {0.5f, 0.5f}}, // point
564 {{0.5f, 0.5f}, {25.5f, 25.5f}}}; // diagonal
565 SkScalar pad = 5.f + dashPaint.getStrokeWidth();
566 canvas->translate(pad / 2.f, pad / 2.f);
567 canvas->save();
568 SkScalar h = 0.f;
569 for (const auto& line : kLines) {
570 h = SkTMax(h, SkScalarAbs(line.fA.fY - line.fB.fY));
571 }
572 for (const auto& line : kLines) {
573 SkScalar w = SkScalarAbs(line.fA.fX - line.fB.fX);
574 for (auto cap : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) {
575 dashPaint.setStrokeCap(cap);
576 for (auto aa : {false, true}) {
577 dashPaint.setAntiAlias(aa);
578 canvas->drawLine(line.fA, line.fB, dashPaint);
579 canvas->translate(0.f, pad + h);
580 }
581 }
582 canvas->restore();
583 canvas->translate(pad + w, 0.f);
584 canvas->save();
585 }
586}
587
robertphillips9f2251c2014-11-04 13:33:50 -0800588//////////////////////////////////////////////////////////////////////////////
589
halcanary385fe4d2015-08-26 13:07:48 -0700590DEF_GM(return new DashingGM;)
591DEF_GM(return new Dashing2GM;)
592DEF_GM(return new Dashing3GM;)
593DEF_GM(return new Dashing4GM;)
594DEF_GM(return new Dashing5GM(true);)
595DEF_GM(return new Dashing5GM(false);)