Make 'width', 'height', and 'aspect-ratio' features use the page size when printing. (Bug 466559) r+sr=bzbarsky a=blocking1.9.1+
authorL. David Baron <dbaron@dbaron.org>
Thu, 04 Dec 2008 08:09:53 -0800
changeset 22340 436c476d46615d3cd3469cb8d425aab83848cb86
parent 22339 f8b47378d8eab5e01424c8ad0f5465ef77c6ec5d
child 22341 1fb56a23792d7ca28f4a715b2efddc055b4586b2
push id3921
push userdbaron@mozilla.com
push dateThu, 04 Dec 2008 16:11:03 +0000
treeherdermozilla-central@2af723cd1680 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblocking1.9.1
bugs466559
milestone1.9.2a1pre
Make 'width', 'height', and 'aspect-ratio' features use the page size when printing. (Bug 466559) r+sr=bzbarsky a=blocking1.9.1+
layout/style/nsMediaFeatures.cpp
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -54,30 +54,43 @@ static const PRInt32 kOrientationKeyword
 };
 
 static const PRInt32 kScanKeywords[] = {
   eCSSKeyword_progressive,              NS_STYLE_SCAN_PROGRESSIVE,
   eCSSKeyword_interlace,                NS_STYLE_SCAN_INTERLACE,
   eCSSKeyword_UNKNOWN,                  -1
 };
 
+// A helper for three features below
+static nsSize
+GetSize(nsPresContext* aPresContext)
+{
+    nsSize size;
+    if (aPresContext->IsRootPaginatedDocument())
+        // We want the page size, including unprintable areas and margins.
+        size = aPresContext->GetPageSize();
+    else
+        size = aPresContext->GetVisibleArea().Size();
+    return size;
+}
+
 static nsresult
 GetWidth(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
-    nscoord width = aPresContext->GetVisibleArea().width;
-    float pixelWidth = aPresContext->AppUnitsToFloatCSSPixels(width);
+    nsSize size = GetSize(aPresContext);
+    float pixelWidth = aPresContext->AppUnitsToFloatCSSPixels(size.width);
     aResult.SetFloatValue(pixelWidth, eCSSUnit_Pixel);
     return NS_OK;
 }
 
 static nsresult
 GetHeight(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
-    nscoord height = aPresContext->GetVisibleArea().height;
-    float pixelHeight = aPresContext->AppUnitsToFloatCSSPixels(height);
+    nsSize size = GetSize(aPresContext);
+    float pixelHeight = aPresContext->AppUnitsToFloatCSSPixels(size.height);
     aResult.SetFloatValue(pixelHeight, eCSSUnit_Pixel);
     return NS_OK;
 }
 
 static nsIDeviceContext*
 GetDeviceContextFor(nsPresContext* aPresContext)
 {
   // Do this dance rather than aPresContext->DeviceContext() to get
@@ -88,38 +101,46 @@ GetDeviceContextFor(nsPresContext* aPres
     nsCOMPtr<nsIDocShell>(do_QueryInterface(
       nsCOMPtr<nsISupports>(aPresContext->GetContainer()))));
   if (!ctx) {
     ctx = aPresContext->DeviceContext();
   }
   return ctx;
 }
 
