From 5af73c285afa9883693ce7bfd75a0d7cbc249f18 Mon Sep 17 00:00:00 2001 From: dh Date: Thu, 7 Mar 2019 18:22:10 +0530 Subject: [PATCH] Setting orientation for crop-to-fit and fill. When the input format is other than pdf(like ps), pdftopdf is unable to set orientation for crop-to-fit and fill. New function is_landsape is added to get orientation of input file and orientation is set accordingly. --- filter/pdftopdf/pdftopdf.cc | 2 ++ filter/pdftopdf/pdftopdf_processor.cc | 6 ++++++ filter/pdftopdf/pdftopdf_processor.h | 3 +++ filter/pdftopdf/qpdf_pdftopdf_processor.cc | 16 ++++++++++++++++ filter/pdftopdf/qpdf_pdftopdf_processor.h | 1 + 5 files changed, 28 insertions(+) diff --git a/filter/pdftopdf/pdftopdf.cc b/filter/pdftopdf/pdftopdf.cc index bf29ec0f6..3b9bb3043 100644 --- a/filter/pdftopdf/pdftopdf.cc +++ b/filter/pdftopdf/pdftopdf.cc @@ -369,6 +369,8 @@ void getParameters(ppd_file_t *ppd,int num_options,cups_option_t *options,Proces static const Rotation ipp2rot[4]={ROT_0, ROT_90, ROT_270, ROT_180}; param.orientation=ipp2rot[ipprot-3]; } + } else { + param.noOrientation = true; } ppd_size_t *pagesize; diff --git a/filter/pdftopdf/pdftopdf_processor.cc b/filter/pdftopdf/pdftopdf_processor.cc index 7f62b4f16..850bddc90 100644 --- a/filter/pdftopdf/pdftopdf_processor.cc +++ b/filter/pdftopdf/pdftopdf_processor.cc @@ -177,6 +177,12 @@ bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters ¶m) // {{ if(param.fillprint||param.cropfit){ fprintf(stderr,"[DEBUG]: Cropping input pdf and Enabling fitplot.\n"); + if(param.noOrientation&&pages.size()) + { + bool land = pages[0]->is_landscape(param.orientation); + if(land) + param.orientation = param.normal_landscape; + } for(int i=0;i<(int)pages.size();i++) { std::shared_ptr page = pages[i]; diff --git a/filter/pdftopdf/pdftopdf_processor.h b/filter/pdftopdf/pdftopdf_processor.h index d71fc16af..b1bbac549 100644 --- a/filter/pdftopdf/pdftopdf_processor.h +++ b/filter/pdftopdf/pdftopdf_processor.h @@ -16,6 +16,7 @@ ProcessingParameters() fitplot(false), fillprint(false), //print-scaling = fill cropfit(false), + noOrientation(false), orientation(ROT_0),normal_landscape(ROT_270), paper_is_landscape(false), duplex(false), @@ -58,6 +59,7 @@ ProcessingParameters() bool fitplot; bool fillprint; //print-scaling = fill bool cropfit; // -o crop-to-fit + bool noOrientation; PageRect page; Rotation orientation,normal_landscape; // normal_landscape (i.e. default direction) is e.g. needed for number-up=2 bool paper_is_landscape; @@ -111,6 +113,7 @@ class PDFTOPDF_PageHandle { virtual void add_border_rect(const PageRect &rect,BorderType border,float fscale) =0; // TODO?! add standalone crop(...) method (not only for subpages) virtual Rotation crop(const PageRect &cropRect,Rotation orientation,Position xpos,Position ypos,bool scale) =0; + virtual bool is_landscape(Rotation orientation) =0 ; virtual void add_subpage(const std::shared_ptr &sub,float xpos,float ypos,float scale,const PageRect *crop=NULL) =0; virtual void mirror() =0; virtual void rotate(Rotation rot) =0; diff --git a/filter/pdftopdf/qpdf_pdftopdf_processor.cc b/filter/pdftopdf/qpdf_pdftopdf_processor.cc index 0761a4240..7321e427c 100644 --- a/filter/pdftopdf/qpdf_pdftopdf_processor.cc +++ b/filter/pdftopdf/qpdf_pdftopdf_processor.cc @@ -239,6 +239,22 @@ Rotation QPDF_PDFTOPDF_PageHandle::crop(const PageRect &cropRect,Rotation orient return getRotate(page); } +bool QPDF_PDFTOPDF_PageHandle::is_landscape(Rotation orientation) +{ + page.assertInitialized(); + if(orientation==ROT_0||orientation==ROT_180) + page.replaceOrRemoveKey("/Rotate",makeRotate(ROT_90)); + else + page.replaceOrRemoveKey("/Rotate",makeRotate(ROT_0)); + + PageRect currpage= getBoxAsRect(getTrimBox(page)); + double width = currpage.right-currpage.left; + double height = currpage.top-currpage.bottom; + if(width>height) + return true; + return false; +} + // TODO: better cropping // TODO: test/fix with qsub rotation void QPDF_PDFTOPDF_PageHandle::add_subpage(const std::shared_ptr &sub,float xpos,float ypos,float scale,const PageRect *crop) // {{{ diff --git a/filter/pdftopdf/qpdf_pdftopdf_processor.h b/filter/pdftopdf/qpdf_pdftopdf_processor.h index d1394a8af..f721d9390 100644 --- a/filter/pdftopdf/qpdf_pdftopdf_processor.h +++ b/filter/pdftopdf/qpdf_pdftopdf_processor.h @@ -13,6 +13,7 @@ class QPDF_PDFTOPDF_PageHandle : public PDFTOPDF_PageHandle { virtual void rotate(Rotation rot); virtual void add_label(const PageRect &rect, const std::string label); virtual Rotation crop(const PageRect &cropRect,Rotation orientation,Position xpos,Position ypos,bool scale); + virtual bool is_landscape(Rotation orientation); void debug(const PageRect &rect,float xpos,float ypos); private: bool isExisting() const; -- 2.47.2