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