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