Bug 685326 - Move Round{,In,Out} from gfxRect to BaseRect, and make them no-ops on IntRect. r=Bas
authorJoe Drew <joe@drew.ca>
Mon, 14 Nov 2011 17:29:14 +1300
changeset 80214 e6705f49eaaa059af84ec15e61348b8de1885fc0
parent 80213 677fe017e6a03295ef9351dec38b77a8ad317ab4
child 80215 6ae6d3beeaf408278412b715abec499731dd987d
push idunknown
push userunknown
push dateunknown
reviewersBas
bugs685326
milestone11.0a1
Bug 685326 - Move Round{,In,Out} from gfxRect to BaseRect, and make them no-ops on IntRect. r=Bas
gfx/2d/BaseRect.h
gfx/2d/Rect.h
gfx/thebes/gfxRect.cpp
gfx/thebes/gfxRect.h
--- a/gfx/2d/BaseRect.h
+++ b/gfx/2d/BaseRect.h
@@ -303,16 +303,73 @@ struct BaseRect {
   // Helper methods for computing the extents
   T X() const { return x; }
   T Y() const { return y; }
   T Width() const { return width; }
   T Height() const { return height; }
   T XMost() const { return x + width; }
   T YMost() const { return y + height; }
 
+  // Round the rectangle edges to integer coordinates, such that the rounded
+  // rectangle has the same set of pixel centers as the original rectangle.
+  // Edges at offset 0.5 round up.
+  // Suitable for most places where integral device coordinates
+  // are needed, but note that any translation should be applied first to
+  // avoid pixel rounding errors.
+  // Note that this is *not* rounding to nearest integer if the values are negative.
+  // They are always rounding as floor(n + 0.5).
+  // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14
+  // If you need similar method which is using NS_round(), you should create
+  // new |RoundAwayFromZero()| method.
+  void Round()
+  {
+    T x0 = static_cast<T>(floor(T(X()) + 0.5));
+    T y0 = static_cast<T>(floor(T(Y()) + 0.5));
+    T x1 = static_cast<T>(floor(T(XMost()) + 0.5));
+    T y1 = static_cast<T>(floor(T(YMost()) + 0.5));
+
+    x = x0;
+    y = y0;
+
+    width = x1 - x0;
+    height = y1 - y0;
+  }
+
+  // Snap the rectangle edges to integer coordinates, such that the
+  // original rectangle contains the resulting rectangle.
+  void RoundIn()
+  {
+    T x0 = static_cast<T>(ceil(T(X())));
+    T y0 = static_cast<T>(ceil(T(Y())));
+    T x1 = static_cast<T>(floor(T(XMost())));
+    T y1 = static_cast<T>(floor(T(YMost())));
+
+    x = x0;
+    y = y0;
+
+    width = x1 - x0;
+    height = y1 - y0;
+  }
+
+  // Snap the rectangle edges to integer coordinates, such that the
+  // resulting rectangle contains the original rectangle.
+  void RoundOut()
+  {
+    T x0 = static_cast<T>(floor(T(X())));
+    T y0 = static_cast<T>(floor(T(Y())));
+    T x1 = static_cast<T>(ceil(T(XMost())));
+    T y1 = static_cast<T>(ceil(T(YMost())));
+
+    x = x0;
+    y = y0;
+
+    width = x1 - x0;
+    height = y1 - y0;
+  }
+
   // Scale 'this' by aScale, converting coordinates to integers so that the result is
   // the smallest integer-coordinate rectangle containing the unrounded result.
   // Note: this can turn an empty rectangle into a non-empty rectangle
   void ScaleRoundOut(double aScale) { ScaleRoundOut(aScale, aScale); }
   // Scale 'this' by aXScale and aYScale, converting coordinates to integers so
   // that the result is the smallest integer-coordinate rectangle containing the
   // unrounded result.
   // Note: this can turn an empty rectangle into a non-empty rectangle
--- a/gfx/2d/Rect.h
+++ b/gfx/2d/Rect.h
@@ -59,16 +59,21 @@ struct IntRect :
     public BaseRect<int32_t, IntRect, IntPoint, IntSize, Margin> {
     typedef BaseRect<int32_t, IntRect, IntPoint, mozilla::gfx::IntSize, Margin> Super;
 
     IntRect() : Super() {}
     IntRect(IntPoint aPos, mozilla::gfx::IntSize aSize) :
         Super(aPos, aSize) {}
     IntRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height) :
         Super(_x, _y, _width, _height) {}
+
+    // Rounding isn't meaningful on an integer rectangle.
+    void Round() {}
+    void RoundIn() {}
+    void RoundOut() {}
 };
 
 struct Rect :
     public BaseRect<Float, Rect, Point, Size, Margin> {
     typedef BaseRect<Float, Rect, Point, mozilla::gfx::Size, Margin> Super;
 
     Rect() : Super() {}
     Rect(Point aPos, mozilla::gfx::Size aSize) :
--- a/gfx/thebes/gfxRect.cpp
+++ b/gfx/thebes/gfxRect.cpp
@@ -50,62 +50,16 @@ gfxRect::WithinEpsilonOfIntegerPixels(gf
 {
     NS_ASSERTION(-0.5 < aEpsilon && aEpsilon < 0.5, "Nonsense epsilon value");
     return (WithinEpsilonOfInteger(x, aEpsilon) &&
             WithinEpsilonOfInteger(y, aEpsilon) &&
             WithinEpsilonOfInteger(width, aEpsilon) &&
             WithinEpsilonOfInteger(height, aEpsilon));
 }
 
-void
-gfxRect::Round()
-{
-    // Note that don't use NS_round here. See the comment for this method in gfxRect.h
-    gfxFloat x0 = floor(X() + 0.5);
-    gfxFloat y0 = floor(Y() + 0.5);
-    gfxFloat x1 = floor(XMost() + 0.5);
-    gfxFloat y1 = floor(YMost() + 0.5);
-
-    x = x0;
-    y = y0;
-
-    width = x1 - x0;
-    height = y1 - y0;
-}
-
-void
-gfxRect::RoundIn()
-{
-    gfxFloat x0 = ceil(X());
-    gfxFloat y0 = ceil(Y());
-    gfxFloat x1 = floor(XMost());
-    gfxFloat y1 = floor(YMost());
-
-    x = x0;
-    y = y0;
-
-    width = x1 - x0;
-    height = y1 - y0;
-}
-
-void
-gfxRect::RoundOut()
-{
-    gfxFloat x0 = floor(X());
-    gfxFloat y0 = floor(Y());
-    gfxFloat x1 = ceil(XMost());
-    gfxFloat y1 = ceil(YMost());
-
-    x = x0;
-    y = y0;
-
-    width = x1 - x0;
-    height = y1 - y0;
-}
-
 /* Clamp r to CAIRO_COORD_MIN .. CAIRO_COORD_MAX
  * these are to be device coordinates.
  *
  * Cairo is currently using 24.8 fixed point,
  * so -2^24 .. 2^24-1 is our valid
  */
 
 #define CAIRO_COORD_MAX (16777215.0)
--- a/gfx/thebes/gfxRect.h
+++ b/gfx/thebes/gfxRect.h
@@ -102,37 +102,16 @@ struct THEBES_API gfxRect :
     /**
      * 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.
      */
     bool WithinEpsilonOfIntegerPixels(gfxFloat aEpsilon) const;
 
-    // Round the rectangle edges to integer coordinates, such that the rounded
-    // rectangle has the same set of pixel centers as the original rectangle.
-    // Edges at offset 0.5 round up.
-    // Suitable for most places where integral device coordinates
-    // are needed, but note that any translation should be applied first to
-    // avoid pixel rounding errors.
-    // Note that this is *not* rounding to nearest integer if the values are negative.
-    // They are always rounding as floor(n + 0.5).
-    // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14
-    // If you need similar method which is using NS_round(), you should create
-    // new |RoundAwayFromZero()| method.
-    void Round();
-
-    // Snap the rectangle edges to integer coordinates, such that the
-    // original rectangle contains the resulting rectangle.
-    void RoundIn();
-    
-    // Snap the rectangle edges to integer coordinates, such that the
-    // resulting rectangle contains the original rectangle.
-    void RoundOut();
-
     gfxPoint AtCorner(mozilla::css::Corner corner) const {
         switch (corner) {
             case NS_CORNER_TOP_LEFT: return TopLeft();
             case NS_CORNER_TOP_RIGHT: return TopRight();
             case NS_CORNER_BOTTOM_RIGHT: return BottomRight();
             case NS_CORNER_BOTTOM_LEFT: return BottomLeft();
             default:
                 NS_ERROR("Invalid corner!");