blob: aadb94dfcee2c8aa783ef8045b91a61d31bebf8f [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"
8#include "SkCanvas.h"
9#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000011#include "SkRandom.h"
12
13namespace skiagm {
14
15class EmptyPathGM : public GM {
16public:
17 EmptyPathGM() {}
18
19protected:
20 SkString onShortName() {
21 return SkString("emptypath");
22 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
tfarinaf5393182014-06-09 23:59:03 -070024 SkISize onISize() { return SkISize::Make(600, 280); }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000025
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000026 void drawEmpty(SkCanvas* canvas,
27 SkColor color,
28 const SkRect& clip,
29 SkPaint::Style style,
30 SkPath::FillType fill) {
31 SkPath path;
32 path.setFillType(fill);
33 SkPaint paint;
34 paint.setColor(color);
35 paint.setStyle(style);
36 canvas->save();
37 canvas->clipRect(clip);
38 canvas->drawPath(path, paint);
39 canvas->restore();
40 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000041
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000042 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000043 struct FillAndName {
44 SkPath::FillType fFill;
45 const char* fName;
46 };
47 static const FillAndName gFills[] = {
48 {SkPath::kWinding_FillType, "Winding"},
49 {SkPath::kEvenOdd_FillType, "Even / Odd"},
50 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
51 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
52 };
53 struct StyleAndName {
54 SkPaint::Style fStyle;
55 const char* fName;
56 };
57 static const StyleAndName gStyles[] = {
58 {SkPaint::kFill_Style, "Fill"},
59 {SkPaint::kStroke_Style, "Stroke"},
60 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
61 };
62
63 SkPaint titlePaint;
64 titlePaint.setColor(SK_ColorBLACK);
65 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070066 sk_tool_utils::set_portable_typeface(&titlePaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000067 titlePaint.setTextSize(15 * SK_Scalar1);
68 const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
69 "Indicated Style and Fill";
70 canvas->drawText(title, strlen(title),
71 20 * SK_Scalar1,
72 20 * SK_Scalar1,
73 titlePaint);
74
scroggof9d61012014-12-15 12:54:51 -080075 SkRandom rand;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000076 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
77 int i = 0;
78 canvas->save();
79 canvas->translate(10 * SK_Scalar1, 0);
80 canvas->save();
bsalomon@google.comf12449b2011-10-10 14:03:33 +000081 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
82 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000083 if (0 == i % 4) {
84 canvas->restore();
85 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
86 canvas->save();
87 } else {
88 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
89 }
90 ++i;
91
92
93 SkColor color = rand.nextU();
caryclarkd85093c2015-06-12 11:49:04 -070094 color = 0xff000000 | color; // force solid
caryclark65cdba62015-06-15 06:51:08 -070095 color = sk_tool_utils::color_to_565(color);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000096 this->drawEmpty(canvas, color, rect,
97 gStyles[style].fStyle, gFills[fill].fFill);
98
99 SkPaint rectPaint;
100 rectPaint.setColor(SK_ColorBLACK);
101 rectPaint.setStyle(SkPaint::kStroke_Style);
102 rectPaint.setStrokeWidth(-1);
103 rectPaint.setAntiAlias(true);
104 canvas->drawRect(rect, rectPaint);
105
106 SkPaint labelPaint;
107 labelPaint.setColor(color);
108 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700109 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000110 labelPaint.setTextSize(12 * SK_Scalar1);
111 canvas->drawText(gStyles[style].fName,
112 strlen(gStyles[style].fName),
113 0, rect.height() + 15 * SK_Scalar1,
114 labelPaint);
115 canvas->drawText(gFills[fill].fName,
116 strlen(gFills[fill].fName),
117 0, rect.height() + 28 * SK_Scalar1,
118 labelPaint);
119 }
120 }
121 canvas->restore();
122 canvas->restore();
123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000125private:
126 typedef GM INHERITED;
127};
reed58e524c2015-09-04 10:03:26 -0700128DEF_GM( return new EmptyPathGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000129
130//////////////////////////////////////////////////////////////////////////////
131
reed58e524c2015-09-04 10:03:26 -0700132static void make_path_move(SkPath* path, const SkPoint pts[3]) {
133 for (int i = 0; i < 3; ++i) {
134 path->moveTo(pts[i]);
135 }
136}
137
138static void make_path_move_close(SkPath* path, const SkPoint pts[3]) {
139 for (int i = 0; i < 3; ++i) {
140 path->moveTo(pts[i]);
141 path->close();
142 }
143}
144
145static void make_path_move_line(SkPath* path, const SkPoint pts[3]) {
146 for (int i = 0; i < 3; ++i) {
147 path->moveTo(pts[i]);
148 path->lineTo(pts[i]);
149 }
150}
151
152typedef void (*MakePathProc)(SkPath*, const SkPoint pts[3]);
153
154static void make_path_move_mix(SkPath* path, const SkPoint pts[3]) {
155 path->moveTo(pts[0]);
156 path->moveTo(pts[1]); path->close();
157 path->moveTo(pts[2]); path->lineTo(pts[2]);
158}
159
160class EmptyStrokeGM : public GM {
161 SkPoint fPts[3];
162
163public:
164 EmptyStrokeGM() {
165 fPts[0].set(40, 40);
166 fPts[1].set(80, 40);
167 fPts[2].set(120, 40);
168 }
169
170protected:
msarett92602962015-09-04 13:12:55 -0700171 SkString onShortName() override {
reed58e524c2015-09-04 10:03:26 -0700172 return SkString("emptystroke");
173 }
174
175 SkISize onISize() override { return SkISize::Make(200, 240); }
176
177 void onDraw(SkCanvas* canvas) override {
178 const MakePathProc procs[] = {
179 make_path_move, // expect red red red
180 make_path_move_close, // expect black black black
181 make_path_move_line, // expect black black black
182 make_path_move_mix, // expect red black black,
183 };
184
185 SkPaint strokePaint;
186 strokePaint.setStyle(SkPaint::kStroke_Style);
187 strokePaint.setStrokeWidth(21);
188 strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
189
190 SkPaint dotPaint;
191 dotPaint.setColor(SK_ColorRED);
192 strokePaint.setStyle(SkPaint::kStroke_Style);
193 dotPaint.setStrokeWidth(7);
194
195 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
196 SkPath path;
197 procs[i](&path, fPts);
198 canvas->drawPoints(SkCanvas::kPoints_PointMode, 3, fPts, dotPaint);
199 canvas->drawPath(path, strokePaint);
200 canvas->translate(0, 40);
201 }
202 }
halcanary9d524f22016-03-29 09:03:52 -0700203
reed58e524c2015-09-04 10:03:26 -0700204private:
205 typedef GM INHERITED;
206};
207DEF_GM( return new EmptyStrokeGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000208
209}