2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
13 typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
15 static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
16 SkRect r = src.rect();
25 for (int i = 0; i < 4; ++i) {
26 radii[i] = src.radii((SkRRect::Corner)i);
28 for (int i = 0; i < 4; ++i) {
32 dst->setRectRadii(r, radii);
35 static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
36 SkRect r = src.rect();
45 for (int i = 0; i < 4; ++i) {
46 radii[i] = src.radii((SkRRect::Corner)i);
48 dst->setRectRadii(r, radii);
51 static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
52 SkRect r = src.rect();
61 for (int i = 0; i < 4; ++i) {
62 radii[i] = src.radii((SkRRect::Corner)i);
64 for (int i = 0; i < 4; ++i) {
72 dst->setRectRadii(r, radii);
75 static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
76 return newSize * radius / oldSize;
79 static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
80 SkRect r = src.rect();
89 for (int i = 0; i < 4; ++i) {
90 radii[i] = src.radii((SkRRect::Corner)i);
92 for (int i = 0; i < 4; ++i) {
93 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());
96 dst->setRectRadii(r, radii);
99 static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
101 paint.setAntiAlias(true);
102 paint.setStyle(SkPaint::kStroke_Style);
104 if (rrect.isRect()) {
105 paint.setColor(SK_ColorRED);
106 } else if (rrect.isOval()) {
107 paint.setColor(0xFF008800);
108 } else if (rrect.isSimple()) {
109 paint.setColor(SK_ColorBLUE);
111 paint.setColor(SK_ColorBLACK);
113 canvas->drawRRect(rrect, paint);
116 static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
118 for (SkScalar d = -30; d <= 30; d += 5) {
119 proc(rrect, d, d, &rr);
120 draw_rrect_color(canvas, rr);
124 class RRectGM : public skiagm::GM {
130 SkString onShortName() override {
131 return SkString("rrect");
134 SkISize onISize() override {
135 return SkISize::Make(820, 710);
138 void onDraw(SkCanvas* canvas) override {
139 static const InsetProc insetProcs[] = {
140 inset0, inset1, inset2, inset3
144 SkRect r = { 0, 0, 120, 100 };
145 SkVector radii[4] = {
146 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
151 rrect[2].setRectXY(r, 20, 20);
152 rrect[3].setRectRadii(r, radii);
154 canvas->translate(50.5f, 50.5f);
155 for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
157 for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
158 drawrr(canvas, rrect[i], insetProcs[j]);
159 canvas->translate(200, 0);
162 canvas->translate(0, 170);
167 typedef GM INHERITED;
170 DEF_GM( return new RRectGM; )