]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
imagetoraster, imagetopdf: With print-scaling=none center image correctly
authorTill Kamppeter <till.kamppeter@gmail.com>
Thu, 17 Feb 2022 16:44:57 +0000 (17:44 +0100)
committerTill Kamppeter <till.kamppeter@gmail.com>
Thu, 17 Feb 2022 16:44:57 +0000 (17:44 +0100)
In the imageto... filters with print-scaling=none do the math for
centering the image on the page without the unprintable margins, so
that the image gets correctly centered relative to the page, not to
the printable area of the page.

As in imagetoraster we have to pass on a bitmap of the printable area
for CUPS Raster. we correct for the margins after calculating image
size and position without margins.

(manually backported from commit 0a1c76fc384615c8d22c171666a3157295bb789c)

filter/imagetopdf.c
filter/imagetoraster.c

index 0ebf0980791df97b6b6d072d13fc903d71d4ca9c..8884b68d7c6e144afb23dd44ab3f90c1d563791c 100644 (file)
@@ -1059,6 +1059,15 @@ main(int  argc,                          /* I - Number of command-line arguments */
   }
   if(fillprint||cropfit)
   {
+    /* For cropfit do the math without the unprintable margins to get correct
+       centering */
+    if (cropfit)
+    {
+      PageBottom = 0.0;
+      PageTop = PageLength;
+      PageLeft = 0.0;
+      PageRight = PageWidth;
+    }
     float w = (float)cupsImageGetWidth(img);
     float h = (float)cupsImageGetHeight(img);
     float pw = PageRight-PageLeft;
@@ -1129,25 +1138,17 @@ main(int  argc,                         /* I - Number of command-line arguments */
       img = img2;
       if(flag==4)
       {
-       PageBottom += (PageTop - PageBottom -
-                      final_w * 72.0 / img->xppi) / 2;
-       PageTop = PageBottom +
-                 final_w * 72.0 / img->xppi;
-       PageLeft += (PageRight - PageLeft -
-                    final_h * 72.0 / img->yppi) / 2;
-       PageRight = PageLeft +
-                   final_h * 72.0 / img->yppi;
+       PageBottom += (PageLength - final_w * 72.0 / img->xppi) / 2;
+       PageTop = PageBottom + final_w * 72.0 / img->xppi;
+       PageLeft += (PageWidth - final_h * 72.0 / img->yppi) / 2;
+       PageRight = PageLeft + final_h * 72.0 / img->yppi;
       }
       else
       {
-       PageBottom += (PageTop - PageBottom -
-                      final_h * 72.0 / img->yppi) / 2;
-       PageTop = PageBottom +
-                 final_h * 72.0 / img->yppi;
-       PageLeft += (PageRight - PageLeft -
-                    final_w * 72.0 / img->xppi) / 2;
-       PageRight = PageLeft +
-                    final_w * 72.0 / img->xppi;
+       PageBottom += (PageLength - final_h * 72.0 / img->yppi) / 2;
+       PageTop = PageBottom + final_h * 72.0 / img->yppi;
+       PageLeft += (PageWidth - final_w * 72.0 / img->xppi) / 2;
+       PageRight = PageLeft + final_w * 72.0 / img->xppi;
       }
       if(PageBottom<0) PageBottom = 0;
       if(PageLeft<0) PageLeft = 0;
index a63ac1ce8c84cc048268c37da1bcf504ca5e33eb..4c3bf069e8e98bc7515ceb38c308898ae9329a24 100644 (file)
@@ -734,8 +734,10 @@ main(int  argc,                            /* I - Number of command-line arguments */
     {
       float w = (float)cupsImageGetWidth(img);
       float h = (float)cupsImageGetHeight(img);
-      float pw = PageRight-PageLeft;
-      float ph = PageTop-PageBottom;
+      /* For cropfit do the math without the unprintable margins to get correct
+        centering, for fillprint, fill the printable area */
+      float pw = (cropfit ? PageWidth : PageRight-PageLeft);
+      float ph = (cropfit ? PageLength : PageTop-PageBottom);
       const char *val;
       int tempOrientation = Orientation;
       int flag =3;
@@ -798,35 +800,79 @@ main(int  argc,                           /* I - Number of command-line arguments */
         if (h > ph * img->yppi / 72.0)
           final_h = ph * img->yppi / 72.0;
        float posw=(w-final_w)/2,posh=(h-final_h)/2;
-       posw = (1+XPosition)*posw;
+        posw = (1+XPosition)*posw;
        posh = (1-YPosition)*posh;
-       cups_image_t *img2 = cupsImageCrop(img,posw,posh,final_w,final_h);
-       cupsImageClose(img);
-       img = img2;
+       /* Check whether the unprintable margins hide away a part of the image,
+          if so, correct the image cut */
        if(flag==4)
-        {
-         PageBottom += (PageTop - PageBottom -
-                        final_w * 72.0 / img->xppi) / 2;
-         PageTop = PageBottom +
-                   final_w * 72.0 / img->xppi;
-         PageLeft += (PageRight - PageLeft -
-                      final_h * 72.0 / img->yppi) / 2;
-         PageRight = PageLeft +
-                     final_h * 72.0 / img->yppi;
+       {
+         float margin, cutoff;
+         margin = (PageLength - final_w * 72.0 / img->xppi) / 2;
+         if (margin >= PageBottom)
+           PageBottom = margin;
+         else
+         {
+           cutoff = (PageBottom - margin) * img->xppi / 72.0;
+           final_w -= cutoff;
+           posw += cutoff;
+         }
+         margin = PageBottom + final_w * 72.0 / img->xppi;
+         if (margin <= PageTop)
+           PageTop = margin;
+         else
+           final_w -= (margin - PageTop) * img->xppi / 72.0;
+         margin = (PageWidth - final_h * 72.0 / img->yppi) / 2;
+         if (margin >= PageLeft)
+           PageLeft = margin;
+         else
+         {
+           cutoff = (PageLeft - margin) * img->yppi / 72.0;
+           final_h -= cutoff;
+           posh += cutoff;
+         }
+         margin = PageLeft + final_h * 72.0 / img->yppi;
+         if (margin <= PageRight)
+           PageRight = margin;
+         else
+           final_h -= (margin - PageRight) * img->yppi / 72.0;
        }
        else
        {
-         PageBottom += (PageTop - PageBottom -
-                        final_h * 72.0 / img->yppi) / 2;
-         PageTop = PageBottom +
-                   final_h * 72.0 / img->yppi;
-         PageLeft += (PageRight - PageLeft -
-                      final_w * 72.0 / img->xppi) / 2;
-         PageRight = PageLeft +
-                     final_w * 72.0 / img->xppi;
+         float margin, cutoff;
+         margin = (PageLength - final_h * 72.0 / img->yppi) / 2;
+         if (margin >= PageBottom)
+           PageBottom = margin;
+         else
+         {
+           cutoff = (PageBottom - margin) * img->yppi / 72.0;
+           final_h -= cutoff;
+           posh += cutoff;
+         }
+         margin = PageBottom + final_h * 72.0 / img->yppi;
+         if (margin <= PageTop)
+           PageTop = margin;
+         else
+           final_h -= (margin - PageTop) * img->yppi / 72.0;
+         margin = (PageWidth - final_w * 72.0 / img->xppi) / 2;
+         if (margin >= PageLeft)
+           PageLeft = margin;
+         else
+         {
+           cutoff = (PageLeft - margin) * img->xppi / 72.0;
+           final_w -= cutoff;
+           posw += cutoff;
+         }
+         margin = PageLeft + final_w * 72.0 / img->xppi;
+         if (margin <= PageRight)
+           PageRight = margin;
+         else
+           final_w -= (margin - PageRight) * img->xppi / 72.0;
        }
        if(PageBottom<0) PageBottom = 0;
        if(PageLeft<0) PageLeft = 0;
+       cups_image_t *img2 = cupsImageCrop(img,posw,posh,final_w,final_h);
+       cupsImageClose(img);
+       img = img2;
       }
     }
   }