show contains and intersects predicates in sample



git-svn-id: http://skia.googlecode.com/svn/trunk@3824 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SampleRegion.cpp b/samplecode/SampleRegion.cpp
index 48153ef..a46b488 100644
--- a/samplecode/SampleRegion.cpp
+++ b/samplecode/SampleRegion.cpp
@@ -15,6 +15,105 @@
 #include "SkUtils.h"
 #include "SkImageDecoder.h"
 
+static void test_strokerect(SkCanvas* canvas) {
+    int width = 100;
+    int height = 100;
+
+    SkBitmap bitmap;
+    bitmap.setConfig(SkBitmap::kA8_Config, width*2, height*2);
+    bitmap.allocPixels();
+    bitmap.eraseColor(0);
+
+    SkScalar dx = 20;
+    SkScalar dy = 20;
+    
+    SkPath path;
+    path.addRect(0.0f, 0.0f, width, height, SkPath::kCW_Direction);
+    SkRect r = SkRect::MakeWH(width, height);
+    
+    SkCanvas c(bitmap);
+    c.translate(dx, dy);
+
+    SkPaint paint;
+    paint.setStyle(SkPaint::kStroke_Style);
+    paint.setStrokeWidth(1);
+
+    // use the rect
+    c.clear(0);
+    c.drawRect(r, paint);
+    canvas->drawBitmap(bitmap, 0, 0, NULL);
+
+    // use the path
+    c.clear(0);
+    c.drawPath(path, paint);
+    canvas->drawBitmap(bitmap, 2*width, 0, NULL);
+}
+
+static void drawFadingText(SkCanvas* canvas,
+                           const char* text, size_t len, SkScalar x, SkScalar y,
+                           const SkPaint& paint) {
+    // Need a bounds for the text
+    SkRect bounds;
+    SkPaint::FontMetrics fm;
+    
+    paint.getFontMetrics(&fm);
+    bounds.set(x, y + fm.fTop, x + paint.measureText(text, len), y + fm.fBottom);
+
+    // may need to outset bounds a little, to account for hinting and/or
+    // antialiasing
+    bounds.inset(-SkIntToScalar(2), -SkIntToScalar(2));
+
+    canvas->saveLayer(&bounds, NULL);
+    canvas->drawText(text, len, x, y, paint);
+
+    const SkPoint pts[] = {
+        { bounds.fLeft, y },
+        { bounds.fRight, y }
+    };
+    const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 };
+
+    // pos[1] value is where we start to fade, relative to the width
+    // of our pts[] array.
+    const SkScalar pos[] = { 0, SkFloatToScalar(0.9), SK_Scalar1 };
+
+    SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 3,
+                                                 SkShader::kClamp_TileMode);
+    SkPaint p;
+    p.setShader(s)->unref();
+    p.setXfermodeMode(SkXfermode::kDstIn_Mode);
+    canvas->drawRect(bounds, p);
+
+    canvas->restore();
+}
+
+static void test_text(SkCanvas* canvas) {
+    SkPaint paint;
+    paint.setAntiAlias(true);
+    paint.setTextSize(20);
+    
+    const char* str = "Hamburgefons";
+    size_t len = strlen(str);
+    SkScalar x = 20;
+    SkScalar y = 20;
+
+    canvas->drawText(str, len, x, y, paint);
+
+    y += 20;
+
+    const SkPoint pts[] = { { x, y }, { x + paint.measureText(str, len), y } };
+    const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 };
+    const SkScalar pos[] = { 0, 0.9f, 1 };
+    SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos,
+                                                 SK_ARRAY_COUNT(colors),
+                                                 SkShader::kClamp_TileMode);
+    paint.setShader(s)->unref();
+    canvas->drawText(str, len, x, y, paint);
+
+    y += 20;
+    paint.setShader(NULL);
+    drawFadingText(canvas, str, len, x, y, paint);
+}
+
 #ifdef SK_BUILD_FOR_WIN
 // windows doesn't have roundf
 inline float roundf(float x) { return (x-floor(x))>0.5 ? ceil(x) : floor(x); }
