added queryable parameters
[swftools.git] / lib / pdf / pdf.cc
1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
3 #include "../log.h"
4 #include "config.h"
5 #include "GlobalParams.h"
6 #include "InfoOutputDev.h"
7 #include "GFXOutputDev.h"
8 #include "../mem.h"
9 #include "pdf.h"
10 #define NO_ARGPARSER
11 #include "../args.h"
12
13 static parameter_t* device_config = 0;
14 static parameter_t* device_config_next = 0;
15
16 int jpeg_dpi = 0;
17 int ppm_dpi = 0;
18
19 static double zoom = 72; /* xpdf: 86 */
20
21 static char* global_page_range = 0;
22
23 typedef struct _pdf_page_info
24 {
25     int xMin, yMin, xMax, yMax;
26     int width,height;
27     int number_of_images;
28     int number_of_links;
29     int number_of_fonts;
30     char has_info;
31 } pdf_page_info_t;
32
33 typedef struct _pdf_doc_internal
34 {
35     int protect;
36     PDFDoc*doc;
37     Object docinfo;
38     InfoOutputDev*info;
39     GFXOutputDev*outputDev;
40     pdf_page_info_t*pages;
41 } pdf_doc_internal_t;
42
43 typedef struct _pdf_page_internal
44 {
45 } pdf_page_internal_t;
46
47 typedef struct _dev_output_internal
48 {
49     GFXOutputDev*outputDev;
50 } dev_output_internal_t;
51
52
53 static char* dirseparator()
54 {
55 #ifdef WIN32
56     return "\\";
57 #else
58     return "/";
59 #endif
60 }
61
62
63 void pdfpage_destroy(gfxpage_t*pdf_page)
64 {
65     pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
66     free(pdf_page->internal);pdf_page->internal = 0;
67     free(pdf_page);pdf_page=0;
68 }
69
70 void render2(gfxpage_t*page, gfxdevice_t*output)
71 {
72     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
73
74     if(!pi) {
75         msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
76         return;
77     }
78
79     if(!pi->pages[page->nr-1].has_info) {
80         msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
81         return;
82     }
83
84     if(pi->protect) {
85         gfxdevice_t*dev = pi->outputDev->device;
86         dev->setparameter(dev, "protect", "1");
87     }
88     pi->outputDev->setInfo(pi->info);
89     pi->outputDev->setXRef(pi->doc, pi->doc->getXRef());
90     pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
91     pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
92     pi->outputDev->endframe();
93 }
94
95     
96 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
97 {
98     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
99     pi->outputDev->setDevice(output);
100     pi->outputDev->setMove(0,0);
101     pi->outputDev->setClip(0,0,0,0);
102     render2(page, output);
103     pi->outputDev->setDevice(0);
104 }
105
106 void pdfpage_rendersection(gfxpage_t*page, gfxdevice_t*output, gfxcoord_t x, gfxcoord_t y, gfxcoord_t _x1, gfxcoord_t _y1, gfxcoord_t _x2, gfxcoord_t _y2)
107 {
108     int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
109     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
110     pi->outputDev->setDevice(output);
111     pi->outputDev->setMove((int)x,(int)y);
112     if((x1|y1|x2|y2)==0) x2++;
113     pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
114     render2(page, output);
115     pi->outputDev->setDevice(0);
116 }
117
118 void pdf_doc_destroy(gfxdocument_t*gfx)
119 {
120     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
121
122     delete i->doc; i->doc=0;
123     free(i->pages); i->pages = 0;
124
125     i->docinfo.free();
126     
127     if(i->info) {
128         delete i->info;i->info=0;
129     }
130
131     free(gfx->internal);gfx->internal=0;
132     free(gfx);gfx=0;
133
134     if(global_page_range) {
135         free(global_page_range);
136         global_page_range = 0;
137     }
138 }
139
140 void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
141 {
142     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
143     if(!strcmp(name, "pagemap")) {
144         GFXOutputDev*o = i->outputDev;
145         int pdfpage=0, outputpage=0;
146         sscanf(value,"%d:%d", &pdfpage, &outputpage);
147         o->preparePage(pdfpage, outputpage);
148     } else {
149         msg("<warning> Ignored parameter: %s=%s", name, value);
150     }
151 }
152
153 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
154 {
155     pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
156
157     if(page < 1 || page > doc->num_pages)
158         return 0;
159     
160     gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
161     pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
162     memset(pi, 0, sizeof(pdf_page_internal_t));
163     pdf_page->internal = pi;
164
165     pdf_page->destroy = pdfpage_destroy;
166     pdf_page->render = pdfpage_render;
167     pdf_page->rendersection = pdfpage_rendersection;
168     pdf_page->width = di->pages[page-1].width;
169     pdf_page->height = di->pages[page-1].height;
170
171     pdf_page->parent = doc;
172     pdf_page->nr = page;
173     return pdf_page;
174 }
175
176 static char*getInfoString(Dict *infoDict, char *key)
177 {
178     Object obj;
179     GString *s1, *s2;
180     int i;
181
182     if (infoDict->lookup(key, &obj)->isString()) {
183         s1 = obj.getString();
184         if ((s1->getChar(0) & 0xff) == 0xfe &&
185             (s1->getChar(1) & 0xff) == 0xff) {
186             s2 = new GString();
187             for (i = 2; i < obj.getString()->getLength(); i += 2) {
188               if (s1->getChar(i) == '\0') {
189                 s2->append(s1->getChar(i+1));
190               } else {
191                 delete s2;
192                 s2 = new GString("<unicode>");
193                 break;
194               }
195             }
196             char*ret = strdup(s2->getCString());
197             delete s2;
198             obj.free();
199             return ret;
200         } else {
201             char*ret = strdup(s1->getCString());
202             obj.free();
203             return ret;
204         }
205     }
206     return strdup("");
207 }
208
209 static char*getInfoDate(Dict *infoDict, char *key) 
210 {
211     Object obj;
212     char *s;
213
214     if (infoDict->lookup(key, &obj)->isString()) {
215         s = obj.getString()->getCString();
216         if (s[0] == 'D' && s[1] == ':') {
217           s += 2;
218         }
219         char*ret = strdup(s);
220         obj.free();
221         return ret;
222     }
223     return strdup("");
224 }
225
226 char* pdf_doc_getinfo(gfxdocument_t*doc, char*name)
227 {
228     pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
229     if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
230     else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
231     else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
232     else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
233     else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
234     else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
235     else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
236     else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
237     else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
238     else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
239     else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
240     else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
241     else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
242     else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
243     else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
244     else if(!strcmp(name, "version")) { 
245         char buf[32];
246         sprintf(buf, "%.1f", i->doc->getPDFVersion());
247         return strdup(buf);
248     }
249     return 0;
250 }
251
252
253 void storeDeviceParameter(char*name, char*value)
254 {
255     parameter_t*p = new parameter_t();
256     p->name = strdup(name);
257     p->value = strdup(value);
258     p->next = 0;
259     if(device_config_next) {
260         device_config_next->next = p;
261         device_config_next = p;
262     } else {
263         device_config = p;
264         device_config_next = p;
265     }
266 }
267
268 void pdf_set_parameter(char*name, char*value)
269 {
270     msg("<verbose> setting parameter %s to \"%s\"", name, value);
271     if(!strncmp(name, "fontdir", strlen("fontdir"))) {
272         addGlobalFontDir(value);
273     } else if(!strcmp(name, "pages")) {
274         global_page_range = strdup(value);
275     } else if(!strncmp(name, "font", strlen("font"))) {
276         addGlobalFont(value);
277     } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
278         addGlobalLanguageDir(value);
279     } else if(!strcmp(name, "zoom")) {
280         char buf[80];
281         zoom = atof(value);
282         sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
283         storeDeviceParameter("jpegsubpixels", buf);
284         sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
285         storeDeviceParameter("ppmsubpixels", buf);
286     } else if(!strcmp(name, "jpegdpi")) {
287         char buf[80];
288         jpeg_dpi = atoi(value);
289         sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
290         storeDeviceParameter("jpegsubpixels", buf);
291     } else if(!strcmp(name, "ppmdpi")) {
292         char buf[80];
293         ppm_dpi = atoi(value);
294         sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
295         storeDeviceParameter("ppmsubpixels", buf);
296     } else {
297         storeDeviceParameter(name,value);
298     }
299 }
300
301 gfxdocument_t*pdf_open(char*filename)
302 {
303     gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
304     memset(pdf_doc, 0, sizeof(gfxdocument_t));
305     pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
306     memset(i, 0, sizeof(pdf_doc_internal_t));
307     pdf_doc->internal = i;
308     char*userPassword=0;
309     
310     filename = strdup(filename);
311
312     char*x = 0;
313     if((x = strchr(filename, '|'))) {
314         *x = 0;
315         userPassword = x+1;
316     }
317     
318     GString *fileName = new GString(filename);
319     GString *userPW;
320
321     // read config file
322     if(!globalParams)
323         globalParams = new GlobalParams("");
324
325     // open PDF file
326     if (userPassword && userPassword[0]) {
327       userPW = new GString(userPassword);
328     } else {
329       userPW = NULL;
330     }
331     i->doc = new PDFDoc(fileName, userPW);
332     if (userPW) {
333       delete userPW;
334     }
335     if (!i->doc->isOk()) {
336         printf("xpdf reports document as broken.\n");
337         return 0;
338     }
339
340     // get doc info
341     i->doc->getDocInfo(&i->docinfo);
342     
343     pdf_doc->num_pages = i->doc->getNumPages();
344     i->protect = 0;
345     if (i->doc->isEncrypted()) {
346           if(!i->doc->okToCopy()) {
347               printf("PDF disallows copying.\n");
348               return 0;
349           }
350           if(!i->doc->okToChange() || !i->doc->okToAddNotes())
351               i->protect = 1;
352     }
353
354     InfoOutputDev*io = new InfoOutputDev();
355     int t;
356     i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
357     memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
358     for(t=1;t<=pdf_doc->num_pages;t++) {
359         if(!global_page_range || is_in_range(t, global_page_range)) {
360             i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
361             i->doc->processLinks((OutputDev*)io, t);
362             i->pages[t-1].xMin = io->x1;
363             i->pages[t-1].yMin = io->y1;
364             i->pages[t-1].xMax = io->x2;
365             i->pages[t-1].yMax = io->y2;
366             i->pages[t-1].width = io->x2 - io->x1;
367             i->pages[t-1].height = io->y2 - io->y1;
368             i->pages[t-1].number_of_images = io->num_images;
369             i->pages[t-1].number_of_links = io->num_links;
370             i->pages[t-1].number_of_fonts = io->num_fonts;
371             i->pages[t-1].has_info = 1;
372         }
373     }
374     i->info = io;
375     i->outputDev = new GFXOutputDev(device_config);
376
377     pdf_doc->get = 0;
378     pdf_doc->destroy = pdf_doc_destroy;
379     pdf_doc->set_parameter = pdf_doc_set_parameter;
380     pdf_doc->getinfo = pdf_doc_getinfo;
381     pdf_doc->getpage = pdf_doc_getpage;
382
383
384     return pdf_doc;
385
386 }
387
388 gfxsource_t*gfxsource_pdf_create()
389 {
390     gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
391     memset(src, 0, sizeof(gfxsource_t));
392     src->set_parameter = pdf_set_parameter;
393     src->open = pdf_open;
394     return src;
395 }