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