destroy globalparams if no pdf is currently open
[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     pi->outputDev->endframe();
93 }
94
95     
96 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
97 {
98     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
99     pi->outputDev->setDevice(output);
100     pi->outputDev->setMove(0,0);
101     pi->outputDev->setClip(0,0,0,0);
102     render2(page, output);
103     pi->outputDev->setDevice(0);
104 }
105
106 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)
107 {
108     int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
109     pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
110     pi->outputDev->setDevice(output);
111     pi->outputDev->setMove((int)x,(int)y);
112     if((x1|y1|x2|y2)==0) x2++;
113     pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
114     render2(page, output);
115     pi->outputDev->setDevice(0);
116 }
117
118 static int globalparams_count=0;
119
120 void pdf_doc_destroy(gfxdocument_t*gfx)
121 {
122     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
123
124     delete i->doc; i->doc=0;
125     free(i->pages); i->pages = 0;
126
127     i->docinfo.free();
128     
129     if(i->info) {
130         delete i->info;i->info=0;
131     }
132
133     free(gfx->internal);gfx->internal=0;
134     free(gfx);gfx=0;
135
136     if(global_page_range) {
137         free(global_page_range);
138         global_page_range = 0;
139     }
140     
141     globalparams_count--;
142     if(!globalparams_count) {
143         delete globalParams;
144         globalParams = 0;
145         globalparams_count = 0;
146     }
147 }
148
149 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
150 {
151     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
152     GFXOutputDev*o = i->outputDev;
153     if(!strcmp(name, "pagemap")) {
154         int pdfpage=0, outputpage=0;
155         sscanf(value,"%d:%d", &pdfpage, &outputpage);
156         o->preparePage(pdfpage, outputpage);
157     } else {
158         o->setParameter(name, value);
159     }
160 }
161
162 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
163 {
164     pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
165
166     if(page < 1 || page > doc->num_pages)
167         return 0;
168     
169     gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
170     pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
171     memset(pi, 0, sizeof(pdf_page_internal_t));
172     pdf_page->internal = pi;
173
174     pdf_page->destroy = pdfpage_destroy;
175     pdf_page->render = pdfpage_render;
176     pdf_page->rendersection = pdfpage_rendersection;
177     pdf_page->width = di->pages[page-1].width;
178     pdf_page->height = di->pages[page-1].height;
179
180     pdf_page->parent = doc;
181     pdf_page->nr = page;
182     return pdf_page;
183 }
184
185 static char*getInfoString(Dict *infoDict, char *key)
186 {
187     Object obj;
188     GString *s1, *s2;
189     int i;
190
191     if (infoDict && infoDict->lookup(key, &obj)->isString()) {
192         s1 = obj.getString();
193         if ((s1->getChar(0) & 0xff) == 0xfe &&
194             (s1->getChar(1) & 0xff) == 0xff) {
195             s2 = new GString();
196             for (i = 2; i < obj.getString()->getLength(); i += 2) {
197               if (s1->getChar(i) == '\0') {
198                 s2->append(s1->getChar(i+1));
199               } else {
200                 delete s2;
201                 s2 = new GString("<unicode>");
202                 break;
203               }
204             }
205             char*ret = strdup(s2->getCString());
206             delete s2;
207             obj.free();
208             return ret;
209         } else {
210             char*ret = strdup(s1->getCString());
211             obj.free();
212             return ret;
213         }
214     }
215     return strdup("");
216 }
217
218 static char*getInfoDate(Dict *infoDict, char *key) 
219 {
220     Object obj;
221     char *s;
222
223     if (infoDict && infoDict->lookup(key, &obj)->isString()) {
224         s = obj.getString()->getCString();
225         if (s[0] == 'D' && s[1] == ':') {
226           s += 2;
227         }
228         char*ret = strdup(s);
229         obj.free();
230         return ret;
231     }
232     return strdup("");
233 }
234
235 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
236 {
237     pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
238     if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
239     else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
240     else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
241     else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
242     else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
243     else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
244     else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
245     else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
246     else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
247     else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
248     else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
249     else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
250     else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
251     else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
252     else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
253     else if(!strcmp(name, "version")) { 
254         char buf[32];
255         sprintf(buf, "%.1f", i->doc->getPDFVersion());
256         return strdup(buf);
257     }
258     return 0;
259 }
260
261
262 static void storeDeviceParameter(const char*name, const char*value)
263 {
264     parameter_t*p = new parameter_t();
265     p->name = strdup(name);
266     p->value = strdup(value);
267     p->next = 0;
268     if(device_config_next) {
269         device_config_next->next = p;
270         device_config_next = p;
271     } else {
272         device_config = p;
273         device_config_next = p;
274     }
275 }
276
277 static void pdf_set_parameter(const char*name, const char*value)
278 {
279     msg("<verbose> setting parameter %s to \"%s\"", name, value);
280     if(!strncmp(name, "fontdir", strlen("fontdir"))) {
281         addGlobalFontDir(value);
282     } else if(!strcmp(name, "pages")) {
283         global_page_range = strdup(value);
284     } else if(!strncmp(name, "font", strlen("font"))) {
285         addGlobalFont(value);
286     } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
287         addGlobalLanguageDir(value);
288     } else if(!strcmp(name, "zoom")) {
289         char buf[80];
290         zoom = atof(value);
291         sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
292         storeDeviceParameter("jpegsubpixels", buf);
293         sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
294         storeDeviceParameter("ppmsubpixels", buf);
295     } else if(!strcmp(name, "jpegdpi")) {
296         char buf[80];
297         jpeg_dpi = atoi(value);
298         sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
299         storeDeviceParameter("jpegsubpixels", buf);
300     } else if(!strcmp(name, "ppmdpi")) {
301         char buf[80];
302         ppm_dpi = atoi(value);
303         sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
304         storeDeviceParameter("ppmsubpixels", buf);
305     } else {
306         storeDeviceParameter(name,value);
307     }
308 }
309
310 static gfxdocument_t*pdf_open(const char*filename)
311 {
312     if(!globalParams) {
313         globalParams = new GFXGlobalParams();
314         globalparams_count++;
315     }
316     
317     gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
318     memset(pdf_doc, 0, sizeof(gfxdocument_t));
319     pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
320     memset(i, 0, sizeof(pdf_doc_internal_t));
321     pdf_doc->internal = i;
322     char*userPassword=0;
323     
324     filename = strdup(filename);
325
326     char*x = 0;
327     if((x = strchr(filename, '|'))) {
328         *x = 0;
329         userPassword = x+1;
330     }
331     
332     GString *fileName = new GString(filename);
333     GString *userPW;
334
335     // open PDF file
336     if (userPassword && userPassword[0]) {
337       userPW = new GString(userPassword);
338     } else {
339       userPW = NULL;
340     }
341     i->doc = new PDFDoc(fileName, userPW);
342     if (userPW) {
343       delete userPW;
344     }
345     if (!i->doc->isOk()) {
346         printf("xpdf reports document as broken.\n");
347         return 0;
348     }
349
350     // get doc info
351     i->doc->getDocInfo(&i->docinfo);
352     
353     pdf_doc->num_pages = i->doc->getNumPages();
354     i->protect = 0;
355     if (i->doc->isEncrypted()) {
356           if(!i->doc->okToCopy()) {
357               printf("PDF disallows copying.\n");
358               return 0;
359           }
360           if(!i->doc->okToChange() || !i->doc->okToAddNotes())
361               i->protect = 1;
362     }
363
364     InfoOutputDev*io = new InfoOutputDev(i->doc->getXRef());
365     int t;
366     i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
367     memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
368     for(t=1;t<=pdf_doc->num_pages;t++) {
369         if(!global_page_range || is_in_range(t, global_page_range)) {
370             i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
371             i->doc->processLinks((OutputDev*)io, t);
372             i->pages[t-1].xMin = io->x1;
373             i->pages[t-1].yMin = io->y1;
374             i->pages[t-1].xMax = io->x2;
375             i->pages[t-1].yMax = io->y2;
376             i->pages[t-1].width = io->x2 - io->x1;
377             i->pages[t-1].height = io->y2 - io->y1;
378             i->pages[t-1].number_of_images = io->num_images;
379             i->pages[t-1].number_of_links = io->num_links;
380             i->pages[t-1].number_of_fonts = io->num_fonts;
381             i->pages[t-1].has_info = 1;
382         }
383     }
384     i->info = io;
385     i->outputDev = new GFXOutputDev(device_config);
386
387     pdf_doc->get = 0;
388     pdf_doc->destroy = pdf_doc_destroy;
389     pdf_doc->set_parameter = pdf_doc_set_parameter;
390     pdf_doc->getinfo = pdf_doc_getinfo;
391     pdf_doc->getpage = pdf_doc_getpage;
392
393
394     return pdf_doc;
395
396 }
397
398 gfxsource_t*gfxsource_pdf_create()
399 {
400     gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
401     memset(src, 0, sizeof(gfxsource_t));
402     src->set_parameter = pdf_set_parameter;
403     src->open = pdf_open;
404     return src;
405 }