add dashing2 gm to exercise dashing on curves and polygons
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 18 May 2012 17:59:08 +0000 (17:59 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 18 May 2012 17:59:08 +0000 (17:59 +0000)
git-svn-id: http://skia.googlecode.com/svn/trunk@3997 2bbb7eff-a529-9590-31e7-b0007b416f81

gm/dashing.cpp

index 2be32bf..2fdda4f 100644 (file)
@@ -22,9 +22,7 @@ static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint) {
     canvas->drawLine(0, 0, SkIntToScalar(600), 0, p);
 }
 
-namespace skiagm {
-
-class DashingGM : public GM {
+class DashingGM : public skiagm::GM {
 public:
     DashingGM() {}
 
@@ -33,7 +31,7 @@ protected:
         return SkString("dashing");
     }
 
-    SkISize onISize() { return make_isize(640, 300); }
+    SkISize onISize() { return skiagm::make_isize(640, 300); }
 
     virtual void onDraw(SkCanvas* canvas) {
         static const struct {
@@ -67,14 +65,103 @@ protected:
             }
         }
     }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+static void make_unit_star(SkPath* path, int n) {
+    SkScalar rad = -SK_ScalarPI / 2;
+    const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
+    
+    path->moveTo(0, -SK_Scalar1);
+    for (int i = 1; i < n; i++) {
+        rad += drad;
+        SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
+        path->lineTo(cosV, sinV);
+    }
+    path->close();
+}
+
+static void make_path_line(SkPath* path, const SkRect& bounds) {
+    path->moveTo(bounds.left(), bounds.top());
+    path->lineTo(bounds.right(), bounds.bottom());
+}
+
+static void make_path_rect(SkPath* path, const SkRect& bounds) {
+    path->addRect(bounds);
+}
+
+static void make_path_oval(SkPath* path, const SkRect& bounds) {
+    path->addOval(bounds);
+}
+
+static void make_path_star(SkPath* path, const SkRect& bounds) {
+    make_unit_star(path, 5);
+    SkMatrix matrix;
+    matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
+    path->transform(matrix);
+}
+
+class Dashing2GM : public skiagm::GM {
+public:
+    Dashing2GM() {}
+    
+protected:
+    SkString onShortName() {
+        return SkString("dashing2");
+    }
+    
+    SkISize onISize() { return skiagm::make_isize(640, 480); }
+    
+    virtual void onDraw(SkCanvas* canvas) {
+        static const int gIntervals[] = {
+            3,  // 3 dashes: each count [0] followed by intervals [1..count]
+            2,  10, 10,
+            4,  20, 5, 5, 5,
+            2,  2, 2
+        };
 
-private:
-    typedef GM INHERITED;
+        void (*gProc[])(SkPath*, const SkRect&) = {
+            make_path_line, make_path_rect, make_path_oval, make_path_star,
+        };
+    
+        SkPaint paint;
+        paint.setAntiAlias(true);
+        paint.setStyle(SkPaint::kStroke_Style);
+        paint.setStrokeWidth(SkIntToScalar(6));
+        
+        SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
+        bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
+        SkScalar dx = bounds.width() * 4 / 3;
+        SkScalar dy = bounds.height() * 4 / 3;
+
+        const int* intervals = &gIntervals[1];
+        for (int y = 0; y < gIntervals[0]; ++y) {
+            SkScalar vals[SK_ARRAY_COUNT(gIntervals)];  // more than enough
+            int count = *intervals++;
+            for (int i = 0; i < count; ++i) {
+                vals[i] = SkIntToScalar(*intervals++);
+            }
+            SkScalar phase = vals[0] / 2;
+            paint.setPathEffect(new SkDashPathEffect(vals, count, phase))->unref();
+            
+            for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
+                SkPath path;
+                SkRect r = bounds;
+                r.offset(x * dx, y * dy);
+                gProc[x](&path, r);
+                
+                canvas->drawPath(path, paint);
+            }
+        }
+    }
 };
 
 //////////////////////////////////////////////////////////////////////////////
 
-static GM* gF(void*) { return new DashingGM; }
-static GMRegistry gR(gF);
+static skiagm::GM* F0(void*) { return new DashingGM; }
+static skiagm::GM* F1(void*) { return new Dashing2GM; }
+
+static skiagm::GMRegistry gR0(F0);
+static skiagm::GMRegistry gR1(F1);
 
-}