Bug 1672529 - Honor page margins of zero when requested by an @page rule. r=jwatt, a=jcristau
authorJonathan Kew <jkew@mozilla.com>
Mon, 26 Oct 2020 13:02:49 +0000
changeset 618982 368d17ec7a2df69a349a0ca1e6a48c1368c5555e
parent 618981 c88c6f80d2d274d2563acc297b54685ab0f7023b
child 618983 86c6ac60afca3f420110dcfb855d0dbab801f88c
push id2439
push userjcristau@mozilla.com
push dateMon, 26 Oct 2020 13:38:53 +0000
treeherdermozilla-release@368d17ec7a2d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwatt, jcristau
bugs1672529, 1668406
milestone82.0.1
Bug 1672529 - Honor page margins of zero when requested by an @page rule. r=jwatt, a=jcristau Rebased to mozilla-release (needed because bug 1668406 is not present there).
layout/generic/nsPageFrame.cpp
--- a/layout/generic/nsPageFrame.cpp
+++ b/layout/generic/nsPageFrame.cpp
@@ -84,25 +84,36 @@ nsReflowStatus nsPageFrame::ReflowPageCo
                              LogicalSize(frame->GetWritingMode(), maxSize));
   kidReflowInput.mFlags.mIsTopOfPage = true;
   kidReflowInput.mFlags.mTableIsSplittable = true;
 
   // Use the margins given in the @page rule.
   // If a margin is 'auto', use the margin from the print settings for that
   // side.
   const auto& marginStyle = kidReflowInput.mStyleMargin->mMargin;
+  // We clamp to the paper's unwriteable margins to avoid clipping, *except*
+  // that we will respect a margin of zero if specified, assuming this means
+  // the document is intended to fit the paper size exactly, and the client is
+  // taking full responsibility for what happens around the edges.
   for (const auto side : mozilla::AllPhysicalSides()) {
     if (marginStyle.Get(side).IsAuto()) {
       mPageContentMargin.Side(side) =
           aPresContext->GetDefaultPageMargin().Side(side);
     } else {
-      nscoord unwriteable = nsPresContext::CSSTwipsToAppUnits(
-          mPD->mPrintSettings->GetUnwriteableMarginInTwips().Side(side));
-      mPageContentMargin.Side(side) = std::max(
-          kidReflowInput.ComputedPhysicalMargin().Side(side), unwriteable);
+      nscoord computed = kidReflowInput.ComputedPhysicalMargin().Side(side);
+      // Respecting a zero margin is particularly important when the client
+      // is PDF.js where the PDF already contains the margins.
+      if (computed == 0) {
+        mPageContentMargin.Side(side) = 0;
+      } else {
+        nscoord unwriteable = nsPresContext::CSSTwipsToAppUnits(
+            mPD->mPrintSettings->GetUnwriteableMarginInTwips().Side(side));
+        mPageContentMargin.Side(side) = std::max(
+            kidReflowInput.ComputedPhysicalMargin().Side(side), unwriteable);
+      }
     }
   }
 
   nscoord maxWidth = maxSize.width - mPageContentMargin.LeftRight() / scale;
   nscoord maxHeight;
   if (maxSize.height == NS_UNCONSTRAINEDSIZE) {
     maxHeight = NS_UNCONSTRAINEDSIZE;
   } else {