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