Bug 537890. Part 10: Fix nsPresContext and nsIDeviceContext unit-conversion methods to be explicit about using CSS logical units instead of physical units. r=dbaron
authorRobert O'Callahan <robert@ocallahan.org>
Fri, 13 Aug 2010 21:58:04 +1200
changeset 50407 ae101cf2e88207ff0eddb3168f1b38dd009f3e32
parent 50406 e6debc61f80dbb496a8ef6cbd369ff9b9db909be
child 50408 54941eb68f532032f2de7b15f8146ba57eac8335
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs537890
milestone2.0b4pre
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 537890. Part 10: Fix nsPresContext and nsIDeviceContext unit-conversion methods to be explicit about using CSS logical units instead of physical units. r=dbaron
content/svg/content/src/SVGLength.cpp
content/svg/content/src/SVGLength.h
content/svg/content/src/nsSVGLength2.cpp
content/svg/content/src/nsSVGLength2.h
content/svg/content/src/nsSVGSVGElement.cpp
content/svg/content/src/nsSVGSVGElement.h
gfx/src/nsIDeviceContext.h
gfx/src/thebes/nsThebesDeviceContext.cpp
layout/base/nsDocumentViewer.cpp
layout/base/nsPresContext.cpp
layout/base/nsPresContext.h
layout/generic/nsSimplePageSequence.cpp
layout/mathml/nsMathMLChar.cpp
layout/mathml/nsMathMLmmultiscriptsFrame.cpp
layout/mathml/nsMathMLmoverFrame.cpp
layout/mathml/nsMathMLmsubFrame.cpp
layout/mathml/nsMathMLmsupFrame.cpp
layout/mathml/nsMathMLmunderFrame.cpp
layout/mathml/nsMathMLmunderoverFrame.cpp
layout/printing/nsPrintEngine.cpp
layout/style/nsCSSValue.cpp
layout/style/nsComputedDOMStyle.cpp
layout/style/nsComputedDOMStyle.h
layout/style/nsMediaFeatures.cpp
layout/style/nsROCSSPrimitiveValue.cpp
layout/style/nsROCSSPrimitiveValue.h
layout/style/nsRuleNode.cpp
layout/style/nsStyleStruct.cpp
--- a/content/svg/content/src/SVGLength.cpp
+++ b/content/svg/content/src/SVGLength.cpp
@@ -190,54 +190,38 @@ SVGLength::GetValueInSpecifiedUnit(PRUin
 float
 SVGLength::GetUserUnitsPerUnit(const nsSVGElement *aElement, PRUint8 aAxis) const
 {
   switch (mUnit) {
     case nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER:
     case nsIDOMSVGLength::SVG_LENGTHTYPE_PX:
       return 1.0f;
     case nsIDOMSVGLength::SVG_LENGTHTYPE_MM:
-      return INCHES_PER_MM_FLOAT * GetUserUnitsPerInch(aElement);
+      return INCHES_PER_MM_FLOAT * GetUserUnitsPerInch();
     case nsIDOMSVGLength::SVG_LENGTHTYPE_CM:
-      return INCHES_PER_CM_FLOAT * GetUserUnitsPerInch(aElement);
+      return INCHES_PER_CM_FLOAT * GetUserUnitsPerInch();
     case nsIDOMSVGLength::SVG_LENGTHTYPE_IN:
-      return GetUserUnitsPerInch(aElement);
+      return GetUserUnitsPerInch();
     case nsIDOMSVGLength::SVG_LENGTHTYPE_PT:
-      return (1.0f/POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch(aElement);
+      return (1.0f/POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch();
     case nsIDOMSVGLength::SVG_LENGTHTYPE_PC:
-      return (12.0f/POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch(aElement);
+      return (12.0f/POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch();
     case nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE:
       return GetUserUnitsPerPercent(aElement, aAxis);
     case nsIDOMSVGLength::SVG_LENGTHTYPE_EMS:
       return nsSVGUtils::GetFontSize(const_cast<nsSVGElement*>(aElement));
     case nsIDOMSVGLength::SVG_LENGTHTYPE_EXS:
       return nsSVGUtils::GetFontXHeight(const_cast<nsSVGElement*>(aElement));
     default:
       NS_NOTREACHED("Unknown unit type");
       return std::numeric_limits<float>::quiet_NaN();
   }
 }
 
 /* static */ float
-SVGLength::GetUserUnitsPerInch(const nsIContent *aContent)
-{
-  if (aContent) {
-    nsPresContext *context = nsContentUtils::GetContextForContent(
-                                           const_cast<nsIContent*>(aContent));
-    if (context) {
-      float uuPerInch = float(context->AppUnitsPerInch()) /
-                        float(nsPresContext::AppUnitsPerCSSPixel());
-      NS_ASSERTION(uuPerInch > 0.0f, "Non-positive user units per inch");
-      return uuPerInch;
-    }
-  }
-  return std::numeric_limits<float>::quiet_NaN();
-}
-
-/* static */ float
 SVGLength::GetUserUnitsPerPercent(const nsSVGElement *aElement, PRUint8 aAxis)
 {
   if (aElement) {
     nsSVGSVGElement *viewportElement = const_cast<nsSVGElement*>(aElement)->GetCtx();
     if (viewportElement) {
       return NS_MAX(viewportElement->GetLength(aAxis) / 100.0f, 0.0f);
     }
   }
--- a/content/svg/content/src/SVGLength.h
+++ b/content/svg/content/src/SVGLength.h
@@ -210,35 +210,23 @@ private:
    *
    * This method returns numeric_limits<float>::quiet_NaN() if the conversion
    * factor between the length's current unit and user units is undefined (see
    * the comments for GetUserUnitsPerInch and GetUserUnitsPerPercent).
    */
   float GetUserUnitsPerUnit(const nsSVGElement *aElement, PRUint8 aAxis) const;
 
   /**
-   * The conversion factor between user units (CSS px) and a CSS absolute unit
-   * (in this case inches) is the same for all elements in a given document,
-   * except for elements for which the factor is undefined. The conversion
-   * factor is undefined for elements that are not in a document tree, and for
-   * elements in a document tree that don't have a pres context (elements under
-   * a display:none iframe, or elements belonging to a data document (an XHR
-   * response document or a document created via createDocument()).
-   *
-   * This helper acts as the basis for conversion between user units and all
-   * CSS absolute units (the conversion factors between CSS absolute units are
-   * fixed). Inches are chosen as the canonical unit because that's what
-   * pres/device contexts store, so it makes sense for this helper to also work
-   * in those terms to eliminate unnecessary multiplications/divisions that
-   * must then be reversed.
-   *
-   * This function returns a positive value if the conversion factor is
-   * defined, otherwise it returns numeric_limits<float>::quiet_NaN().
+   * The conversion factor between user units (CSS px) and CSS inches is
+   * constant: 96 px per inch.
    */
-  static float GetUserUnitsPerInch(const nsIContent *aContent);
+  static float GetUserUnitsPerInch()
+  {
+    return 96.0;
+  }
 
   /**
    * The conversion factor between user units and percentage units depends on
    * aElement being non-null, and on aElement having a viewport element
    * ancestor with only appropriate SVG elements between aElement and that
    * ancestor. If that's not the case, then the conversion factor is undefined.
    *
    * This function returns a non-negative value if the conversion factor is
--- a/content/svg/content/src/nsSVGLength2.cpp
+++ b/content/svg/content/src/nsSVGLength2.cpp
@@ -180,41 +180,16 @@ GetValueFromString(const nsAString &aVal
     if (IsValidUnitType(*aUnitType)) {
       return NS_OK;
     }
   }
   
   return NS_ERROR_DOM_SYNTAX_ERR;
 }
 
-float
-nsSVGLength2::GetMMPerPixel(nsSVGSVGElement *aCtx) const
-{
-  if (!aCtx)
-    return 1;
-
-  float mmPerPx = aCtx->GetMMPerPx(mCtxType);
-
-  if (mmPerPx == 0.0f) {
-    NS_ASSERTION(mmPerPx != 0.0f, "invalid mm/pixels");
-    mmPerPx = 1e-4f; // some small value
-  }
-
-  return mmPerPx;
-}
-
-/*static*/ float
-nsSVGLength2::GetMMPerPixel(nsIFrame *aNonSVGFrame)
-{
-  nsPresContext* presContext = aNonSVGFrame->PresContext();
-  float pixelsPerInch =
-    presContext->AppUnitsToFloatCSSPixels(presContext->AppUnitsPerInch());
-  return MM_PER_INCH_FLOAT/pixelsPerInch;
-}
-
 static float
 FixAxisLength(float aLength)
 {
   if (aLength == 0.0f) {
     NS_WARNING("zero axis length");
     return 1e-20f;
   }
   return aLength;
@@ -268,25 +243,25 @@ nsSVGLength2::GetUnitScaleFactor(nsSVGEl
 float
 nsSVGLength2::GetUnitScaleFactor(nsSVGSVGElement *aCtx, PRUint8 aUnitType) const
 {
   switch (aUnitType) {
   case nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER:
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PX:
     return 1;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_MM:
-    return GetMMPerPixel(aCtx);
+    return GetMMPerPixel();
   case nsIDOMSVGLength::SVG_LENGTHTYPE_CM:
-    return GetMMPerPixel(aCtx) / 10.0f;
+    return GetMMPerPixel() / 10.0f;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_IN:
-    return GetMMPerPixel(aCtx) / MM_PER_INCH_FLOAT;
+    return GetMMPerPixel() / MM_PER_INCH_FLOAT;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PT:
-    return GetMMPerPixel(aCtx) * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT;
+    return GetMMPerPixel() * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PC:
-    return GetMMPerPixel(aCtx) * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT / 12.0f;
+    return GetMMPerPixel() * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT / 12.0f;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE:
     return 100.0f / GetAxisLength(aCtx);
   case nsIDOMSVGLength::SVG_LENGTHTYPE_EMS:
     return 1 / GetEmLength(aCtx);
   case nsIDOMSVGLength::SVG_LENGTHTYPE_EXS:
     return 1 / GetExLength(aCtx);
   default:
     NS_NOTREACHED("Unknown unit type");
@@ -301,25 +276,25 @@ nsSVGLength2::GetUnitScaleFactor(nsIFram
   if (content->IsSVG())
     return GetUnitScaleFactor(static_cast<nsSVGElement*>(content), aUnitType);
 
   switch (aUnitType) {
   case nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER:
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PX:
     return 1;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_MM:
-    return GetMMPerPixel(aFrame);
+    return GetMMPerPixel();
   case nsIDOMSVGLength::SVG_LENGTHTYPE_CM:
-    return GetMMPerPixel(aFrame) / 10.0f;
+    return GetMMPerPixel() / 10.0f;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_IN:
-    return GetMMPerPixel(aFrame) / MM_PER_INCH_FLOAT;
+    return GetMMPerPixel() / MM_PER_INCH_FLOAT;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PT:
-    return GetMMPerPixel(aFrame) * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT;
+    return GetMMPerPixel() * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PC:
-    return GetMMPerPixel(aFrame) * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT / 12.0f;
+    return GetMMPerPixel() * POINTS_PER_INCH_FLOAT / MM_PER_INCH_FLOAT / 12.0f;
   case nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE:
     return 100.0f / GetAxisLength(aFrame);
   case nsIDOMSVGLength::SVG_LENGTHTYPE_EMS:
     return 1 / GetEmLength(aFrame);
   case nsIDOMSVGLength::SVG_LENGTHTYPE_EXS:
     return 1 / GetExLength(aFrame);
   default:
     NS_NOTREACHED("Unknown unit type");
--- a/content/svg/content/src/nsSVGLength2.h
+++ b/content/svg/content/src/nsSVGLength2.h
@@ -102,17 +102,17 @@ private:
   
   float mAnimVal;
   float mBaseVal;
   PRUint8 mSpecifiedUnitType;
   PRUint8 mAttrEnum; // element specified tracking for attribute
   PRUint8 mCtxType; // X, Y or Unspecified
   PRPackedBool mIsAnimated;
   
-  static float GetMMPerPixel(nsIFrame *aNonSVGFrame);
+  static float GetMMPerPixel() { return MM_PER_INCH_FLOAT / 96; }
   float GetAxisLength(nsIFrame *aNonSVGFrame) const;
   static float GetEmLength(nsIFrame *aFrame)
     { return nsSVGUtils::GetFontSize(aFrame); }
   static float GetExLength(nsIFrame *aFrame)
     { return nsSVGUtils::GetFontXHeight(aFrame); }
   float GetUnitScaleFactor(nsIFrame *aFrame, PRUint8 aUnitType) const;
 
   float GetMMPerPixel(nsSVGSVGElement *aCtx) const;
--- a/content/svg/content/src/nsSVGSVGElement.cpp
+++ b/content/svg/content/src/nsSVGSVGElement.cpp
@@ -196,17 +196,16 @@ NS_INTERFACE_MAP_END_INHERITING(nsSVGSVG
 // Implementation
 
 nsSVGSVGElement::nsSVGSVGElement(already_AddRefed<nsINodeInfo> aNodeInfo,
                                  PRUint32 aFromParser)
   : nsSVGSVGElementBase(aNodeInfo),
     mCoordCtx(nsnull),
     mViewportWidth(0),
     mViewportHeight(0),
-    mCoordCtxMmPerPx(0),
     mCurrentTranslate(0.0f, 0.0f),
     mCurrentScale(1.0f),
     mPreviousTranslate(0.0f, 0.0f),
     mPreviousScale(1.0f),
     mRedrawSuspendCount(0)
 #ifdef MOZ_SMIL
     ,mStartAnimationOnBindToTree(!aFromParser)
 #endif // MOZ_SMIL
@@ -304,50 +303,32 @@ nsSVGSVGElement::GetViewport(nsIDOMSVGRe
   // XXX
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 /* readonly attribute float pixelUnitToMillimeterX; */
 NS_IMETHODIMP
 nsSVGSVGElement::GetPixelUnitToMillimeterX(float *aPixelUnitToMillimeterX)
 {
-  // to correctly determine this, the caller would need to pass in the
-  // right PresContext...
-  nsPresContext *context = nsContentUtils::GetContextForContent(this);
-  if (!context) {
-    *aPixelUnitToMillimeterX = 0.28f; // 90dpi
-    return NS_OK;
-  }
-
-  *aPixelUnitToMillimeterX = MM_PER_INCH_FLOAT /
-      nsPresContext::AppUnitsToIntCSSPixels(context->AppUnitsPerInch());
+  *aPixelUnitToMillimeterX = MM_PER_INCH_FLOAT / 96;
   return NS_OK;
 }
 
 /* readonly attribute float pixelUnitToMillimeterY; */
 NS_IMETHODIMP
 nsSVGSVGElement::GetPixelUnitToMillimeterY(float *aPixelUnitToMillimeterY)
 {
   return GetPixelUnitToMillimeterX(aPixelUnitToMillimeterY);
 }
 
 /* readonly attribute float screenPixelToMillimeterX; */
 NS_IMETHODIMP
 nsSVGSVGElement::GetScreenPixelToMillimeterX(float *aScreenPixelToMillimeterX)
 {
-  // to correctly determine this, the caller would need to pass in the
-  // right PresContext...
-  nsPresContext *context = nsContentUtils::GetContextForContent(this);
-  if (!context) {
-    *aScreenPixelToMillimeterX = 0.28f; // 90dpi
-    return NS_OK;
-  }
-
-  *aScreenPixelToMillimeterX = MM_PER_INCH_FLOAT /
-      nsPresContext::AppUnitsToIntCSSPixels(context->AppUnitsPerInch());
+  *aScreenPixelToMillimeterX = MM_PER_INCH_FLOAT / 96;
   return NS_OK;
 }
 
 /* readonly attribute float screenPixelToMillimeterY; */
 NS_IMETHODIMP
 nsSVGSVGElement::GetScreenPixelToMillimeterY(float *aScreenPixelToMillimeterY)
 {
   return GetScreenPixelToMillimeterX(aScreenPixelToMillimeterY);
@@ -1148,25 +1129,16 @@ nsSVGSVGElement::GetLength(PRUint8 aCtxT
   case nsSVGUtils::Y:
     return h;
   case nsSVGUtils::XY:
     return float(nsSVGUtils::ComputeNormalizedHypotenuse(w, h));
   }
   return 0;
 }
 
-float
-nsSVGSVGElement::GetMMPerPx(PRUint8 aCtxType)
-{
-  if (mCoordCtxMmPerPx == 0.0f) {
-    GetScreenPixelToMillimeterX(&mCoordCtxMmPerPx);
-  }
-  return mCoordCtxMmPerPx;
-}
-
 //----------------------------------------------------------------------
 // nsSVGElement methods
 
 /* virtual */ gfxMatrix
 nsSVGSVGElement::PrependLocalTransformTo(const gfxMatrix &aMatrix)
 {
   if (nsSVGUtils::IsInnerSVG(this)) {
     float x, y;
--- a/content/svg/content/src/nsSVGSVGElement.h
+++ b/content/svg/content/src/nsSVGSVGElement.h
@@ -197,17 +197,16 @@ public:
   virtual void DidChangeViewBox(PRBool aDoSetAttr);
   virtual void DidChangePreserveAspectRatio(PRBool aDoSetAttr);
 
   virtual void DidAnimateViewBox();
   virtual void DidAnimatePreserveAspectRatio();
   
   // nsSVGSVGElement methods:
   float GetLength(PRUint8 mCtxType);
-  float GetMMPerPx(PRUint8 mCtxType = 0);
 
   // public helpers:
   gfxMatrix GetViewBoxTransform();
 
   virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
 
   svgFloatSize GetViewportSize() const {
     return svgFloatSize(mViewportWidth, mViewportHeight);
@@ -282,18 +281,16 @@ protected:
   //
   //   http://www.w3.org/TR/SVG11/coords.html#ViewportSpace
   //
   // XXXjwatt Currently only used for outer <svg>, but maybe we could use -1 to
   // flag this as an inner <svg> to save the overhead of GetCtx calls?
   // XXXjwatt our frame should probably reset these when it's destroyed.
   float mViewportWidth, mViewportHeight;
 
-  float mCoordCtxMmPerPx;
-
 #ifdef MOZ_SMIL
   // The time container for animations within this SVG document fragment. Set
   // for all outermost <svg> elements (not nested <svg> elements).
   nsAutoPtr<nsSMILTimeContainer> mTimedDocumentRoot;
 #endif // MOZ_SMIL
 
   // zoom and pan
   // IMPORTANT: see the comment in RecordCurrentScaleTranslate before writing
--- a/gfx/src/nsIDeviceContext.h
+++ b/gfx/src/nsIDeviceContext.h
@@ -291,20 +291,26 @@ public:
 
   /**
    * Convert app units to device pixels which is used for gfx/thebes.
    */
   gfxFloat AppUnitsToGfxUnits(nscoord aAppUnits) const
   { return gfxFloat(aAppUnits) / AppUnitsPerDevPixel(); }
 
   /**
-   * Gets the number of app units in one inch; this is the device's DPI
-   * times AppUnitsPerDevPixel().
+   * Gets the number of app units in one physical inch; this is the
+   * device's DPI times AppUnitsPerDevPixel().
    */
-  PRInt32 AppUnitsPerInch() const { return mAppUnitsPerInch; }
+  PRInt32 AppUnitsPerPhysicalInch() const { return mAppUnitsPerPhysicalInch; }
+
+  /**
+   * Gets the number of app units in one CSS inch; this is the
+   * 96 times AppUnitsPerCSSPixel.
+   */
+  static PRInt32 AppUnitsPerCSSInch() { return 96 * AppUnitsPerCSSPixel(); }
 
   /**
    * Fill in an nsFont based on the ID of a system font.  This function
    * may or may not fill in the size, so the size should be set to a
    * reasonable default before calling.
    *
    * @param aID    The system font ID.
    * @param aInfo  The font structure to be filled in.
@@ -464,17 +470,17 @@ public:
    * without any initialization.  Only really used by
    * Gtk native theme stuff.
    */
   NS_IMETHOD ClearCachedSystemFonts() = 0;
 
   /**
    * Check to see if the DPI has changed
    * @return whether there was actually a change in the DPI
-   *         (whether AppUnitsPerDevPixel() or AppUnitsPerInch() changed)
+   *         (whether AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch() changed)
   */
   virtual PRBool CheckDPIChange() = 0;
 
   /**
    * Set the pixel scaling factor: all lengths are multiplied by this factor
    * when we convert them to device pixels. Returns whether the ratio of 
    * app units to dev pixels changed because of the scale factor.
    */
@@ -489,16 +495,16 @@ public:
   /**
    * Get the unscaled ratio of app units to dev pixels; useful if something
    * needs to be converted from to unscaled pixels
    */
   PRInt32 UnscaledAppUnitsPerDevPixel() const { return mAppUnitsPerDevNotScaledPixel; }
 
 protected:
   PRInt32 mAppUnitsPerDevPixel;
-  PRInt32 mAppUnitsPerInch;
+  PRInt32 mAppUnitsPerPhysicalInch;
   PRInt32 mAppUnitsPerDevNotScaledPixel;
   float  mPixelScale;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(nsIDeviceContext, NS_IDEVICE_CONTEXT_IID)
 
 #endif /* nsIDeviceContext_h___ */
--- a/gfx/src/thebes/nsThebesDeviceContext.cpp
+++ b/gfx/src/thebes/nsThebesDeviceContext.cpp
@@ -275,17 +275,17 @@ nsThebesDeviceContext::nsThebesDeviceCon
 #ifdef PR_LOGGING
     if (!gThebesGFXLog)
         gThebesGFXLog = PR_NewLogModule("thebesGfx");
 #endif
 
     PR_LOG(gThebesGFXLog, PR_LOG_DEBUG, ("#### Creating DeviceContext %p\n", this));
 
     mAppUnitsPerDevPixel = nscoord(-1);
-    mAppUnitsPerInch = nscoord(-1);
+    mAppUnitsPerPhysicalInch = nscoord(-1);
     mAppUnitsPerDevNotScaledPixel = nscoord(-1);
     mPixelScale = 1.0f;
 
     mFontCache = nsnull;
     mWidget = nsnull;
     mFontAliasTable = nsnull;
 
 #ifdef NS_DEBUG
@@ -680,17 +680,17 @@ nsThebesDeviceContext::SetDPI()
         }
 
         mAppUnitsPerDevNotScaledPixel =
             PR_MAX(1, NS_lround(AppUnitsPerCSSPixel() / devPixelsPerCSSPixel));
     }
 
     NS_ASSERTION(dpi != -1.0, "no dpi set");
 
-    mAppUnitsPerInch = NS_lround(dpi * mAppUnitsPerDevNotScaledPixel);
+    mAppUnitsPerPhysicalInch = NS_lround(dpi * mAppUnitsPerDevNotScaledPixel);
     UpdateScaledAppUnits();
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsThebesDeviceContext::Init(nsIWidget *aWidget)
 {
@@ -1177,32 +1177,34 @@ nsThebesDeviceContext::CalcPrintingSize(
         break;
     }
 #endif
     default:
         NS_ERROR("trying to print to unknown surface type");
     }
 
     if (inPoints) {
-        mWidth = NSToCoordRound(float(size.width) * AppUnitsPerInch() / 72);
-        mHeight = NSToCoordRound(float(size.height) * AppUnitsPerInch() / 72);
+        // For printing, CSS inches and physical inches are identical
+        // so it doesn't matter which we use here
+        mWidth = NSToCoordRound(float(size.width) * AppUnitsPerPhysicalInch() / 72);
+        mHeight = NSToCoordRound(float(size.height) * AppUnitsPerPhysicalInch() / 72);
     } else {
         mWidth = NSToIntRound(size.width);
         mHeight = NSToIntRound(size.height);
     }
 }
 
 PRBool nsThebesDeviceContext::CheckDPIChange() {
     PRInt32 oldDevPixels = mAppUnitsPerDevNotScaledPixel;
-    PRInt32 oldInches = mAppUnitsPerInch;
+    PRInt32 oldInches = mAppUnitsPerPhysicalInch;
 
     SetDPI();
 
     return oldDevPixels != mAppUnitsPerDevNotScaledPixel ||
-           oldInches != mAppUnitsPerInch;
+           oldInches != mAppUnitsPerPhysicalInch;
 }
 
 PRBool
 nsThebesDeviceContext::SetPixelScale(float aScale)
 {
     if (aScale <= 0) {
         NS_NOTREACHED("Invalid pixel scale value");
         return PR_FALSE;
--- a/layout/base/nsDocumentViewer.cpp
+++ b/layout/base/nsDocumentViewer.cpp
@@ -921,18 +921,18 @@ DocumentViewerImpl::InitInternal(nsIWidg
         // the document viewer at the moment to get the right device context
         // (this won't break anyone, since page layout mode was never really
         // usable)
 #endif
         double pageWidth = 0, pageHeight = 0;
         mPresContext->GetPrintSettings()->GetEffectivePageSize(&pageWidth,
                                                                &pageHeight);
         mPresContext->SetPageSize(
-          nsSize(mPresContext->TwipsToAppUnits(NSToIntFloor(pageWidth)),
-                 mPresContext->TwipsToAppUnits(NSToIntFloor(pageHeight))));
+          nsSize(mPresContext->CSSTwipsToAppUnits(NSToIntFloor(pageWidth)),
+                 mPresContext->CSSTwipsToAppUnits(NSToIntFloor(pageHeight))));
         mPresContext->SetIsRootPaginatedDocument(PR_TRUE);
         mPresContext->SetPageScale(1.0f);
       }
 #endif
     }
   }
 
   nsCOMPtr<nsIInterfaceRequestor> requestor(do_QueryReferent(mContainer));
@@ -3670,17 +3670,17 @@ DocumentViewerImpl::Print(nsIPrintSettin
   if (pDoc)
     return pDoc->Print();
 
   if (!mPrintEngine) {
     mPrintEngine = new nsPrintEngine();
     NS_ENSURE_TRUE(mPrintEngine, NS_ERROR_OUT_OF_MEMORY);
 
     rv = mPrintEngine->Initialize(this, docShell, mDocument, 
-                                  float(mDeviceContext->AppUnitsPerInch()) /
+                                  float(mDeviceContext->AppUnitsPerCSSInch()) /
                                   float(mDeviceContext->AppUnitsPerDevPixel()) /
                                   mPageZoom,
                                   mParentWidget,
 #ifdef NS_DEBUG
                                   mDebugFile
 #else
                                   nsnull
 #endif
@@ -3738,17 +3738,17 @@ DocumentViewerImpl::PrintPreview(nsIPrin
     aChildDOMWin->GetDocument(getter_AddRefs(domDoc));
     nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
     NS_ENSURE_STATE(doc);
 
     mPrintEngine = new nsPrintEngine();
     NS_ENSURE_TRUE(mPrintEngine, NS_ERROR_OUT_OF_MEMORY);
 
     rv = mPrintEngine->Initialize(this, docShell, doc,
-                                  float(mDeviceContext->AppUnitsPerInch()) /
+                                  float(mDeviceContext->AppUnitsPerCSSInch()) /
                                   float(mDeviceContext->AppUnitsPerDevPixel()) /
                                   mPageZoom,
                                   mParentWidget,
 #ifdef NS_DEBUG
                                   mDebugFile
 #else
                                   nsnull
 #endif
@@ -3858,23 +3858,18 @@ DocumentViewerImpl::PrintPreviewNavigate
 
   if (fndPageFrame) {
     nscoord deadSpaceGapTwips = 0;
     nsIPageSequenceFrame * sqf = do_QueryFrame(seqFrame);
     if (sqf) {
       sqf->GetDeadSpaceValue(&deadSpaceGapTwips);
     }
 
-    // To compute deadSpaceGap, use the same presContext as was used
-    // to layout the seqFrame. (That presContext may have different
-    // TwipsToAppUnits conversion from this->mPresContext)
-    nscoord deadSpaceGap = 
-      seqFrame->PresContext()->TwipsToAppUnits(deadSpaceGapTwips);
-
-    nscoord newYPosn = 
+    nscoord deadSpaceGap = nsPresContext::CSSTwipsToAppUnits(deadSpaceGapTwips);
+    nscoord newYPosn =
       nscoord(mPrintEngine->GetPrintPreviewScale() * 
               float(fndPageFrame->GetPosition().y - deadSpaceGap));
     sf->ScrollTo(nsPoint(pt.x, newYPosn), nsIScrollableFrame::INSTANT);
   }
   return NS_OK;
 
 }
 
--- a/layout/base/nsPresContext.cpp
+++ b/layout/base/nsPresContext.cpp
@@ -480,17 +480,17 @@ nsPresContext::GetFontPreferences()
   pref.Assign("font.minimum-size.");
   pref.Append(langGroup);
 
   PRInt32 size = nsContentUtils::GetIntPref(pref.get());
   if (unit == eUnit_px) {
     mMinimumFontSize = CSSPixelsToAppUnits(size);
   }
   else if (unit == eUnit_pt) {
-    mMinimumFontSize = this->PointsToAppUnits(size);
+    mMinimumFontSize = CSSPointsToAppUnits(size);
   }
 
   // get attributes specific to each generic font
   nsCAutoString generic_dot_langGroup;
   for (PRInt32 eType = eDefaultFont_Variable; eType < eDefaultFont_COUNT; ++eType) {
     generic_dot_langGroup.Assign(kGenericFont[eType]);
     generic_dot_langGroup.Append(langGroup);
 
@@ -544,20 +544,20 @@ nsPresContext::GetFontPreferences()
     // parity, we enable the ability to set a different font-size on all platforms.
 
     // get font.size.[generic].[langGroup]
     // size=0 means 'Auto', i.e., generic fonts retain the size of the variable font
     MAKE_FONT_PREF_KEY(pref, "font.size", generic_dot_langGroup);
     size = nsContentUtils::GetIntPref(pref.get());
     if (size > 0) {
       if (unit == eUnit_px) {
-        font->size = nsPresContext::CSSPixelsToAppUnits(size);
+        font->size = CSSPixelsToAppUnits(size);
       }
       else if (unit == eUnit_pt) {
-        font->size = this->PointsToAppUnits(size);
+        font->size = CSSPointsToAppUnits(size);
       }
     }
 
     // get font.size-adjust.[generic].[langGroup]
     // XXX only applicable on GFX ports that handle |font-size-adjust|
     MAKE_FONT_PREF_KEY(pref, "font.size-adjust", generic_dot_langGroup);
     cvalue = nsContentUtils::GetCharPref(pref.get());
     if (!cvalue.IsEmpty()) {
--- a/layout/base/nsPresContext.h
+++ b/layout/base/nsPresContext.h
@@ -560,17 +560,17 @@ public:
   void SetFullZoom(float aZoom);
 
   nscoord GetAutoQualityMinFontSize() {
     return DevPixelsToAppUnits(mAutoQualityMinFontSizePixelsPref);
   }
   
   static PRInt32 AppUnitsPerCSSPixel() { return nsIDeviceContext::AppUnitsPerCSSPixel(); }
   PRInt32 AppUnitsPerDevPixel() const  { return mDeviceContext->AppUnitsPerDevPixel(); }
-  PRInt32 AppUnitsPerInch() const      { return mDeviceContext->AppUnitsPerInch(); }
+  static PRInt32 AppUnitsPerCSSInch() { return nsIDeviceContext::AppUnitsPerCSSInch(); }
 
   static nscoord CSSPixelsToAppUnits(PRInt32 aPixels)
   { return NSIntPixelsToAppUnits(aPixels,
                                  nsIDeviceContext::AppUnitsPerCSSPixel()); }
 
   static nscoord CSSPixelsToAppUnits(float aPixels)
   { return NSFloatPixelsToAppUnits(aPixels,
              float(nsIDeviceContext::AppUnitsPerCSSPixel())); }
@@ -614,29 +614,29 @@ public:
   { return mDeviceContext->AppUnitsToGfxUnits(aAppUnits); }
 
   gfxRect AppUnitsToGfxUnits(const nsRect& aAppRect) const
   { return gfxRect(AppUnitsToGfxUnits(aAppRect.x),
                    AppUnitsToGfxUnits(aAppRect.y),
                    AppUnitsToGfxUnits(aAppRect.width),
                    AppUnitsToGfxUnits(aAppRect.height)); }
 
-  nscoord TwipsToAppUnits(float aTwips) const
+  static nscoord CSSTwipsToAppUnits(float aTwips)
   { return NSToCoordRoundWithClamp(
-      mDeviceContext->AppUnitsPerInch() * NS_TWIPS_TO_INCHES(aTwips)); }
+      nsIDeviceContext::AppUnitsPerCSSInch() * NS_TWIPS_TO_INCHES(aTwips)); }
 
   // Margin-specific version, since they often need TwipsToAppUnits
-  nsMargin TwipsToAppUnits(const nsIntMargin &marginInTwips) const
-  { return nsMargin(TwipsToAppUnits(marginInTwips.left), 
-                    TwipsToAppUnits(marginInTwips.top),
-                    TwipsToAppUnits(marginInTwips.right),
-                    TwipsToAppUnits(marginInTwips.bottom)); }
+  static nsMargin CSSTwipsToAppUnits(const nsIntMargin &marginInTwips)
+  { return nsMargin(CSSTwipsToAppUnits(marginInTwips.left), 
+                    CSSTwipsToAppUnits(marginInTwips.top),
+                    CSSTwipsToAppUnits(marginInTwips.right),
+                    CSSTwipsToAppUnits(marginInTwips.bottom)); }
 
-  nscoord PointsToAppUnits(float aPoints) const
-  { return NSToCoordRound(aPoints * mDeviceContext->AppUnitsPerInch() /
+  static nscoord CSSPointsToAppUnits(float aPoints)
+  { return NSToCoordRound(aPoints * nsIDeviceContext::AppUnitsPerCSSInch() /
                           POINTS_PER_INCH_FLOAT); }
 
   nscoord RoundAppUnitsToNearestDevPixels(nscoord aAppUnits) const
   { return DevPixelsToAppUnits(AppUnitsToDevPixels(aAppUnits)); }
 
   struct ScrollbarStyles {
     // Always one of NS_STYLE_OVERFLOW_SCROLL, NS_STYLE_OVERFLOW_HIDDEN,
     // or NS_STYLE_OVERFLOW_AUTO.
--- a/layout/generic/nsSimplePageSequence.cpp
+++ b/layout/generic/nsSimplePageSequence.cpp
@@ -113,23 +113,23 @@ NS_NewSimplePageSequenceFrame(nsIPresShe
 NS_IMPL_FRAMEARENA_HELPERS(nsSimplePageSequenceFrame)
 
 nsSimplePageSequenceFrame::nsSimplePageSequenceFrame(nsStyleContext* aContext) :
   nsContainerFrame(aContext),
   mTotalPages(-1),
   mSelectionHeight(-1),
   mYSelOffset(0)
 {
-  nscoord halfInch = PresContext()->TwipsToAppUnits(NS_INCHES_TO_TWIPS(0.5));
+  nscoord halfInch = PresContext()->CSSTwipsToAppUnits(NS_INCHES_TO_TWIPS(0.5));
   mMargin.SizeTo(halfInch, halfInch, halfInch, halfInch);
 
   // XXX Unsafe to assume successful allocation
   mPageData = new nsSharedPageData();
   mPageData->mHeadFootFont = new nsFont(*PresContext()->GetDefaultFont(kGenericFont_serif));
-  mPageData->mHeadFootFont->size = PresContext()->PointsToAppUnits(10);
+  mPageData->mHeadFootFont->size = nsPresContext::CSSPointsToAppUnits(10);
 
   nsresult rv;
   mPageData->mPrintOptions = do_GetService(sPrintOptionsContractID, &rv);
 
   // Doing this here so we only have to go get these formats once
   SetPageNumberFormat("pagenumber",  "%1$d", PR_TRUE);
   SetPageNumberFormat("pageofpages", "%1$d of %2$d", PR_FALSE);
 }
@@ -185,34 +185,34 @@ nsSimplePageSequenceFrame::Reflow(nsPres
     nsIntMargin unwriteableTwips;
     mPageData->mPrintSettings->GetUnwriteableMarginInTwips(unwriteableTwips);
     NS_ASSERTION(unwriteableTwips.left  >= 0 && unwriteableTwips.top >= 0 &&
                  unwriteableTwips.right >= 0 && unwriteableTwips.bottom >= 0,
                  "Unwriteable twips should be non-negative");
 
     nsIntMargin marginTwips;
     mPageData->mPrintSettings->GetMarginInTwips(marginTwips);
-    mMargin = aPresContext->TwipsToAppUnits(marginTwips + unwriteableTwips);
+    mMargin = aPresContext->CSSTwipsToAppUnits(marginTwips + unwriteableTwips);
 
     PRInt16 printType;
     mPageData->mPrintSettings->GetPrintRange(&printType);
     mPrintRangeType = printType;
 
     nsIntMargin edgeTwips;
     mPageData->mPrintSettings->GetEdgeInTwips(edgeTwips);
 
     // sanity check the values. three inches are sometimes needed
     PRInt32 inchInTwips = NS_INCHES_TO_TWIPS(3.0);
     edgeTwips.top = NS_MIN(NS_MAX(edgeTwips.top, 0), inchInTwips);
     edgeTwips.bottom = NS_MIN(NS_MAX(edgeTwips.bottom, 0), inchInTwips);
     edgeTwips.left = NS_MIN(NS_MAX(edgeTwips.left, 0), inchInTwips);
     edgeTwips.right = NS_MIN(NS_MAX(edgeTwips.right, 0), inchInTwips);
 
     mPageData->mEdgePaperMargin =
-      aPresContext->TwipsToAppUnits(edgeTwips + unwriteableTwips);
+      aPresContext->CSSTwipsToAppUnits(edgeTwips + unwriteableTwips);
   }
 
   // *** Special Override ***
   // If this is a sub-sdoc (meaning it doesn't take the whole page)
   // and if this Document is in the upper left hand corner
   // we need to suppress the top margin or it will reflow too small
 
   nsSize pageSize = aPresContext->GetPageSize();
@@ -227,23 +227,23 @@ nsSimplePageSequenceFrame::Reflow(nsPres
   mPageData->mReflowMargin = mMargin;
 
   // Compute the size of each page and the x coordinate that each page will
   // be placed at
   nscoord extraThreshold = NS_MAX(pageSize.width, pageSize.height)/10;
   PRInt32 gapInTwips = nsContentUtils::GetIntPref("print.print_extra_margin");
   gapInTwips = NS_MAX(0, gapInTwips);
 
-  nscoord extraGap = aPresContext->TwipsToAppUnits(gapInTwips);
+  nscoord extraGap = aPresContext->CSSTwipsToAppUnits(gapInTwips);
   extraGap = NS_MIN(extraGap, extraThreshold); // clamp to 1/10 of the largest dim of the page
 
   nscoord  deadSpaceGap = 0;
   if (isPrintPreview) {
     GetDeadSpaceValue(&gapInTwips);
-    deadSpaceGap = aPresContext->TwipsToAppUnits(gapInTwips);
+    deadSpaceGap = aPresContext->CSSTwipsToAppUnits(gapInTwips);
   }
 
   nsMargin extraMargin(0,0,0,0);
   nsSize   shadowSize(0,0);
   if (aPresContext->IsScreen()) {
     extraMargin.SizeTo(extraGap, extraGap, extraGap, extraGap);
     nscoord fourPixels = nsPresContext::CSSPixelsToAppUnits(4);
     shadowSize.SizeTo(fourPixels, fourPixels);
@@ -613,17 +613,17 @@ nsSimplePageSequenceFrame::PrintNextPage
 
 #if defined(XP_UNIX) && !defined(XP_MACOSX)
       // On linux, need to rotate landscape-mode output on printed surfaces
       PRInt32 orientation;
       mPageData->mPrintSettings->GetOrientation(&orientation);
       if (nsIPrintSettings::kLandscapeOrientation == orientation) {
         // Shift up by one landscape-page-height (in points) before we rotate.
         float offset = POINTS_PER_INCH_FLOAT *
-           (mCurrentPageFrame->GetSize().height / float(dc->AppUnitsPerInch()));
+           (mCurrentPageFrame->GetSize().height / float(dc->AppUnitsPerCSSInch()));
         renderingContext->ThebesContext()->Translate(gfxPoint(offset, 0));
         renderingContext->ThebesContext()->Rotate(M_PI/2);
       }
 #endif // XP_UNIX && !XP_MACOSX
 
       nsRect drawingRect(nsPoint(0, 0),
                          mCurrentPageFrame->GetSize());
       nsRegion drawingRegion(drawingRect);
--- a/layout/mathml/nsMathMLChar.cpp
+++ b/layout/mathml/nsMathMLChar.cpp
@@ -992,17 +992,17 @@ IsSizeOK(nsPresContext* aPresContext, ns
     && PRBool(float(PR_ABS(a - b))
               < (1.0f - NS_MATHML_DELIMITER_FACTOR) * float(b));
   // Nearer: True if 'a' is around max{ +/-10% of 'b' , 'b' - 5pt },
   // as documented in The TeXbook, Ch.17, p.152.
   // i.e. within 10% and within 5pt
   PRBool isNearer = PR_FALSE;
   if (aHint & (NS_STRETCH_NEARER | NS_STRETCH_LARGEOP)) {
     float c = NS_MAX(float(b) * NS_MATHML_DELIMITER_FACTOR,
-                     float(b) - aPresContext->PointsToAppUnits(NS_MATHML_DELIMITER_SHORTFALL_POINTS));
+                     float(b) - nsPresContext::CSSPointsToAppUnits(NS_MATHML_DELIMITER_SHORTFALL_POINTS));
     isNearer = PRBool(float(PR_ABS(b - a)) <= (float(b) - c));
   }
   // Smaller: Mainly for transitory use, to compare two candidate
   // choices
   PRBool isSmaller =
     (aHint & NS_STRETCH_SMALLER)
     && PRBool((float(a) >= (NS_MATHML_DELIMITER_FACTOR * float(b)))
               && (a <= b));
--- a/layout/mathml/nsMathMLmmultiscriptsFrame.cpp
+++ b/layout/mathml/nsMathMLmmultiscriptsFrame.cpp
@@ -170,17 +170,17 @@ nsMathMLmmultiscriptsFrame::Place(nsIRen
   fm->GetXHeight (xHeight);
 
   nscoord ruleSize;
   GetRuleThickness (aRenderingContext, fm, ruleSize);
 
   // scriptspace from TeX for extra spacing after sup/subscript (0.5pt in plain TeX)
   // forced to be at least 1 pixel here
   nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
-  nscoord scriptSpace = NS_MAX(PresContext()->PointsToAppUnits(0.5f), onePixel);
+  nscoord scriptSpace = NS_MAX(nsPresContext::CSSPointsToAppUnits(0.5f), onePixel);
 
   /////////////////////////////////////
   // first the shift for the subscript
 
   // subScriptShift{1,2}
   // = minimum amount to shift the subscript down
   // = sub{1,2} in TeXbook
   // subScriptShift1 = subscriptshift attribute * x-height
--- a/layout/mathml/nsMathMLmoverFrame.cpp
+++ b/layout/mathml/nsMathMLmoverFrame.cpp
@@ -252,17 +252,17 @@ nsMathMLmoverFrame::Place(nsIRenderingCo
 { 
   if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishData.flags) &&
       !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags)) {
     // place like superscript
     return nsMathMLmsupFrame::PlaceSuperScript(PresContext(),
                                                aRenderingContext,
                                                aPlaceOrigin,
                                                aDesiredSize,
-                                               this, 0, PresContext()->PointsToAppUnits(0.5f));
+                                               this, 0, nsPresContext::CSSPointsToAppUnits(0.5f));
   }
 
   ////////////////////////////////////
   // Get the children's desired sizes
 
   nsBoundingMetrics bmBase, bmOver;
   nsHTMLReflowMetrics baseSize;
   nsHTMLReflowMetrics overSize;
--- a/layout/mathml/nsMathMLmsubFrame.cpp
+++ b/layout/mathml/nsMathMLmsubFrame.cpp
@@ -84,17 +84,17 @@ nsMathMLmsubFrame::TransmitAutomaticData
 }
 
 /* virtual */ nsresult
 nsMathMLmsubFrame::Place (nsIRenderingContext& aRenderingContext,
                           PRBool               aPlaceOrigin,
                           nsHTMLReflowMetrics& aDesiredSize)
 {
   // extra spacing after sup/subscript
-  nscoord scriptSpace = PresContext()->PointsToAppUnits(0.5f); // 0.5pt as in plain TeX
+  nscoord scriptSpace = nsPresContext::CSSPointsToAppUnits(0.5f); // 0.5pt as in plain TeX
 
   // check if the subscriptshift attribute is there
   nscoord subScriptShift = 0;
   nsAutoString value;
   GetAttribute(mContent, mPresentationData.mstyle,
                nsGkAtoms::subscriptshift_, value);
   if (!value.IsEmpty()) {
     nsCSSValue cssValue;
--- a/layout/mathml/nsMathMLmsupFrame.cpp
+++ b/layout/mathml/nsMathMLmsupFrame.cpp
@@ -84,17 +84,17 @@ nsMathMLmsupFrame::TransmitAutomaticData
 }
 
 /* virtual */ nsresult
 nsMathMLmsupFrame::Place(nsIRenderingContext& aRenderingContext,
                          PRBool               aPlaceOrigin,
                          nsHTMLReflowMetrics& aDesiredSize)
 {
   // extra spacing after sup/subscript
-  nscoord scriptSpace = PresContext()->PointsToAppUnits(0.5f); // 0.5pt as in plain TeX
+  nscoord scriptSpace = nsPresContext::CSSPointsToAppUnits(0.5f); // 0.5pt as in plain TeX
 
   // check if the superscriptshift attribute is there
   nsAutoString value;
   nscoord supScriptShift = 0;
   GetAttribute(mContent, mPresentationData.mstyle,
                nsGkAtoms::superscriptshift_, value);
   if (!value.IsEmpty()) {
     nsCSSValue cssValue;
--- a/layout/mathml/nsMathMLmunderFrame.cpp
+++ b/layout/mathml/nsMathMLmunderFrame.cpp
@@ -249,17 +249,17 @@ nsMathMLmunderFrame::Place(nsIRenderingC
 {
   if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishData.flags) &&
       !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags)) {
     // place like subscript
     return nsMathMLmsubFrame::PlaceSubScript(PresContext(),
                                              aRenderingContext,
                                              aPlaceOrigin,
                                              aDesiredSize,
-                                             this, 0, PresContext()->PointsToAppUnits(0.5f));
+                                             this, 0, nsPresContext::CSSPointsToAppUnits(0.5f));
   }
 
   ////////////////////////////////////
   // Get the children's desired sizes
 
   nsBoundingMetrics bmBase, bmUnder;
   nsHTMLReflowMetrics baseSize;
   nsHTMLReflowMetrics underSize;
--- a/layout/mathml/nsMathMLmunderoverFrame.cpp
+++ b/layout/mathml/nsMathMLmunderoverFrame.cpp
@@ -285,17 +285,18 @@ nsMathMLmunderoverFrame::Place(nsIRender
 {
   if ( NS_MATHML_EMBELLISH_IS_MOVABLELIMITS(mEmbellishData.flags) &&
       !NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags)) {
     // place like sub-superscript pair
     return nsMathMLmsubsupFrame::PlaceSubSupScript(PresContext(),
                                                    aRenderingContext,
                                                    aPlaceOrigin,
                                                    aDesiredSize,
-                                                   this, 0, 0, PresContext()->PointsToAppUnits(0.5f));
+                                                   this, 0, 0,
+                                                   nsPresContext::CSSPointsToAppUnits(0.5f));
   }
 
   ////////////////////////////////////
   // Get the children's desired sizes
 
   nsBoundingMetrics bmBase, bmUnder, bmOver;
   nsHTMLReflowMetrics baseSize;
   nsHTMLReflowMetrics underSize;
--- a/layout/printing/nsPrintEngine.cpp
+++ b/layout/printing/nsPrintEngine.cpp
@@ -2016,17 +2016,17 @@ nsPrintEngine::ReflowPrintObject(nsPrint
   aPO->mPresContext->SetContainer(supps);
 
   aPO->mPresShell->BeginObservingDocument();
 
   aPO->mPresContext->SetPageSize(adjSize);
   aPO->mPresContext->SetIsRootPaginatedDocument(documentIsTopLevel);
   aPO->mPresContext->SetPageScale(aPO->mZoomRatio);
   // Calculate scale factor from printer to screen
-  float printDPI = float(mPrt->mPrintDC->AppUnitsPerInch()) /
+  float printDPI = float(mPrt->mPrintDC->AppUnitsPerCSSInch()) /
                    float(mPrt->mPrintDC->AppUnitsPerDevPixel());
   aPO->mPresContext->SetPrintPreviewScale(mScreenDPI / printDPI);
 
   if (mIsCreatingPrintPreview && documentIsTopLevel) {
     mDocViewerPrint->SetPrintPreviewPresentation(aPO->mWindow,
                                                  aPO->mViewManager,
                                                  aPO->mPresContext,
                                                  aPO->mPresShell);
@@ -2353,18 +2353,18 @@ nsPrintEngine::DoPrint(nsPrintObject * a
                                         &endFrame, endPageNum, endRect);
           if (NS_SUCCEEDED(rv)) {
             mPrt->mPrintSettings->SetStartPageRange(startPageNum);
             mPrt->mPrintSettings->SetEndPageRange(endPageNum);
             nsIntMargin marginTwips(0,0,0,0);
             nsIntMargin unwrtMarginTwips(0,0,0,0);
             mPrt->mPrintSettings->GetMarginInTwips(marginTwips);
             mPrt->mPrintSettings->GetUnwriteableMarginInTwips(unwrtMarginTwips);
-            nsMargin totalMargin = poPresContext->TwipsToAppUnits(marginTwips + 
-                                                              unwrtMarginTwips);
+            nsMargin totalMargin = poPresContext->CSSTwipsToAppUnits(marginTwips + 
+                                                                     unwrtMarginTwips);
             if (startPageNum == endPageNum) {
               {
                 startRect.y -= totalMargin.top;
                 endRect.y   -= totalMargin.top;
 
                 // Clip out selection regions above the top of the first page
                 if (startRect.y < 0) {
                   // Reduce height to be the height of the positive-territory
--- a/layout/style/nsCSSValue.cpp
+++ b/layout/style/nsCSSValue.cpp
@@ -231,18 +231,18 @@ imgIRequest* nsCSSValue::GetImageValue()
   NS_ASSERTION(mUnit == eCSSUnit_Image, "not an Image value");
   return mValue.mImage->mRequest;
 }
 
 nscoord nsCSSValue::GetFixedLength(nsPresContext* aPresContext) const
 {
   NS_ASSERTION(mUnit == eCSSUnit_PhysicalMillimeter, "not a fixed length unit");
 
-  float twips = NS_MILLIMETERS_TO_TWIPS(mValue.mFloat);
-  return aPresContext->TwipsToAppUnits(twips);
+  float inches = mValue.mFloat / MM_PER_INCH_FLOAT;
+  return inches * aPresContext->DeviceContext()->AppUnitsPerPhysicalInch();
 }
 
 nscoord nsCSSValue::GetPixelLength() const
 {
   NS_ASSERTION(IsPixelLengthUnit(), "not a fixed length unit");
 
   double scaleFactor;
   switch (mUnit) {
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -132,17 +132,17 @@ GetContainingBlockFor(nsIFrame* aFrame) 
   if (!aFrame) {
     return nsnull;
   }
   return nsHTMLReflowState::GetContainingBlockFor(aFrame);
 }
 
 nsComputedDOMStyle::nsComputedDOMStyle()
   : mDocumentWeak(nsnull), mOuterFrame(nsnull),
-    mInnerFrame(nsnull), mPresShell(nsnull), mAppUnitsPerInch(0),
+    mInnerFrame(nsnull), mPresShell(nsnull),
     mExposeVisitedStyle(PR_FALSE)
 {
 }
 
 
 nsComputedDOMStyle::~nsComputedDOMStyle()
 {
 }
@@ -235,18 +235,16 @@ nsComputedDOMStyle::Init(nsIDOMElement *
       // the DOM spec sucks.
       mPseudo = nsnull;
     }
   }
 
   nsPresContext *presCtx = aPresShell->GetPresContext();
   NS_ENSURE_TRUE(presCtx, NS_ERROR_FAILURE);
 
-  mAppUnitsPerInch = presCtx->AppUnitsPerInch();
-
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsComputedDOMStyle::GetPropertyValue(const nsCSSProperty aPropID,
                                      nsAString& aValue)
 {
   // This is mostly to avoid code duplication with GetPropertyCSSValue(); if
@@ -3450,17 +3448,17 @@ nsresult
 nsComputedDOMStyle::DoGetTop(nsIDOMCSSValue** aValue)
 {
   return GetOffsetWidthFor(NS_SIDE_TOP, aValue);
 }
 
 nsROCSSPrimitiveValue*
 nsComputedDOMStyle::GetROCSSPrimitiveValue()
 {
-  nsROCSSPrimitiveValue *primitiveValue = new nsROCSSPrimitiveValue(mAppUnitsPerInch);
+  nsROCSSPrimitiveValue *primitiveValue = new nsROCSSPrimitiveValue();
 
   NS_ASSERTION(primitiveValue != 0, "ran out of memory");
 
   return primitiveValue;
 }
 
 nsDOMCSSValueList*
 nsComputedDOMStyle::GetROCSSValueList(PRBool aCommaDelimited)
@@ -3781,19 +3779,18 @@ nsComputedDOMStyle::GetBorderStyleFor(mo
     nsCSSProps::ValueToKeywordEnum(GetStyleBorder()->GetBorderStyle(aSide),
                                    nsCSSProps::kBorderStyleKTable));
 
   NS_ADDREF(*aValue = val);
   return NS_OK;
 }
 
 struct StyleCoordSerializeCalcOps {
-  StyleCoordSerializeCalcOps(nsAString& aResult, PRInt32 aAppUnitsPerInch)
-    : mResult(aResult),
-      mAppUnitsPerInch(aAppUnitsPerInch)
+  StyleCoordSerializeCalcOps(nsAString& aResult)
+    : mResult(aResult)
   {
   }
 
   typedef nsStyleCoord input_type;
   typedef nsStyleCoord::Array input_array_type;
 
   static nsCSSUnit GetUnit(const input_type& aValue) {
     if (aValue.IsCalcUnit()) {
@@ -3804,18 +3801,17 @@ struct StyleCoordSerializeCalcOps {
 
   void Append(const char* aString)
   {
     mResult.AppendASCII(aString);
   }
 
   void AppendLeafValue(const input_type& aValue)
   {
-    nsRefPtr<nsROCSSPrimitiveValue> val =
-      new nsROCSSPrimitiveValue(mAppUnitsPerInch);
+    nsRefPtr<nsROCSSPrimitiveValue> val = new nsROCSSPrimitiveValue();
     if (aValue.GetUnit() == eStyleUnit_Percent) {
       val->SetPercent(aValue.GetPercentValue());
     } else {
       NS_ABORT_IF_FALSE(aValue.GetUnit() == eStyleUnit_Coord,
                         "unexpected unit");
       val->SetAppUnits(aValue.GetCoordValue());
     }
 
@@ -3827,17 +3823,16 @@ struct StyleCoordSerializeCalcOps {
   void AppendNumber(const input_type& aValue)
   {
     NS_ABORT_IF_FALSE(PR_FALSE,
                       "should not have numbers in nsStyleCoord calc()");
   }
 
 private:
   nsAString &mResult;
-  PRInt32 mAppUnitsPerInch;
 };
 
 
 void
 nsComputedDOMStyle::SetValueToCoord(nsROCSSPrimitiveValue* aValue,
                                     const nsStyleCoord& aCoord,
                                     PercentageBaseGetter aPercentageBaseGetter,
                                     const PRInt32 aTable[],
@@ -3899,17 +3894,17 @@ nsComputedDOMStyle::SetValueToCoord(nsRO
         nscoord percentageBase;
         if (aPercentageBaseGetter &&
             (this->*aPercentageBaseGetter)(percentageBase)) {
           nscoord val =
             nsRuleNode::ComputeCoordPercentCalc(aCoord, percentageBase);
           aValue->SetAppUnits(NS_MAX(aMinAppUnits, NS_MIN(val, aMaxAppUnits)));
         } else {
           nsAutoString tmp;
-          StyleCoordSerializeCalcOps ops(tmp, mAppUnitsPerInch);
+          StyleCoordSerializeCalcOps ops(tmp);
           css::SerializeCalc(aCoord, ops);
           aValue->SetString(tmp); // not really SetString
         }
       } else {
         NS_ERROR("Can't handle this unit");
       }
       break;
   }
--- a/layout/style/nsComputedDOMStyle.h
+++ b/layout/style/nsComputedDOMStyle.h
@@ -488,18 +488,16 @@ private:
    */
   nsIFrame* mInnerFrame;
   /*
    * While computing style data, the presshell we're working with.  Null
    * otherwise.
    */
   nsIPresShell* mPresShell;
 
-  PRInt32 mAppUnitsPerInch; /* For unit conversions */
-
   PRPackedBool mExposeVisitedStyle;
 
 #ifdef DEBUG
   PRBool mFlushedPendingReflows;
 #endif
 };
 
 nsresult
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -231,17 +231,17 @@ GetMonochrome(nsPresContext* aPresContex
 }
 
 static nsresult
 GetResolution(nsPresContext* aPresContext, const nsMediaFeature*,
               nsCSSValue& aResult)
 {
     // Resolution values are in device pixels, not CSS pixels.
     nsIDeviceContext *dx = GetDeviceContextFor(aPresContext);
-    float dpi = float(dx->AppUnitsPerInch()) / float(dx->AppUnitsPerDevPixel());
+    float dpi = float(dx->AppUnitsPerPhysicalInch()) / float(dx->AppUnitsPerDevPixel());
     aResult.SetFloatValue(dpi, eCSSUnit_Inch);
     return NS_OK;
 }
 
 static nsresult
 GetScan(nsPresContext* aPresContext, const nsMediaFeature*,
         nsCSSValue& aResult)
 {
--- a/layout/style/nsROCSSPrimitiveValue.cpp
+++ b/layout/style/nsROCSSPrimitiveValue.cpp
@@ -39,18 +39,18 @@
 
 #include "nsROCSSPrimitiveValue.h"
 
 #include "nsPresContext.h"
 #include "nsStyleUtil.h"
 #include "nsDOMCSSRGBColor.h"
 #include "nsIDOMRect.h"
 
-nsROCSSPrimitiveValue::nsROCSSPrimitiveValue(PRInt32 aAppUnitsPerInch)
-  : mType(CSS_PX), mAppUnitsPerInch(aAppUnitsPerInch)
+nsROCSSPrimitiveValue::nsROCSSPrimitiveValue()
+  : mType(CSS_PX)
 {
   mValue.mAppUnits = 0;
 }
 
 
 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue()
 {
   Reset();
@@ -308,38 +308,41 @@ nsROCSSPrimitiveValue::GetFloatValue(PRU
     case CSS_PX :
       if (mType != CSS_PX)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
       *aReturn = nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits);
       break;
     case CSS_CM :
       if (mType != CSS_PX)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
-      *aReturn = mValue.mAppUnits * CM_PER_INCH_FLOAT / float(mAppUnitsPerInch);
+      *aReturn = mValue.mAppUnits * CM_PER_INCH_FLOAT /
+        nsPresContext::AppUnitsPerCSSInch();
       break;
     case CSS_MM :
       if (mType != CSS_PX)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
-      *aReturn = mValue.mAppUnits * MM_PER_INCH_FLOAT / float(mAppUnitsPerInch);
+      *aReturn = mValue.mAppUnits * MM_PER_INCH_FLOAT /
+        nsPresContext::AppUnitsPerCSSInch();
       break;
     case CSS_IN :
       if (mType != CSS_PX)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
-      *aReturn = mValue.mAppUnits / float(mAppUnitsPerInch);
+      *aReturn = mValue.mAppUnits / nsPresContext::AppUnitsPerCSSInch();
       break;
     case CSS_PT :
       if (mType != CSS_PX)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
       *aReturn = mValue.mAppUnits * POINTS_PER_INCH_FLOAT / 
-        float(mAppUnitsPerInch);
+        nsPresContext::AppUnitsPerCSSInch();
       break;
     case CSS_PC :
       if (mType != CSS_PX)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
-      *aReturn = mValue.mAppUnits * 6.0f / float(mAppUnitsPerInch);
+      *aReturn = mValue.mAppUnits * 6.0f /
+        nsPresContext::AppUnitsPerCSSInch();
       break;
     case CSS_PERCENTAGE :
       if (mType != CSS_PERCENTAGE)
         return NS_ERROR_DOM_INVALID_ACCESS_ERR;
       *aReturn = mValue.mFloat * 100;
       break;
     case CSS_NUMBER :
       if (mType != CSS_NUMBER)
--- a/layout/style/nsROCSSPrimitiveValue.h
+++ b/layout/style/nsROCSSPrimitiveValue.h
@@ -54,17 +54,17 @@ public:
 
   // nsIDOMCSSPrimitiveValue
   NS_DECL_NSIDOMCSSPRIMITIVEVALUE
 
   // nsIDOMCSSValue
   NS_DECL_NSIDOMCSSVALUE
 
   // nsROCSSPrimitiveValue
-  nsROCSSPrimitiveValue(PRInt32 aAppUnitsPerInch);
+  nsROCSSPrimitiveValue();
   virtual ~nsROCSSPrimitiveValue();
 
   void SetNumber(float aValue);
   void SetNumber(PRInt32 aValue);
   void SetNumber(PRUint32 aValue);
   void SetPercent(float aValue);
   void SetAppUnits(nscoord aValue);
   void SetAppUnits(float aValue);
@@ -86,14 +86,12 @@ private:
     nscoord         mAppUnits;
     float           mFloat;
     nsDOMCSSRGBColor* mColor;
     nsIDOMRect*     mRect;
     PRUnichar*      mString;
     nsIURI*         mURI;
     nsCSSKeyword    mKeyword;
   } mValue;
-  
-  PRInt32 mAppUnitsPerInch;
 };
 
 #endif /* nsROCSSPrimitiveValue_h___ */
 
--- a/layout/style/nsRuleNode.cpp
+++ b/layout/style/nsRuleNode.cpp
@@ -3093,17 +3093,17 @@ nsRuleNode::SetFont(nsPresContext* aPres
       //    (and the assumption is) it is always a proportional font. Then we
       //    always use 2 points smaller than what the browser has defined as
       //    the default proportional font.
       case eSystemFont_Field:
       case eSystemFont_Button:
       case eSystemFont_List:
         // Assumption: system defined font is proportional
         systemFont.size =
-          NS_MAX(defaultVariableFont->size - aPresContext->PointsToAppUnits(2), 0);
+          NS_MAX(defaultVariableFont->size - nsPresContext::CSSPointsToAppUnits(2), 0);
         break;
     }
 #endif
   } else {
     // In case somebody explicitly used -moz-use-system-font.
     systemFont = *defaultVariableFont;
   }
 
--- a/layout/style/nsStyleStruct.cpp
+++ b/layout/style/nsStyleStruct.cpp
@@ -118,17 +118,17 @@ static nsChangeHint CalcShadowDifference
 nsStyleFont::nsStyleFont(const nsFont& aFont, nsPresContext *aPresContext)
   : mFont(aFont),
     mGenericID(kGenericFont_NONE)
 {
   MOZ_COUNT_CTOR(nsStyleFont);
   mSize = mFont.size = nsStyleFont::ZoomText(aPresContext, mFont.size);
 #ifdef MOZ_MATHML
   mScriptUnconstrainedSize = mSize;
-  mScriptMinSize = aPresContext->TwipsToAppUnits(
+  mScriptMinSize = aPresContext->CSSTwipsToAppUnits(
       NS_POINTS_TO_TWIPS(NS_MATHML_DEFAULT_SCRIPT_MIN_SIZE_PT));
   mScriptLevel = 0;
   mScriptSizeMultiplier = NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER;
 #endif
 }
 
 nsStyleFont::nsStyleFont(const nsStyleFont& aSrc)
   : mFont(aSrc.mFont)
@@ -147,17 +147,17 @@ nsStyleFont::nsStyleFont(const nsStyleFo
 nsStyleFont::nsStyleFont(nsPresContext* aPresContext)
   : mFont(*(aPresContext->GetDefaultFont(kPresContext_DefaultVariableFont_ID))),
     mGenericID(kGenericFont_NONE)
 {
   MOZ_COUNT_CTOR(nsStyleFont);
   mSize = mFont.size = nsStyleFont::ZoomText(aPresContext, mFont.size);
 #ifdef MOZ_MATHML
   mScriptUnconstrainedSize = mSize;
-  mScriptMinSize = aPresContext->TwipsToAppUnits(
+  mScriptMinSize = aPresContext->CSSTwipsToAppUnits(
       NS_POINTS_TO_TWIPS(NS_MATHML_DEFAULT_SCRIPT_MIN_SIZE_PT));
   mScriptLevel = 0;
   mScriptSizeMultiplier = NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER;
 #endif
 }
 
 void* 
 nsStyleFont::operator new(size_t sz, nsPresContext* aContext) CPP_THROW_NEW {