]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
Add support for print-scaling option. 118/head
authorDheeraj <dhirajyadav135@gmail.com>
Tue, 7 May 2019 19:45:47 +0000 (01:15 +0530)
committerDheeraj <dhirajyadav135@gmail.com>
Mon, 13 May 2019 13:43:01 +0000 (19:13 +0530)
Support for print-scaling feature has been added to imagetoraster,imagetopdf and pdftopdf filters.

filter/imagetopdf.c
filter/imagetoraster.c
filter/pdftopdf/pdftopdf.cc
filter/pdftopdf/pdftopdf_processor.cc
filter/pdftopdf/pdftopdf_processor.h

index 859888d2e5a31157071717f7c0ad767d70dd4b16..183133ad3d740ada344ca5b7c4eb6a597f523809 100644 (file)
@@ -811,30 +811,6 @@ main(int  argc,                            /* I - Number of command-line arguments */
       }
   }
 
-  /*
-  *   print-scaling = fill functionality.
-  */
-  if((val = cupsGetOption("print-scaling",num_options,options)) !=0) {
-    if(!strcasecmp(val,"fill")) {
-        fillprint = 1;
-    }
-  }
-  else if((val = cupsGetOption("fill",num_options,options))!=0) {
-    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
-    {
-      fillprint = 1;
-    }
-  }
-  /*
-   * crop-to-fit
-   */
-  if((val = cupsGetOption("crop-to-fit",num_options,options))!= NULL){
-    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
-    {
-      cropfit=1;
-    }
-  }
-
   if ((val = cupsGetOption("OutputOrder",num_options,options)) != 0) {
     if (!strcasecmp(val, "Reverse")) {
       Reverse = 1;
@@ -900,21 +876,6 @@ main(int  argc,                            /* I - Number of command-line arguments */
   if ((val = cupsGetOption("brightness", num_options, options)) != NULL)
       brightness = atoi(val) * 0.01f;
 
-  if ((val = cupsGetOption("scaling", num_options, options)) != NULL)
-    zoom = atoi(val) * 0.01;
-  else if (((val =
-            cupsGetOption("fit-to-page", num_options, options)) != NULL) ||
-          ((val = cupsGetOption("fitplot", num_options, options)) != NULL))
-  {
-    if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") ||
-       !strcasecmp(val, "true"))
-      zoom = 1.0;
-    else
-      zoom = 0.0;
-  }
-  else if ((val = cupsGetOption("natural-scaling", num_options, options)) != NULL)
-    zoom = 0.0;
-
   if ((val = cupsGetOption("ppi", num_options, options)) != NULL)
   {
     if (sscanf(val, "%dx%d", &xppi, &yppi) < 2)
@@ -997,6 +958,114 @@ main(int  argc,                           /* I - Number of command-line arguments */
   colorspace = ColorDevice ? CUPS_IMAGE_RGB_CMYK : CUPS_IMAGE_WHITE;
 
   img = cupsImageOpen(filename, colorspace, CUPS_IMAGE_WHITE, sat, hue, NULL);
+  if(img!=NULL){
+
+  int margin_defined = 0;
+  int fidelity = 0;
+  int document_large = 0;
+
+  if(ppd->custom_margins[0]||ppd->custom_margins[1]||
+      ppd->custom_margins[2]||ppd->custom_margins[3])   // In case of custom margins
+    margin_defined = 1;
+  if(PageLength!=PageTop-PageBottom||PageWidth!=PageRight-PageLeft)
+    margin_defined = 1;
+
+  if((val = cupsGetOption("ipp-attribute-fidelity",num_options,options)) != NULL) {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes")||
+        !strcasecmp(val,"on")) {
+      fidelity = 1;
+    }
+  }
+
+  float w = (float)cupsImageGetWidth(img);
+  float h = (float)cupsImageGetHeight(img);
+  float pw = PageRight-PageLeft;
+  float ph = PageTop-PageBottom;
+  int tempOrientation = Orientation;
+  int flag =3;
+  if((val = cupsGetOption("orientation-requested",num_options,options))!=NULL) {
+    tempOrientation = atoi(val);
+  }
+  else if((val = cupsGetOption("landscape",num_options,options))!=NULL) {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes")) {
+      tempOrientation = 4;
+    }
+  }
+  if(tempOrientation==0) {
+    int temp1 = pw,
+          temp2 = ph,
+          temp3 = pw,
+          temp4 = ph;
+      if(temp1>w) temp1 = w;
+      if(temp2>h) temp2 = h;
+      if(temp3>h) temp3 = h;
+      if(temp4>w) temp4 = w;
+      if(temp1*temp2<temp3*temp4) {
+        tempOrientation = 4;
+      }
+  }
+  if(tempOrientation==4||tempOrientation==5) {
+    int tmp = pw;
+    pw = ph;
+    ph = tmp;
+  }
+  if(w>pw||h>ph) {
+    document_large = 1;
+  }
+
+  if((val = cupsGetOption("print-scaling",num_options,options)) != NULL) {
+    if(!strcasecmp(val,"auto")) {
+      if(fidelity||document_large) {
+        if(margin_defined)
+          zoom = 1.0;       // fit method
+        else
+          fillprint = 1;    // fill method
+      }
+      else
+        cropfit = 1;        // none method
+    }
+    else if(!strcasecmp(val,"auto-fit")) {
+      if(fidelity||document_large)
+        zoom = 1.0;         // fit method
+      else
+        cropfit = 1;        // none method
+    }
+    else if(!strcasecmp(val,"fill"))
+      fillprint = 1;        // fill method
+    else if(!strcasecmp(val,"fit"))
+      zoom = 1.0;           // fitplot = 1 or fit method
+    else
+      cropfit=1;            // none or crop-to-fit
+  }
+  else{       // print-scaling is not defined, look for alternate options.
+
+  if ((val = cupsGetOption("scaling", num_options, options)) != NULL)
+    zoom = atoi(val) * 0.01;
+  else if (((val =
+            cupsGetOption("fit-to-page", num_options, options)) != NULL) ||
+          ((val = cupsGetOption("fitplot", num_options, options)) != NULL))
+  {
+    if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") ||
+             !strcasecmp(val, "true"))
+      zoom = 1.0;
+    else
+      zoom = 0.0;
+  }
+  else if ((val = cupsGetOption("natural-scaling", num_options, options)) != NULL)
+    zoom = 0.0;
+
+  if((val = cupsGetOption("fill",num_options,options))!=0) {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes")) {
+      fillprint = 1;
+    }
+  }
+
+  if((val = cupsGetOption("crop-to-fit",num_options,options))!= NULL){
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes")) {
+      cropfit=1;
+    }
+  } }
+  }
   if(fillprint||cropfit)
   {
     float w = (float)cupsImageGetWidth(img);
index 7ec11cf329008b01fdf5beba40ef139c4f0a1c99..aaa5ff2eeac160afe48ad16ad802a7a54667f2eb 100644 (file)
@@ -375,39 +375,6 @@ main(int  argc,                            /* I - Number of command-line arguments */
       b = 10.0f;
   }
 
-  if ((val = cupsGetOption("scaling", num_options, options)) != NULL)
-    zoom = atoi(val) * 0.01;
-  else if (((val =
-            cupsGetOption("fit-to-page", num_options, options)) != NULL) ||
-          ((val = cupsGetOption("fitplot", num_options, options)) != NULL))
-  {
-    if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") ||
-       !strcasecmp(val, "true"))
-      zoom = 1.0;
-    else
-      zoom = 0.0;
-  }
-  else if ((val = cupsGetOption("natural-scaling", num_options, options)) != NULL)
-    zoom = 0.0;
-
-  if((val = cupsGetOption("print-scaling",num_options,options)) !=0) {
-    if(!strcasecmp(val,"fill")) {
-        fillprint = 1;
-    }
-  }
-  else if((val = cupsGetOption("fill",num_options,options))!=0) {
-    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
-    {
-      fillprint = 1;
-    }
-  }
-  if((val = cupsGetOption("crop-to-fit",num_options,options))!= NULL){
-    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
-    {
-      cropfit=1;
-    }
-  }
-
   if ((val = cupsGetOption("ppi", num_options, options)) != NULL)
   {
     if (sscanf(val, "%dx%d", &xppi, &yppi) < 2)
@@ -707,6 +674,123 @@ main(int  argc,                           /* I - Number of command-line arguments */
     img = cupsImageOpen(filename, primary, secondary, sat, hue, NULL);
   else
     img = cupsImageOpen(filename, primary, secondary, sat, hue, lut);
+
+  if(img!=NULL){
+
+  int margin_defined = 0;
+  int fidelity = 0;
+  int document_large = 0;
+
+  if(ppd->custom_margins[0]||ppd->custom_margins[1]
+      ||ppd->custom_margins[2]||ppd->custom_margins[3])
+    margin_defined = 1;
+
+  if(PageLength!=PageTop-PageBottom||PageWidth!=PageRight-PageLeft)
+  {
+    margin_defined = 1;
+  }
+
+  if((val = cupsGetOption("ipp-attribute-fidelity",num_options,options)) != NULL)
+  {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes")||
+        !strcasecmp(val,"on"))
+    {
+      fidelity = 1;
+    }
+  }
+
+  float w = (float)cupsImageGetWidth(img);
+  float h = (float)cupsImageGetHeight(img);
+  float pw = PageRight-PageLeft;
+  float ph = PageTop-PageBottom;
+  int tempOrientation = Orientation;
+  if((val = cupsGetOption("orientation-requested",num_options,options))!=NULL)
+  {
+    tempOrientation = atoi(val);
+  }
+  else if((val = cupsGetOption("landscape",num_options,options))!=NULL)
+  {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
+    {
+      tempOrientation = 4;
+    }
+  }
+  if(tempOrientation==0)
+  {
+    if(min(pw,w)*min(ph,h)<min(pw,h)*min(ph,w))
+    {
+      tempOrientation = 4;
+    }
+  }
+  if(tempOrientation==4||tempOrientation==5)
+  {
+    int tmp = pw;
+    pw = ph;
+    ph = tmp;
+  }
+  if(w>pw||h>ph)
+  {
+    document_large = 1;
+  }
+
+  if((val = cupsGetOption("print-scaling",num_options,options)) != NULL)
+  {
+    if(!strcasecmp(val,"auto"))
+    {
+      if(fidelity||document_large)
+      {
+        if(margin_defined)
+          zoom = 1.0;       // fit method
+        else
+          fillprint = 1;    // fill method
+      }
+      else
+        cropfit = 1;        // none method
+    }
+    else if(!strcasecmp(val,"auto-fit"))
+    {
+      if(fidelity||document_large)
+        zoom = 1.0;         // fit method
+      else
+        cropfit = 1;        // none method
+    }
+    else if(!strcasecmp(val,"fill"))
+      fillprint = 1;        // fill method
+    else if(!strcasecmp(val,"fit"))
+      zoom = 1.0;           // fitplot = 1 or fit method
+    else
+      cropfit=1;            // none or crop-to-fit
+  }
+  else{       // print-scaling is not defined, look for alternate options.
+    if ((val = cupsGetOption("scaling", num_options, options)) != NULL)
+    zoom = atoi(val) * 0.01;
+  else if (((val =
+            cupsGetOption("fit-to-page", num_options, options)) != NULL) ||
+          ((val = cupsGetOption("fitplot", num_options, options)) != NULL))
+  {
+    if (!strcasecmp(val, "yes") || !strcasecmp(val, "on") ||
+       !strcasecmp(val, "true"))
+      zoom = 1.0;
+    else
+      zoom = 0.0;
+  }
+  else if ((val = cupsGetOption("natural-scaling", num_options, options)) != NULL)
+    zoom = 0.0;
+
+  if((val = cupsGetOption("fill",num_options,options))!=0) {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
+    {
+      fillprint = 1;
+    }
+  }
+  if((val = cupsGetOption("crop-to-fit",num_options,options))!= NULL){
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
+    {
+      cropfit=1;
+    }
+  } }
+  }
+
   if(img!=NULL)
   {
     if(fillprint||cropfit)
index 3b9bb3043ec8d8f97989c6c1bea332511cedcc29..0a299cc65a47e0dadc3bca6e8cbf7183768e08b0 100644 (file)
@@ -318,6 +318,25 @@ void getParameters(ppd_file_t *ppd,int num_options,cups_option_t *options,Proces
     param.numCopies=1;
   }
 
+  if((val = cupsGetOption("ipp-attribute-fidelity",num_options,options))!=NULL) {
+    if(!strcasecmp(val,"true")||!strcasecmp(val,"yes") ||
+      !strcasecmp(val,"on"))
+      param.fidelity = true;
+  }
+
+  if((val = cupsGetOption("print-scaling",num_options,options)) != NULL) {
+    if(!strcasecmp(val,"auto"))
+      param.autoprint = true;
+    else if(!strcasecmp(val,"auto-fit"))
+      param.autofit = true;
+    else if(!strcasecmp(val,"fill"))
+      param.fillprint = true;
+    else if(!strcasecmp(val,"fit"))
+      param.fitplot = true;
+    else
+      param.cropfit = true;
+  }
+  else {
   if ((val=cupsGetOption("fitplot",num_options,options)) == NULL) {
     if ((val=cupsGetOption("fit-to-page",num_options,options)) == NULL) {
       val=cupsGetOption("ipp-attribute-fidelity",num_options,options);
@@ -326,26 +345,19 @@ void getParameters(ppd_file_t *ppd,int num_options,cups_option_t *options,Proces
   // TODO?  pstops checks =="true", pdftops !is_false  ... pstops says: fitplot only for PS (i.e. not for PDF, cmp. cgpdftopdf)
   param.fitplot=(val)&&(!is_false(val));
 
-  if((val=cupsGetOption("print-scaling",num_options,options))!=NULL) {
-    if(!strcasecmp(val,"fill")) {
-      param.fillprint=true;
-    }
-  }
-  else if((val = cupsGetOption("fill",num_options,options))!=0) {
+  if((val = cupsGetOption("fill",num_options,options))!=0) {
     if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
     {
       param.fillprint = true;
     }
   }
-  /*
-   * crop-to-fit
-   */
   if((val = cupsGetOption("crop-to-fit",num_options,options))!= NULL){
     if(!strcasecmp(val,"true")||!strcasecmp(val,"yes"))
     {
       param.cropfit=1;
     }
   }
+  }
 
   if (ppd && (ppd->landscape < 0)) { // direction the printer rotates landscape (90 or -90)
     param.normal_landscape=ROT_270;
index 3a5def201f8f694f69754e2db02c131514462928..6d2d32ff58f3eb1d9725513708490aa1b343b685 100644 (file)
@@ -175,6 +175,46 @@ bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters &param) // {{
   }
   const int numPages=std::max(shuffle.size(),pages.size());
 
+  if(param.autoprint||param.autofit){
+    bool margin_defined = true;
+    bool document_large = false;
+    int pw = param.page.right-param.page.left;
+    int ph = param.page.top-param.page.bottom;
+    int w=0,h=0;
+    Rotation tempRot=param.orientation;
+    PageRect r= pages[0]->getRect();
+    w = r.width;
+    h = r.height;
+
+    if(tempRot==ROT_90||tempRot==ROT_270)
+    {
+      std::swap(w,h);
+    }
+    if(w>=pw||h>=ph)
+    {
+      document_large = true;
+    }
+    if((param.page.width==pw)&&
+        (param.page.height==ph))
+        margin_defined = false;
+    if(param.autoprint){
+      if(param.fidelity||document_large) {
+        if(margin_defined)
+          param.fitplot = true;
+        else
+          param.fillprint = true;
+      }
+      else
+        param.cropfit = true;
+    }
+    else{
+      if(param.fidelity||document_large)
+        param.fitplot = true;
+      else
+        param.cropfit = true;
+    }
+  }
+
   if(param.fillprint||param.cropfit){
     fprintf(stderr,"[DEBUG]: Cropping input pdf and Enabling fitplot.\n");
     if(param.noOrientation&&pages.size())
index b1bbac5493597db87266750c6685ab07c62107f1..ec7b2997b24be3f67f37244623a7d98a50fc6214 100644 (file)
@@ -16,6 +16,9 @@ ProcessingParameters()
     fitplot(false),
     fillprint(false),  //print-scaling = fill
     cropfit(false),
+    autoprint(false),
+    autofit(false),
+    fidelity(false),
     noOrientation(false),
     orientation(ROT_0),normal_landscape(ROT_270),
     paper_is_landscape(false),
@@ -59,6 +62,9 @@ ProcessingParameters()
   bool fitplot;
   bool fillprint;   //print-scaling = fill
   bool cropfit;     // -o crop-to-fit
+  bool autoprint;   // print-scaling = auto
+  bool autofit;     // print-scaling = auto-fit
+  bool fidelity;
   bool noOrientation;
   PageRect page;
   Rotation orientation,normal_landscape;  // normal_landscape (i.e. default direction) is e.g. needed for number-up=2