add alternative to isRect named asRect
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Sat, 25 Jan 2014 16:54:31 +0000 (16:54 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Sat, 25 Jan 2014 16:54:31 +0000 (16:54 +0000)
This defines yunchao's proposed interface in terms of
an existing implementation.

BUG=skia:
R=reed@google.com, yunchao.he@intel.com

Author: caryclark@google.com

Review URL: https://codereview.chromium.org/140953003

git-svn-id: http://skia.googlecode.com/svn/trunk@13183 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkPath.h
src/core/SkPath.cpp
tests/PathTest.cpp

index b2bb4b5..dd7a672 100644 (file)
@@ -555,6 +555,25 @@ public:
         return computedDir == dir;
     }
 
+    enum PathAsRect {
+        /** The path can not draw the same as its bounds. */
+        kNone_PathAsRect,
+        /** The path draws the same as its bounds when stroked or filled. */
+        kStroke_PathAsRect,
+        /** The path draws the same as its bounds when filled. */
+        kFill_PathAsRect,
+    };
+
+    /** Returns kFill_PathAsRect or kStroke_PathAsRect if drawing the path (either filled or
+        stroked) will be equivalent to filling/stroking the path's bounding rect. If
+        either is true, and direction is not null, sets the direction of the contour. If the
+        path is not drawn equivalent to a rect, returns kNone_PathAsRect and ignores direction.
+
+        @param direction If not null, set to the contour's direction when it is drawn as a rect
+        @return the path's PathAsRect type
+     */
+    PathAsRect asRect(Direction* direction = NULL) const;
+
     /** Returns true if the path specifies a rectangle. If so, and if isClosed is
         not null, set isClosed to true if the path is closed. Also, if returning true
         and direction is not null, return the rect direction. If the path does not
index 8602f4b..04c807b 100644 (file)
@@ -504,6 +504,14 @@ bool SkPath::isRectContour(bool allowPartial, int* currVerb, const SkPoint** pts
     return result;
 }
 
+SkPath::PathAsRect SkPath::asRect(Direction* direction) const {
+    SK_COMPILE_ASSERT(0 == kNone_PathAsRect, path_as_rect_mismatch);
+    SK_COMPILE_ASSERT(1 == kStroke_PathAsRect, path_as_rect_mismatch);
+    SK_COMPILE_ASSERT(2 == kFill_PathAsRect, path_as_rect_mismatch);
+    bool isClosed = false;
+    return (PathAsRect) (isRect(&isClosed, direction) + isClosed);
+}
+
 bool SkPath::isRect(SkRect* rect) const {
     SkDEBUGCODE(this->validate();)
     int currVerb = 0;
index 1af5bd3..e872b0c 100644 (file)
@@ -1443,6 +1443,7 @@ static void test_isRect_open_close(skiatest::Reporter* reporter) {
     REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
     REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
     REPORTER_ASSERT(reporter, isClosed);
+    REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect(NULL));
 }
 
 // Simple isRect test is inline TestPath, below.
@@ -1560,6 +1561,15 @@ static void test_isRect(skiatest::Reporter* reporter) {
             REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
             REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
             REPORTER_ASSERT(reporter, direction == cheapDirection);
+            direction = (SkPath::Direction) -1;
+            if (tests[testIndex].fClose) {
+                REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect());
+                REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect(&direction));
+            } else {
+                REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect());
+                REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(&direction));
+            }
+            REPORTER_ASSERT(reporter, direction == cheapDirection);
         } else {
             SkRect computed;
             computed.set(123, 456, 789, 1011);
@@ -1572,6 +1582,9 @@ static void test_isRect(skiatest::Reporter* reporter) {
             REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
             REPORTER_ASSERT(reporter, isClosed == (bool) -1);
             REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
+            REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect());
+            REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect(&direction));
+            REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
         }
     }