[OS/2] Bug 435010: fix printing to PDF
authormozilla@weilbacher.org
Sat, 24 May 2008 09:21:54 -0700
changeset 15161 557aa356c998192658c532c0a0ccc694bde728c8
parent 15160 171eae00fb01f4be1753a05ccf39f0c3e9a744ec
child 15162 55a094f5dfcad14b9c90a6f2bd4e88ec5cafdbf5
push idunknown
push userunknown
push dateunknown
bugs435010
milestone1.9pre
[OS/2] Bug 435010: fix printing to PDF
widget/src/os2/nsDeviceContextSpecOS2.cpp
--- a/widget/src/os2/nsDeviceContextSpecOS2.cpp
+++ b/widget/src/os2/nsDeviceContextSpecOS2.cpp
@@ -431,16 +431,29 @@ NS_IMETHODIMP nsDeviceContextSpecOS2::Ge
     width = width * hDPI / 1440;
     height = height * vDPI / 1440;
 #ifdef debug_thebes_print
     printf("nsDeviceContextSpecOS2::GetSurfaceForPrinter(): %fx%fpx (res=%fx%f)\n"
            "  expected size: %7.2f MiB\n",
            width, height, hDPI, vDPI, width*height*4./1024./1024.);
 #endif
 
+    // perhaps restrict to usable area
+    // (this or scaling down won't help, we will just get more pages and still crash!)
+    if (DevQueryCaps(mPrintDC, CAPS_WIDTH, 1, &value) && width > (double)value)
+      width = (double)value;
+    if (DevQueryCaps(mPrintDC, CAPS_HEIGHT, 1, &value) && height > (double)value)
+      height = (double)value;
+
+#ifdef debug_thebes_print
+    printf("nsDeviceContextSpecOS2::GetSurfaceForPrinter(): capped? %fx%fpx (res=%fx%f)\n"
+           "  expected size: %7.2f MiB per page\n",
+           width, height, hDPI, vDPI, width*height*4./1024./1024.);
+#endif
+
     // Now pass the created DC into the thebes surface for printing.
     // It gets destroyed there.
     newSurface = new(std::nothrow)
       gfxOS2Surface(mPrintDC, gfxIntSize(int(ceil(width)), int(ceil(height))));
   }
   if (!newSurface) {
     *surface = nsnull;
     return NS_ERROR_FAILURE;
@@ -471,41 +484,60 @@ NS_IMETHODIMP nsDeviceContextSpecOS2::Be
                                                     PRInt32 aStartPage,
                                                     PRInt32 aEndPage)
 {
 #ifdef debug_thebes_print
   printf("nsDeviceContextSpecOS2[%#x]::BeginPrinting(%s, %s)\n", (unsigned)this,
          NS_LossyConvertUTF16toASCII(nsString(aTitle)).get(),
          NS_LossyConvertUTF16toASCII(nsString(aPrintToFileName)).get());
 #endif
+  // don't try to send device escapes for non-native output (like PDF)
+  PRInt16 outputFormat;
+  mPrintSettings->GetOutputFormat(&outputFormat);
+  if (outputFormat != nsIPrintSettings::kOutputFormatNative) {
+    return NS_OK;
+  }
+
   char *title = GetACPString(aTitle);
   const PSZ pszGenericDocName = "Mozilla Document";
   PSZ pszDocName = title ? title : pszGenericDocName;
   LONG lResult = DevEscape(mPrintDC, DEVESC_STARTDOC,
                            strlen(pszDocName) + 1, pszDocName,
                            (PLONG)NULL, (PBYTE)NULL);
   mPrintingStarted = PR_TRUE;
   if (title) {
     nsMemory::Free(title);
   }
 
   return lResult == DEV_OK ? NS_OK : NS_ERROR_GFX_PRINTER_STARTDOC;
 }
 
 NS_IMETHODIMP nsDeviceContextSpecOS2::EndDocument()
 {
+  PRInt16 outputFormat;
+  mPrintSettings->GetOutputFormat(&outputFormat);
+  if (outputFormat != nsIPrintSettings::kOutputFormatNative) {
+    return NS_OK;
+  }
+
   LONG lOutCount = 2;
   USHORT usJobID = 0;
   LONG lResult = DevEscape(mPrintDC, DEVESC_ENDDOC, 0L, (PBYTE)NULL,
                            &lOutCount, (PBYTE)&usJobID);
   return lResult == DEV_OK ? NS_OK : NS_ERROR_GFX_PRINTER_ENDDOC;
 }
 
 NS_IMETHODIMP nsDeviceContextSpecOS2::BeginPage()
 {
+  PRInt16 outputFormat;
+  mPrintSettings->GetOutputFormat(&outputFormat);
+  if (outputFormat != nsIPrintSettings::kOutputFormatNative) {
+    return NS_OK;
+  }
+
   if (mPrintingStarted) {
     // we don't want an extra page break at the start of the document
     mPrintingStarted = PR_FALSE;
     return NS_OK;
   }
   LONG lResult = DevEscape(mPrintDC, DEVESC_NEWFRAME, 0L, (PBYTE)NULL,
                            (PLONG)NULL, (PBYTE)NULL);
   return lResult == DEV_OK ? NS_OK : NS_ERROR_GFX_PRINTER_STARTPAGE;