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