schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkPath.h" |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 17 | class QuadPathGM : public GM { |
| 18 | public: |
| 19 | QuadPathGM() {} |
| 20 | |
| 21 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 22 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | SkString onShortName() override { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 24 | return SkString("quadpath"); |
| 25 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkISize onISize() override { return SkISize::Make(1240, 390); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 29 | void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, |
| 30 | const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join, |
| 31 | SkPaint::Style style, SkPath::FillType fill, |
| 32 | SkScalar strokeWidth) { |
| 33 | path.setFillType(fill); |
| 34 | SkPaint paint; |
| 35 | paint.setStrokeCap(cap); |
| 36 | paint.setStrokeWidth(strokeWidth); |
| 37 | paint.setStrokeJoin(join); |
| 38 | paint.setColor(color); |
| 39 | paint.setStyle(style); |
| 40 | canvas->save(); |
| 41 | canvas->clipRect(clip); |
| 42 | canvas->drawPath(path, paint); |
| 43 | canvas->restore(); |
| 44 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | void onDraw(SkCanvas* canvas) override { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 47 | struct FillAndName { |
| 48 | SkPath::FillType fFill; |
| 49 | const char* fName; |
| 50 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 51 | constexpr FillAndName gFills[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 52 | {SkPath::kWinding_FillType, "Winding"}, |
| 53 | {SkPath::kEvenOdd_FillType, "Even / Odd"}, |
| 54 | {SkPath::kInverseWinding_FillType, "Inverse Winding"}, |
| 55 | {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, |
| 56 | }; |
| 57 | struct StyleAndName { |
| 58 | SkPaint::Style fStyle; |
| 59 | const char* fName; |
| 60 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 61 | constexpr StyleAndName gStyles[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 62 | {SkPaint::kFill_Style, "Fill"}, |
| 63 | {SkPaint::kStroke_Style, "Stroke"}, |
| 64 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 65 | }; |
| 66 | struct CapAndName { |
| 67 | SkPaint::Cap fCap; |
| 68 | SkPaint::Join fJoin; |
| 69 | const char* fName; |
| 70 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 71 | constexpr CapAndName gCaps[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 72 | {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, |
| 73 | {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, |
| 74 | {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} |
| 75 | }; |
| 76 | struct PathAndName { |
| 77 | SkPath fPath; |
| 78 | const char* fName; |
| 79 | }; |
| 80 | PathAndName path; |
| 81 | path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1); |
| 82 | path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1, |
| 83 | 75*SK_Scalar1, 10*SK_Scalar1); |
| 84 | path.fName = "moveTo-quad"; |
| 85 | |
| 86 | SkPaint titlePaint; |
| 87 | titlePaint.setColor(SK_ColorBLACK); |
| 88 | titlePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 89 | sk_tool_utils::set_portable_typeface(&titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 90 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 91 | const char title[] = "Quad Drawn Into Rectangle Clips With " |
| 92 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 93 | canvas->drawString(title, |
| 94 | 20 * SK_Scalar1, |
| 95 | 20 * SK_Scalar1, |
| 96 | titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 97 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 98 | SkRandom rand; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 99 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 100 | canvas->save(); |
| 101 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 102 | canvas->save(); |
| 103 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 104 | if (0 < cap) { |
| 105 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); |
| 106 | } |
| 107 | canvas->save(); |
| 108 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 109 | if (0 < fill) { |
| 110 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 111 | } |
| 112 | canvas->save(); |
| 113 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 114 | if (0 < style) { |
| 115 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 116 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 117 | |
caryclark | 5df0575 | 2015-07-15 11:57:00 -0700 | [diff] [blame] | 118 | SkColor color = sk_tool_utils::color_to_565(0xff007000); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 119 | this->drawPath(path.fPath, canvas, color, rect, |
| 120 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 121 | gFills[fill].fFill, SK_Scalar1*10); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 123 | SkPaint rectPaint; |
| 124 | rectPaint.setColor(SK_ColorBLACK); |
| 125 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 126 | rectPaint.setStrokeWidth(-1); |
| 127 | rectPaint.setAntiAlias(true); |
| 128 | canvas->drawRect(rect, rectPaint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 129 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 130 | SkPaint labelPaint; |
| 131 | labelPaint.setColor(color); |
| 132 | labelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 133 | sk_tool_utils::set_portable_typeface(&labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 134 | labelPaint.setTextSize(10 * SK_Scalar1); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 135 | canvas->drawString(gStyles[style].fName, |
| 136 | 0, rect.height() + 12 * SK_Scalar1, |
| 137 | labelPaint); |
| 138 | canvas->drawString(gFills[fill].fName, |
| 139 | 0, rect.height() + 24 * SK_Scalar1, |
| 140 | labelPaint); |
| 141 | canvas->drawString(gCaps[cap].fName, |
| 142 | 0, rect.height() + 36 * SK_Scalar1, |
| 143 | labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 144 | } |
| 145 | canvas->restore(); |
| 146 | } |
| 147 | canvas->restore(); |
| 148 | } |
| 149 | canvas->restore(); |
| 150 | canvas->restore(); |
| 151 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 152 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 153 | private: |
| 154 | typedef GM INHERITED; |
| 155 | }; |
| 156 | |
| 157 | class QuadClosePathGM : public GM { |
| 158 | public: |
| 159 | QuadClosePathGM() {} |
| 160 | |
| 161 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 162 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 163 | SkString onShortName() override { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 164 | return SkString("quadclosepath"); |
| 165 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 166 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 167 | SkISize onISize() override { return SkISize::Make(1240, 390); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 168 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 169 | void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, |
| 170 | const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join, |
| 171 | SkPaint::Style style, SkPath::FillType fill, |
| 172 | SkScalar strokeWidth) { |
| 173 | path.setFillType(fill); |
| 174 | SkPaint paint; |
| 175 | paint.setStrokeCap(cap); |
| 176 | paint.setStrokeWidth(strokeWidth); |
| 177 | paint.setStrokeJoin(join); |
| 178 | paint.setColor(color); |
| 179 | paint.setStyle(style); |
| 180 | canvas->save(); |
| 181 | canvas->clipRect(clip); |
| 182 | canvas->drawPath(path, paint); |
| 183 | canvas->restore(); |
| 184 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 186 | void onDraw(SkCanvas* canvas) override { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 187 | struct FillAndName { |
| 188 | SkPath::FillType fFill; |
| 189 | const char* fName; |
| 190 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 191 | constexpr FillAndName gFills[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 192 | {SkPath::kWinding_FillType, "Winding"}, |
| 193 | {SkPath::kEvenOdd_FillType, "Even / Odd"}, |
| 194 | {SkPath::kInverseWinding_FillType, "Inverse Winding"}, |
| 195 | {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, |
| 196 | }; |
| 197 | struct StyleAndName { |
| 198 | SkPaint::Style fStyle; |
| 199 | const char* fName; |
| 200 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 201 | constexpr StyleAndName gStyles[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 202 | {SkPaint::kFill_Style, "Fill"}, |
| 203 | {SkPaint::kStroke_Style, "Stroke"}, |
| 204 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 205 | }; |
| 206 | struct CapAndName { |
| 207 | SkPaint::Cap fCap; |
| 208 | SkPaint::Join fJoin; |
| 209 | const char* fName; |
| 210 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 211 | constexpr CapAndName gCaps[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 212 | {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, |
| 213 | {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, |
| 214 | {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} |
| 215 | }; |
| 216 | struct PathAndName { |
| 217 | SkPath fPath; |
| 218 | const char* fName; |
| 219 | }; |
| 220 | PathAndName path; |
| 221 | path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1); |
| 222 | path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1, |
| 223 | 75*SK_Scalar1, 10*SK_Scalar1); |
| 224 | path.fPath.close(); |
| 225 | path.fName = "moveTo-quad-close"; |
| 226 | |
| 227 | SkPaint titlePaint; |
| 228 | titlePaint.setColor(SK_ColorBLACK); |
| 229 | titlePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 230 | sk_tool_utils::set_portable_typeface(&titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 231 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 232 | const char title[] = "Quad Closed Drawn Into Rectangle Clips With " |
| 233 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 234 | canvas->drawString(title, |
| 235 | 20 * SK_Scalar1, |
| 236 | 20 * SK_Scalar1, |
| 237 | titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 238 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 239 | SkRandom rand; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 240 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 241 | canvas->save(); |
| 242 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 243 | canvas->save(); |
| 244 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 245 | if (0 < cap) { |
| 246 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); |
| 247 | } |
| 248 | canvas->save(); |
| 249 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 250 | if (0 < fill) { |
| 251 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 252 | } |
| 253 | canvas->save(); |
| 254 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 255 | if (0 < style) { |
| 256 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 257 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 258 | |
caryclark | 5df0575 | 2015-07-15 11:57:00 -0700 | [diff] [blame] | 259 | SkColor color = sk_tool_utils::color_to_565(0xff007000); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 260 | this->drawPath(path.fPath, canvas, color, rect, |
| 261 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 262 | gFills[fill].fFill, SK_Scalar1*10); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 263 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 264 | SkPaint rectPaint; |
| 265 | rectPaint.setColor(SK_ColorBLACK); |
| 266 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 267 | rectPaint.setStrokeWidth(-1); |
| 268 | rectPaint.setAntiAlias(true); |
| 269 | canvas->drawRect(rect, rectPaint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 270 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 271 | SkPaint labelPaint; |
| 272 | labelPaint.setColor(color); |
| 273 | labelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 274 | sk_tool_utils::set_portable_typeface(&labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 275 | labelPaint.setTextSize(10 * SK_Scalar1); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 276 | canvas->drawString(gStyles[style].fName, |
| 277 | 0, rect.height() + 12 * SK_Scalar1, |
| 278 | labelPaint); |
| 279 | canvas->drawString(gFills[fill].fName, |
| 280 | 0, rect.height() + 24 * SK_Scalar1, |
| 281 | labelPaint); |
| 282 | canvas->drawString(gCaps[cap].fName, |
| 283 | 0, rect.height() + 36 * SK_Scalar1, |
| 284 | labelPaint); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 285 | } |
| 286 | canvas->restore(); |
| 287 | } |
| 288 | canvas->restore(); |
| 289 | } |
| 290 | canvas->restore(); |
| 291 | canvas->restore(); |
| 292 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 293 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 294 | private: |
| 295 | typedef GM INHERITED; |
| 296 | }; |
| 297 | |
| 298 | ////////////////////////////////////////////////////////////////////////////// |
| 299 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 300 | static GM* QuadPathFactory(void*) { return new QuadPathGM; } |
| 301 | static GMRegistry regQuadPath(QuadPathFactory); |
| 302 | |
| 303 | static GM* QuadClosePathFactory(void*) { return new QuadClosePathGM; } |
| 304 | static GMRegistry regQuadClosePath(QuadClosePathFactory); |
| 305 | |
schenney@chromium.org | 0b71f6c | 2012-01-04 00:18:53 +0000 | [diff] [blame] | 306 | } |