blob: d3a6a133c7a6f771fc2d479a97911b3f74657705 [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 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypeface.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/utils/SkRandom.h"
22#include "tools/ToolUtils.h"
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000023
24namespace skiagm {
25
26class EmptyPathGM : public GM {
27public:
28 EmptyPathGM() {}
29
30protected:
31 SkString onShortName() {
32 return SkString("emptypath");
33 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
tfarinaf5393182014-06-09 23:59:03 -070035 SkISize onISize() { return SkISize::Make(600, 280); }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000036
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000037 void drawEmpty(SkCanvas* canvas,
38 SkColor color,
39 const SkRect& clip,
40 SkPaint::Style style,
41 SkPath::FillType fill) {
42 SkPath path;
43 path.setFillType(fill);
44 SkPaint paint;
45 paint.setColor(color);
46 paint.setStyle(style);
47 canvas->save();
48 canvas->clipRect(clip);
49 canvas->drawPath(path, paint);
50 canvas->restore();
51 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000053 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000054 struct FillAndName {
55 SkPath::FillType fFill;
56 const char* fName;
57 };
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr FillAndName gFills[] = {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000059 {SkPath::kWinding_FillType, "Winding"},
60 {SkPath::kEvenOdd_FillType, "Even / Odd"},
61 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
62 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
63 };
64 struct StyleAndName {
65 SkPaint::Style fStyle;
66 const char* fName;
67 };
mtkleindbfd7ab2016-09-01 11:24:54 -070068 constexpr StyleAndName gStyles[] = {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000069 {SkPaint::kFill_Style, "Fill"},
70 {SkPaint::kStroke_Style, "Stroke"},
71 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
72 };
73
Mike Kleinea3f0142019-03-20 11:12:10 -050074 SkFont font(ToolUtils::create_portable_typeface(), 15);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000075 const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
76 "Indicated Style and Fill";
Hal Canary6ac0df82019-01-07 16:01:22 -050077 canvas->drawString(title, 20.0f, 20.0f, font, SkPaint());
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000078
scroggof9d61012014-12-15 12:54:51 -080079 SkRandom rand;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000080 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
81 int i = 0;
82 canvas->save();
83 canvas->translate(10 * SK_Scalar1, 0);
84 canvas->save();
bsalomon@google.comf12449b2011-10-10 14:03:33 +000085 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
86 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000087 if (0 == i % 4) {
88 canvas->restore();
89 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
90 canvas->save();
91 } else {
92 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
93 }
94 ++i;
95
96
97 SkColor color = rand.nextU();
caryclarkd85093c2015-06-12 11:49:04 -070098 color = 0xff000000 | color; // force solid
Mike Kleinea3f0142019-03-20 11:12:10 -050099 color = ToolUtils::color_to_565(color);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000100 this->drawEmpty(canvas, color, rect,
101 gStyles[style].fStyle, gFills[fill].fFill);
102
103 SkPaint rectPaint;
104 rectPaint.setColor(SK_ColorBLACK);
105 rectPaint.setStyle(SkPaint::kStroke_Style);
106 rectPaint.setStrokeWidth(-1);
107 rectPaint.setAntiAlias(true);
108 canvas->drawRect(rect, rectPaint);
109
110 SkPaint labelPaint;
111 labelPaint.setColor(color);
Mike Kleinea3f0142019-03-20 11:12:10 -0500112 SkFont labelFont(ToolUtils::create_portable_typeface(), 12);
Hal Canary6ac0df82019-01-07 16:01:22 -0500113 canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f,
114 labelFont, labelPaint);
115 canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f,
116 labelFont, labelPaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000117 }
118 }
119 canvas->restore();
120 canvas->restore();
121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000123private:
124 typedef GM INHERITED;
125};
reed58e524c2015-09-04 10:03:26 -0700126DEF_GM( return new EmptyPathGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000127
128//////////////////////////////////////////////////////////////////////////////
129
reed58e524c2015-09-04 10:03:26 -0700130static void make_path_move(SkPath* path, const SkPoint pts[3]) {
131 for (int i = 0; i < 3; ++i) {
132 path->moveTo(pts[i]);
133 }
134}
135
136static void make_path_move_close(SkPath* path, const SkPoint pts[3]) {
137 for (int i = 0; i < 3; ++i) {
138 path->moveTo(pts[i]);
139 path->close();
140 }
141}
142
143static void make_path_move_line(SkPath* path, const SkPoint pts[3]) {
144 for (int i = 0; i < 3; ++i) {
145 path->moveTo(pts[i]);
146 path->lineTo(pts[i]);
147 }
148}
149
150typedef void (*MakePathProc)(SkPath*, const SkPoint pts[3]);
151
152static void make_path_move_mix(SkPath* path, const SkPoint pts[3]) {
153 path->moveTo(pts[0]);
154 path->moveTo(pts[1]); path->close();
155 path->moveTo(pts[2]); path->lineTo(pts[2]);
156}
157
158class EmptyStrokeGM : public GM {
159 SkPoint fPts[3];
160
161public:
162 EmptyStrokeGM() {
163 fPts[0].set(40, 40);
164 fPts[1].set(80, 40);
165 fPts[2].set(120, 40);
166 }
167
168protected:
msarett92602962015-09-04 13:12:55 -0700169 SkString onShortName() override {
reed58e524c2015-09-04 10:03:26 -0700170 return SkString("emptystroke");
171 }
172
173 SkISize onISize() override { return SkISize::Make(200, 240); }
174
175 void onDraw(SkCanvas* canvas) override {
176 const MakePathProc procs[] = {
177 make_path_move, // expect red red red
178 make_path_move_close, // expect black black black
179 make_path_move_line, // expect black black black
180 make_path_move_mix, // expect red black black,
181 };
182
183 SkPaint strokePaint;
184 strokePaint.setStyle(SkPaint::kStroke_Style);
185 strokePaint.setStrokeWidth(21);
186 strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
187
188 SkPaint dotPaint;
189 dotPaint.setColor(SK_ColorRED);
190 strokePaint.setStyle(SkPaint::kStroke_Style);
191 dotPaint.setStrokeWidth(7);
192
193 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
194 SkPath path;
195 procs[i](&path, fPts);
196 canvas->drawPoints(SkCanvas::kPoints_PointMode, 3, fPts, dotPaint);
197 canvas->drawPath(path, strokePaint);
198 canvas->translate(0, 40);
199 }
200 }
halcanary9d524f22016-03-29 09:03:52 -0700201
reed58e524c2015-09-04 10:03:26 -0700202private:
203 typedef GM INHERITED;
204};
205DEF_GM( return new EmptyStrokeGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000206
207}