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