Bug 635035, part 5: Add a helper to check whether a gfxRect is rounded within a given epsilon. sr=roc
authorChris Jones <jones.chris.g@gmail.com>
Wed, 09 Mar 2011 11:27:37 -0600
changeset 63370 4ab41ca945e0f4e03be72cd6da4b3b3b03f5fe56
parent 63369 d4a93181df15b4a678c0eeb6999e3f71c8fa5af7
child 63371 edf7507120f7f33ba37c21c3bd9e4763ea914228
push idunknown
push userunknown
push dateunknown
reviewersroc
bugs635035
milestone2.0b13pre
Bug 635035, part 5: Add a helper to check whether a gfxRect is rounded within a given epsilon. sr=roc
gfx/thebes/gfxRect.cpp
gfx/thebes/gfxRect.h
--- a/gfx/thebes/gfxRect.cpp
+++ b/gfx/thebes/gfxRect.cpp
@@ -82,16 +82,32 @@ gfxRect::Contains(const gfxRect& aRect) 
 
 PRBool
 gfxRect::Contains(const gfxPoint& aPoint) const
 {
   return aPoint.x >= X() && aPoint.x <= XMost() &&
          aPoint.y >= Y() && aPoint.y <= YMost();
 }
 
+static PRBool
+WithinEpsilonOfInteger(gfxFloat aX, gfxFloat aEpsilon)
+{
+    return fabs(NS_round(aX) - aX) <= fabs(aEpsilon);
+}
+
+PRBool
+gfxRect::WithinEpsilonOfIntegerPixels(gfxFloat aEpsilon) const
+{
+    NS_ASSERTION(-0.5 < aEpsilon && aEpsilon < 0.5, "Nonsense epsilon value");
+    return (WithinEpsilonOfInteger(pos.x, aEpsilon) &&
+            WithinEpsilonOfInteger(pos.y, aEpsilon) &&
+            WithinEpsilonOfInteger(size.width, aEpsilon) &&
+            WithinEpsilonOfInteger(size.height, aEpsilon));
+}
+
 void
 gfxRect::Round()
 {
     // Note that don't use NS_round here. See the comment for this method in gfxRect.h
     gfxFloat x0 = NS_floor(X() + 0.5);
     gfxFloat y0 = NS_floor(Y() + 0.5);
     gfxFloat x1 = NS_floor(XMost() + 0.5);
     gfxFloat y1 = NS_floor(YMost() + 0.5);
--- a/gfx/thebes/gfxRect.h
+++ b/gfx/thebes/gfxRect.h
@@ -109,17 +109,24 @@ struct THEBES_API gfxRect {
     gfxFloat XMost() const { return pos.x + size.width; }
     gfxFloat YMost() const { return pos.y + size.height; }
 
     PRBool IsEmpty() const { return size.width <= 0 || size.height <= 0; }
     gfxRect Intersect(const gfxRect& aRect) const;
     gfxRect Union(const gfxRect& aRect) const;
     PRBool Contains(const gfxRect& aRect) const;
     PRBool Contains(const gfxPoint& aPoint) const;
-    // XXX figure out what methods (intersect, union, etc) we use and add them.
+
+    /**
+     * Return true if all components of this rect are within
+     * aEpsilon of integer coordinates, defined as
+     *   |round(coord) - coord| <= |aEpsilon|
+     * for x,y,width,height.
+     */
+    PRBool WithinEpsilonOfIntegerPixels(gfxFloat aEpsilon) const;
 
     gfxPoint TopLeft() { return pos; }
     gfxPoint BottomRight() { return gfxPoint(XMost(), YMost()); }
 
     void Inset(gfxFloat k) {
         pos.x += k;
         pos.y += k;
         size.width = PR_MAX(0.0, size.width - k * 2.0);