blob: c13f1456c7577a598a09332c65ef959a1695f003 [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
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkRRect.h"
11#include "SkPath.h"
12
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000013typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
14
15static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
16 SkRect r = src.rect();
17
18 r.inset(dx, dy);
19 if (r.isEmpty()) {
20 dst->setEmpty();
21 return;
22 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000023
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000024 SkVector radii[4];
25 for (int i = 0; i < 4; ++i) {
26 radii[i] = src.radii((SkRRect::Corner)i);
27 }
28 for (int i = 0; i < 4; ++i) {
29 radii[i].fX -= dx;
30 radii[i].fY -= dy;
31 }
32 dst->setRectRadii(r, radii);
33}
34
35static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
36 SkRect r = src.rect();
37
38 r.inset(dx, dy);
39 if (r.isEmpty()) {
40 dst->setEmpty();
41 return;
42 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000043
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000044 SkVector radii[4];
45 for (int i = 0; i < 4; ++i) {
46 radii[i] = src.radii((SkRRect::Corner)i);
47 }
48 dst->setRectRadii(r, radii);
49}
50
reed@google.comec93bf92012-12-17 13:39:25 +000051static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
52 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000053
reed@google.comec93bf92012-12-17 13:39:25 +000054 r.inset(dx, dy);
55 if (r.isEmpty()) {
56 dst->setEmpty();
57 return;
58 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000059
reed@google.comec93bf92012-12-17 13:39:25 +000060 SkVector radii[4];
61 for (int i = 0; i < 4; ++i) {
62 radii[i] = src.radii((SkRRect::Corner)i);
63 }
64 for (int i = 0; i < 4; ++i) {
65 if (radii[i].fX) {
66 radii[i].fX -= dx;
67 }
68 if (radii[i].fY) {
69 radii[i].fY -= dy;
70 }
71 }
72 dst->setRectRadii(r, radii);
73}
74
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000075static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
reed@google.comec93bf92012-12-17 13:39:25 +000076 return newSize * radius / oldSize;
77}
78
79static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
80 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000081
reed@google.comec93bf92012-12-17 13:39:25 +000082 r.inset(dx, dy);
83 if (r.isEmpty()) {
84 dst->setEmpty();
85 return;
86 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000087
reed@google.comec93bf92012-12-17 13:39:25 +000088 SkVector radii[4];
89 for (int i = 0; i < 4; ++i) {
90 radii[i] = src.radii((SkRRect::Corner)i);
91 }
92 for (int i = 0; i < 4; ++i) {
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000093 radii[i].fX = prop(radii[i].fX, r.width(), src.rect().width());
94 radii[i].fY = prop(radii[i].fY, r.height(), src.rect().height());
reed@google.comec93bf92012-12-17 13:39:25 +000095 }
96 dst->setRectRadii(r, radii);
97}
98
mike@reedtribe.org37071642012-12-17 02:10:42 +000099static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
100 SkPaint paint;
101 paint.setAntiAlias(true);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000102 paint.setStyle(SkPaint::kStroke_Style);
103
mike@reedtribe.org37071642012-12-17 02:10:42 +0000104 if (rrect.isRect()) {
105 paint.setColor(SK_ColorRED);
106 } else if (rrect.isOval()) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000107 paint.setColor(0xFF008800);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000108 } else if (rrect.isSimple()) {
109 paint.setColor(SK_ColorBLUE);
110 } else {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000111 paint.setColor(SK_ColorBLACK);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000112 }
113 canvas->drawRRect(rrect, paint);
114}
115
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000116static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
117 SkRRect rr;
mike@reedtribe.org51386382012-12-17 03:48:12 +0000118 for (SkScalar d = -30; d <= 30; d += 5) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000119 proc(rrect, d, d, &rr);
120 draw_rrect_color(canvas, rr);
121 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000122}
123
124class RRectGM : public skiagm::GM {
125public:
126 RRectGM() {}
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000127
mike@reedtribe.org37071642012-12-17 02:10:42 +0000128protected:
129 virtual SkString onShortName() {
130 return SkString("rrect");
131 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000132
mike@reedtribe.org37071642012-12-17 02:10:42 +0000133 virtual SkISize onISize() {
reed@google.comec93bf92012-12-17 13:39:25 +0000134 return SkISize::Make(820, 710);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000135 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000136
mike@reedtribe.org37071642012-12-17 02:10:42 +0000137 virtual void onDraw(SkCanvas* canvas) {
reed@google.comec93bf92012-12-17 13:39:25 +0000138 static const InsetProc insetProcs[] = {
139 inset0, inset1, inset2, inset3
140 };
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000141
mike@reedtribe.org37071642012-12-17 02:10:42 +0000142 SkRRect rrect[4];
reed@google.comec93bf92012-12-17 13:39:25 +0000143 SkRect r = { 0, 0, 120, 100 };
mike@reedtribe.org37071642012-12-17 02:10:42 +0000144 SkVector radii[4] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000145 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000146 };
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000147
mike@reedtribe.org37071642012-12-17 02:10:42 +0000148 rrect[0].setRect(r);
149 rrect[1].setOval(r);
150 rrect[2].setRectXY(r, 20, 20);
151 rrect[3].setRectRadii(r, radii);
152
reed@google.comec93bf92012-12-17 13:39:25 +0000153 canvas->translate(50.5f, 50.5f);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000154 for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
155 canvas->save();
156 for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
157 drawrr(canvas, rrect[i], insetProcs[j]);
reed@google.comec93bf92012-12-17 13:39:25 +0000158 canvas->translate(200, 0);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000159 }
160 canvas->restore();
reed@google.comec93bf92012-12-17 13:39:25 +0000161 canvas->translate(0, 170);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000162 }
163 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000164
mike@reedtribe.org37071642012-12-17 02:10:42 +0000165private:
166 typedef GM INHERITED;
167};
168
169DEF_GM( return new RRectGM; )