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