]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
libcupsfilters: Fixed pdftopdf() for asymmetric margins
authorTill Kamppeter <till.kamppeter@gmail.com>
Sun, 2 Jan 2022 03:49:32 +0000 (00:49 -0300)
committerTill Kamppeter <till.kamppeter@gmail.com>
Sun, 2 Jan 2022 04:14:35 +0000 (01:14 -0300)
On some printers the left unprintable margin does not equal the right
one and/or the top margin does not equal the bottom one. pdftopdf()
did not always place the input pages correctly on the output sheets,
having de-centered printouts in crop-to-fit mode
("print-scaling=none") or missing content at the borders in
scale-to-fit mode ("print-scaling=fit").

Probably pdftopdf() did not get (well) tested with such printers.

This commit addresses these issues:

- In crop-to-fit mode we set the margins of the output page to
  all-zero befor starting all the math. This way we assure pages get
  centered and the correct (asymmetric) crop is done by the following
  filters (driver) or the printer (by simply ignoring the pixels
  falling into the margins). Alao for N-up here the input pages are
  filling exactly the halves, quarters, ... of the sheet, allowing
  easy mounting of booklets. pdftopdf() is producing an output PDF
  with pages of the size of the output sheet (not of its printable
  area) anyway and it does not need to passon pages where the
  unprintable margins are actually white.

- Set the auto-fit bit for crop-to-fill mode ("print-scaling=fill"),
  to assure that the pages cropped to the aspect ratio of the
  printable area of the output page are correctly scaled into that
  area (this got probably accidentally dropped in an earlier commit).

- Corrected the margin offsets at the coordinate origin for N-up
  printing in Landscape orientation (2-up, 6-up, 8-up, ...), so that
  they work with asymmetric margins.

- Let the crop() method for a PDF page not create a page smaller than
  the destination page. When "cropping" a smaller input page, set its
  crop box so that it gets centered on the output page. This makes the
  input pages always printed in original size in crop mode, even if
  they are smaller than the output page.

cupsfilters/pdftopdf/pdftopdf_processor.cc
cupsfilters/pdftopdf/qpdf_pdftopdf_processor.cc

index 7b5d2f7149a5f778a8ffdcd62aa708521b52f368..cc006bf453eb760677307302bfba6bbba127b08b 100644 (file)
@@ -260,6 +260,19 @@ bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters &param,pdftop
                                    "Do not scale, center, crop if needed" :
                                    "Not defined, should never happen"))));
 
+  // In Crop mode we do not scale the original document, it should keep the
+  // exact same size. With N-Up it should be scaled to fit exacly the halves,
+  // quarters, ... of the sheet, regardless of unprintable margins.
+  // Therefore we remove the unprintable margins to do all the math without
+  // them.
+  if (param.cropfit)
+  {
+    param.page.left = 0;
+    param.page.bottom = 0;
+    param.page.right = param.page.width;
+    param.page.top = param.page.height;
+  }
+
   if (param.fillprint || param.cropfit)
   {
     for (int i = 0; i < (int)pages.size(); i ++)
@@ -272,6 +285,8 @@ bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters &param,pdftop
       page->crop(param.page, orientation, param.xpos, param.ypos,
                 !param.cropfit, doc);
     }
+    if (param.fillprint)
+      param.fitplot = true;
   }
 
   std::shared_ptr<PDFTOPDF_PageHandle> curpage;
@@ -304,8 +319,8 @@ bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters &param,pdftop
     // TODO? better
     if (param.nup.nupX != 1 || param.nup.nupY != 1 || param.fitplot)
     {
-      xpos = param.page.bottom;
-      ypos = param.page.width - param.page.right;
+      xpos = param.page.height - param.page.top;
+      ypos = param.page.left;
     }
     std::swap(param.page.width, param.page.height);
     std::swap(param.nup.width, param.nup.height);
index a443e9a4889760b0567ebd64aab1cf7e8ba87daf..f88bb272800f65d6e179b2240281d27cd007a44b 100644 (file)
@@ -210,9 +210,10 @@ Rotation QPDF_PDFTOPDF_PageHandle::crop(const PageRect &cropRect,Rotation orient
       final_h = height;
     }
   }
-  else{
-    final_w = std::min(width,pageWidth);
-    final_h = std::min(height,pageHeight);
+  else
+  {
+    final_w = pageWidth;
+    final_h = pageHeight;
   }
   if (doc->logfunc) doc->logfunc(doc->logdata, FILTER_LOGLEVEL_DEBUG,
              "pdftopdf: After Cropping: %lf %lf %lf %lf",