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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkScalar.h" |
| 16 | #include "include/core/SkTypeface.h" |
| 17 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/utils/SkRandom.h" |
| 19 | #include "tools/ToolUtils.h" |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 20 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 21 | static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, |
| 22 | const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join, |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 23 | SkPaint::Style style, SkPathFillType fill, |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 24 | SkScalar strokeWidth) { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 25 | path.setFillType(fill); |
| 26 | SkPaint paint; |
| 27 | paint.setStrokeCap(cap); |
| 28 | paint.setStrokeWidth(strokeWidth); |
| 29 | paint.setStrokeJoin(join); |
| 30 | paint.setColor(color); |
| 31 | paint.setStyle(style); |
| 32 | canvas->save(); |
| 33 | canvas->clipRect(clip); |
| 34 | canvas->drawPath(path, paint); |
| 35 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 36 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 38 | static void draw(SkCanvas* canvas, bool doClose) { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 39 | struct FillAndName { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 40 | SkPathFillType fFill; |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 41 | const char* fName; |
| 42 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 43 | constexpr FillAndName gFills[] = { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 44 | {SkPathFillType::kWinding, "Winding"}, |
| 45 | {SkPathFillType::kEvenOdd, "Even / Odd"}, |
| 46 | {SkPathFillType::kInverseWinding, "Inverse Winding"}, |
| 47 | {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"}, |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 48 | }; |
| 49 | struct StyleAndName { |
| 50 | SkPaint::Style fStyle; |
| 51 | const char* fName; |
| 52 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 53 | constexpr StyleAndName gStyles[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 54 | {SkPaint::kFill_Style, "Fill"}, |
| 55 | {SkPaint::kStroke_Style, "Stroke"}, |
| 56 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 57 | }; |
| 58 | struct CapAndName { |
| 59 | SkPaint::Cap fCap; |
| 60 | SkPaint::Join fJoin; |
| 61 | const char* fName; |
| 62 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 63 | constexpr CapAndName gCaps[] = { |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 64 | {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, |
| 65 | {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, |
| 66 | {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} |
| 67 | }; |
| 68 | struct PathAndName { |
| 69 | SkPath fPath; |
| 70 | const char* fName; |
| 71 | }; |
| 72 | PathAndName path; |
| 73 | path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1); |
| 74 | path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 75 | if (doClose) { |
| 76 | path.fPath.close(); |
| 77 | path.fName = "moveTo-line-close"; |
| 78 | } else { |
| 79 | path.fName = "moveTo-line"; |
| 80 | } |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 81 | |
| 82 | SkPaint titlePaint; |
| 83 | titlePaint.setColor(SK_ColorBLACK); |
| 84 | titlePaint.setAntiAlias(true); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 85 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 86 | SkFont font(ToolUtils::create_portable_typeface(), 15.0f); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 87 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 88 | const char titleNoClose[] = "Line Drawn Into Rectangle Clips With " |
| 89 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 90 | const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With " |
| 91 | "Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 92 | const char* title = doClose ? titleClose : titleNoClose; |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 93 | canvas->drawString(title, 20.0f, 20.0f, font, titlePaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 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 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 115 | SkColor color = ToolUtils::color_to_565(0xff007000); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 116 | drawPath(path.fPath, canvas, color, rect, |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 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); |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 129 | font.setSize(10); |
| 130 | canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f, |
| 131 | font, labelPaint); |
| 132 | canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f, |
| 133 | font, labelPaint); |
| 134 | canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f, |
| 135 | font, labelPaint); |
schenney@chromium.org | 45cbfdd | 2011-12-20 21:48:14 +0000 | [diff] [blame] | 136 | } |
| 137 | canvas->restore(); |
| 138 | } |
| 139 | canvas->restore(); |
| 140 | } |
| 141 | canvas->restore(); |
| 142 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 143 | } |
| 144 | DEF_SIMPLE_GM(linepath, canvas, 1240, 390) { |
| 145 | draw(canvas, false); |
| 146 | } |
| 147 | DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) { |
| 148 | draw(canvas, true); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 149 | } |