2012-12-17 02:10:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkRRect.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
|
2012-12-17 02:34:28 +00:00
|
|
|
typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
|
|
|
|
|
|
|
|
static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
|
|
|
|
SkRect r = src.rect();
|
|
|
|
|
|
|
|
r.inset(dx, dy);
|
|
|
|
if (r.isEmpty()) {
|
|
|
|
dst->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 02:34:28 +00:00
|
|
|
SkVector radii[4];
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
radii[i] = src.radii((SkRRect::Corner)i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
radii[i].fX -= dx;
|
|
|
|
radii[i].fY -= dy;
|
|
|
|
}
|
|
|
|
dst->setRectRadii(r, radii);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
|
|
|
|
SkRect r = src.rect();
|
|
|
|
|
|
|
|
r.inset(dx, dy);
|
|
|
|
if (r.isEmpty()) {
|
|
|
|
dst->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 02:34:28 +00:00
|
|
|
SkVector radii[4];
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
radii[i] = src.radii((SkRRect::Corner)i);
|
|
|
|
}
|
|
|
|
dst->setRectRadii(r, radii);
|
|
|
|
}
|
|
|
|
|
2012-12-17 13:39:25 +00:00
|
|
|
static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
|
|
|
|
SkRect r = src.rect();
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 13:39:25 +00:00
|
|
|
r.inset(dx, dy);
|
|
|
|
if (r.isEmpty()) {
|
|
|
|
dst->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 13:39:25 +00:00
|
|
|
SkVector radii[4];
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
radii[i] = src.radii((SkRRect::Corner)i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (radii[i].fX) {
|
|
|
|
radii[i].fX -= dx;
|
|
|
|
}
|
|
|
|
if (radii[i].fY) {
|
|
|
|
radii[i].fY -= dy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst->setRectRadii(r, radii);
|
|
|
|
}
|
|
|
|
|
2013-02-27 15:41:12 +00:00
|
|
|
static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
|
2012-12-17 13:39:25 +00:00
|
|
|
return newSize * radius / oldSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
|
|
|
|
SkRect r = src.rect();
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 13:39:25 +00:00
|
|
|
r.inset(dx, dy);
|
|
|
|
if (r.isEmpty()) {
|
|
|
|
dst->setEmpty();
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 13:39:25 +00:00
|
|
|
SkVector radii[4];
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
radii[i] = src.radii((SkRRect::Corner)i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2013-02-27 15:41:12 +00:00
|
|
|
radii[i].fX = prop(radii[i].fX, r.width(), src.rect().width());
|
|
|
|
radii[i].fY = prop(radii[i].fY, r.height(), src.rect().height());
|
2012-12-17 13:39:25 +00:00
|
|
|
}
|
|
|
|
dst->setRectRadii(r, radii);
|
|
|
|
}
|
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-12-17 02:34:28 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
if (rrect.isRect()) {
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
} else if (rrect.isOval()) {
|
2015-07-29 12:27:47 +00:00
|
|
|
paint.setColor(sk_tool_utils::color_to_565(0xFF008800));
|
2012-12-17 02:10:42 +00:00
|
|
|
} else if (rrect.isSimple()) {
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
} else {
|
2012-12-17 02:34:28 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
2012-12-17 02:10:42 +00:00
|
|
|
}
|
|
|
|
canvas->drawRRect(rrect, paint);
|
|
|
|
}
|
|
|
|
|
2012-12-17 02:34:28 +00:00
|
|
|
static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
|
|
|
|
SkRRect rr;
|
2012-12-17 03:48:12 +00:00
|
|
|
for (SkScalar d = -30; d <= 30; d += 5) {
|
2012-12-17 02:34:28 +00:00
|
|
|
proc(rrect, d, d, &rr);
|
|
|
|
draw_rrect_color(canvas, rr);
|
|
|
|
}
|
2012-12-17 02:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class RRectGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
RRectGM() {}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-12-17 02:10:42 +00:00
|
|
|
return SkString("rrect");
|
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2012-12-17 13:39:25 +00:00
|
|
|
return SkISize::Make(820, 710);
|
2012-12-17 02:10:42 +00:00
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr InsetProc insetProcs[] = {
|
2012-12-17 13:39:25 +00:00
|
|
|
inset0, inset1, inset2, inset3
|
|
|
|
};
|
2012-12-17 02:34:28 +00:00
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
SkRRect rrect[4];
|
2012-12-17 13:39:25 +00:00
|
|
|
SkRect r = { 0, 0, 120, 100 };
|
2012-12-17 02:10:42 +00:00
|
|
|
SkVector radii[4] = {
|
2012-12-17 13:39:25 +00:00
|
|
|
{ 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
|
2012-12-17 02:10:42 +00:00
|
|
|
};
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
rrect[0].setRect(r);
|
|
|
|
rrect[1].setOval(r);
|
|
|
|
rrect[2].setRectXY(r, 20, 20);
|
|
|
|
rrect[3].setRectRadii(r, radii);
|
|
|
|
|
2012-12-17 13:39:25 +00:00
|
|
|
canvas->translate(50.5f, 50.5f);
|
2012-12-17 02:34:28 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
|
|
|
|
canvas->save();
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
|
|
|
|
drawrr(canvas, rrect[i], insetProcs[j]);
|
2012-12-17 13:39:25 +00:00
|
|
|
canvas->translate(200, 0);
|
2012-12-17 02:34:28 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2012-12-17 13:39:25 +00:00
|
|
|
canvas->translate(0, 170);
|
2012-12-17 02:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 02:03:03 +00:00
|
|
|
|
2012-12-17 02:10:42 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new RRectGM; )
|