1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
3 #include "../devices/rescale.h"
6 #include "GlobalParams.h"
7 #include "InfoOutputDev.h"
8 #include "GFXOutputDev.h"
9 #include "BitmapOutputDev.h"
15 static double zoom = 72; /* xpdf: 86 */
16 static int jpeg_dpi = 0;
17 static int ppm_dpi = 0;
18 static int multiply = 1;
19 static char* global_page_range = 0;
21 static parameter_t* device_config = 0;
22 static parameter_t* device_config_next = 0;
24 typedef struct _pdf_page_info
26 int xMin, yMin, xMax, yMax;
34 typedef struct _pdf_doc_internal
40 CommonOutputDev*outputDev;
41 pdf_page_info_t*pages;
45 typedef struct _pdf_page_internal
47 } pdf_page_internal_t;
49 typedef struct _dev_output_internal
51 CommonOutputDev*outputDev;
52 } dev_output_internal_t;
55 static char* dirseparator()
65 void pdfpage_destroy(gfxpage_t*pdf_page)
67 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
68 free(pdf_page->internal);pdf_page->internal = 0;
69 free(pdf_page);pdf_page=0;
72 void render2(gfxpage_t*page, gfxdevice_t*dev)
74 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
77 gfxdevice_rescale_setdevice(pi->middev, dev);
78 pi->middev->setparameter(pi->middev, "protect", "1");
81 dev->setparameter(dev, "protect", "1");
84 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
88 if(!pi->pages[page->nr-1].has_info) {
89 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
93 pi->outputDev->setDevice(dev);
96 dev->setparameter(dev, "protect", "1");
99 /* pass global parameters to output device */
100 parameter_t*p = device_config;
102 dev->setparameter(dev, p->name, p->value);
105 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, /*doLinks*/(int)1);
106 pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
107 pi->outputDev->setDevice(0);
109 gfxdevice_rescale_setdevice(pi->middev, 0x00000000);
114 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
116 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
117 pi->outputDev->setMove(0,0);
118 pi->outputDev->setClip(0,0,0,0);
119 render2(page, output);
122 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)
124 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
126 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
127 if((x1|y1|x2|y2)==0) x2++;
129 pi->outputDev->setMove((int)x*multiply,(int)y*multiply);
130 pi->outputDev->setClip((int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
131 render2(page, output);
134 static int globalparams_count=0;
136 void pdf_doc_destroy(gfxdocument_t*gfx)
138 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
141 delete i->outputDev;i->outputDev=0;
144 gfxdevice_rescale_setdevice(i->middev, 0x00000000);
145 i->middev->finish(i->middev);
147 delete i->doc; i->doc=0;
148 free(i->pages); i->pages = 0;
153 delete i->info;i->info=0;
156 free(gfx->internal);gfx->internal=0;
159 if(global_page_range) {
160 free(global_page_range);
161 global_page_range = 0;
164 /*globalparams_count--;
165 if(!globalparams_count) {
168 globalparams_count = 0;
172 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
174 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
175 CommonOutputDev*o = i->outputDev;
176 if(!strcmp(name, "pagemap")) {
177 int pdfpage=0, outputpage=0;
178 sscanf(value,"%d:%d", &pdfpage, &outputpage);
179 o->preparePage(pdfpage, outputpage);
181 o->setParameter(name, value);
185 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
187 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
189 if(page < 1 || page > doc->num_pages)
192 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
193 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
194 memset(pi, 0, sizeof(pdf_page_internal_t));
195 pdf_page->internal = pi;
197 pdf_page->destroy = pdfpage_destroy;
198 pdf_page->render = pdfpage_render;
199 pdf_page->rendersection = pdfpage_rendersection;
200 pdf_page->width = di->pages[page-1].width;
201 pdf_page->height = di->pages[page-1].height;
203 pdf_page->parent = doc;
208 static char*getInfoString(Dict *infoDict, char *key)
214 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
215 s1 = obj.getString();
216 if ((s1->getChar(0) & 0xff) == 0xfe &&
217 (s1->getChar(1) & 0xff) == 0xff) {
219 for (i = 2; i < obj.getString()->getLength(); i += 2) {
220 if (s1->getChar(i) == '\0') {
221 s2->append(s1->getChar(i+1));
224 s2 = new GString("<unicode>");
228 char*ret = strdup(s2->getCString());
233 char*ret = strdup(s1->getCString());
241 static char*getInfoDate(Dict *infoDict, char *key)
246 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
247 s = obj.getString()->getCString();
248 if (s[0] == 'D' && s[1] == ':') {
251 char*ret = strdup(s);
258 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
260 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
261 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
262 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
263 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
264 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
265 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
266 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
267 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
268 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
269 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
270 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
271 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
272 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
273 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
274 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
275 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
276 else if(!strcmp(name, "version")) {
278 sprintf(buf, "%.1f", i->doc->getPDFVersion());
285 static void storeDeviceParameter(const char*name, const char*value)
287 parameter_t*p = new parameter_t();
288 p->name = strdup(name);
289 p->value = strdup(value);
291 if(device_config_next) {
292 device_config_next->next = p;
293 device_config_next = p;
296 device_config_next = p;
300 typedef struct _gfxsource_internal
302 int config_bitmap_optimizing;
303 } gfxsource_internal_t;
305 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
307 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
308 msg("<verbose> setting parameter %s to \"%s\"", name, value);
309 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
310 addGlobalFontDir(value);
311 } else if(!strcmp(name, "pages")) {
312 global_page_range = strdup(value);
313 } else if(!strncmp(name, "font", strlen("font"))) {
314 addGlobalFont(value);
315 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
316 addGlobalLanguageDir(value);
317 } else if(!strcmp(name, "zoom")) {
320 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
321 storeDeviceParameter("jpegsubpixels", buf);
322 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
323 storeDeviceParameter("ppmsubpixels", buf);
324 } else if(!strcmp(name, "jpegdpi")) {
326 jpeg_dpi = atoi(value);
327 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
328 storeDeviceParameter("jpegsubpixels", buf);
329 } else if(!strcmp(name, "ppmdpi")) {
331 ppm_dpi = atoi(value);
332 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
333 storeDeviceParameter("ppmsubpixels", buf);
334 } else if(!strcmp(name, "poly2bitmap")) {
335 i->config_bitmap_optimizing = 1;
336 } else if(!strcmp(name, "multiply")) {
337 multiply = atoi(value);
338 } else if(!strcmp(name, "help")) {
339 printf("\nPDF device global parameters:\n");
340 printf("fontdir=<dir> a directory with additional fonts\n");
341 printf("font=<filename> an dditional font filename\n");
342 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
343 printf("zoom=<dpi> the resultion (default: 72)\n");
345 storeDeviceParameter(name,value);
348 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
350 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
351 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
352 memset(pdf_doc, 0, sizeof(gfxdocument_t));
353 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
354 memset(i, 0, sizeof(pdf_doc_internal_t));
355 pdf_doc->internal = i;
358 filename = strdup(filename);
361 if((x = strchr(filename, '|'))) {
366 GString *fileName = new GString(filename);
370 if (userPassword && userPassword[0]) {
371 userPW = new GString(userPassword);
375 i->doc = new PDFDoc(fileName, userPW);
379 if (!i->doc->isOk()) {
380 printf("xpdf reports document as broken.\n");
385 i->doc->getDocInfo(&i->docinfo);
387 pdf_doc->num_pages = i->doc->getNumPages();
389 if (i->doc->isEncrypted()) {
390 if(!i->doc->okToCopy()) {
391 printf("PDF disallows copying.\n");
394 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
398 i->info = new InfoOutputDev(i->doc->getXRef());
400 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
401 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
402 for(t=1;t<=pdf_doc->num_pages;t++) {
403 if(!global_page_range || is_in_range(t, global_page_range)) {
404 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
405 i->doc->processLinks((OutputDev*)i->info, t);
406 i->pages[t-1].xMin = i->info->x1;
407 i->pages[t-1].yMin = i->info->y1;
408 i->pages[t-1].xMax = i->info->x2;
409 i->pages[t-1].yMax = i->info->y2;
410 i->pages[t-1].width = i->info->x2 - i->info->x1;
411 i->pages[t-1].height = i->info->y2 - i->info->y1;
412 i->pages[t-1].number_of_images = i->info->num_images;
413 i->pages[t-1].number_of_links = i->info->num_links;
414 i->pages[t-1].number_of_fonts = i->info->num_fonts;
415 i->pages[t-1].has_info = 1;
419 if(isrc->config_bitmap_optimizing) {
420 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
421 i->outputDev = (CommonOutputDev*)outputDev;
423 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
424 i->outputDev = (CommonOutputDev*)outputDev;
427 /* pass global parameters to PDF driver*/
428 parameter_t*p = device_config;
430 i->outputDev->setParameter(p->name, p->value);
436 i->middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
437 gfxdevice_rescale_init(i->middev, 0x00000000, 0, 0, 1.0 / multiply);
441 pdf_doc->destroy = pdf_doc_destroy;
442 pdf_doc->set_parameter = pdf_doc_set_parameter;
443 pdf_doc->getinfo = pdf_doc_getinfo;
444 pdf_doc->getpage = pdf_doc_getpage;
451 gfxsource_t*gfxsource_pdf_create()
453 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
454 memset(src, 0, sizeof(gfxsource_t));
455 src->set_parameter = pdf_set_parameter;
456 src->open = pdf_open;
457 src->internal = malloc(sizeof(gfxsource_internal_t));
458 memset(src->internal, 0, sizeof(gfxsource_internal_t));
461 globalParams = new GFXGlobalParams();
462 //globalparams_count++;