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