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 | */ |
| 7 | #include "gm.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "SkRandom.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 14 | class LinePathGM : public GM { |
| 15 | public: |
| 16 | LinePathGM() {} |
| 17 | |
| 18 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 19 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 20 | SkString onShortName() { |
| 21 | return SkString("linepath"); |
| 22 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 23 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 24 | SkISize onISize() { return SkISize::Make(1240, 390); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 25 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 26 | void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, |
| 27 | const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join, |
| 28 | SkPaint::Style style, SkPath::FillType fill, |
| 29 | SkScalar strokeWidth) { |
| 30 | path.setFillType(fill); |
| 31 | SkPaint paint; |
| 32 | paint.setStrokeCap(cap); |
| 33 | paint.setStrokeWidth(strokeWidth); |
| 34 | paint.setStrokeJoin(join); |
| 35 | paint.setColor(color); |
| 36 | paint.setStyle(style); |
| 37 | canvas->save(); |
| 38 | canvas->clipRect(clip); |
| 39 | canvas->drawPath(path, paint); |
| 40 | canvas->restore(); |
| 41 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 43 | virtual void onDraw(SkCanvas* canvas) { |
| 44 | struct FillAndName { |
| 45 | SkPath::FillType fFill; |
| 46 | const char* fName; |
| 47 | }; |
| 48 | static const FillAndName gFills[] = { |
| 49 | {SkPath::kWinding_FillType, "Winding"}, |
| 50 | {SkPath::kEvenOdd_FillType, "Even / Odd"}, |
| 51 | {SkPath::kInverseWinding_FillType, "Inverse Winding"}, |
| 52 | {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, |
| 53 | }; |
| 54 | struct StyleAndName { |
| 55 | SkPaint::Style fStyle; |
| 56 | const char* fName; |
| 57 | }; |
| 58 | static const StyleAndName gStyles[] = { |
| 59 | {SkPaint::kFill_Style, "Fill"}, |
| 60 | {SkPaint::kStroke_Style, "Stroke"}, |
| 61 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 62 | }; |
| 63 | struct CapAndName { |
| 64 | SkPaint::Cap fCap; |
| 65 | SkPaint::Join fJoin; |
| 66 | const char* fName; |
| 67 | }; |
| 68 | static const CapAndName gCaps[] = { |
| 69 | {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, |
| 70 | {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, |
| 71 | {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} |
| 72 | }; |
| 73 | struct PathAndName { |
| 74 | SkPath fPath; |
| 75 | const char* fName; |
| 76 | }; |
| 77 | PathAndName path; |
| 78 | path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1); |
| 79 | path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1); |
| 80 | path.fName = "moveTo-line"; |
| 81 | |
| 82 | SkPaint titlePaint; |
| 83 | titlePaint.setColor(SK_ColorBLACK); |
| 84 | titlePaint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 85 | sk_tool_utils::set_portable_typeface(&titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 86 | titlePaint.setLCDRenderText(true); |
| 87 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 88 | const char title[] = "Line Drawn Into Rectangle Clips With " |
| 89 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 90 | canvas->drawText(title, strlen(title), |
| 91 | 20 * SK_Scalar1, |
| 92 | 20 * SK_Scalar1, |
| 93 | titlePaint); |
| 94 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 95 | SkRandom rand; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 96 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 97 | canvas->save(); |
| 98 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 99 | canvas->save(); |
| 100 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 101 | if (0 < cap) { |
| 102 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); |
| 103 | } |
| 104 | canvas->save(); |
| 105 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 106 | if (0 < fill) { |
| 107 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 108 | } |
| 109 | canvas->save(); |
| 110 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 111 | if (0 < style) { |
| 112 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 113 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 114 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 115 | SkColor color = 0xff007000; |
| 116 | this->drawPath(path.fPath, canvas, color, rect, |
| 117 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 118 | gFills[fill].fFill, SK_Scalar1*10); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 120 | SkPaint rectPaint; |
| 121 | rectPaint.setColor(SK_ColorBLACK); |
| 122 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 123 | rectPaint.setStrokeWidth(-1); |
| 124 | rectPaint.setAntiAlias(true); |
| 125 | canvas->drawRect(rect, rectPaint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 126 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 127 | SkPaint labelPaint; |
| 128 | labelPaint.setColor(color); |
| 129 | labelPaint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 130 | sk_tool_utils::set_portable_typeface(&labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 131 | labelPaint.setLCDRenderText(true); |
| 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); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [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.setLCDRenderText(true); |
| 231 | titlePaint.setTextSize(15 * SK_Scalar1); |
| 232 | const char title[] = "Line Closed Drawn Into Rectangle Clips With " |
| 233 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 234 | canvas->drawText(title, strlen(title), |
| 235 | 20 * SK_Scalar1, |
| 236 | 20 * SK_Scalar1, |
| 237 | titlePaint); |
| 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 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 259 | SkColor color = 0xff007000; |
| 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); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [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.setLCDRenderText(true); |
| 276 | labelPaint.setTextSize(10 * SK_Scalar1); |
| 277 | canvas->drawText(gStyles[style].fName, |
| 278 | strlen(gStyles[style].fName), |
| 279 | 0, rect.height() + 12 * SK_Scalar1, |
| 280 | labelPaint); |
| 281 | canvas->drawText(gFills[fill].fName, |
| 282 | strlen(gFills[fill].fName), |
| 283 | 0, rect.height() + 24 * SK_Scalar1, |
| 284 | labelPaint); |
| 285 | canvas->drawText(gCaps[cap].fName, |
| 286 | strlen(gCaps[cap].fName), |
| 287 | 0, rect.height() + 36 * SK_Scalar1, |
| 288 | labelPaint); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 289 | } |
| 290 | canvas->restore(); |
| 291 | } |
| 292 | canvas->restore(); |
| 293 | } |
| 294 | canvas->restore(); |
| 295 | canvas->restore(); |
| 296 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 297 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 298 | private: |
| 299 | typedef GM INHERITED; |
| 300 | }; |
| 301 | |
| 302 | ////////////////////////////////////////////////////////////////////////////// |
| 303 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 304 | static GM* LinePathFactory(void*) { return new LinePathGM; } |
| 305 | static GMRegistry regLinePath(LinePathFactory); |
| 306 | |
| 307 | static GM* LineClosePathFactory(void*) { return new LineClosePathGM; } |
| 308 | static GMRegistry regLineClosePath(LineClosePathFactory); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 309 | |
| 310 | } |