blob: e13e6180c3c084726fce415f6515d6b0e4c43c8a [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"
Mike Reedad5494d2020-08-25 17:36:28 -040013#include "include/core/SkPathBuilder.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
Mike Reedad5494d2020-08-25 17:36:28 -0400128static SkPath make_path_move() {
129 SkPathBuilder builder;
Hal Canaryfa3305a2019-07-18 12:36:54 -0400130 for (SkPoint p : kPts) {
Mike Reedad5494d2020-08-25 17:36:28 -0400131 builder.moveTo(p);
reed58e524c2015-09-04 10:03:26 -0700132 }
Mike Reedad5494d2020-08-25 17:36:28 -0400133 return builder.detach();
reed58e524c2015-09-04 10:03:26 -0700134}
135
Mike Reedad5494d2020-08-25 17:36:28 -0400136static SkPath make_path_move_close() {
137 SkPathBuilder builder;
Hal Canaryfa3305a2019-07-18 12:36:54 -0400138 for (SkPoint p : kPts) {
Mike Reedad5494d2020-08-25 17:36:28 -0400139 builder.moveTo(p).close();
reed58e524c2015-09-04 10:03:26 -0700140 }
Mike Reedad5494d2020-08-25 17:36:28 -0400141 return builder.detach();
reed58e524c2015-09-04 10:03:26 -0700142}
143
Mike Reedad5494d2020-08-25 17:36:28 -0400144static SkPath make_path_move_line() {
145 SkPathBuilder builder;
Hal Canaryfa3305a2019-07-18 12:36:54 -0400146 for (SkPoint p : kPts) {
Mike Reedad5494d2020-08-25 17:36:28 -0400147 builder.moveTo(p).lineTo(p);
reed58e524c2015-09-04 10:03:26 -0700148 }
Mike Reedad5494d2020-08-25 17:36:28 -0400149 return builder.detach();
reed58e524c2015-09-04 10:03:26 -0700150}
151
Mike Reedad5494d2020-08-25 17:36:28 -0400152static SkPath make_path_move_mix() {
153 return SkPathBuilder().moveTo(kPts[0])
154 .moveTo(kPts[1]).close()
155 .moveTo(kPts[2]).lineTo(kPts[2])
156 .detach();
reed58e524c2015-09-04 10:03:26 -0700157}
158
159class EmptyStrokeGM : public GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400160 SkString onShortName() override { return SkString("emptystroke"); }
reed58e524c2015-09-04 10:03:26 -0700161
Hal Canaryfa3305a2019-07-18 12:36:54 -0400162 SkISize onISize() override { return {200, 240}; }
reed58e524c2015-09-04 10:03:26 -0700163
164 void onDraw(SkCanvas* canvas) override {
Mike Reedad5494d2020-08-25 17:36:28 -0400165 static constexpr SkPath (*kProcs[])() = {
reed58e524c2015-09-04 10:03:26 -0700166 make_path_move, // expect red red red
167 make_path_move_close, // expect black black black
168 make_path_move_line, // expect black black black
169 make_path_move_mix, // expect red black black,
170 };
171
172 SkPaint strokePaint;
173 strokePaint.setStyle(SkPaint::kStroke_Style);
174 strokePaint.setStrokeWidth(21);
175 strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
176
177 SkPaint dotPaint;
178 dotPaint.setColor(SK_ColorRED);
179 strokePaint.setStyle(SkPaint::kStroke_Style);
180 dotPaint.setStrokeWidth(7);
181
Hal Canaryfa3305a2019-07-18 12:36:54 -0400182 for (auto proc : kProcs) {
Hal Canaryfa3305a2019-07-18 12:36:54 -0400183 canvas->drawPoints(SkCanvas::kPoints_PointMode, kPtsCount, kPts, dotPaint);
Mike Reedad5494d2020-08-25 17:36:28 -0400184 canvas->drawPath(proc(), strokePaint);
reed58e524c2015-09-04 10:03:26 -0700185 canvas->translate(0, 40);
186 }
187 }
reed58e524c2015-09-04 10:03:26 -0700188};
189DEF_GM( return new EmptyStrokeGM; )
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000190
John Stilesa6841be2020-08-06 14:11:56 -0400191} // namespace skiagm