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); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 79 | |
| 80 | SkFont font(sk_tool_utils::create_portable_typeface(), 15.0f); |
| 81 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 82 | const char titleNoClose[] = "Line Drawn Into Rectangle Clips With " |
| 83 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 84 | const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With " |
| 85 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 86 | const char* title = doClose ? titleClose : titleNoClose; |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 87 | canvas->drawString(title, 20.0f, 20.0f, font, titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 88 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 89 | SkRandom rand; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 90 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 91 | canvas->save(); |
| 92 | canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 93 | canvas->save(); |
| 94 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 95 | if (0 < cap) { |
| 96 | canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); |
| 97 | } |
| 98 | canvas->save(); |
| 99 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 100 | if (0 < fill) { |
| 101 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 102 | } |
| 103 | canvas->save(); |
| 104 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 105 | if (0 < style) { |
| 106 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 107 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 108 | |
caryclark | 108b5b2 | 2015-07-15 06:18:47 -0700 | [diff] [blame] | 109 | SkColor color = sk_tool_utils::color_to_565(0xff007000); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 110 | drawPath(path.fPath, canvas, color, rect, |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 111 | gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 112 | gFills[fill].fFill, SK_Scalar1*10); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 114 | SkPaint rectPaint; |
| 115 | rectPaint.setColor(SK_ColorBLACK); |
| 116 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 117 | rectPaint.setStrokeWidth(-1); |
| 118 | rectPaint.setAntiAlias(true); |
| 119 | canvas->drawRect(rect, rectPaint); |
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 labelPaint; |
| 122 | labelPaint.setColor(color); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 123 | font.setSize(10); |
| 124 | canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f, |
| 125 | font, labelPaint); |
| 126 | canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f, |
| 127 | font, labelPaint); |
| 128 | canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f, |
| 129 | font, labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 130 | } |
| 131 | canvas->restore(); |
| 132 | } |
| 133 | canvas->restore(); |
| 134 | } |
| 135 | canvas->restore(); |
| 136 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 137 | } |
| 138 | DEF_SIMPLE_GM(linepath, canvas, 1240, 390) { |
| 139 | draw(canvas, false); |
| 140 | } |
| 141 | DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) { |
| 142 | draw(canvas, true); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 143 | } |