bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +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 | */ |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [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" |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 13 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkScalar.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTypeface.h" |
| 20 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/utils/SkRandom.h" |
| 22 | #include "tools/ToolUtils.h" |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 23 | |
| 24 | namespace skiagm { |
| 25 | |
| 26 | class EmptyPathGM : public GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 27 | SkString onShortName() override { return SkString("emptypath"); } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 28 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 29 | SkISize onISize() override { return {600, 280}; } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 31 | void drawEmpty(SkCanvas* canvas, |
| 32 | SkColor color, |
| 33 | const SkRect& clip, |
| 34 | SkPaint::Style style, |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 35 | SkPathFillType fill) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 36 | SkPath path; |
| 37 | path.setFillType(fill); |
| 38 | SkPaint paint; |
| 39 | paint.setColor(color); |
| 40 | paint.setStyle(style); |
| 41 | canvas->save(); |
| 42 | canvas->clipRect(clip); |
| 43 | canvas->drawPath(path, paint); |
| 44 | canvas->restore(); |
| 45 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 47 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 48 | struct FillAndName { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 49 | SkPathFillType fFill; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 50 | const char* fName; |
| 51 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 52 | constexpr FillAndName gFills[] = { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 53 | {SkPathFillType::kWinding, "Winding"}, |
| 54 | {SkPathFillType::kEvenOdd, "Even / Odd"}, |
| 55 | {SkPathFillType::kInverseWinding, "Inverse Winding"}, |
| 56 | {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"}, |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 57 | }; |
| 58 | struct StyleAndName { |
| 59 | SkPaint::Style fStyle; |
| 60 | const char* fName; |
| 61 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 62 | constexpr StyleAndName gStyles[] = { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 63 | {SkPaint::kFill_Style, "Fill"}, |
| 64 | {SkPaint::kStroke_Style, "Stroke"}, |
| 65 | {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 66 | }; |
| 67 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 68 | SkFont font(ToolUtils::create_portable_typeface(), 15); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 69 | const char title[] = "Empty Paths Drawn Into Rectangle Clips With " |
| 70 | "Indicated Style and Fill"; |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 71 | canvas->drawString(title, 20.0f, 20.0f, font, SkPaint()); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 72 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 73 | SkRandom rand; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 74 | SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 75 | int i = 0; |
| 76 | canvas->save(); |
| 77 | canvas->translate(10 * SK_Scalar1, 0); |
| 78 | canvas->save(); |
bsalomon@google.com | f12449b | 2011-10-10 14:03:33 +0000 | [diff] [blame] | 79 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
| 80 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 81 | if (0 == i % 4) { |
| 82 | canvas->restore(); |
| 83 | canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 84 | canvas->save(); |
| 85 | } else { |
| 86 | canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 87 | } |
| 88 | ++i; |
| 89 | |
| 90 | |
| 91 | SkColor color = rand.nextU(); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 92 | color = 0xff000000 | color; // force solid |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 93 | color = ToolUtils::color_to_565(color); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 94 | this->drawEmpty(canvas, color, rect, |
| 95 | gStyles[style].fStyle, gFills[fill].fFill); |
| 96 | |
| 97 | SkPaint rectPaint; |
| 98 | rectPaint.setColor(SK_ColorBLACK); |
| 99 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 100 | rectPaint.setStrokeWidth(-1); |
| 101 | rectPaint.setAntiAlias(true); |
| 102 | canvas->drawRect(rect, rectPaint); |
| 103 | |
| 104 | SkPaint labelPaint; |
| 105 | labelPaint.setColor(color); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 106 | SkFont labelFont(ToolUtils::create_portable_typeface(), 12); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 107 | canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f, |
| 108 | labelFont, labelPaint); |
| 109 | canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f, |
| 110 | labelFont, labelPaint); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | canvas->restore(); |
| 114 | canvas->restore(); |
| 115 | } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 116 | }; |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 117 | DEF_GM( return new EmptyPathGM; ) |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 121 | static constexpr int kPtsCount = 3; |
| 122 | static constexpr SkPoint kPts[kPtsCount] = { |
| 123 | {40, 40}, |
| 124 | {80, 40}, |
| 125 | {120, 40}, |
| 126 | }; |
| 127 | |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 128 | static SkPath make_path_move() { |
| 129 | SkPathBuilder builder; |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 130 | for (SkPoint p : kPts) { |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 131 | builder.moveTo(p); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 132 | } |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 133 | return builder.detach(); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 136 | static SkPath make_path_move_close() { |
| 137 | SkPathBuilder builder; |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 138 | for (SkPoint p : kPts) { |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 139 | builder.moveTo(p).close(); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 140 | } |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 141 | return builder.detach(); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 144 | static SkPath make_path_move_line() { |
| 145 | SkPathBuilder builder; |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 146 | for (SkPoint p : kPts) { |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 147 | builder.moveTo(p).lineTo(p); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 148 | } |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 149 | return builder.detach(); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 152 | static SkPath make_path_move_mix() { |
| 153 | return SkPathBuilder().moveTo(kPts[0]) |
| 154 | .moveTo(kPts[1]).close() |
| 155 | .moveTo(kPts[2]).lineTo(kPts[2]) |
| 156 | .detach(); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | class EmptyStrokeGM : public GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 160 | SkString onShortName() override { return SkString("emptystroke"); } |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 161 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 162 | SkISize onISize() override { return {200, 240}; } |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 163 | |
| 164 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 165 | static constexpr SkPath (*kProcs[])() = { |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 166 | make_path_move, // expect red red red |
| 167 | make_path_move_close, // expect black black black |
| 168 | make_path_move_line, // expect black black black |
| 169 | make_path_move_mix, // expect red black black, |
| 170 | }; |
| 171 | |
| 172 | SkPaint strokePaint; |
| 173 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 174 | strokePaint.setStrokeWidth(21); |
| 175 | strokePaint.setStrokeCap(SkPaint::kSquare_Cap); |
| 176 | |
| 177 | SkPaint dotPaint; |
| 178 | dotPaint.setColor(SK_ColorRED); |
| 179 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 180 | dotPaint.setStrokeWidth(7); |
| 181 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 182 | for (auto proc : kProcs) { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 183 | canvas->drawPoints(SkCanvas::kPoints_PointMode, kPtsCount, kPts, dotPaint); |
Mike Reed | ad5494d | 2020-08-25 17:36:28 -0400 | [diff] [blame] | 184 | canvas->drawPath(proc(), strokePaint); |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 185 | canvas->translate(0, 40); |
| 186 | } |
| 187 | } |
reed | 58e524c | 2015-09-04 10:03:26 -0700 | [diff] [blame] | 188 | }; |
| 189 | DEF_GM( return new EmptyStrokeGM; ) |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 190 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 191 | } // namespace skiagm |