1 #ifndef PDFTOPDF_PROCESSOR_H
2 #define PDFTOPDF_PROCESSOR_H
3 
4 #include "pptypes.h"
5 #include "nup.h"
6 #include "intervalset.h"
7 #include <vector>
8 #include <string>
9 
10 enum BookletMode { BOOKLET_OFF, BOOKLET_ON, BOOKLET_JUSTSHUFFLE };
11 
12 struct ProcessingParameters {
ProcessingParametersProcessingParameters13 ProcessingParameters()
14 : jobId(0),numCopies(1),
15     user(0),title(0),
16     fitplot(false),
17     fillprint(false),  //print-scaling = fill
18     cropfit(false),
19     autoprint(false),
20     autofit(false),
21     fidelity(false),
22     noOrientation(false),
23     orientation(ROT_0),normal_landscape(ROT_270),
24     paper_is_landscape(false),
25     duplex(false),
26     border(NONE),
27     reverse(false),
28 
29     pageLabel(),
30     evenPages(true),oddPages(true),
31 
32     mirror(false),
33 
34     xpos(CENTER),ypos(CENTER),
35 
36     collate(false),
37     evenDuplex(false),
38 
39     booklet(BOOKLET_OFF),bookSignature(-1),
40 
41     autoRotate(false),
42 
43     emitJCL(true),deviceCopies(1),
44     deviceCollate(false),setDuplex(false),
45 
46     page_logging(-1)
47   {
48     page.width=612.0; // letter
49     page.height=792.0;
50     page.top=page.height-36.0;
51     page.bottom=36.0;
52     page.left=18.0;
53     page.right=page.width-18.0;
54 
55     // everything
56     pageRange.add(1);
57     pageRange.finish();
58   }
59 
60   int jobId, numCopies;
61   const char *user, *title; // will stay around
62   bool fitplot;
63   bool fillprint;   //print-scaling = fill
64   bool cropfit;     // -o crop-to-fit
65   bool autoprint;   // print-scaling = auto
66   bool autofit;     // print-scaling = auto-fit
67   bool fidelity;
68   bool noOrientation;
69   PageRect page;
70   Rotation orientation,normal_landscape;  // normal_landscape (i.e. default direction) is e.g. needed for number-up=2
71   bool paper_is_landscape;
72   bool duplex;
73   BorderType border;
74   NupParameters nup;
75   bool reverse;
76 
77   std::string pageLabel;
78   bool evenPages,oddPages;
79   IntervalSet pageRange;
80 
81   bool mirror;
82 
83   Position xpos,ypos;
84 
85   bool collate;
86 
87   bool evenDuplex; // make number of pages a multiple of 2
88 
89   BookletMode booklet;
90   int bookSignature;
91 
92   bool autoRotate;
93 
94   // ppd/jcl changes
95   bool emitJCL;
96   int deviceCopies;
97   bool deviceCollate;
98   bool setDuplex;
99   // unsetMirror  (always)
100 
101   int page_logging;
102   int copies_to_be_logged;
103 
104   // helper functions
105   bool withPage(int outno) const; // 1 based
106   void dump() const;
107 };
108 
109 #include <stdio.h>
110 #include <memory>
111 
112 enum ArgOwnership { WillStayAlive,MustDuplicate,TakeOwnership };
113 
114 class PDFTOPDF_PageHandle {
115  public:
~PDFTOPDF_PageHandle()116   virtual ~PDFTOPDF_PageHandle() {}
117   virtual PageRect getRect() const =0;
118   // fscale:  inverse_scale (from nup, fitplot)
119   virtual void add_border_rect(const PageRect &rect,BorderType border,float fscale) =0;
120   // TODO?! add standalone crop(...) method (not only for subpages)
121   virtual Rotation crop(const PageRect &cropRect,Rotation orientation,Position xpos,Position ypos,bool scale) =0;
122   virtual bool is_landscape(Rotation orientation) =0 ;
123   virtual void add_subpage(const std::shared_ptr<PDFTOPDF_PageHandle> &sub,float xpos,float ypos,float scale,const PageRect *crop=NULL) =0;
124   virtual void mirror() =0;
125   virtual void rotate(Rotation rot) =0;
126   virtual void add_label(const PageRect &rect, const std::string label) =0;
127 };
128 
129 // TODO: ... error output?
130 class PDFTOPDF_Processor { // abstract interface
131  public:
~PDFTOPDF_Processor()132   virtual ~PDFTOPDF_Processor() {}
133 
134   // TODO: ... qpdf wants password at load time
135   virtual bool loadFile(FILE *f,ArgOwnership take=WillStayAlive,int flatten_forms=1) =0;
136   virtual bool loadFilename(const char *name,int flatten_forms=1) =0;
137 
138   // TODO? virtual bool may_modify/may_print/?
139   virtual bool check_print_permissions() =0;
140 
141   virtual std::vector<std::shared_ptr<PDFTOPDF_PageHandle>> get_pages() =0; // shared_ptr because of type erasure (deleter)
142 
143   virtual std::shared_ptr<PDFTOPDF_PageHandle> new_page(float width,float height) =0;
144 
145   virtual void add_page(std::shared_ptr<PDFTOPDF_PageHandle> page,bool front) =0; // at back/front -- either from get_pages() or new_page()+add_subpage()-calls  (or [also allowed]: empty)
146 
147   //  void remove_page(std::shared_ptr<PDFTOPDF_PageHandle> ph);  // not needed: we construct from scratch, at least conceptually.
148 
149   virtual void multiply(int copies,bool collate) =0;
150 
151   virtual void autoRotateAll(bool dst_lscape,Rotation normal_landscape) =0; // TODO elsewhere?!
152   virtual void addCM(const char *defaulticc,const char *outputicc) =0;
153 
154   virtual void setComments(const std::vector<std::string> &comments) =0;
155 
156   virtual void emitFile(FILE *dst,ArgOwnership take=WillStayAlive) =0;
157   virtual void emitFilename(const char *name) =0; // NULL -> stdout
158 
159   virtual bool hasAcroForm() =0;
160 };
161 
162 class PDFTOPDF_Factory {
163  public:
164   // never NULL, but may throw.
165   static PDFTOPDF_Processor *processor();
166 };
167 
168 //bool checkBookletSignature(int signature) { return (signature%4==0); }
169 std::vector<int> bookletShuffle(int numPages,int signature=-1);
170 
171 // This is all we want:
172 bool processPDFTOPDF(PDFTOPDF_Processor &proc,ProcessingParameters &param);
173 
174 #endif
175