Bug 1315121 - OS X Remote printing (print_via_parent) scaled prints don't fill page; r=mconley a=jcristau
authorHaik Aftandilian <haftandilian@mozilla.com>
Thu, 17 Nov 2016 15:29:08 -0800
changeset 352650 84a91fbd335a5c562c67a48372cadbd83496f8a0
parent 352649 55b599ca3530c8286f6ad15405d99d2bcaecfbd1
child 352651 c916ea845d3653d33cfcbcb9441bd4242c7b37de
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley, jcristau
bugs1315121
milestone52.0a2
Bug 1315121 - OS X Remote printing (print_via_parent) scaled prints don't fill page; r=mconley a=jcristau Send the size from PMGetAdjustedPaperRect to the child to use as the page size. Add an Mac implementation of GetEffectivePageSize that returns the adjusted dimensions. Add an Mac implementation of SetPaperWidth and SetPaperHeight so that adjusted dimensions can be manually set for reftest-print. MozReview-Commit-ID: GgTFgBzkxTy
embedding/components/printingui/ipc/PPrintingTypes.ipdlh
widget/cocoa/nsPrintDialogX.mm
widget/cocoa/nsPrintOptionsX.mm
widget/cocoa/nsPrintSettingsX.h
widget/cocoa/nsPrintSettingsX.mm
--- a/embedding/components/printingui/ipc/PPrintingTypes.ipdlh
+++ b/embedding/components/printingui/ipc/PPrintingTypes.ipdlh
@@ -113,12 +113,14 @@ struct PrintData {
    */
   float scalingFactor;
   /*
    * Scaling factor for converting from OS X native paper size
    * units to inches.
    */
   float widthScale;
   float heightScale;
+  double adjustedPaperWidth;
+  double adjustedPaperHeight;
 };
 
 } // namespace embedding
 } // namespace mozilla
--- a/widget/cocoa/nsPrintDialogX.mm
+++ b/widget/cocoa/nsPrintDialogX.mm
@@ -140,17 +140,28 @@ nsPrintDialogServiceX::Show(nsPIDOMWindo
   bool isShrinkToFitChecked;
   settingsX->GetShrinkToFit(&isShrinkToFitChecked);
   if (isShrinkToFitChecked) {
     NSMutableDictionary* dict = [copy dictionary];
     if (dict) {
       [dict setObject: [NSNumber numberWithFloat: 1]
                forKey: NSPrintScalingFactor];
     }
+    // Set the scaling factor to 100% in the NSPrintInfo
+    // object so that it will not affect the paper size
+    // retrieved from the PMPageFormat routines.
+    [copy setScalingFactor:1.0];
+  } else {
+    aSettings->SetScaling([copy scalingFactor]);
   }
+
+  // Set the adjusted paper size now that we've updated
+  // the scaling factor.
+  settingsX->InitAdjustedPaperSize();
+
   [copy release];
 
   int16_t pageRange;
   aSettings->GetPrintRange(&pageRange);
   if (pageRange != nsIPrintSettings::kRangeSelection) {
     PMPrintSettings nativePrintSettings = settingsX->GetPMPrintSettings();
     UInt32 firstPage, lastPage;
     OSStatus status = ::PMGetFirstPage(nativePrintSettings, &firstPage);
--- a/widget/cocoa/nsPrintOptionsX.mm
+++ b/widget/cocoa/nsPrintOptionsX.mm
@@ -66,16 +66,21 @@ nsPrintOptionsX::SerializeToPrintData(ns
     return NS_ERROR_FAILURE;
   }
 
   NSPrintInfo* printInfo = settingsX->GetCocoaPrintInfo();
   if (NS_WARN_IF(!printInfo)) {
     return NS_ERROR_FAILURE;
   }
 
+  double adjustedWidth, adjustedHeight;
+  settingsX->GetAdjustedPaperSize(&adjustedWidth, &adjustedHeight);
+  data->adjustedPaperWidth() = adjustedWidth;
+  data->adjustedPaperHeight() = adjustedHeight;
+
   NSDictionary* dict = [printInfo dictionary];
   if (NS_WARN_IF(!dict)) {
     return NS_ERROR_FAILURE;
   }
 
   NSString* printerName = [dict objectForKey: NSPrintPrinterName];
   if (printerName) {
     nsCocoaUtils::GetStringForNSString(printerName, data->printerName());
@@ -280,16 +285,19 @@ nsPrintOptionsX::DeserializeToPrintSetti
   if (NS_WARN_IF(!newPrintInfo)) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   // And now swap in the new NSPrintInfo we've just populated.
   settingsX->SetCocoaPrintInfo(newPrintInfo);
   [newPrintInfo release];
 
+  settingsX->SetAdjustedPaperSize(data.adjustedPaperWidth(),
+                                  data.adjustedPaperHeight());
+
   return NS_OK;
 }
 
 nsresult
 nsPrintOptionsX::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName, uint32_t aFlags)
 {
   nsresult rv;
   
--- a/widget/cocoa/nsPrintSettingsX.h
+++ b/widget/cocoa/nsPrintSettingsX.h
@@ -20,29 +20,45 @@ public:
   NS_DECL_ISUPPORTS_INHERITED
 
   nsPrintSettingsX();
   nsresult Init();
   NSPrintInfo* GetCocoaPrintInfo() { return mPrintInfo; }
   void SetCocoaPrintInfo(NSPrintInfo* aPrintInfo);
   virtual nsresult ReadPageFormatFromPrefs();
   virtual nsresult WritePageFormatToPrefs();
+  virtual nsresult GetEffectivePageSize(double *aWidth,
+      double *aHeight) override;
+
+  // In addition to setting the paper width and height, these
+  // overrides set the adjusted width and height returned from
+  // GetEffectivePageSize. This is needed when a paper size is
+  // set manually without using a print dialog a la reftest-print.
+  virtual nsresult SetPaperWidth(double aPaperWidth) override;
+  virtual nsresult SetPaperHeight(double aPaperWidth) override;
 
   PMPrintSettings GetPMPrintSettings();
   PMPrintSession GetPMPrintSession();
   PMPageFormat GetPMPageFormat();
   void SetPMPageFormat(PMPageFormat aPageFormat);
 
   // Re-initialize mUnwriteableMargin with values from mPageFormat.
   // Should be called whenever mPageFormat is initialized or overwritten.
   nsresult InitUnwriteableMargin();
 
+  // Re-initialize mAdjustedPaper{Width,Height} with values from mPageFormat.
+  // Should be called whenever mPageFormat is initialized or overwritten.
+  nsresult InitAdjustedPaperSize();
+
   void SetInchesScale(float aWidthScale, float aHeightScale);
   void GetInchesScale(float *aWidthScale, float *aHeightScale);
 
+  void SetAdjustedPaperSize(double aWidth, double aHeight);
+  void GetAdjustedPaperSize(double *aWidth, double *aHeight);
+
 protected:
   virtual ~nsPrintSettingsX();
 
   nsPrintSettingsX(const nsPrintSettingsX& src);
   nsPrintSettingsX& operator=(const nsPrintSettingsX& rhs);
 
   nsresult _Clone(nsIPrintSettings **_retval) override;
   nsresult _Assign(nsIPrintSettings *aPS) override;
@@ -52,13 +68,15 @@ protected:
   OSStatus CreateDefaultPrintSettings(PMPrintSession aSession, PMPrintSettings& outSettings);
 
   NSPrintInfo* mPrintInfo;
 
   // Scaling factors used to convert the NSPrintInfo
   // paper size units to inches
   float mWidthScale;
   float mHeightScale;
+  double mAdjustedPaperWidth;
+  double mAdjustedPaperHeight;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(nsPrintSettingsX, NS_PRINTSETTINGSX_IID)
 
 #endif // nsPrintSettingsX_h_
--- a/widget/cocoa/nsPrintSettingsX.mm
+++ b/widget/cocoa/nsPrintSettingsX.mm
@@ -63,16 +63,17 @@ nsPrintSettingsX& nsPrintSettingsX::oper
   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this);
 }
 
 nsresult nsPrintSettingsX::Init()
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
   InitUnwriteableMargin();
