blob: 2583ff17c1f39e7aa3037c9f88a5b1d4707a987d [file] [log] [blame]
schenney@chromium.org4da06ab2011-12-20 15:14:18 +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 */
bungemand3ebb482015-08-05 13:57:49 -07007
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000010#include "SkCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000012#include "SkPaint.h"
13#include "SkRandom.h"
14
halcanary2a243382015-09-09 08:16:41 -070015static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
16 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
17 SkPaint::Style style, SkPath::FillType fill,
18 SkScalar strokeWidth) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000019 path.setFillType(fill);
20 SkPaint paint;
21 paint.setStrokeCap(cap);
22 paint.setStrokeWidth(strokeWidth);
23 paint.setStrokeJoin(join);
24 paint.setColor(color);
25 paint.setStyle(style);
26 canvas->save();
27 canvas->clipRect(clip);
28 canvas->drawPath(path, paint);
29 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070030}
rmistry@google.comd6176b02012-08-23 18:14:13 +000031
halcanary2a243382015-09-09 08:16:41 -070032static void draw(SkCanvas* canvas, bool doClose) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000033 struct FillAndName {
34 SkPath::FillType fFill;
35 const char* fName;
36 };
mtkleindbfd7ab2016-09-01 11:24:54 -070037 constexpr FillAndName gFills[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000038 {SkPath::kWinding_FillType, "Winding"},
39 {SkPath::kEvenOdd_FillType, "Even / Odd"},
40 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
41 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
42 };
43 struct StyleAndName {
44 SkPaint::Style fStyle;
45 const char* fName;
46 };
mtkleindbfd7ab2016-09-01 11:24:54 -070047 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000048 {SkPaint::kFill_Style, "Fill"},
49 {SkPaint::kStroke_Style, "Stroke"},
50 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
51 };
52 struct CapAndName {
53 SkPaint::Cap fCap;
54 SkPaint::Join fJoin;
55 const char* fName;
56 };
mtkleindbfd7ab2016-09-01 11:24:54 -070057 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000058 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
59 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
60 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
61 };
62 struct PathAndName {
63 SkPath fPath;
64 const char* fName;
65 };
66 PathAndName path;
67 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
68 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
halcanary2a243382015-09-09 08:16:41 -070069 if (doClose) {
70 path.fPath.close();
71 path.fName = "moveTo-line-close";
72 } else {
73 path.fName = "moveTo-line";
74 }
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000075
76 SkPaint titlePaint;
77 titlePaint.setColor(SK_ColorBLACK);
78 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070079 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000080 titlePaint.setTextSize(15 * SK_Scalar1);
halcanary2a243382015-09-09 08:16:41 -070081 const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
82 "Indicated Style, Fill and Linecaps, with stroke width 10";
83 const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
84 "Indicated Style, Fill and Linecaps, with stroke width 10";
85 const char* title = doClose ? titleClose : titleNoClose;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000086 canvas->drawText(title, strlen(title),
87 20 * SK_Scalar1,
88 20 * SK_Scalar1,
89 titlePaint);
90
scroggof9d61012014-12-15 12:54:51 -080091 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000092 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
93 canvas->save();
94 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
95 canvas->save();
96 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
97 if (0 < cap) {
98 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
99 }
100 canvas->save();
101 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
102 if (0 < fill) {
103 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
104 }
105 canvas->save();
106 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
107 if (0 < style) {
108 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000109 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110
caryclark108b5b22015-07-15 06:18:47 -0700111 SkColor color = sk_tool_utils::color_to_565(0xff007000);
halcanary2a243382015-09-09 08:16:41 -0700112 drawPath(path.fPath, canvas, color, rect,
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000113 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
114 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000116 SkPaint rectPaint;
117 rectPaint.setColor(SK_ColorBLACK);
118 rectPaint.setStyle(SkPaint::kStroke_Style);
119 rectPaint.setStrokeWidth(-1);
120 rectPaint.setAntiAlias(true);
121 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000123 SkPaint labelPaint;
124 labelPaint.setColor(color);
125 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700126 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000127 labelPaint.setTextSize(10 * SK_Scalar1);
128 canvas->drawText(gStyles[style].fName,
129 strlen(gStyles[style].fName),
130 0, rect.height() + 12 * SK_Scalar1,
131 labelPaint);
132 canvas->drawText(gFills[fill].fName,
133 strlen(gFills[fill].fName),
134 0, rect.height() + 24 * SK_Scalar1,
135 labelPaint);
136 canvas->drawText(gCaps[cap].fName,
137 strlen(gCaps[cap].fName),
138 0, rect.height() + 36 * SK_Scalar1,
139 labelPaint);
140 }
141 canvas->restore();
142 }
143 canvas->restore();
144 }
145 canvas->restore();
146 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700147}
148DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
149 draw(canvas, false);
150}
151DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
152 draw(canvas, true);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000153}