Bug 637852. Part 3: Add nsPoint::ScaleToNearestPixels, nsRect::ScaleToNearestPixels, nsRect::ScaleToInsidePixels and nsRect::ScaleToOutsidePixels APIs. r=joe
authorRobert O'Callahan <robert@ocallahan.org>
Thu, 23 Jun 2011 00:11:27 +1200
changeset 71518 b53df216be6181b6488332f66fa0954a29c019a8
parent 71517 c9dd59391c7d104e4d9faa863b3cbf655f562a41
child 71519 1da2c78a1a72954bcddcb0eb62be930f48802dcd
push id20559
push usereakhgari@mozilla.com
push dateWed, 22 Jun 2011 19:51:00 +0000
treeherdermozilla-central@cd95d565c4d9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjoe
bugs637852
milestone7.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 637852. Part 3: Add nsPoint::ScaleToNearestPixels, nsRect::ScaleToNearestPixels, nsRect::ScaleToInsidePixels and nsRect::ScaleToOutsidePixels APIs. r=joe
gfx/src/nsPoint.h
gfx/src/nsRect.h
--- a/gfx/src/nsPoint.h
+++ b/gfx/src/nsPoint.h
@@ -47,39 +47,50 @@ struct nsIntPoint;
 
 struct nsPoint : public mozilla::BasePoint<nscoord, nsPoint> {
   typedef mozilla::BasePoint<nscoord, nsPoint> Super;
 
   nsPoint() : Super() {}
   nsPoint(const nsPoint& aPoint) : Super(aPoint) {}
   nsPoint(nscoord aX, nscoord aY) : Super(aX, aY) {}
 
+  inline nsIntPoint ScaleToNearestPixels(float aXScale, float aYScale,
+                                         nscoord aAppUnitsPerPixel) const;
   inline nsIntPoint ToNearestPixels(nscoord aAppUnitsPerPixel) const;
 
   // Converts this point from aFromAPP, an appunits per pixel ratio, to aToAPP.
   inline nsPoint ConvertAppUnits(PRInt32 aFromAPP, PRInt32 aToAPP) const;
 };
 
 struct nsIntPoint : public mozilla::BasePoint<PRInt32, nsIntPoint> {
   typedef mozilla::BasePoint<PRInt32, nsIntPoint> Super;
 
   nsIntPoint() : Super() {}
   nsIntPoint(const nsIntPoint& aPoint) : Super(aPoint) {}
   nsIntPoint(PRInt32 aX, PRInt32 aY) : Super(aX, aY) {}
 };
 
 inline nsIntPoint
-nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const {
+nsPoint::ScaleToNearestPixels(float aXScale, float aYScale,
+                              nscoord aAppUnitsPerPixel) const
+{
   return nsIntPoint(
-      NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel)),
-      NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel)));
+      NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale),
+      NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale));
+}
+
+inline nsIntPoint
+nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const
+{
+  return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
 }
 
 inline nsPoint
-nsPoint::ConvertAppUnits(PRInt32 aFromAPP, PRInt32 aToAPP) const {
+nsPoint::ConvertAppUnits(PRInt32 aFromAPP, PRInt32 aToAPP) const
+{
   if (aFromAPP != aToAPP) {
     nsPoint point;
     point.x = NSToCoordRound(NSCoordScale(x, aFromAPP, aToAPP));
     point.y = NSToCoordRound(NSCoordScale(y, aFromAPP, aToAPP));
     return point;
   }
   return *this;
 }
--- a/gfx/src/nsRect.h
+++ b/gfx/src/nsRect.h
@@ -88,18 +88,24 @@ struct NS_GFX nsRect :
 
   // Converts this rect from aFromAPP, an appunits per pixel ratio, to aToAPP.
   // In the RoundOut version we make the rect the smallest rect containing the
   // unrounded result. In the RoundIn version we make the rect the largest rect
   // contained in the unrounded result.
   inline nsRect ConvertAppUnitsRoundOut(PRInt32 aFromAPP, PRInt32 aToAPP) const;
   inline nsRect ConvertAppUnitsRoundIn(PRInt32 aFromAPP, PRInt32 aToAPP) const;
 
+  inline nsIntRect ScaleToNearestPixels(float aXScale, float aYScale,
+                                        nscoord aAppUnitsPerPixel) const;
   inline nsIntRect ToNearestPixels(nscoord aAppUnitsPerPixel) const;
+  inline nsIntRect ScaleToOutsidePixels(float aXScale, float aYScale,
+                                        nscoord aAppUnitsPerPixel) const;
   inline nsIntRect ToOutsidePixels(nscoord aAppUnitsPerPixel) const;
+  inline nsIntRect ScaleToInsidePixels(float aXScale, float aYScale,
+                                       nscoord aAppUnitsPerPixel) const;
   inline nsIntRect ToInsidePixels(nscoord aAppUnitsPerPixel) const;
 };
 
 struct NS_GFX nsIntRect :
   public mozilla::BaseRect<PRInt32, nsIntRect, nsIntPoint, nsIntSize, nsIntMargin> {
   typedef mozilla::BaseRect<PRInt32, nsIntRect, nsIntPoint, nsIntSize, nsIntMargin> Super;
 
   // Constructors
@@ -170,56 +176,77 @@ nsRect::ConvertAppUnitsRoundIn(PRInt32 a
   rect.width = (right - rect.x);
   rect.height = (bottom - rect.y);
 
   return rect;
 }
 
 // scale the rect but round to preserve centers
 inline nsIntRect
-nsRect::ToNearestPixels(nscoord aAppUnitsPerPixel) const
+nsRect::ScaleToNearestPixels(float aXScale, float aYScale,
+                             nscoord aAppUnitsPerPixel) const
 {
   nsIntRect rect;
-  rect.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel));
-  rect.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel));
+  rect.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale);
+  rect.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale);
   rect.width  = NSToIntRoundUp(NSAppUnitsToDoublePixels(XMost(),
-                               aAppUnitsPerPixel)) - rect.x;
+                               aAppUnitsPerPixel) * aXScale) - rect.x;
   rect.height = NSToIntRoundUp(NSAppUnitsToDoublePixels(YMost(),
-                               aAppUnitsPerPixel)) - rect.y;
+                               aAppUnitsPerPixel) * aYScale) - rect.y;
   return rect;
 }
 
 // scale the rect but round to smallest containing rect
 inline nsIntRect
