mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 9 | #include "SkPath.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame^] | 10 | #include "SkRRect.h" |
| 11 | #include "ToolUtils.h" |
| 12 | #include "gm.h" |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 13 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 14 | typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*); |
| 15 | |
| 16 | static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 17 | SkRect r = src.rect(); |
| 18 | |
| 19 | r.inset(dx, dy); |
| 20 | if (r.isEmpty()) { |
| 21 | dst->setEmpty(); |
| 22 | return; |
| 23 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 24 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 25 | SkVector radii[4]; |
| 26 | for (int i = 0; i < 4; ++i) { |
| 27 | radii[i] = src.radii((SkRRect::Corner)i); |
| 28 | } |
| 29 | for (int i = 0; i < 4; ++i) { |
| 30 | radii[i].fX -= dx; |
| 31 | radii[i].fY -= dy; |
| 32 | } |
| 33 | dst->setRectRadii(r, radii); |
| 34 | } |
| 35 | |
| 36 | static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 37 | SkRect r = src.rect(); |
| 38 | |
| 39 | r.inset(dx, dy); |
| 40 | if (r.isEmpty()) { |
| 41 | dst->setEmpty(); |
| 42 | return; |
| 43 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 44 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 45 | SkVector radii[4]; |
| 46 | for (int i = 0; i < 4; ++i) { |
| 47 | radii[i] = src.radii((SkRRect::Corner)i); |
| 48 | } |
| 49 | dst->setRectRadii(r, radii); |
| 50 | } |
| 51 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 52 | static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 53 | SkRect r = src.rect(); |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 54 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 55 | r.inset(dx, dy); |
| 56 | if (r.isEmpty()) { |
| 57 | dst->setEmpty(); |
| 58 | return; |
| 59 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 60 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 61 | SkVector radii[4]; |
| 62 | for (int i = 0; i < 4; ++i) { |
| 63 | radii[i] = src.radii((SkRRect::Corner)i); |
| 64 | } |
| 65 | for (int i = 0; i < 4; ++i) { |
| 66 | if (radii[i].fX) { |
| 67 | radii[i].fX -= dx; |
| 68 | } |
| 69 | if (radii[i].fY) { |
| 70 | radii[i].fY -= dy; |
| 71 | } |
| 72 | } |
| 73 | dst->setRectRadii(r, radii); |
| 74 | } |
| 75 | |
sugoi@google.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 76 | static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 77 | return newSize * radius / oldSize; |
| 78 | } |
| 79 | |
| 80 | static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 81 | SkRect r = src.rect(); |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 82 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 83 | r.inset(dx, dy); |
| 84 | if (r.isEmpty()) { |
| 85 | dst->setEmpty(); |
| 86 | return; |
| 87 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 88 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 89 | SkVector radii[4]; |
| 90 | for (int i = 0; i < 4; ++i) { |
| 91 | radii[i] = src.radii((SkRRect::Corner)i); |
| 92 | } |
| 93 | for (int i = 0; i < 4; ++i) { |
sugoi@google.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 94 | radii[i].fX = prop(radii[i].fX, r.width(), src.rect().width()); |
| 95 | radii[i].fY = prop(radii[i].fY, r.height(), src.rect().height()); |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 96 | } |
| 97 | dst->setRectRadii(r, radii); |
| 98 | } |
| 99 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 100 | static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) { |
| 101 | SkPaint paint; |
| 102 | paint.setAntiAlias(true); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 103 | paint.setStyle(SkPaint::kStroke_Style); |
| 104 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 105 | if (rrect.isRect()) { |
| 106 | paint.setColor(SK_ColorRED); |
| 107 | } else if (rrect.isOval()) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame^] | 108 | paint.setColor(ToolUtils::color_to_565(0xFF008800)); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 109 | } else if (rrect.isSimple()) { |
| 110 | paint.setColor(SK_ColorBLUE); |
| 111 | } else { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 112 | paint.setColor(SK_ColorBLACK); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 113 | } |
| 114 | canvas->drawRRect(rrect, paint); |
| 115 | } |
| 116 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 117 | static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) { |
| 118 | SkRRect rr; |
mike@reedtribe.org | 5138638 | 2012-12-17 03:48:12 +0000 | [diff] [blame] | 119 | for (SkScalar d = -30; d <= 30; d += 5) { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 120 | proc(rrect, d, d, &rr); |
| 121 | draw_rrect_color(canvas, rr); |
| 122 | } |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | class RRectGM : public skiagm::GM { |
| 126 | public: |
| 127 | RRectGM() {} |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 128 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 129 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 130 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 131 | SkString onShortName() override { |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 132 | return SkString("rrect"); |
| 133 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 134 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 135 | SkISize onISize() override { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 136 | return SkISize::Make(820, 710); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 137 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 140 | constexpr InsetProc insetProcs[] = { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 141 | inset0, inset1, inset2, inset3 |
| 142 | }; |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 143 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 144 | SkRRect rrect[4]; |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 145 | SkRect r = { 0, 0, 120, 100 }; |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 146 | SkVector radii[4] = { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 147 | { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 } |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 148 | }; |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 149 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 150 | rrect[0].setRect(r); |
| 151 | rrect[1].setOval(r); |
| 152 | rrect[2].setRectXY(r, 20, 20); |
| 153 | rrect[3].setRectRadii(r, radii); |
| 154 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 155 | canvas->translate(50.5f, 50.5f); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 156 | for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) { |
| 157 | canvas->save(); |
| 158 | for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) { |
| 159 | drawrr(canvas, rrect[i], insetProcs[j]); |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 160 | canvas->translate(200, 0); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 161 | } |
| 162 | canvas->restore(); |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 163 | canvas->translate(0, 170); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 166 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 167 | private: |
| 168 | typedef GM INHERITED; |
| 169 | }; |
| 170 | |
| 171 | DEF_GM( return new RRectGM; ) |