+// A helper for three features below.
+static nsSize
+GetDeviceSize(nsPresContext* aPresContext)
+{
+    nsSize size;
+    if (aPresContext->IsRootPaginatedDocument())
+        // We want the page size, including unprintable areas and margins.
+        // XXX The spec actually says we want the "page sheet size", but
+        // how is that different?
+        size = aPresContext->GetPageSize();
+    else
+        GetDeviceContextFor(aPresContext)->
+            GetDeviceSurfaceDimensions(size.width, size.height);
+    return size;
+}
+
 static nsresult
 GetDeviceWidth(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
-    // XXX: I'm not sure if this is really the right thing for print:
-    // do we want to include unprintable areas / page margins?
-    nsIDeviceContext *dx = GetDeviceContextFor(aPresContext);
-    nscoord width, height;
-    dx->GetDeviceSurfaceDimensions(width, height);
-    float pixelWidth = aPresContext->AppUnitsToFloatCSSPixels(width);
+    nsSize size = GetDeviceSize(aPresContext);
+    float pixelWidth = aPresContext->AppUnitsToFloatCSSPixels(size.width);
     aResult.SetFloatValue(pixelWidth, eCSSUnit_Pixel);
     return NS_OK;
 }
 
 static nsresult
 GetDeviceHeight(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
-    // XXX: I'm not sure if this is really the right thing for print:
-    // do we want to include unprintable areas / page margins?
-    nsIDeviceContext *dx = GetDeviceContextFor(aPresContext);
-    nscoord width, height;
-    dx->GetDeviceSurfaceDimensions(width, height);
-    float pixelHeight = aPresContext->AppUnitsToFloatCSSPixels(height);
+    nsSize size = GetDeviceSize(aPresContext);
+    float pixelHeight = aPresContext->AppUnitsToFloatCSSPixels(size.height);
     aResult.SetFloatValue(pixelHeight, eCSSUnit_Pixel);
     return NS_OK;
 }
 
 static nsresult
 GetOrientation(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
     nsSize size = aPresContext->GetVisibleArea().Size();
@@ -130,46 +151,40 @@ GetOrientation(nsPresContext* aPresConte
         // Per spec, square viewports should be 'portrait'
         orientation = NS_STYLE_ORIENTATION_PORTRAIT;
     }
 
     aResult.SetIntValue(orientation, eCSSUnit_Enumerated);
     return NS_OK;
 }
 
+// Helper for two features below
 static nsresult
-GetAspectRatio(nsPresContext* aPresContext, nsCSSValue& aResult)
+MakeArray(const nsSize& aSize, nsCSSValue& aResult)
 {
     nsRefPtr<nsCSSValue::Array> a = nsCSSValue::Array::Create(2);
     NS_ENSURE_TRUE(a, NS_ERROR_OUT_OF_MEMORY);
 
-    nsSize size = aPresContext->GetVisibleArea().Size();
-    a->Item(0).SetIntValue(size.width, eCSSUnit_Integer);
-    a->Item(1).SetIntValue(size.height, eCSSUnit_Integer);
+    a->Item(0).SetIntValue(aSize.width, eCSSUnit_Integer);
+    a->Item(1).SetIntValue(aSize.height, eCSSUnit_Integer);
 
     aResult.SetArrayValue(a, eCSSUnit_Array);
     return NS_OK;
 }
 
 static nsresult
+GetAspectRatio(nsPresContext* aPresContext, nsCSSValue& aResult)
+{
+    return MakeArray(GetSize(aPresContext), aResult);
+}
+
+static nsresult
 GetDeviceAspectRatio(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
-    nsRefPtr<nsCSSValue::Array> a = nsCSSValue::Array::Create(2);
-    NS_ENSURE_TRUE(a, NS_ERROR_OUT_OF_MEMORY);
-
-    // XXX: I'm not sure if this is really the right thing for print:
-    // do we want to include unprintable areas / page margins?
-    nsIDeviceContext *dx = GetDeviceContextFor(aPresContext);
-    nscoord width, height;
-    dx->GetDeviceSurfaceDimensions(width, height);
-    a->Item(0).SetIntValue(width, eCSSUnit_Integer);
-    a->Item(1).SetIntValue(height, eCSSUnit_Integer);
-
-    aResult.SetArrayValue(a, eCSSUnit_Array);
-    return NS_OK;
+    return MakeArray(GetDeviceSize(aPresContext), aResult);
 }
 
 
 static nsresult
 GetColor(nsPresContext* aPresContext, nsCSSValue& aResult)
 {
     // FIXME:  This implementation is bogus.  nsThebesDeviceContext
     // doesn't provide reliable information (should be fixed in bug