blob: 154ece5f6e0aede579e8786dc6c352ee9ef998f0 [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 {
Hal Canaryfa3305a2019-07-18 12:36:54 -040027 SkString onShortName() override { return SkString("emptypath"); }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000028
Hal Canaryfa3305a2019-07-18 12:36:54 -040029 SkISize onISize() override { return {600, 280}; }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000030
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000031 void drawEmpty(SkCanvas* canvas,
32 SkColor color,
33 const SkRect& clip,
34 SkPaint::Style style,
Mike Reed7d34dc72019-11-26 12:17:17 -050035 SkPathFillType fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000036 SkPath path;
37 path.setFillType(fill);
38 SkPaint paint;
39 paint.setColor(color);
40 paint.setStyle(style);
41 canvas->save();
42 canvas->clipRect(clip);
43 canvas->drawPath(path, paint);
44 canvas->restore();
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
Hal Canaryfa3305a2019-07-18 12:36:54 -040047 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000048 struct FillAndName {
Mike Reed7d34dc72019-11-26 12:17:17 -050049 SkPathFillType fFill;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000050 const char* fName;
51 };
mtkleindbfd7ab2016-09-01 11:24:54 -070052 constexpr FillAndName gFills[] = {
Mike Reed7d34dc72019-11-26 12:17:17 -050053 {SkPathFillType::kWinding, "Winding"},
54 {SkPathFillType::kEvenOdd, "Even / Odd"},
55 {SkPathFillType::kInverseWinding, "Inverse Winding"},
56 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000057 };
58 struct StyleAndName {
59 SkPaint::Style fStyle;
60 const char* fName;
61 };
mtkleindbfd7ab2016-09-01 11:24:54 -070062 constexpr StyleAndName gStyles[] = {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000063 {SkPaint::kFill_Style, "Fill"},
64 {SkPaint::kStroke_Style, "Stroke"},
65 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
66 };
67
Mike Kleinea3f0142019-03-20 11:12:10 -050068 SkFont font(ToolUtils::create_portable_typeface(), 15);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000069 const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
70 "Indicated Style and Fill";
Hal Canary6ac0df82019-01-07 16:01:22 -050071 canvas->drawString(title, 20.0f, 20.0f, font, SkPaint());
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000072
scroggof9d61012014-12-15 12:54:51 -080073 SkRandom rand;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000074 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
75 int i = 0;
76 canvas->save();
77 canvas->translate(10 * SK_Scalar1, 0);
78 canvas->save();
bsalomon@google.comf12449b2011-10-10 14:03:33 +000079 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
80 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000081 if (0 == i % 4) {
82 canvas->restore();
83 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
84 canvas->save();
85 } else {
86 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
87 }
88 ++i;
89
90
91 SkColor color = rand.nextU();
caryclarkd85093c2015-06-12 11:49:04 -070092 color = 0xff000000 | color; // force solid
Mike Kleinea3f0142019-03-20 11:12:10 -050093 color = ToolUtils::color_to_565(color);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000094 this->drawEmpty(canvas, color, rect,
95 gStyles[style].fStyle, gFills[fill].fFill);
96
97 SkPaint rectPaint;
98 rectPaint.setColor(SK_ColorBLACK);
99 rectPaint.setStyle(SkPaint::kStroke_Style);
100 rectPaint.setStrokeWidth(-1);
101 rectPaint.setAntiAlias(true);
102 canvas->drawRect(rect, rectPaint);
103
104 SkPaint labelPaint;
105 labelPaint.setColor(color);
Mike Kleinea3f0142019-03-20 11:12:10 -0500106 SkFont labelFont(ToolUtils::create_portable_typeface(), 12);
Hal Canary6ac0df82019-01-07 16:01:22 -0500107 canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f,
108 labelFont, labelPaint);
109 canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f,
110 labelFont, labelPaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000111 }
112 }
113 canvas->restore();
114 canvas->restore();
115 }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000116};
reed58e524c2015-09-04 10:03:26 -0700117DEF_GM( return new EmptyPathGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000118
119//////////////////////////////////////////////////////////////////////////////
120
Hal Canaryfa3305a2019-07-18 12:36:54 -0400121static constexpr int kPtsCount = 3;
122static constexpr SkPoint kPts[kPtsCount] = {
123 {40, 40},
124 {80, 40},
125 {120, 40},
126};
127
128static void make_path_move(SkPath* path) {
129 for (SkPoint p : kPts) {
130 path->moveTo(p);
reed58e524c2015-09-04 10:03:26 -0700131 }
132}
133
Hal Canaryfa3305a2019-07-18 12:36:54 -0400134static void make_path_move_close(SkPath* path) {
135 for (SkPoint p : kPts) {
136 path->moveTo(p);
reed58e524c2015-09-04 10:03:26 -0700137 path->close();
138 }
139}
140
Hal Canaryfa3305a2019-07-18 12:36:54 -0400141static void make_path_move_line(SkPath* path) {
142 for (SkPoint p : kPts) {
143 path->moveTo(p);
144 path->lineTo(p);
reed58e524c2015-09-04 10:03:26 -0700145 }
146}
147
Hal Canaryfa3305a2019-07-18 12:36:54 -0400148static void make_path_move_mix(SkPath* path) {
149 path->moveTo(kPts[0]);
150 path->moveTo(kPts[1]); path->close();
151 path->moveTo(kPts[2]); path->lineTo(kPts[2]);
reed58e524c2015-09-04 10:03:26 -0700152}
153
154class EmptyStrokeGM : public GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400155 SkString onShortName() override { return SkString("emptystroke"); }
reed58e524c2015-09-04 10:03:26 -0700156
Hal Canaryfa3305a2019-07-18 12:36:54 -0400157 SkISize onISize() override { return {200, 240}; }
reed58e524c2015-09-04 10:03:26 -0700158
159 void onDraw(SkCanvas* canvas) override {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400160 static constexpr void (*kProcs[])(SkPath*) = {
reed58e524c2015-09-04 10:03:26 -0700161 make_path_move, // expect red red red
162 make_path_move_close, // expect black black black
163 make_path_move_line, // expect black black black
164 make_path_move_mix, // expect red black black,
165 };
166
167 SkPaint strokePaint;
168 strokePaint.setStyle(SkPaint::kStroke_Style);
169 strokePaint.setStrokeWidth(21);
170 strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
171
172 SkPaint dotPaint;
173 dotPaint.setColor(SK_ColorRED);
174 strokePaint.setStyle(SkPaint::kStroke_Style);
175 dotPaint.setStrokeWidth(7);
176
Hal Canaryfa3305a2019-07-18 12:36:54 -0400177 for (auto proc : kProcs) {
reed58e524c2015-09-04 10:03:26 -0700178 SkPath path;
Hal Canaryfa3305a2019-07-18 12:36:54 -0400179 proc(&path);
180 canvas->drawPoints(SkCanvas::kPoints_PointMode, kPtsCount, kPts, dotPaint);
reed58e524c2015-09-04 10:03:26 -0700181 canvas->drawPath(path, strokePaint);
182 canvas->translate(0, 40);
183 }
184 }
reed58e524c2015-09-04 10:03:26 -0700185};
186DEF_GM( return new EmptyStrokeGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000187
188}