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