blob: c7d9cca73b5216bb71fe542bf9845441d0a5fb13 [file] [log] [blame]
mike@reedtribe.org37071642012-12-17 02:10:42 +00001/*
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.org37071642012-12-17 02:10:42 +00008#include "SkCanvas.h"
mike@reedtribe.org37071642012-12-17 02:10:42 +00009#include "SkPath.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050010#include "SkRRect.h"
11#include "ToolUtils.h"
12#include "gm.h"
mike@reedtribe.org37071642012-12-17 02:10:42 +000013
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000014typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
15
16static 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.com7a03d862012-12-18 02:03:03 +000024
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000025 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
36static 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.com7a03d862012-12-18 02:03:03 +000044
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000045 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.comec93bf92012-12-17 13:39:25 +000052static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
53 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000054
reed@google.comec93bf92012-12-17 13:39:25 +000055 r.inset(dx, dy);
56 if (r.isEmpty()) {
57 dst->setEmpty();
58 return;
59 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000060
reed@google.comec93bf92012-12-17 13:39:25 +000061 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.com0f0d9b72013-02-27 15:41:12 +000076static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
reed@google.comec93bf92012-12-17 13:39:25 +000077 return newSize * radius / oldSize;
78}
79
80static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
81 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000082
reed@google.comec93bf92012-12-17 13:39:25 +000083 r.inset(dx, dy);
84 if (r.isEmpty()) {
85 dst->setEmpty();
86 return;
87 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000088
reed@google.comec93bf92012-12-17 13:39:25 +000089 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.com0f0d9b72013-02-27 15:41:12 +000094 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.comec93bf92012-12-17 13:39:25 +000096 }
97 dst->setRectRadii(r, radii);
98}
99
mike@reedtribe.org37071642012-12-17 02:10:42 +0000100static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
101 SkPaint paint;
102 paint.setAntiAlias(true);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000103 paint.setStyle(SkPaint::kStroke_Style);
104
mike@reedtribe.org37071642012-12-17 02:10:42 +0000105 if (rrect.isRect()) {
106 paint.setColor(SK_ColorRED);
107 } else if (rrect.isOval()) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500108 paint.setColor(ToolUtils::color_to_565(0xFF008800));
mike@reedtribe.org37071642012-12-17 02:10:42 +0000109 } else if (rrect.isSimple()) {
110 paint.setColor(SK_ColorBLUE);
111 } else {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000112 paint.setColor(SK_ColorBLACK);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000113 }
114 canvas->drawRRect(rrect, paint);
115}
116
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000117static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
118 SkRRect rr;
mike@reedtribe.org51386382012-12-17 03:48:12 +0000119 for (SkScalar d = -30; d <= 30; d += 5) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000120 proc(rrect, d, d, &rr);
121 draw_rrect_color(canvas, rr);
122 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000123}
124
125class RRectGM : public skiagm::GM {
126public:
127 RRectGM() {}
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000128
mike@reedtribe.org37071642012-12-17 02:10:42 +0000129protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000130
mtklein36352bf2015-03-25 18:17:31 -0700131 SkString onShortName() override {
mike@reedtribe.org37071642012-12-17 02:10:42 +0000132 return SkString("rrect");
133 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000134
mtklein36352bf2015-03-25 18:17:31 -0700135 SkISize onISize() override {
reed@google.comec93bf92012-12-17 13:39:25 +0000136 return SkISize::Make(820, 710);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000137 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000138
mtklein36352bf2015-03-25 18:17:31 -0700139 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700140 constexpr InsetProc insetProcs[] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000141 inset0, inset1, inset2, inset3
142 };
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000143
mike@reedtribe.org37071642012-12-17 02:10:42 +0000144 SkRRect rrect[4];
reed@google.comec93bf92012-12-17 13:39:25 +0000145 SkRect r = { 0, 0, 120, 100 };
mike@reedtribe.org37071642012-12-17 02:10:42 +0000146 SkVector radii[4] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000147 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000148 };
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000149
mike@reedtribe.org37071642012-12-17 02:10:42 +0000150 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.comec93bf92012-12-17 13:39:25 +0000155 canvas->translate(50.5f, 50.5f);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000156 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.comec93bf92012-12-17 13:39:25 +0000160 canvas->translate(200, 0);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000161 }
162 canvas->restore();
reed@google.comec93bf92012-12-17 13:39:25 +0000163 canvas->translate(0, 170);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000164 }
165 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000166
mike@reedtribe.org37071642012-12-17 02:10:42 +0000167private:
168 typedef GM INHERITED;
169};
170
171DEF_GM( return new RRectGM; )