From 867714e47b9733c704b0dc70963f88942baafe67 Mon Sep 17 00:00:00 2001 From: Dheeraj Date: Wed, 8 May 2019 01:15:47 +0530 Subject: [PATCH] Add support for print-scaling option. Support for print-scaling feature has been added to imagetoraster,imagetopdf and pdftopdf filters. --- filter/imagetopdf.c | 146 +++++++++++++++++++------- filter/imagetoraster.c | 146 ++++++++++++++++++++------ filter/pdftopdf/pdftopdf.cc | 30 ++++-- filter/pdftopdf/pdftopdf_processor.cc | 40 +++++++ filter/pdftopdf/pdftopdf_processor.h | 6 ++ 5 files changed, 287 insertions(+), 81 deletions(-) diff --git a/filter/imagetopdf.c b/filter/imagetopdf.c index 859888d2e..b5d3a0924 100644 --- a/filter/imagetopdf.c +++ b/filter/imagetopdf.c @@ -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,113 @@ 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]) + { + 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*temp2pw||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); diff --git a/filter/imagetoraster.c b/filter/imagetoraster.c index 7ec11cf32..c0642d01a 100644 --- a/filter/imagetoraster.c +++ b/filter/imagetoraster.c @@ -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,119 @@ 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((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)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) diff --git a/filter/pdftopdf/pdftopdf.cc b/filter/pdftopdf/pdftopdf.cc index 3b9bb3043..0a299cc65 100644 --- a/filter/pdftopdf/pdftopdf.cc +++ b/filter/pdftopdf/pdftopdf.cc @@ -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; diff --git a/filter/pdftopdf/pdftopdf_processor.cc b/filter/pdftopdf/pdftopdf_processor.cc index 3a5def201..6d2d32ff5 100644 --- a/filter/pdftopdf/pdftopdf_processor.cc +++ b/filter/pdftopdf/pdftopdf_processor.cc @@ -175,6 +175,46 @@ bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters ¶m) // {{ } 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()) diff --git a/filter/pdftopdf/pdftopdf_processor.h b/filter/pdftopdf/pdftopdf_processor.h index b1bbac549..ec7b2997b 100644 --- a/filter/pdftopdf/pdftopdf_processor.h +++ b/filter/pdftopdf/pdftopdf_processor.h @@ -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