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