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