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