blob: 4cbf5030c2aaabb8e26795a992eb25ce64641de7 [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 }
23
24 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 }
43
44 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
mike@reedtribe.org37071642012-12-17 02:10:42 +000051static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
52 SkPaint paint;
53 paint.setAntiAlias(true);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000054 paint.setStyle(SkPaint::kStroke_Style);
55
mike@reedtribe.org37071642012-12-17 02:10:42 +000056 if (rrect.isRect()) {
57 paint.setColor(SK_ColorRED);
58 } else if (rrect.isOval()) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000059 paint.setColor(0xFF008800);
mike@reedtribe.org37071642012-12-17 02:10:42 +000060 } else if (rrect.isSimple()) {
61 paint.setColor(SK_ColorBLUE);
62 } else {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000063 paint.setColor(SK_ColorBLACK);
mike@reedtribe.org37071642012-12-17 02:10:42 +000064 }
65 canvas->drawRRect(rrect, paint);
66}
67
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000068static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
69 SkRRect rr;
70 for (SkScalar d = -30; d <= 30; d += 10) {
71 proc(rrect, d, d, &rr);
72 draw_rrect_color(canvas, rr);
73 }
mike@reedtribe.org37071642012-12-17 02:10:42 +000074}
75
76class RRectGM : public skiagm::GM {
77public:
78 RRectGM() {}
79
80protected:
81 virtual SkString onShortName() {
82 return SkString("rrect");
83 }
84
85 virtual SkISize onISize() {
86 return SkISize::Make(640, 480);
87 }
88
89 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000090 static const InsetProc insetProcs[] = { inset0, inset1 };
91
mike@reedtribe.org37071642012-12-17 02:10:42 +000092 SkRRect rrect[4];
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000093 SkRect r = { 0, 0, 120, 160 };
mike@reedtribe.org37071642012-12-17 02:10:42 +000094 SkVector radii[4] = {
95 { 0, 0 }, { 20, 20 }, { 10, 40 }, { 40, 40 }
96 };
97
98 rrect[0].setRect(r);
99 rrect[1].setOval(r);
100 rrect[2].setRectXY(r, 20, 20);
101 rrect[3].setRectRadii(r, radii);
102
103 canvas->translate(50, 50);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000104 for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
105 canvas->save();
106 for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
107 drawrr(canvas, rrect[i], insetProcs[j]);
108 canvas->translate(rrect[i].width() * 5 / 3, 0);
109 }
110 canvas->restore();
111 canvas->translate(0, rrect[0].height() * 5 / 3);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000112 }
113 }
114
115private:
116 typedef GM INHERITED;
117};
118
119DEF_GM( return new RRectGM; )
120