applied getinfo_crash.patch and info_only.patch by Ricardo Pedroso
[swftools.git] / lib / pdf / pdf.cc
1 #include <stdio.h>
2 #include <string.h>
3 #include "../gfxdevice.h"
4 #include "../gfxsource.h"
5 #include "../devices/rescale.h"
6 #include "../log.h"
7 #include "config.h"
8 #include "GlobalParams.h"
9 #include "InfoOutputDev.h"
10 #include "GFXOutputDev.h"
11 #include "FullBitmapOutputDev.h"
12 #include "BitmapOutputDev.h"
13 #include "../mem.h"
14 #include "pdf.h"
15 #define NO_ARGPARSER
16 #include "../args.h"
17
18 static double zoom = 72; /* xpdf: 86 */
19 static int zoomtowidth = 0;
20 static int multiply = 1;
21 static char* global_page_range = 0;
22
23 static int globalparams_count=0;
24
25 typedef struct _parameter
26 {
27     struct _parameter *next;
28     const char*name;
29     const char*value;
30 } parameter_t;
31 typedef struct _parameterlist
32 {
33     parameter_t* device_config;
34     parameter_t* device_config_next;
35 } parameterlist_t;
36
37 typedef struct _pdf_page_info
38 {
39     int xMin, yMin, xMax, yMax;
40     int width,height;
41     int number_of_images;
42     int number_of_links;
43     int number_of_fonts;
44     char has_info;
45 } pdf_page_info_t;
46
47 typedef struct _pdf_doc_internal
48 {
49     char config_bitmap_optimizing;
50     char config_full_bitmap_optimizing;
51     char config_print;
52     parameterlist_t parameters;
53
54     int protect;
55     int nocopy;
56     int noprint;
57     
58     PDFDoc*doc;
59     Object docinfo;
60     InfoOutputDev*info;
61
62     pdf_page_info_t*pages;
63     char*filename;
64
65     /* page map */
66     int*pagemap;
67     int pagemap_size;
68     int pagemap_pos;
69
70     gfxsource_t*parent;
71 } pdf_doc_internal_t;
72
73 typedef struct _pdf_page_internal
74 {
75 } pdf_page_internal_t;
76
77 typedef struct _dev_output_internal
78 {
79     CommonOutputDev*outputDev;
80 } dev_output_internal_t;
81
82
83 typedef struct _gfxsource_internal
84 {
85     parameterlist_t parameters;
86 } gfxsource_internal_t;
87
88
89 static const char* dirseparator()
90 {
91 #ifdef WIN32
92     return "\\";
93 #else
94     return "/";
95 #endif
96 }
97
98 static void storeDeviceParameter(parameterlist_t*i, const char*name, const char*value)
99 {
100     parameter_t*o = i->device_config;
101     while(o) {
102         if(!strcmp(name, o->name)) {
103             /* overwrite old value */
104             free((void*)o->value);
105             o->value = strdup(value);
106             return;
107         }
108         o = o->next;
109     }
110     parameter_t*p = new parameter_t();
111     p->name = strdup(name);
112     p->value = strdup(value);
113     p->next = 0;
114
115     if(i->device_config_next) {
116         i->device_config_next->next = p;
117         i->device_config_next = p;
118     } else {
119         i->device_config = p;
120         i->device_config_next = p;
121     }
122 }
123
124
125
126 void pdfpage_destroy(gfxpage_t*pdf_page)
127 {
128     pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
129     free(pdf_page->internal);pdf_page->internal = 0;
130     free(pdf_page);pdf_page=0;
131 }
132
133 static void render2(gfxpage_t*page, gfxdevice_t*dev, int x,int y, int x1,int y1,int x2,int y2)
134 {
135     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
136     gfxsource_internal_t*i = (gfxsource_internal_t*)pi->parent->internal;
137
138     if(!pi->config_print && pi->nocopy) {msg("<fatal> PDF disallows copying");exit(0);}
139     if(pi->config_print && pi->noprint) {msg("<fatal> PDF disallows printing");exit(0);}
140
141     CommonOutputDev*outputDev = 0;
142     if(pi->config_full_bitmap_optimizing) {
143         FullBitmapOutputDev*d = new FullBitmapOutputDev(pi->info, pi->doc);
144         outputDev = (CommonOutputDev*)d;
145     } else if(pi->config_bitmap_optimizing) {
146         BitmapOutputDev*d = new BitmapOutputDev(pi->info, pi->doc);
147         outputDev = (CommonOutputDev*)d;
148     } else {
149         GFXOutputDev*d = new GFXOutputDev(pi->info, pi->doc);
150         outputDev = (CommonOutputDev*)d;
151     }
152     /* pass global parameters to PDF driver*/
153     parameter_t*p = i->parameters.device_config;
154     while(p) {
155         outputDev->setParameter(p->name, p->value);
156         p = p->next;
157     }
158     p = pi->parameters.device_config;
159     while(p) {
160         outputDev->setParameter(p->name, p->value);
161         p = p->next;
162     }
163
164     outputDev->setPageMap(pi->pagemap, pi->pagemap_pos);
165     outputDev->setMove(x,y);
166     outputDev->setClip(x1,y1,x2,y2);
167
168     gfxdevice_t* middev=0;
169     if(multiply>1) {
170         middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
171         gfxdevice_rescale_init(middev, 0x00000000, 0, 0, 1.0 / multiply);
172         gfxdevice_rescale_setdevice(middev, dev);
173         middev->setparameter(middev, "protect", "1");
174         dev = middev;
175     } 
176         
177     if(!pi) {
178         msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
179         return;
180     }
181
182     if(!pi->pages[page->nr-1].has_info) {
183         msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
184         return;
185     }
186
187     if(pi->protect) {
188         dev->setparameter(dev, "protect", "1");
189     }
190
191     outputDev->setDevice(dev);
192     pi->doc->displayPage((OutputDev*)outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, pi->config_print);
193     pi->doc->processLinks((OutputDev*)outputDev, page->nr);
194     outputDev->finishPage();
195     outputDev->setDevice(0);
196     delete outputDev;
197
198     if(middev) {
199         gfxdevice_rescale_setdevice(middev, 0x00000000);
200         middev->finish(middev);
201     }
202
203 }
204
205     
206 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
207 {
208     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
209     render2(page, output, 0,0, 0,0,0,0);
210 }
211
212 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)
213 {
214     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
215
216     int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
217     if((x1|y1|x2|y2)==0) x2++;
218
219     render2(page, output, (int)x*multiply,(int)y*multiply,
220                           (int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
221 }
222
223 void pdf_doc_destroy(gfxdocument_t*gfx)
224 {
225     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
226
227     delete i->doc; i->doc=0;
228     free(i->pages); i->pages = 0;
229
230     i->docinfo.free();
231
232     if(i->filename) {
233         free(i->filename);i->filename=0;
234     }
235     
236     if(i->info) {
237         delete i->info;i->info=0;
238     }
239
240     free(gfx->internal);gfx->internal=0;
241     free(gfx);gfx=0;
242
243     if(global_page_range) {
244         free(global_page_range);
245         global_page_range = 0;
246     }
247     
248     /*globalparams_count--;
249     if(!globalparams_count) {
250         delete globalParams;
251         globalParams = 0;
252         globalparams_count = 0;
253     }*/
254 }
255
256 static void add_page_to_map(gfxdocument_t*gfx, int pdfpage, int outputpage)
257 {
258     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
259     if(pdfpage < 0)
260         return;
261     if(pdfpage >= i->pagemap_size) {
262         int oldlen = i->pagemap_size;
263         i->pagemap_size = oldlen + 1024;
264         if(pdfpage > i->pagemap_size)
265             i->pagemap_size = pdfpage+1;
266
267         if(i->pages) {
268             i->pagemap = (int*)malloc(i->pagemap_size*sizeof(int));
269         } else {
270             i->pagemap = (int*)realloc(i->pages, i->pagemap_size*sizeof(int));
271         }
272         memset(&i->pagemap[oldlen], -1, (i->pagemap_size-oldlen)*sizeof(int));
273     }
274     i->pagemap[pdfpage] = outputpage;
275     if(pdfpage > i->pagemap_pos)
276         i->pagemap_pos = pdfpage;
277 }
278
279 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
280 {
281     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
282     if(!strcmp(name, "pagemap")) {
283         int pdfpage=0, outputpage=0;
284         sscanf(value,"%d:%d", &pdfpage, &outputpage);
285         add_page_to_map(gfx, pdfpage, outputpage);
286     } else if(!strcmp(name, "poly2bitmap")) {
287         i->config_bitmap_optimizing = atoi(value);
288     } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
289         i->config_full_bitmap_optimizing = atoi(value);
290     } else if(!strcmp(name, "asprint")) {
291         i->config_print = 1;
292     } else {
293         storeDeviceParameter(&i->parameters, name, value);
294     }
295 }
296
297 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
298 {
299     pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
300
301     if(page < 1 || page > doc->num_pages)
302         return 0;
303     
304     gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
305     pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
306     memset(pi, 0, sizeof(pdf_page_internal_t));
307     pdf_page->internal = pi;
308
309     pdf_page->destroy = pdfpage_destroy;
310     pdf_page->render = pdfpage_render;
311     pdf_page->rendersection = pdfpage_rendersection;
312     pdf_page->width = di->pages[page-1].width;
313     pdf_page->height = di->pages[page-1].height;
314
315     pdf_page->parent = doc;
316     pdf_page->nr = page;
317     return pdf_page;
318 }
319
320 static char*getInfoString(Dict *infoDict, const char *key)
321 {
322     Object obj;
323     GString *s1, *s2;
324     int i;
325
326     if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
327         s1 = obj.getString();
328         if ((s1->getChar(0) & 0xff) == 0xfe &&
329             (s1->getChar(1) & 0xff) == 0xff) {
330             s2 = new GString();
331             for (i = 2; i < obj.getString()->getLength(); i += 2) {
332               if (s1->getChar(i) == '\0') {
333                 s2->append(s1->getChar(i+1));
334               } else {
335                 delete s2;
336                 s2 = new GString("<unicode>");
337                 break;
338               }
339             }
340             char*ret = strdup(s2->getCString());
341             delete s2;
342             obj.free();
343             return ret;
344         } else {
345             char*ret = strdup(s1->getCString());
346             obj.free();
347             return ret;
348         }
349     }
350     return strdup("");
351 }
352
353 static char*getInfoDate(Dict *infoDict, const char *key) 
354 {
355     Object obj;
356     char *s;
357
358     if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
359         s = obj.getString()->getCString();
360         if (s[0] == 'D' && s[1] == ':') {
361           s += 2;
362         }
363         char*ret = strdup(s);
364         obj.free();
365         return ret;
366     }
367     return strdup("");
368 }
369
370 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
371 {
372     pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
373     if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
374     else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
375     else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
376     else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
377     else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
378     else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
379     else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
380     else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
381     else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
382     else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
383     else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
384     else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
385     else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
386     else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
387     else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
388     else if(!strcmp(name, "version")) { 
389         char buf[32];
390         sprintf(buf, "%.1f", i->doc->getPDFVersion());
391         return strdup(buf);
392     }
393     return strdup("");
394 }
395
396
397 /* shortcut to InfoOutputDev.cc */
398 extern int config_addspace;
399 extern int config_fontquality;
400 extern int config_bigchar;
401
402 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
403 {
404     gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
405
406     msg("<verbose> setting parameter %s to \"%s\"", name, value);
407     if(!strncmp(name, "fontdir", strlen("fontdir"))) {
408         addGlobalFontDir(value);
409     } else if(!strcmp(name, "detectspaces")) {
410         config_addspace = atoi(value);
411     } else if(!strcmp(name, "fontquality")) {
412         config_fontquality = atoi(value);
413     } else if(!strcmp(name, "bigchar")) {
414         config_bigchar = atoi(value);
415     } else if(!strcmp(name, "pages")) {
416         global_page_range = strdup(value);
417     } else if(!strncmp(name, "font", strlen("font")) && name[4]!='q') {
418         addGlobalFont(value);
419     } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
420         addGlobalLanguageDir(value);
421     } else if(!strcmp(name, "zoomtowidth")) {
422         zoomtowidth = atoi(value);
423     } else if(!strcmp(name, "zoom")) {
424         char buf[80];
425         zoom = atof(value);
426     } else if(!strcmp(name, "jpegdpi") || !strcmp(name, "ppmdpi")) {
427         msg("<error> %s not supported anymore. Please use jpegsubpixels/ppmsubpixels");
428     } else if(!strcmp(name, "multiply")) {
429         multiply = atoi(value);
430     } else if(!strcmp(name, "help")) {
431         printf("\nPDF device global parameters:\n");
432         printf("fontdir=<dir>     a directory with additional fonts\n");
433         printf("font=<filename>   an additional font filename\n");
434         printf("pages=<range>     the range of pages to convert (example: pages=1-100,210-)\n");
435         printf("zoom=<dpi>        the resultion (default: 72)\n");
436         printf("languagedir=<dir> Add an xpdf language directory\n");
437         printf("multiply=<times>  Render everything at <times> the resolution\n");
438         printf("poly2bitmap       Convert graphics to bitmaps\n");
439         printf("bitmap            Convert everything to bitmaps\n");
440     }   
441 }
442
443 void pdf_doc_prepare(gfxdocument_t*doc, gfxdevice_t*dev)
444 {
445     pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
446     i->info->dumpfonts(dev);
447 }
448
449 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
450 {
451     gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
452     gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
453     memset(pdf_doc, 0, sizeof(gfxdocument_t));
454     pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
455     memset(i, 0, sizeof(pdf_doc_internal_t));
456     i->parent = src;
457     pdf_doc->internal = i;
458     char*userPassword=0;
459     
460     i->filename = strdup(filename);
461
462     char*x = 0;
463     if((x = strchr((char*)filename, '|'))) {
464         *x = 0;
465         userPassword = x+1;
466     }
467     
468     GString *fileName = new GString(filename);
469     GString *userPW;
470
471     // open PDF file
472     if (userPassword && userPassword[0]) {
473       userPW = new GString(userPassword);
474     } else {
475       userPW = NULL;
476     }
477     i->doc = new PDFDoc(fileName, userPW);
478     if (userPW) {
479       delete userPW;
480     }
481     if (!i->doc->isOk()) {
482         printf("xpdf reports document as broken.\n");
483         return 0;
484     }
485
486     // get doc info
487     i->doc->getDocInfo(&i->docinfo);
488     
489     pdf_doc->num_pages = i->doc->getNumPages();
490     i->protect = 0;
491     if (i->doc->isEncrypted()) {
492           if(!i->doc->okToCopy()) {
493               i->nocopy = 1;
494           }
495           if(!i->doc->okToPrint()) {
496               i->noprint = 1;
497           }
498           if(!i->doc->okToChange() || !i->doc->okToAddNotes())
499               i->protect = 1;
500     }
501         
502     if(zoomtowidth && i->doc->getNumPages()) {
503         Page*page = i->doc->getCatalog()->getPage(1);
504         PDFRectangle *r = page->getCropBox();
505         double width_before = r->x2 - r->x1;
506         zoom = 72.0 * zoomtowidth / width_before;
507         msg("<notice> Rendering at %f DPI. (Page width at 72 DPI: %f, target width: %d)", zoom, width_before, zoomtowidth);
508     }
509
510     i->info = new InfoOutputDev(i->doc->getXRef());
511     int t;
512     i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
513     memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
514     for(t=1;t<=pdf_doc->num_pages;t++) {
515         if(!global_page_range || is_in_range(t, global_page_range)) {
516             i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, i->config_print);
517             i->doc->processLinks((OutputDev*)i->info, t);
518             i->pages[t-1].xMin = i->info->x1;
519             i->pages[t-1].yMin = i->info->y1;
520             i->pages[t-1].xMax = i->info->x2;
521             i->pages[t-1].yMax = i->info->y2;
522             i->pages[t-1].width = i->info->x2 - i->info->x1;
523             i->pages[t-1].height = i->info->y2 - i->info->y1;
524             i->pages[t-1].number_of_images = i->info->num_ppm_images + i->info->num_jpeg_images;
525             i->pages[t-1].number_of_links = i->info->num_links;
526             i->pages[t-1].number_of_fonts = i->info->num_fonts;
527             i->pages[t-1].has_info = 1;
528         }
529     }
530
531     pdf_doc->get = 0;
532     pdf_doc->destroy = pdf_doc_destroy;
533     pdf_doc->set_parameter = pdf_doc_set_parameter;
534     pdf_doc->getinfo = pdf_doc_getinfo;
535     pdf_doc->getpage = pdf_doc_getpage;
536     pdf_doc->prepare = pdf_doc_prepare;
537     
538     return pdf_doc;
539
540 }
541     
542 void pdf_destroy(gfxsource_t*src)
543 {
544     if(!src->internal)
545         return;
546     gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
547     
548     parameter_t*p = i->parameters.device_config;
549     while(p) {
550         parameter_t*next = p->next;
551         if(p->name) free((void*)p->name);p->name = 0;
552         if(p->value) free((void*)p->value);p->value =0;
553         p->next = 0;delete p;
554         p = next;
555     }
556     i->parameters.device_config=i->parameters.device_config_next=0;
557     
558     free(src->internal);src->internal=0;
559
560     delete globalParams;globalParams = 0;
561     free(src);
562 }
563
564 gfxsource_t*gfxsource_pdf_create()
565 {
566     gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
567     memset(src, 0, sizeof(gfxsource_t));
568     src->set_parameter = pdf_set_parameter;
569     src->open = pdf_open;
570     src->destroy = pdf_destroy;
571     src->internal = malloc(sizeof(gfxsource_internal_t));
572     memset(src->internal, 0, sizeof(gfxsource_internal_t));
573
574     if(!globalParams) {
575         globalParams = new GFXGlobalParams();
576         //globalparams_count++;
577     }
578     
579
580     return src;
581 }