add gm for rrect, and rrect.inset/outset
authormike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 17 Dec 2012 02:10:42 +0000 (02:10 +0000)
committermike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 17 Dec 2012 02:10:42 +0000 (02:10 +0000)
git-svn-id: http://skia.googlecode.com/svn/trunk@6832 2bbb7eff-a529-9590-31e7-b0007b416f81

gm/rrect.cpp [new file with mode: 0644]
gyp/gmslides.gypi
include/core/SkRRect.h
src/core/SkRRect.cpp

diff --git a/gm/rrect.cpp b/gm/rrect.cpp
new file mode 100644 (file)
index 0000000..62ca533
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * 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"
+
+static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
+    SkPaint paint;
+    paint.setAntiAlias(true);
+    
+    if (rrect.isRect()) {
+        paint.setColor(SK_ColorRED);
+    } else if (rrect.isOval()) {
+        paint.setColor(SK_ColorGREEN);
+    } else if (rrect.isSimple()) {
+        paint.setColor(SK_ColorBLUE);
+    } else {
+        paint.setColor(SK_ColorGRAY);
+    }
+    canvas->drawRRect(rrect, paint);
+}
+
+static void drawrr(SkCanvas* canvas, const SkRRect& rrect) {
+    SkRRect inner, outer, inner2;
+    
+    SkScalar dx = 30;
+    SkScalar dy = 30;
+
+    rrect.outset(dx, dy, &outer);
+    rrect.inset(dx/2, dy/2, &inner);
+    rrect.inset(dx, dy, &inner2);
+
+    draw_rrect_color(canvas, outer);
+    draw_rrect_color(canvas, rrect);
+    draw_rrect_color(canvas, inner);
+    draw_rrect_color(canvas, inner2);
+
+    SkPaint paint;
+    paint.setAntiAlias(true);
+    paint.setStyle(SkPaint::kStroke_Style);
+    paint.setColor(SK_ColorDKGRAY);
+    canvas->drawRRect(rrect, paint);
+    canvas->drawRRect(inner, paint);
+    canvas->drawRRect(inner2, paint);
+}
+
+class RRectGM : public skiagm::GM {
+public:
+    RRectGM() {}
+    
+protected:
+    virtual SkString onShortName() {
+        return SkString("rrect");
+    }
+    
+    virtual SkISize onISize() {
+        return SkISize::Make(640, 480);
+    }
+    
+    virtual void onDraw(SkCanvas* canvas) {
+        SkRRect rrect[4];
+        SkRect r = { 0, 0, 120, 240 };
+        SkVector radii[4] = {
+            { 0, 0 }, { 20, 20 }, { 10, 40 }, { 40, 40 }
+        };
+        
+        rrect[0].setRect(r);
+        rrect[1].setOval(r);
+        rrect[2].setRectXY(r, 20, 20);
+        rrect[3].setRectRadii(r, radii);
+
+        canvas->translate(50, 50);
+        for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
+            drawrr(canvas, rrect[i]);
+            canvas->translate(rrect[i].width() * 2, 0);
+        }
+    }
+    
+private:
+    typedef GM INHERITED;
+};
+
+DEF_GM( return new RRectGM; )
+
index 047f02efe4e09d637c298783d4d3028637db5fbf..8c026aa54294d0230a732aedf36ee86fbfc4d740 100644 (file)
@@ -62,6 +62,7 @@
     '../gm/points.cpp',
     '../gm/poly2poly.cpp',
     '../gm/quadpaths.cpp',
+    '../gm/rrect.cpp',
     '../gm/samplerstress.cpp',
     '../gm/shaderbounds.cpp',
     '../gm/shadertext.cpp',
index eb2a770f623bf288187ee3202320e5f2a0646518..ac2b7a2810df250fb437d27196300478cab23f9f 100644 (file)
@@ -103,6 +103,9 @@ public:
     inline bool isSimple() const { return kSimple_Type == this->getType(); }
     inline bool isComplex() const { return kComplex_Type == this->getType(); }
 
+    SkScalar width() const { return fRect.width(); }
+    SkScalar height() const { return fRect.height(); }
+
     /**
      * Set this RR to the empty rectangle (0,0,0,0) with 0 x & y radii.
      */
@@ -217,6 +220,17 @@ public:
      */
     bool contains(SkScalar x, SkScalar y) const;
 
+    void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
+    void inset(SkScalar dx, SkScalar dy) {
+        this->inset(dx, dy, this);
+    }
+    void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
+        this->inset(-dx, -dy, dst);
+    }
+    void outset(SkScalar dx, SkScalar dy) {
+        this->inset(-dx, -dy, this);
+    }
+
     SkDEBUGCODE(void validate() const;)
 
     enum {
index c409cd6fd70823b863b1653c785dad32ca66ad1a..1623984fc665cf2a3a16473248d5f581dd7e0029 100644 (file)
@@ -229,6 +229,24 @@ void SkRRect::computeType() const {
 
 ///////////////////////////////////////////////////////////////////////////////
 
+void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
+
+    SkRect r = fRect;
+    r.inset(dx, dy);
+    if (r.isEmpty()) {
+        dst->setEmpty();
+        return;
+    }
+
+    SkVector radii[4];
+    for (int i = 0; i < 4; ++i) {
+        radii[i].set(fRadii[i].fX - dx, fRadii[i].fY - dy);
+    }
+    dst->setRectRadii(r, radii);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 uint32_t SkRRect::writeToMemory(void* buffer) const {
     SkASSERT(kSizeInMemory == sizeof(SkRect) + sizeof(fRadii));