@@ -32,26 +131,27 @@
 
 static void test_union_bug_1505668(SkRegion* ra, SkRegion* rb, SkRegion* rc) {
     static int32_t dataA[] = {
-        0x00000001, 0x000001dd,
-        0x00000001, 0x0000000c, 0x0000000d, 0x00000025,
-        0x7fffffff, 0x000001de, 0x00000001, 0x00000025,
-        0x7fffffff, 0x000004b3, 0x00000001, 0x00000026,
-        0x7fffffff, 0x000004b4, 0x0000000c, 0x00000026,
-        0x7fffffff, 0x00000579, 0x00000000, 0x0000013a,
-        0x7fffffff, 0x000005d8, 0x00000000, 0x0000013b,
-        0x7fffffff, 0x7fffffff
+        0x00000001,
+        0x000001dd, 2, 0x00000001, 0x0000000c, 0x0000000d, 0x00000025, 0x7fffffff,
+        0x000001de, 1, 0x00000001, 0x00000025, 0x7fffffff,
+        0x000004b3, 1, 0x00000001, 0x00000026, 0x7fffffff,
+        0x000004b4, 1, 0x0000000c, 0x00000026, 0x7fffffff,
+        0x00000579, 1, 0x00000000, 0x0000013a, 0x7fffffff,
+        0x000005d8, 1, 0x00000000, 0x0000013b, 0x7fffffff,
+        0x7fffffff
     };
     make_rgn(ra, 0, 1, 315, 1496, SK_ARRAY_COUNT(dataA), dataA);
 
     static int32_t dataB[] = {
-        0x000000b6, 0x000000c4,
-        0x000000a1, 0x000000f0, 0x7fffffff, 0x000000d6,
-        0x7fffffff, 0x000000e4, 0x00000070, 0x00000079,
-        0x000000a1, 0x000000b0, 0x7fffffff, 0x000000e6,
-        0x7fffffff, 0x000000f4, 0x00000070, 0x00000079,
-        0x000000a1, 0x000000b0, 0x7fffffff, 0x000000f6,
-        0x7fffffff, 0x00000104, 0x000000a1, 0x000000b0,
-        0x7fffffff, 0x7fffffff
+        0x000000b6,
+        0x000000c4, 1, 0x000000a1, 0x000000f0, 0x7fffffff,
+        0x000000d6, 0, 0x7fffffff,
+        0x000000e4, 2, 0x00000070, 0x00000079, 0x000000a1, 0x000000b0, 0x7fffffff,
+        0x000000e6, 0, 0x7fffffff,
+        0x000000f4, 2, 0x00000070, 0x00000079, 0x000000a1, 0x000000b0, 0x7fffffff,
+        0x000000f6, 0, 0x7fffffff,
+        0x00000104, 1, 0x000000a1, 0x000000b0, 0x7fffffff,
+        0x7fffffff
     };
     make_rgn(rb, 112, 182, 240, 260, SK_ARRAY_COUNT(dataB), dataB);
     
@@ -103,11 +203,15 @@
         this->setBGColor(0xFFDDDDDD);
     }
 
-    void build_rgn(SkRegion* rgn, SkRegion::Op op) {
+    void build_base_rgn(SkRegion* rgn) {
         rgn->setRect(fBase);
         SkIRect r = fBase;
         r.offset(75, 20);
         rgn->op(r, SkRegion::kUnion_Op);
+    }
+
+    void build_rgn(SkRegion* rgn, SkRegion::Op op) {
+        build_base_rgn(rgn);
         rgn->op(fRect, op);
     }
 
@@ -122,6 +226,23 @@
         return this->INHERITED::onQuery(evt);
     }
     
+    static void drawstr(SkCanvas* canvas, const char text[], const SkPoint& loc,
+                        bool hilite) {
+        SkPaint paint;
+        paint.setAntiAlias(true);
+        paint.setTextSize(SkIntToScalar(20));
+        paint.setColor(hilite ? SK_ColorRED : 0x40FF0000);
+        canvas->drawText(text, strlen(text), loc.fX, loc.fY, paint);
+    }
+
+    void drawPredicates(SkCanvas* canvas, const SkPoint pts[]) {
+        SkRegion rgn;
+        build_base_rgn(&rgn);
+        
+        drawstr(canvas, "Intersects", pts[0], rgn.intersects(fRect));
+        drawstr(canvas, "Contains", pts[1], rgn.contains(fRect));
+    }
+
     void drawOrig(SkCanvas* canvas, bool bg) {
         SkRect      r;
         SkPaint     paint;
@@ -130,8 +251,10 @@
         if (bg)
             paint.setColor(0xFFBBBBBB);
         
-        r.set(fBase);
-        canvas->drawRect(r, paint);
+        SkRegion rgn;
+        build_base_rgn(&rgn);
+        paint_rgn(canvas, rgn, paint);
+
         r.set(fRect);
         canvas->drawRect(r, paint);
     }
@@ -198,6 +321,8 @@
     }
     
     virtual void onDrawContent(SkCanvas* canvas) {
+//        test_strokerect(canvas); return;
+//        test_text(canvas); return;
 #ifdef SK_DEBUG
         if (true) {
             SkRegion a, b, c;
@@ -218,6 +343,11 @@
             }
         }
 #endif
+        const SkPoint origins[] = {
+            { 30*SK_Scalar1, 50*SK_Scalar1 },
+            { 150*SK_Scalar1, 50*SK_Scalar1 },
+        };
+        this->drawPredicates(canvas, origins);
 
         static const struct {
             SkColor         fColor;