-nsRect::ToOutsidePixels(nscoord aAppUnitsPerPixel) const
+nsRect::ScaleToOutsidePixels(float aXScale, float aYScale,
+                             nscoord aAppUnitsPerPixel) const
 {
   nsIntRect rect;
-  rect.x = NSToIntFloor(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)));
-  rect.y = NSToIntFloor(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)));
+  rect.x = NSToIntFloor(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)) * aXScale);
+  rect.y = NSToIntFloor(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)) * aYScale);
   rect.width  = NSToIntCeil(NSAppUnitsToFloatPixels(XMost(),
-                            float(aAppUnitsPerPixel))) - rect.x;
+                            float(aAppUnitsPerPixel)) * aXScale) - rect.x;
   rect.height = NSToIntCeil(NSAppUnitsToFloatPixels(YMost(),
-                            float(aAppUnitsPerPixel))) - rect.y;
+                            float(aAppUnitsPerPixel)) * aYScale) - rect.y;
   return rect;
 }
 
 // scale the rect but round to largest contained rect
 inline nsIntRect
-nsRect::ToInsidePixels(nscoord aAppUnitsPerPixel) const
+nsRect::ScaleToInsidePixels(float aXScale, float aYScale,
+                            nscoord aAppUnitsPerPixel) const
 {
   nsIntRect rect;
-  rect.x = NSToIntCeil(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)));
-  rect.y = NSToIntCeil(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)));
+  rect.x = NSToIntCeil(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)) * aXScale);
+  rect.y = NSToIntCeil(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)) * aYScale);
   rect.width  = NSToIntFloor(NSAppUnitsToFloatPixels(XMost(),
-                             float(aAppUnitsPerPixel))) - rect.x;
+                             float(aAppUnitsPerPixel)) * aXScale) - rect.x;
   rect.height = NSToIntFloor(NSAppUnitsToFloatPixels(YMost(),
-                             float(aAppUnitsPerPixel))) - rect.y;
+                             float(aAppUnitsPerPixel)) * aYScale) - rect.y;
   return rect;
 }
 
+inline nsIntRect
+nsRect::ToNearestPixels(nscoord aAppUnitsPerPixel) const
+{
+  return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
+}
+
+inline nsIntRect
+nsRect::ToOutsidePixels(nscoord aAppUnitsPerPixel) const
+{
+  return ScaleToOutsidePixels(1.0f, 1.0f, aAppUnitsPerPixel);
+}
+
+inline nsIntRect
+nsRect::ToInsidePixels(nscoord aAppUnitsPerPixel) const
+{
+  return ScaleToInsidePixels(1.0f, 1.0f, aAppUnitsPerPixel);
+}
+
 // app units are integer multiples of pixels, so no rounding needed
 inline nsRect
 nsIntRect::ToAppUnits(nscoord aAppUnitsPerPixel) const
 {
   return nsRect(NSIntPixelsToAppUnits(x, aAppUnitsPerPixel),
                 NSIntPixelsToAppUnits(y, aAppUnitsPerPixel),
                 NSIntPixelsToAppUnits(width, aAppUnitsPerPixel),
                 NSIntPixelsToAppUnits(height, aAppUnitsPerPixel));