| 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" | 
| bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" | 
| schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" | 
|  | 13 | #include "SkRandom.h" | 
|  | 14 |  | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 15 | static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, | 
|  | 16 | const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join, | 
|  | 17 | SkPaint::Style style, SkPath::FillType fill, | 
|  | 18 | SkScalar strokeWidth) { | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 19 | path.setFillType(fill); | 
|  | 20 | SkPaint paint; | 
|  | 21 | paint.setStrokeCap(cap); | 
|  | 22 | paint.setStrokeWidth(strokeWidth); | 
|  | 23 | paint.setStrokeJoin(join); | 
|  | 24 | paint.setColor(color); | 
|  | 25 | paint.setStyle(style); | 
|  | 26 | canvas->save(); | 
|  | 27 | canvas->clipRect(clip); | 
|  | 28 | canvas->drawPath(path, paint); | 
|  | 29 | canvas->restore(); | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 30 | } | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 31 |  | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 32 | static void draw(SkCanvas* canvas, bool doClose) { | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 33 | struct FillAndName { | 
|  | 34 | SkPath::FillType fFill; | 
|  | 35 | const char*      fName; | 
|  | 36 | }; | 
| mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 37 | constexpr FillAndName gFills[] = { | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 38 | {SkPath::kWinding_FillType, "Winding"}, | 
|  | 39 | {SkPath::kEvenOdd_FillType, "Even / Odd"}, | 
|  | 40 | {SkPath::kInverseWinding_FillType, "Inverse Winding"}, | 
|  | 41 | {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, | 
|  | 42 | }; | 
|  | 43 | struct StyleAndName { | 
|  | 44 | SkPaint::Style fStyle; | 
|  | 45 | const char*    fName; | 
|  | 46 | }; | 
| mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 47 | constexpr StyleAndName gStyles[] = { | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 48 | {SkPaint::kFill_Style, "Fill"}, | 
|  | 49 | {SkPaint::kStroke_Style, "Stroke"}, | 
|  | 50 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, | 
|  | 51 | }; | 
|  | 52 | struct CapAndName { | 
|  | 53 | SkPaint::Cap  fCap; | 
|  | 54 | SkPaint::Join fJoin; | 
|  | 55 | const char*   fName; | 
|  | 56 | }; | 
| mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 57 | constexpr CapAndName gCaps[] = { | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 58 | {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, | 
|  | 59 | {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, | 
|  | 60 | {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} | 
|  | 61 | }; | 
|  | 62 | struct PathAndName { | 
|  | 63 | SkPath      fPath; | 
|  | 64 | const char* fName; | 
|  | 65 | }; | 
|  | 66 | PathAndName path; | 
|  | 67 | path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1); | 
|  | 68 | path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1); | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 69 | if (doClose) { | 
|  | 70 | path.fPath.close(); | 
|  | 71 | path.fName = "moveTo-line-close"; | 
|  | 72 | } else { | 
|  | 73 | path.fName = "moveTo-line"; | 
|  | 74 | } | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 75 |  | 
|  | 76 | SkPaint titlePaint; | 
|  | 77 | titlePaint.setColor(SK_ColorBLACK); | 
|  | 78 | titlePaint.setAntiAlias(true); | 
| caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 79 | sk_tool_utils::set_portable_typeface(&titlePaint); | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 80 | titlePaint.setTextSize(15 * SK_Scalar1); | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 81 | const char titleNoClose[] = "Line Drawn Into Rectangle Clips With " | 
|  | 82 | "Indicated Style, Fill and Linecaps, with stroke width 10"; | 
|  | 83 | const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With " | 
|  | 84 | "Indicated Style, Fill and Linecaps, with stroke width 10"; | 
|  | 85 | const char* title = doClose ? titleClose : titleNoClose; | 
| Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 86 | canvas->drawString(title, | 
|  | 87 | 20 * SK_Scalar1, | 
|  | 88 | 20 * SK_Scalar1, | 
|  | 89 | titlePaint); | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 90 |  | 
| scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 91 | SkRandom rand; | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 92 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); | 
|  | 93 | canvas->save(); | 
|  | 94 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); | 
|  | 95 | canvas->save(); | 
|  | 96 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { | 
|  | 97 | if (0 < cap) { | 
|  | 98 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); | 
|  | 99 | } | 
|  | 100 | canvas->save(); | 
|  | 101 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { | 
|  | 102 | if (0 < fill) { | 
|  | 103 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); | 
|  | 104 | } | 
|  | 105 | canvas->save(); | 
|  | 106 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { | 
|  | 107 | if (0 < style) { | 
|  | 108 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); | 
| schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 109 | } | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 110 |  | 
| caryclark | 108b5b2 | 2015-07-15 06:18:47 -0700 | [diff] [blame] | 111 | SkColor color = sk_tool_utils::color_to_565(0xff007000); | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 112 | drawPath(path.fPath, canvas, color, rect, | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 113 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, | 
|  | 114 | gFills[fill].fFill, SK_Scalar1*10); | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 |  | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 116 | SkPaint rectPaint; | 
|  | 117 | rectPaint.setColor(SK_ColorBLACK); | 
|  | 118 | rectPaint.setStyle(SkPaint::kStroke_Style); | 
|  | 119 | rectPaint.setStrokeWidth(-1); | 
|  | 120 | rectPaint.setAntiAlias(true); | 
|  | 121 | canvas->drawRect(rect, rectPaint); | 
| 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 labelPaint; | 
|  | 124 | labelPaint.setColor(color); | 
|  | 125 | labelPaint.setAntiAlias(true); | 
| caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 126 | sk_tool_utils::set_portable_typeface(&labelPaint); | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 127 | labelPaint.setTextSize(10 * SK_Scalar1); | 
| Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 128 | canvas->drawString(gStyles[style].fName, | 
|  | 129 | 0, rect.height() + 12 * SK_Scalar1, | 
|  | 130 | labelPaint); | 
|  | 131 | canvas->drawString(gFills[fill].fName, | 
|  | 132 | 0, rect.height() + 24 * SK_Scalar1, | 
|  | 133 | labelPaint); | 
|  | 134 | canvas->drawString(gCaps[cap].fName, | 
|  | 135 | 0, rect.height() + 36 * SK_Scalar1, | 
|  | 136 | labelPaint); | 
| schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 137 | } | 
|  | 138 | canvas->restore(); | 
|  | 139 | } | 
|  | 140 | canvas->restore(); | 
|  | 141 | } | 
|  | 142 | canvas->restore(); | 
|  | 143 | canvas->restore(); | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 144 | } | 
|  | 145 | DEF_SIMPLE_GM(linepath, canvas, 1240, 390) { | 
|  | 146 | draw(canvas, false); | 
|  | 147 | } | 
|  | 148 | DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) { | 
|  | 149 | draw(canvas, true); | 
| schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 150 | } |