commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 11 | #include "SkDashPathEffect.h" |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 13 | static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) { |
| 14 | SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w); |
| 15 | SkPath path; |
| 16 | path.addRect(rect); |
| 17 | return path; |
| 18 | } |
| 19 | |
| 20 | static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) { |
| 21 | SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0); |
| 22 | SkPath path; |
| 23 | path.addRect(rect); |
| 24 | return path; |
| 25 | } |
| 26 | |
| 27 | static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) { |
| 28 | SkPath path; |
| 29 | path.addCircle(cx, cy, d/2, SkPath::kCW_Direction); |
| 30 | return path; |
| 31 | } |
| 32 | |
| 33 | static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) { |
| 34 | SkPath path; |
| 35 | path.moveTo(cx - l / 2, cy); |
| 36 | path.lineTo(cx + l / 2, cy); |
| 37 | return path; |
| 38 | } |
| 39 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 40 | namespace { |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 41 | struct Style { |
| 42 | Style(SkPaint::Style paintStyle, sk_sp<SkPathEffect> pe = sk_sp<SkPathEffect>()) |
| 43 | : fPaintStyle(paintStyle) |
| 44 | , fPathEffect(std::move(pe)) {} |
| 45 | SkPaint::Style fPaintStyle; |
| 46 | sk_sp<SkPathEffect> fPathEffect; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 47 | }; |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 48 | |
| 49 | sk_sp<SkPathEffect> make_dash() { |
| 50 | static constexpr SkScalar kIntervals[] = { 4.f, 3.f }; |
| 51 | return SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0); |
| 52 | } |
| 53 | |
| 54 | Style styles[] { |
| 55 | {SkPaint::kStroke_Style}, |
| 56 | {SkPaint::kStrokeAndFill_Style}, |
| 57 | {SkPaint::kFill_Style}, |
| 58 | {SkPaint::kStroke_Style, make_dash()}, |
| 59 | }; |
| 60 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 61 | SkScalar pathSizes[] = { |
| 62 | 40, |
| 63 | 10, |
| 64 | 0 |
| 65 | }; |
| 66 | SkScalar strokeWidths[] = { |
| 67 | 10, |
| 68 | 0 |
| 69 | }; |
| 70 | SkPath ((*paths[])(SkScalar, SkScalar, SkScalar)) = { |
| 71 | generate_square, |
| 72 | generate_rect_line, |
| 73 | generate_circle, |
| 74 | generate_line |
| 75 | }; |
| 76 | |
| 77 | const SkScalar slideWidth = 90, slideHeight = 90; |
| 78 | const SkScalar slideBoundary = 5; |
| 79 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 80 | } // namespace |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 81 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 82 | DEF_SIMPLE_GM(inverse_paths, canvas, 800, 1200) { |
| 83 | SkScalar cx = slideWidth / 2 + slideBoundary; |
| 84 | SkScalar cy = slideHeight / 2 + slideBoundary; |
| 85 | SkScalar dx = slideWidth + 2 * slideBoundary; |
| 86 | SkScalar dy = slideHeight + 2 * slideBoundary; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 87 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 88 | SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary, |
| 89 | slideBoundary + slideWidth, |
| 90 | slideBoundary + slideHeight); |
| 91 | SkPaint clipPaint; |
| 92 | clipPaint.setStyle(SkPaint::kStroke_Style); |
| 93 | clipPaint.setStrokeWidth(SkIntToScalar(2)); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 94 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 95 | SkPaint outlinePaint; |
| 96 | outlinePaint.setColor(0x40000000); |
| 97 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 98 | outlinePaint.setStrokeWidth(SkIntToScalar(0)); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 99 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 100 | for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles); |
| 101 | styleIndex++) { |
| 102 | for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes); |
| 103 | sizeIndex++) { |
| 104 | SkScalar size = pathSizes[sizeIndex]; |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 105 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 106 | canvas->save(); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 107 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 108 | for (size_t widthIndex = 0; |
| 109 | widthIndex < SK_ARRAY_COUNT(strokeWidths); |
| 110 | widthIndex++) { |
| 111 | SkPaint paint; |
| 112 | paint.setColor(0xff007000); |
| 113 | paint.setStrokeWidth(strokeWidths[widthIndex]); |
| 114 | paint.setStyle(styles[styleIndex].fPaintStyle); |
| 115 | paint.setPathEffect(styles[styleIndex].fPathEffect); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 116 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 117 | for (size_t pathIndex = 0; |
| 118 | pathIndex < SK_ARRAY_COUNT(paths); |
| 119 | pathIndex++) { |
| 120 | canvas->drawRect(clipRect, clipPaint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 121 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 122 | canvas->save(); |
| 123 | canvas->clipRect(clipRect); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 124 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 125 | SkPath path = paths[pathIndex](cx, cy, size); |
| 126 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 127 | canvas->drawPath(path, paint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 128 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 129 | path.setFillType(SkPath::kWinding_FillType); |
| 130 | canvas->drawPath(path, outlinePaint); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 131 | |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 132 | canvas->restore(); |
| 133 | canvas->translate(dx, 0); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 134 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 135 | } |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 136 | canvas->restore(); |
| 137 | canvas->translate(0, dy); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 138 | } |
bsalomon | 687d9d2 | 2016-06-10 12:09:59 -0700 | [diff] [blame] | 139 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 140 | } |