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" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 10 | #include "SkPath.h" |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 11 | #include "SkPaint.h" |
| 12 | #include "SkRandom.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 16 | class LinePathGM : public GM { |
| 17 | public: |
| 18 | LinePathGM() {} |
| 19 | |
| 20 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 21 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 22 | SkString onShortName() { |
| 23 | return SkString("linepath"); |
| 24 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 25 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 26 | SkISize onISize() { 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 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 45 | virtual void onDraw(SkCanvas* canvas) { |
| 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, 15*SK_Scalar1); |
| 81 | path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1); |
| 82 | path.fName = "moveTo-line"; |
| 83 | |
| 84 | SkPaint titlePaint; |
| 85 | titlePaint.setColor(SK_ColorBLACK); |
| 86 | titlePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 87 | sk_tool_utils::set_portable_typeface(&titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 88 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 89 | const char title[] = "Line Drawn Into Rectangle Clips With " |
| 90 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 91 | canvas->drawText(title, strlen(title), |
| 92 | 20 * SK_Scalar1, |
| 93 | 20 * SK_Scalar1, |
| 94 | titlePaint); |
| 95 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 96 | SkRandom rand; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 97 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 98 | canvas->save(); |
| 99 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 100 | canvas->save(); |
| 101 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 102 | if (0 < cap) { |
| 103 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); |
| 104 | } |
| 105 | canvas->save(); |
| 106 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 107 | if (0 < fill) { |
| 108 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 109 | } |
| 110 | canvas->save(); |
| 111 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 112 | if (0 < style) { |
| 113 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 114 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 | |
caryclark | 108b5b2 | 2015-07-15 06:18:47 -0700 | [diff] [blame] | 116 | SkColor color = sk_tool_utils::color_to_565(0xff007000); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 117 | this->drawPath(path.fPath, canvas, color, rect, |
| 118 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 119 | gFills[fill].fFill, SK_Scalar1*10); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 121 | SkPaint rectPaint; |
| 122 | rectPaint.setColor(SK_ColorBLACK); |
| 123 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 124 | rectPaint.setStrokeWidth(-1); |
| 125 | rectPaint.setAntiAlias(true); |
| 126 | canvas->drawRect(rect, rectPaint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 128 | SkPaint labelPaint; |
| 129 | labelPaint.setColor(color); |
| 130 | labelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 131 | sk_tool_utils::set_portable_typeface(&labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 132 | labelPaint.setTextSize(10 * SK_Scalar1); |
| 133 | canvas->drawText(gStyles[style].fName, |
| 134 | strlen(gStyles[style].fName), |
| 135 | 0, rect.height() + 12 * SK_Scalar1, |
| 136 | labelPaint); |
| 137 | canvas->drawText(gFills[fill].fName, |
| 138 | strlen(gFills[fill].fName), |
| 139 | 0, rect.height() + 24 * SK_Scalar1, |
| 140 | labelPaint); |
| 141 | canvas->drawText(gCaps[cap].fName, |
| 142 | strlen(gCaps[cap].fName), |
| 143 | 0, rect.height() + 36 * SK_Scalar1, |
| 144 | labelPaint); |
| 145 | } |
| 146 | canvas->restore(); |
| 147 | } |
| 148 | canvas->restore(); |
| 149 | } |
| 150 | canvas->restore(); |
| 151 | canvas->restore(); |
| 152 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 153 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 154 | private: |
| 155 | typedef GM INHERITED; |
| 156 | }; |
| 157 | |
| 158 | class LineClosePathGM : public GM { |
| 159 | public: |
| 160 | LineClosePathGM() {} |
| 161 | |
| 162 | protected: |
| 163 | SkString onShortName() { |
| 164 | return SkString("lineclosepath"); |
| 165 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 166 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 167 | SkISize onISize() { 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 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 186 | virtual void onDraw(SkCanvas* canvas) { |
| 187 | struct FillAndName { |
| 188 | SkPath::FillType fFill; |
| 189 | const char* fName; |
| 190 | }; |
| 191 | static const FillAndName gFills[] = { |
| 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 | }; |
| 201 | static const StyleAndName gStyles[] = { |
| 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 | }; |
| 211 | static const CapAndName gCaps[] = { |
| 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, 15*SK_Scalar1); |
| 222 | path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1); |
| 223 | path.fPath.close(); |
| 224 | path.fName = "moveTo-line-close"; |
| 225 | |
| 226 | SkPaint titlePaint; |
| 227 | titlePaint.setColor(SK_ColorBLACK); |
| 228 | titlePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 229 | sk_tool_utils::set_portable_typeface(&titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 230 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 231 | const char title[] = "Line Closed Drawn Into Rectangle Clips With " |
| 232 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 233 | canvas->drawText(title, strlen(title), |
| 234 | 20 * SK_Scalar1, |
| 235 | 20 * SK_Scalar1, |
| 236 | titlePaint); |
| 237 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 238 | SkRandom rand; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 239 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 240 | canvas->save(); |
| 241 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 242 | canvas->save(); |
| 243 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 244 | if (0 < cap) { |
| 245 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); |
| 246 | } |
| 247 | canvas->save(); |
| 248 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 249 | if (0 < fill) { |
| 250 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 251 | } |
| 252 | canvas->save(); |
| 253 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 254 | if (0 < style) { |
| 255 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 256 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 257 | |
caryclark | 108b5b2 | 2015-07-15 06:18:47 -0700 | [diff] [blame] | 258 | SkColor color = sk_tool_utils::color_to_565(0xff007000); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 259 | this->drawPath(path.fPath, canvas, color, rect, |
| 260 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 261 | gFills[fill].fFill, SK_Scalar1*10); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 262 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 263 | SkPaint rectPaint; |
| 264 | rectPaint.setColor(SK_ColorBLACK); |
| 265 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 266 | rectPaint.setStrokeWidth(-1); |
| 267 | rectPaint.setAntiAlias(true); |
| 268 | canvas->drawRect(rect, rectPaint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 269 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 270 | SkPaint labelPaint; |
| 271 | labelPaint.setColor(color); |
| 272 | labelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 273 | sk_tool_utils::set_portable_typeface(&labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 274 | labelPaint.setTextSize(10 * SK_Scalar1); |
| 275 | canvas->drawText(gStyles[style].fName, |
| 276 | strlen(gStyles[style].fName), |
| 277 | 0, rect.height() + 12 * SK_Scalar1, |
| 278 | labelPaint); |
| 279 | canvas->drawText(gFills[fill].fName, |
| 280 | strlen(gFills[fill].fName), |
| 281 | 0, rect.height() + 24 * SK_Scalar1, |
| 282 | labelPaint); |
| 283 | canvas->drawText(gCaps[cap].fName, |
| 284 | strlen(gCaps[cap].fName), |
| 285 | 0, rect.height() + 36 * SK_Scalar1, |
| 286 | labelPaint); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 287 | } |
| 288 | canvas->restore(); |
| 289 | } |
| 290 | canvas->restore(); |
| 291 | } |
| 292 | canvas->restore(); |
| 293 | canvas->restore(); |
| 294 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 295 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 296 | private: |
| 297 | typedef GM INHERITED; |
| 298 | }; |
| 299 | |
| 300 | ////////////////////////////////////////////////////////////////////////////// |
| 301 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 302 | static GM* LinePathFactory(void*) { return new LinePathGM; } |
| 303 | static GMRegistry regLinePath(LinePathFactory); |
| 304 | |
| 305 | static GM* LineClosePathFactory(void*) { return new LineClosePathGM; } |
| 306 | static GMRegistry regLineClosePath(LineClosePathFactory); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 307 | |
| 308 | } |