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