blob: 56377e7338e13235781045c1e466353dd37caf4b [file] [log] [blame]
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001/*
2 * Copyright 2011 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#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000012#include "SkRandom.h"
13
14namespace skiagm {
15
16class EmptyPathGM : public GM {
17public:
18 EmptyPathGM() {}
19
20protected:
21 SkString onShortName() {
22 return SkString("emptypath");
23 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000024
tfarinaf5393182014-06-09 23:59:03 -070025 SkISize onISize() { return SkISize::Make(600, 280); }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000026
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000027 void drawEmpty(SkCanvas* canvas,
28 SkColor color,
29 const SkRect& clip,
30 SkPaint::Style style,
31 SkPath::FillType fill) {
32 SkPath path;
33 path.setFillType(fill);
34 SkPaint paint;
35 paint.setColor(color);
36 paint.setStyle(style);
37 canvas->save();
38 canvas->clipRect(clip);
39 canvas->drawPath(path, paint);
40 canvas->restore();
41 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000043 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000044 struct FillAndName {
45 SkPath::FillType fFill;
46 const char* fName;
47 };
mtkleindbfd7ab2016-09-01 11:24:54 -070048 constexpr FillAndName gFills[] = {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000049 {SkPath::kWinding_FillType, "Winding"},
50 {SkPath::kEvenOdd_FillType, "Even / Odd"},
51 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
52 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
53 };
54 struct StyleAndName {
55 SkPaint::Style fStyle;
56 const char* fName;
57 };
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr StyleAndName gStyles[] = {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000059 {SkPaint::kFill_Style, "Fill"},
60 {SkPaint::kStroke_Style, "Stroke"},
61 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
62 };
63
64 SkPaint titlePaint;
65 titlePaint.setColor(SK_ColorBLACK);
66 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070067 sk_tool_utils::set_portable_typeface(&titlePaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000068 titlePaint.setTextSize(15 * SK_Scalar1);
69 const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
70 "Indicated Style and Fill";
71 canvas->drawText(title, strlen(title),
72 20 * SK_Scalar1,
73 20 * SK_Scalar1,
74 titlePaint);
75
scroggof9d61012014-12-15 12:54:51 -080076 SkRandom rand;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000077 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
78 int i = 0;
79 canvas->save();
80 canvas->translate(10 * SK_Scalar1, 0);
81 canvas->save();
bsalomon@google.comf12449b2011-10-10 14:03:33 +000082 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
83 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000084 if (0 == i % 4) {
85 canvas->restore();
86 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
87 canvas->save();
88 } else {
89 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
90 }
91 ++i;
92
93
94 SkColor color = rand.nextU();
caryclarkd85093c2015-06-12 11:49:04 -070095 color = 0xff000000 | color; // force solid
caryclark65cdba62015-06-15 06:51:08 -070096 color = sk_tool_utils::color_to_565(color);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000097 this->drawEmpty(canvas, color, rect,
98 gStyles[style].fStyle, gFills[fill].fFill);
99
100 SkPaint rectPaint;
101 rectPaint.setColor(SK_ColorBLACK);
102 rectPaint.setStyle(SkPaint::kStroke_Style);
103 rectPaint.setStrokeWidth(-1);
104 rectPaint.setAntiAlias(true);
105 canvas->drawRect(rect, rectPaint);
106
107 SkPaint labelPaint;
108 labelPaint.setColor(color);
109 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700110 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000111 labelPaint.setTextSize(12 * SK_Scalar1);
112 canvas->drawText(gStyles[style].fName,
113 strlen(gStyles[style].fName),
114 0, rect.height() + 15 * SK_Scalar1,
115 labelPaint);
116 canvas->drawText(gFills[fill].fName,
117 strlen(gFills[fill].fName),
118 0, rect.height() + 28 * SK_Scalar1,
119 labelPaint);
120 }
121 }
122 canvas->restore();
123 canvas->restore();
124 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000126private:
127 typedef GM INHERITED;
128};
reed58e524c2015-09-04 10:03:26 -0700129DEF_GM( return new EmptyPathGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000130
131//////////////////////////////////////////////////////////////////////////////
132
reed58e524c2015-09-04 10:03:26 -0700133static void make_path_move(SkPath* path, const SkPoint pts[3]) {
134 for (int i = 0; i < 3; ++i) {
135 path->moveTo(pts[i]);
136 }
137}
138
139static void make_path_move_close(SkPath* path, const SkPoint pts[3]) {
140 for (int i = 0; i < 3; ++i) {
141 path->moveTo(pts[i]);
142 path->close();
143 }
144}
145
146static void make_path_move_line(SkPath* path, const SkPoint pts[3]) {
147 for (int i = 0; i < 3; ++i) {
148 path->moveTo(pts[i]);
149 path->lineTo(pts[i]);
150 }
151}
152
153typedef void (*MakePathProc)(SkPath*, const SkPoint pts[3]);
154
155static void make_path_move_mix(SkPath* path, const SkPoint pts[3]) {
156 path->moveTo(pts[0]);
157 path->moveTo(pts[1]); path->close();
158 path->moveTo(pts[2]); path->lineTo(pts[2]);
159}
160
161class EmptyStrokeGM : public GM {
162 SkPoint fPts[3];
163
164public:
165 EmptyStrokeGM() {
166 fPts[0].set(40, 40);
167 fPts[1].set(80, 40);
168 fPts[2].set(120, 40);
169 }
170
171protected:
msarett92602962015-09-04 13:12:55 -0700172 SkString onShortName() override {
reed58e524c2015-09-04 10:03:26 -0700173 return SkString("emptystroke");
174 }
175
176 SkISize onISize() override { return SkISize::Make(200, 240); }
177
178 void onDraw(SkCanvas* canvas) override {
179 const MakePathProc procs[] = {
180 make_path_move, // expect red red red
181 make_path_move_close, // expect black black black
182 make_path_move_line, // expect black black black
183 make_path_move_mix, // expect red black black,
184 };
185
186 SkPaint strokePaint;
187 strokePaint.setStyle(SkPaint::kStroke_Style);
188 strokePaint.setStrokeWidth(21);
189 strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
190
191 SkPaint dotPaint;
192 dotPaint.setColor(SK_ColorRED);
193 strokePaint.setStyle(SkPaint::kStroke_Style);
194 dotPaint.setStrokeWidth(7);
195
196 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
197 SkPath path;
198 procs[i](&path, fPts);
199 canvas->drawPoints(SkCanvas::kPoints_PointMode, 3, fPts, dotPaint);
200 canvas->drawPath(path, strokePaint);
201 canvas->translate(0, 40);
202 }
203 }
halcanary9d524f22016-03-29 09:03:52 -0700204
reed58e524c2015-09-04 10:03:26 -0700205private:
206 typedef GM INHERITED;
207};
208DEF_GM( return new EmptyStrokeGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000209
210}