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