blob: e7a0c9396c9c88c7301db91ac69bdfd2d05d3a9c [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"
9#include "SkCanvas.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000011#include "SkPaint.h"
12#include "SkRandom.h"
13
halcanary2a243382015-09-09 08:16:41 -070014static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
15 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
16 SkPaint::Style style, SkPath::FillType fill,
17 SkScalar strokeWidth) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000018 path.setFillType(fill);
19 SkPaint paint;
20 paint.setStrokeCap(cap);
21 paint.setStrokeWidth(strokeWidth);
22 paint.setStrokeJoin(join);
23 paint.setColor(color);
24 paint.setStyle(style);
25 canvas->save();
26 canvas->clipRect(clip);
27 canvas->drawPath(path, paint);
28 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070029}
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
halcanary2a243382015-09-09 08:16:41 -070031static void draw(SkCanvas* canvas, bool doClose) {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000032 struct FillAndName {
33 SkPath::FillType fFill;
34 const char* fName;
35 };
mtkleindbfd7ab2016-09-01 11:24:54 -070036 constexpr FillAndName gFills[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000037 {SkPath::kWinding_FillType, "Winding"},
38 {SkPath::kEvenOdd_FillType, "Even / Odd"},
39 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
40 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
41 };
42 struct StyleAndName {
43 SkPaint::Style fStyle;
44 const char* fName;
45 };
mtkleindbfd7ab2016-09-01 11:24:54 -070046 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000047 {SkPaint::kFill_Style, "Fill"},
48 {SkPaint::kStroke_Style, "Stroke"},
49 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
50 };
51 struct CapAndName {
52 SkPaint::Cap fCap;
53 SkPaint::Join fJoin;
54 const char* fName;
55 };
mtkleindbfd7ab2016-09-01 11:24:54 -070056 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000057 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
58 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
59 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
60 };
61 struct PathAndName {
62 SkPath fPath;
63 const char* fName;
64 };
65 PathAndName path;
66 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
67 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
halcanary2a243382015-09-09 08:16:41 -070068 if (doClose) {
69 path.fPath.close();
70 path.fName = "moveTo-line-close";
71 } else {
72 path.fName = "moveTo-line";
73 }
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000074
75 SkPaint titlePaint;
76 titlePaint.setColor(SK_ColorBLACK);
77 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070078 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000079 titlePaint.setTextSize(15 * SK_Scalar1);
halcanary2a243382015-09-09 08:16:41 -070080 const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
81 "Indicated Style, Fill and Linecaps, with stroke width 10";
82 const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
83 "Indicated Style, Fill and Linecaps, with stroke width 10";
84 const char* title = doClose ? titleClose : titleNoClose;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000085 canvas->drawText(title, strlen(title),
86 20 * SK_Scalar1,
87 20 * SK_Scalar1,
88 titlePaint);
89
scroggof9d61012014-12-15 12:54:51 -080090 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000091 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
92 canvas->save();
93 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
94 canvas->save();
95 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
96 if (0 < cap) {
97 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
98 }
99 canvas->save();
100 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
101 if (0 < fill) {
102 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
103 }
104 canvas->save();
105 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
106 if (0 < style) {
107 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000108 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109
caryclark108b5b22015-07-15 06:18:47 -0700110 SkColor color = sk_tool_utils::color_to_565(0xff007000);
halcanary2a243382015-09-09 08:16:41 -0700111 drawPath(path.fPath, canvas, color, rect,
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000112 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
113 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000115 SkPaint rectPaint;
116 rectPaint.setColor(SK_ColorBLACK);
117 rectPaint.setStyle(SkPaint::kStroke_Style);
118 rectPaint.setStrokeWidth(-1);
119 rectPaint.setAntiAlias(true);
120 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000122 SkPaint labelPaint;
123 labelPaint.setColor(color);
124 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700125 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000126 labelPaint.setTextSize(10 * SK_Scalar1);
127 canvas->drawText(gStyles[style].fName,
128 strlen(gStyles[style].fName),
129 0, rect.height() + 12 * SK_Scalar1,
130 labelPaint);
131 canvas->drawText(gFills[fill].fName,
132 strlen(gFills[fill].fName),
133 0, rect.height() + 24 * SK_Scalar1,
134 labelPaint);
135 canvas->drawText(gCaps[cap].fName,
136 strlen(gCaps[cap].fName),
137 0, rect.height() + 36 * SK_Scalar1,
138 labelPaint);
139 }
140 canvas->restore();
141 }
142 canvas->restore();
143 }
144 canvas->restore();
145 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700146}
147DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
148 draw(canvas, false);
149}
150DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
151 draw(canvas, true);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000152}