]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
libcupsfilters: Let imageto...() with print-scaling=none center correctly
authorTill Kamppeter <till.kamppeter@gmail.com>
Thu, 17 Feb 2022 13:41:39 +0000 (14:41 +0100)
committerTill Kamppeter <till.kamppeter@gmail.com>
Thu, 17 Feb 2022 13:41:39 +0000 (14:41 +0100)
In the imageto...() filter functions 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.

cupsfilters/imagetopdf.c
cupsfilters/imagetops.c
cupsfilters/imagetoraster.c

index d16c54eeb43e4ec1b8af7b86ea6e046f3511c6d2..7542ed108ff447d1f519bceb4f5318b37265956d 100644 (file)
@@ -1277,6 +1277,15 @@ imagetopdf(int inputfd,         /* I - File descriptor input stream */
   }
   if(fillprint||cropfit)
   {
+    /* For cropfit do the math without the unprintable margins to get correct
+       centering */
+    if (cropfit)
+    {
+      doc.PageBottom = 0.0;
+      doc.PageTop = doc.PageLength;
+      doc.PageLeft = 0.0;
+      doc.PageRight = doc.PageWidth;
+    }
     float w = (float)cupsImageGetWidth(doc.img);
     float h = (float)cupsImageGetHeight(doc.img);
     float pw = doc.PageRight-doc.PageLeft;
@@ -1346,25 +1355,17 @@ imagetopdf(int inputfd,         /* I - File descriptor input stream */
       doc.img = img2;
       if(flag==4)
       {
-       doc.PageBottom += (doc.PageTop - doc.PageBottom -
-                          final_w * 72.0 / doc.img->xppi) / 2;
-       doc.PageTop = doc.PageBottom +
-                     final_w * 72.0 / doc.img->xppi;
-       doc.PageLeft += (doc.PageRight - doc.PageLeft -
-                        final_h * 72.0 / doc.img->yppi) / 2;
-       doc.PageRight = doc.PageLeft +
-                       final_h * 72.0 / doc.img->yppi;
+       doc.PageBottom += (doc.PageLength - final_w * 72.0 / doc.img->xppi) / 2;
+       doc.PageTop = doc.PageBottom + final_w * 72.0 / doc.img->xppi;
+       doc.PageLeft += (doc.PageWidth - final_h * 72.0 / doc.img->yppi) / 2;
+       doc.PageRight = doc.PageLeft + final_h * 72.0 / doc.img->yppi;
       }
       else
       {
-       doc.PageBottom += (doc.PageTop - doc.PageBottom -
-                          final_h * 72.0 / doc.img->yppi) / 2;
-       doc.PageTop = doc.PageBottom +
-                     final_h * 72.0 / doc.img->yppi;
-       doc.PageLeft += (doc.PageRight - doc.PageLeft -
-                        final_w * 72.0 / doc.img->xppi) / 2;
-       doc.PageRight = doc.PageLeft +
-                       final_w * 72.0 / doc.img->xppi;
+       doc.PageBottom += (doc.PageLength - final_h * 72.0 / doc.img->yppi) / 2;
+       doc.PageTop = doc.PageBottom + final_h * 72.0 / doc.img->yppi;
+       doc.PageLeft += (doc.PageWidth - final_w * 72.0 / doc.img->xppi) / 2;
+       doc.PageRight = doc.PageLeft + final_w * 72.0 / doc.img->xppi;
       }
       if(doc.PageBottom<0) doc.PageBottom = 0;
       if(doc.PageLeft<0) doc.PageLeft = 0;
index 01044017a306bec7413b10d084a5aff8dcf27418..2ab40a144edaa4dbe0181f8626c305801cd911c1 100644 (file)
@@ -545,6 +545,17 @@ imagetops(int inputfd,         /* I - File descriptor input stream */
     }
     if (fillprint || cropfit)
     {
+      /* For cropfit do the math without the unprintable margins to get correct
+         centering */
+      if (cropfit)
+      {
+       pw = doc.PageWidth;
+       ph = doc.PageLength;
+       doc.PageBottom = 0.0;
+       doc.PageTop = doc.PageLength;
+       doc.PageLeft = 0.0;
+       doc.PageRight = doc.PageWidth;
+      }
       tempOrientation = doc.Orientation;
       int flag = 3;
       if ((val = cupsGetOption("orientation-requested", num_options,
@@ -608,25 +619,17 @@ imagetops(int inputfd,         /* I - File descriptor input stream */
        img = img2;
        if (flag == 4)
        {
-         doc.PageBottom += (doc.PageTop - doc.PageBottom -
-                            final_w * 72.0 / img->xppi) / 2;
-         doc.PageTop = doc.PageBottom +
-                       final_w * 72.0 / img->xppi;
-         doc.PageLeft += (doc.PageRight - doc.PageLeft -
-                          final_h * 72.0 / img->yppi) / 2;
-         doc.PageRight = doc.PageLeft +
-                         final_h * 72.0 / img->yppi;
+         doc.PageBottom += (doc.PageLength - final_w * 72.0 / img->xppi) / 2;
+         doc.PageTop = doc.PageBottom + final_w * 72.0 / img->xppi;
+         doc.PageLeft += (doc.PageWidth - final_h * 72.0 / img->yppi) / 2;
+         doc.PageRight = doc.PageLeft + final_h * 72.0 / img->yppi;
        }
        else
        {
-         doc.PageBottom += (doc.PageTop - doc.PageBottom -
-                            final_h * 72.0 / img->yppi) / 2;
-         doc.PageTop = doc.PageBottom +
-                       final_h * 72.0 / img->yppi;
-         doc.PageLeft += (doc.PageRight - doc.PageLeft -
-                          final_w * 72.0 / img->xppi) / 2;
-         doc.PageRight = doc.PageLeft +
-                         final_w * 72.0 / img->xppi;
+         doc.PageBottom += (doc.PageLength - final_h * 72.0 / img->yppi) / 2;
+         doc.PageTop = doc.PageBottom + final_h * 72.0 / img->yppi;
+         doc.PageLeft += (doc.PageWidth - final_w * 72.0 / img->xppi) / 2;
+         doc.PageRight = doc.PageLeft + final_w * 72.0 / img->xppi;
        }
        if (doc.PageBottom < 0) doc.PageBottom = 0;
        if (doc.PageLeft < 0) doc.PageLeft = 0;
index d194e01ddf73e0511d2e96269962f900e5d03f0e..5405285002abe10c98b5a613eee0e703ccfb7235 100644 (file)
@@ -1007,8 +1007,10 @@ imagetoraster(int inputfd,         /* I - File descriptor input stream */
     {
       float w = (float)cupsImageGetWidth(img);
       float h = (float)cupsImageGetHeight(img);
-      float pw = doc.PageRight-doc.PageLeft;
-      float ph = doc.PageTop-doc.PageBottom;
+      /* For cropfit do the math without the unprintable margins to get correct
+        centering, for fillprint, fill the printable area */
+      float pw = (cropfit ? doc.PageWidth : doc.PageRight-doc.PageLeft);
+      float ph = (cropfit ? doc.PageLength : doc.PageTop-doc.PageBottom);
       const char *val;
       int tempOrientation = doc.Orientation;
       int flag =3;
@@ -1074,33 +1076,77 @@ imagetoraster(int inputfd,         /* I - File descriptor input stream */
        float posw=(w-final_w)/2,posh=(h-final_h)/2;
         posw = (1+doc.XPosition)*posw;
        posh = (1-doc.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)
-        {
-         doc.PageBottom += (doc.PageTop - doc.PageBottom -
-                            final_w * 72.0 / img->xppi) / 2;
-         doc.PageTop = doc.PageBottom +
-                       final_w * 72.0 / img->xppi;
-         doc.PageLeft += (doc.PageRight - doc.PageLeft -
-                          final_h * 72.0 / img->yppi) / 2;
-         doc.PageRight = doc.PageLeft +
-                         final_h * 72.0 / img->yppi;
+       {
+         float margin, cutoff;
+         margin = (doc.PageLength - final_w * 72.0 / img->xppi) / 2;
+         if (margin >= doc.PageBottom)
+           doc.PageBottom = margin;
+         else
+         {
+           cutoff = (doc.PageBottom - margin) * img->xppi / 72.0;
+           final_w -= cutoff;
+           posw += cutoff;
+         }
+         margin = doc.PageBottom + final_w * 72.0 / img->xppi;
+         if (margin <= doc.PageTop)
+           doc.PageTop = margin;
+         else
+           final_w -= (margin - doc.PageTop) * img->xppi / 72.0;
+         margin = (doc.PageWidth - final_h * 72.0 / img->yppi) / 2;
+         if (margin >= doc.PageLeft)
+           doc.PageLeft = margin;
+         else
+         {
+           cutoff = (doc.PageLeft - margin) * img->yppi / 72.0;
+           final_h -= cutoff;
+           posh += cutoff;
+         }
+         margin = doc.PageLeft + final_h * 72.0 / img->yppi;
+         if (margin <= doc.PageRight)
+           doc.PageRight = margin;
+         else
+           final_h -= (margin - doc.PageRight) * img->yppi / 72.0;
        }
        else
        {
-         doc.PageBottom += (doc.PageTop - doc.PageBottom -
-                            final_h * 72.0 / img->yppi) / 2;
-         doc.PageTop = doc.PageBottom +
-                       final_h * 72.0 / img->yppi;
-         doc.PageLeft += (doc.PageRight - doc.PageLeft -
-                          final_w * 72.0 / img->xppi) / 2;
-         doc.PageRight = doc.PageLeft +
-                         final_w * 72.0 / img->xppi;
+         float margin, cutoff;
+         margin = (doc.PageLength - final_h * 72.0 / img->yppi) / 2;
+         if (margin >= doc.PageBottom)
+           doc.PageBottom = margin;
+         else
+         {
+           cutoff = (doc.PageBottom - margin) * img->yppi / 72.0;
+           final_h -= cutoff;
+           posh += cutoff;
+         }
+         margin = doc.PageBottom + final_h * 72.0 / img->yppi;
+         if (margin <= doc.PageTop)
+           doc.PageTop = margin;
+         else
+           final_h -= (margin - doc.PageTop) * img->yppi / 72.0;
+         margin = (doc.PageWidth - final_w * 72.0 / img->xppi) / 2;
+         if (margin >= doc.PageLeft)
+           doc.PageLeft = margin;
+         else
+         {
+           cutoff = (doc.PageLeft - margin) * img->xppi / 72.0;
+           final_w -= cutoff;
+           posw += cutoff;
+         }
+         margin = doc.PageLeft + final_w * 72.0 / img->xppi;
+         if (margin <= doc.PageRight)
+           doc.PageRight = margin;
+         else
+           final_w -= (margin - doc.PageRight) * img->xppi / 72.0;
        }
        if(doc.PageBottom<0) doc.PageBottom = 0;
        if(doc.PageLeft<0) doc.PageLeft = 0;
+       cups_image_t *img2 = cupsImageCrop(img,posw,posh,final_w,final_h);
+       cupsImageClose(img);
+       img = img2;
       }        
     }
   }