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