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