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