+  InitAdjustedPaperSize();
 
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
 // Should be called whenever the page format changes.
 NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin()
@@ -89,16 +90,33 @@ NS_IMETHODIMP nsPrintSettingsX::InitUnwr
   mUnwriteableMargin.bottom = NS_POINTS_TO_INT_TWIPS(paperMargin.bottom);
   mUnwriteableMargin.right  = NS_POINTS_TO_INT_TWIPS(paperMargin.right);
 
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;  
 }
 
+NS_IMETHODIMP nsPrintSettingsX::InitAdjustedPaperSize()
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  PMPageFormat pageFormat = GetPMPageFormat();
+
+  PMRect paperRect;
+  ::PMGetAdjustedPaperRect(pageFormat, &paperRect);
+
+  mAdjustedPaperWidth = paperRect.right - paperRect.left;
+  mAdjustedPaperHeight = paperRect.bottom - paperRect.top;
+
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
 void
 nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo)
 {
   if (mPrintInfo != aPrintInfo) {
     [mPrintInfo release];
     mPrintInfo = [aPrintInfo retain];
   }
 }
@@ -201,18 +219,54 @@ nsPrintSettingsX::SetPMPageFormat(PMPage
   PMPageFormat oldPageFormat = GetPMPageFormat();
   ::PMCopyPageFormat(aPageFormat, oldPageFormat);
   [mPrintInfo updateFromPMPageFormat];
 }
 
 void
 nsPrintSettingsX::SetInchesScale(float aWidthScale, float aHeightScale)
 {
-  mWidthScale = aWidthScale;
-  mHeightScale = aHeightScale;
+  if (aWidthScale > 0 && aHeightScale > 0) {
+    mWidthScale = aWidthScale;
+    mHeightScale = aHeightScale;
+  }
 }
 
 void
 nsPrintSettingsX::GetInchesScale(float *aWidthScale, float *aHeightScale)
 {
   *aWidthScale = mWidthScale;
   *aHeightScale = mHeightScale;
 }
+
+NS_IMETHODIMP nsPrintSettingsX::SetPaperWidth(double aPaperWidth)
+{
+  mPaperWidth = aPaperWidth;
+  mAdjustedPaperWidth = aPaperWidth * mWidthScale;
+  return NS_OK;
+}
+
+NS_IMETHODIMP nsPrintSettingsX::SetPaperHeight(double aPaperHeight)
+{
+  mPaperHeight = aPaperHeight;
+  mAdjustedPaperHeight = aPaperHeight * mHeightScale;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsPrintSettingsX::GetEffectivePageSize(double *aWidth, double *aHeight)
+{
+  *aWidth  = NS_INCHES_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
+  *aHeight = NS_INCHES_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
+  return NS_OK;
+}
+
+void nsPrintSettingsX::SetAdjustedPaperSize(double aWidth, double aHeight)
+{
+  mAdjustedPaperWidth = aWidth;
+  mAdjustedPaperHeight = aHeight;
+}
+
+void nsPrintSettingsX::GetAdjustedPaperSize(double *aWidth, double *aHeight)
+{
+  *aWidth = mAdjustedPaperWidth;
+  *aHeight = mAdjustedPaperHeight;
+}