Bug 880676 - Units.h cleanup for easier reading of future patches. r=BenWa
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 14 Jun 2013 16:11:30 -0400
changeset 135122 a26c17d194c13b06ba4a4f6efebc78b4b5834fb9
parent 135121 89ddf463d04f985fe67376786cea47b25c841973
child 135123 6e4751b152a142dba3b2154536574d7ec0444704
push idunknown
push userunknown
push dateunknown
reviewersBenWa
bugs880676
milestone24.0a1
Bug 880676 - Units.h cleanup for easier reading of future patches. r=BenWa
layout/base/Units.h
--- a/layout/base/Units.h
+++ b/layout/base/Units.h
@@ -3,152 +3,174 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef MOZ_UNITS_H_
 #define MOZ_UNITS_H_
 
 #include "mozilla/gfx/Point.h"
+#include "mozilla/gfx/Rect.h"
 #include "nsDeviceContext.h"
 
 namespace mozilla {
 
+struct CSSPixel;
+struct LayerPixel;
+struct ScreenPixel;
+
+typedef gfx::PointTyped<CSSPixel> CSSPoint;
+typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
+typedef gfx::SizeTyped<CSSPixel> CSSSize;
+typedef gfx::IntSizeTyped<CSSPixel> CSSIntSize;
+typedef gfx::RectTyped<CSSPixel> CSSRect;
+typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
+
+typedef gfx::PointTyped<LayerPixel> LayerPoint;
+typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
+typedef gfx::SizeTyped<LayerPixel> LayerSize;
+typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
+typedef gfx::RectTyped<LayerPixel> LayerRect;
+typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
+
+typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
+typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
+typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
+typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
+typedef gfx::RectTyped<ScreenPixel> ScreenRect;
+typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
+
 /*
  * The pixels that content authors use to specify sizes in.
  */
 struct CSSPixel {
-  static gfx::IntPointTyped<CSSPixel> RoundToInt(const gfx::PointTyped<CSSPixel>& aPoint) {
-    return gfx::IntPointTyped<CSSPixel>(NS_lround(aPoint.x),
-                                        NS_lround(aPoint.y));
+
+  // Operations within CSS units
+
+  static CSSIntPoint RoundToInt(const CSSPoint& aPoint) {
+    return CSSIntPoint(NS_lround(aPoint.x),
+                       NS_lround(aPoint.y));
+  }
+
+  // Conversions from app units
+
+  static CSSPoint FromAppUnits(const nsPoint& aPoint) {
+    return CSSPoint(NSAppUnitsToFloatPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                    NSAppUnitsToFloatPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
   }
 
-  static gfx::PointTyped<CSSPixel> FromAppUnits(const nsPoint& aPoint) {
-    return gfx::PointTyped<CSSPixel>(NSAppUnitsToFloatPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                     NSAppUnitsToFloatPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
+  static CSSRect FromAppUnits(const nsRect& aRect) {
+    return CSSRect(NSAppUnitsToFloatPixels(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                   NSAppUnitsToFloatPixels(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                   NSAppUnitsToFloatPixels(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                   NSAppUnitsToFloatPixels(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
   }
 
-  static gfx::IntPointTyped<CSSPixel> FromAppUnitsRounded(const nsPoint& aPoint) {
-    return gfx::IntPointTyped<CSSPixel>(NSAppUnitsToIntPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                        NSAppUnitsToIntPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
+  static CSSIntPoint FromAppUnitsRounded(const nsPoint& aPoint) {
+    return CSSIntPoint(NSAppUnitsToIntPixels(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                       NSAppUnitsToIntPixels(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
   }
 
-  static nsPoint ToAppUnits(const gfx::PointTyped<CSSPixel>& aPoint) {
+  static CSSIntRect FromAppUnitsRounded(const nsRect& aRect) {
+    return CSSIntRect(NSAppUnitsToIntPixels(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                      NSAppUnitsToIntPixels(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                      NSAppUnitsToIntPixels(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
+                      NSAppUnitsToIntPixels(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
+  }
+
+  // Conversions to app units
+
+  static nsPoint ToAppUnits(const CSSPoint& aPoint) {
     return nsPoint(NSFloatPixelsToAppUnits(aPoint.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
                    NSFloatPixelsToAppUnits(aPoint.y, float(nsDeviceContext::AppUnitsPerCSSPixel())));
   }
 
-  static nsPoint ToAppUnits(const gfx::IntPointTyped<CSSPixel>& aPoint)
-  {
+  static nsPoint ToAppUnits(const CSSIntPoint& aPoint) {
     return nsPoint(NSIntPixelsToAppUnits(aPoint.x, nsDeviceContext::AppUnitsPerCSSPixel()),
                    NSIntPixelsToAppUnits(aPoint.y, nsDeviceContext::AppUnitsPerCSSPixel()));
   }
 
-  static gfx::RectTyped<CSSPixel> FromAppUnits(const nsRect& aRect) {
-    return gfx::RectTyped<CSSPixel>(NSAppUnitsToFloatPixels(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                    NSAppUnitsToFloatPixels(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                    NSAppUnitsToFloatPixels(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                    NSAppUnitsToFloatPixels(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
-  }
-
-  static nsRect ToAppUnits(const gfx::RectTyped<CSSPixel>& aRect) {
+  static nsRect ToAppUnits(const CSSRect& aRect) {
     return nsRect(NSFloatPixelsToAppUnits(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
                   NSFloatPixelsToAppUnits(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
                   NSFloatPixelsToAppUnits(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
                   NSFloatPixelsToAppUnits(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
   }
-
-  static gfx::IntRectTyped<CSSPixel> FromAppUnitsRounded(const nsRect& aRect)
-  {
-    return gfx::IntRectTyped<CSSPixel>(NSAppUnitsToIntPixels(aRect.x, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                       NSAppUnitsToIntPixels(aRect.y, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                       NSAppUnitsToIntPixels(aRect.width, float(nsDeviceContext::AppUnitsPerCSSPixel())),
-                                       NSAppUnitsToIntPixels(aRect.height, float(nsDeviceContext::AppUnitsPerCSSPixel())));
-  }
 };
 
-typedef gfx::PointTyped<CSSPixel> CSSPoint;
-typedef gfx::IntPointTyped<CSSPixel> CSSIntPoint;
-typedef gfx::SizeTyped<CSSPixel> CSSSize;
-typedef gfx::RectTyped<CSSPixel> CSSRect;
-typedef gfx::IntRectTyped<CSSPixel> CSSIntRect;
-
 /*
  * The pixels that layout rasterizes and delivers to the graphics code.
  * These are generally referred to as "device pixels" in layout code. Layer
  * pixels are affected by:
  * 1) the "display resolution" (see nsIPresShell::SetResolution)
  * 2) the "full zoom" (see nsPresContext::SetFullZoom)
  * 3) the "widget scale" (nsIWidget::GetDefaultScale)
  */
 struct LayerPixel {
-  static gfx::PointTyped<LayerPixel> FromCSSPoint(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
-    return gfx::PointTyped<LayerPixel>(aPoint.x * aResolutionX,
-                                       aPoint.y * aResolutionY);
-  }
+
+  // Operations within layer units
 
-  static gfx::IntPointTyped<LayerPixel> FromCSSPointRounded(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
-    return gfx::IntPointTyped<LayerPixel>(NS_lround(aPoint.x * aResolutionX),
-                                          NS_lround(aPoint.y * aResolutionY));
+  static LayerIntRect RoundToInt(const LayerRect& aRect) {
+    return LayerIntRect(NS_lround(aRect.x),
+                        NS_lround(aRect.y),
+                        NS_lround(aRect.width),
+                        NS_lround(aRect.height));
   }
 
-  static gfx::IntRectTyped<LayerPixel> RoundToInt(const gfx::RectTyped<LayerPixel>& aRect) {
-    return gfx::IntRectTyped<LayerPixel>(NS_lround(aRect.x),
-                                         NS_lround(aRect.y),
-                                         NS_lround(aRect.width),
-                                         NS_lround(aRect.height));
+  // Conversions from CSS units
+
+  static LayerPoint FromCSSPoint(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
+    return LayerPoint(aPoint.x * aResolutionX,
+                      aPoint.y * aResolutionY);
   }
 
-  static gfx::RectTyped<LayerPixel> FromCSSRect(const CSSRect& aRect, float aResolutionX, float aResolutionY) {
-    return gfx::RectTyped<LayerPixel>(aRect.x * aResolutionX,
-                                      aRect.y * aResolutionY,
-                                      aRect.width * aResolutionX,
-                                      aRect.height * aResolutionY);
+  static LayerIntPoint FromCSSPointRounded(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
+    return LayerIntPoint(NS_lround(aPoint.x * aResolutionX),
+                         NS_lround(aPoint.y * aResolutionY));
   }
 
-  static gfx::IntRectTyped<LayerPixel> FromCSSRectRounded(const CSSRect& aRect, float aResolutionX, float aResolutionY) {
+  static LayerRect FromCSSRect(const CSSRect& aRect, float aResolutionX, float aResolutionY) {
+    return LayerRect(aRect.x * aResolutionX,
+                     aRect.y * aResolutionY,
+                     aRect.width * aResolutionX,
+                     aRect.height * aResolutionY);
+  }
+
+  static LayerIntRect FromCSSRectRounded(const CSSRect& aRect, float aResolutionX, float aResolutionY) {
     return RoundToInt(FromCSSRect(aRect, aResolutionX, aResolutionY));
   }
 };
 
-typedef gfx::PointTyped<LayerPixel> LayerPoint;
-typedef gfx::IntPointTyped<LayerPixel> LayerIntPoint;
-typedef gfx::IntSizeTyped<LayerPixel> LayerIntSize;
-typedef gfx::RectTyped<LayerPixel> LayerRect;
-typedef gfx::IntRectTyped<LayerPixel> LayerIntRect;
-
 /*
  * The pixels that are displayed on the screen.
  * On non-OMTC platforms this should be equivalent to LayerPixel units.
  * On OMTC platforms these may diverge from LayerPixel units temporarily,
  * while an asynchronous zoom is happening, but should eventually converge
  * back to LayerPixel units. Some variables (such as those representing
  * chrome UI element sizes) that are not subject to content zoom should
  * generally be represented in ScreenPixel units.
  */
 struct ScreenPixel {
-  static gfx::PointTyped<ScreenPixel> FromCSSPoint(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
-    return gfx::PointTyped<ScreenPixel>(aPoint.x * aResolutionX,
-                                        aPoint.y * aResolutionY);
+
+  // Conversions from CSS units
+
+  static ScreenPoint FromCSSPoint(const CSSPoint& aPoint, float aResolutionX, float aResolutionY) {
+    return ScreenPoint(aPoint.x * aResolutionX,
+                       aPoint.y * aResolutionY);
   }
 
-  static CSSPoint ToCSSPoint(const gfx::PointTyped<ScreenPixel>& aPoint, float aResolutionX, float aResolutionY) {
+  // Conversions to CSS units
+
+  static CSSPoint ToCSSPoint(const ScreenPoint& aPoint, float aResolutionX, float aResolutionY) {
     return CSSPoint(aPoint.x * aResolutionX,
                     aPoint.y * aResolutionY);
   }
 
-  static CSSIntRect ToCSSIntRectRoundIn(const gfx::IntRectTyped<ScreenPixel>& aRect, float aResolutionX, float aResolutionY) {
-    gfx::IntRectTyped<CSSPixel> ret(aRect.x, aRect.y, aRect.width, aRect.height);
+  static CSSIntRect ToCSSIntRectRoundIn(const ScreenIntRect& aRect, float aResolutionX, float aResolutionY) {
+    CSSIntRect ret(aRect.x, aRect.y, aRect.width, aRect.height);
     ret.ScaleInverseRoundIn(aResolutionX, aResolutionY);
     return ret;
   }
 };
 
-typedef gfx::PointTyped<ScreenPixel> ScreenPoint;
-typedef gfx::IntPointTyped<ScreenPixel> ScreenIntPoint;
-typedef gfx::SizeTyped<ScreenPixel> ScreenSize;
-typedef gfx::IntSizeTyped<ScreenPixel> ScreenIntSize;
-typedef gfx::RectTyped<ScreenPixel> ScreenRect;
-typedef gfx::IntRectTyped<ScreenPixel> ScreenIntRect;
-
 };
 
 #endif