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