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 "FullBitmapOutputDev.h"
10 #include "BitmapOutputDev.h"
16 static double zoom = 72; /* xpdf: 86 */
17 static int jpeg_dpi = 0;
18 static int ppm_dpi = 0;
19 static int multiply = 1;
20 static char* global_page_range = 0;
22 static int globalparams_count=0;
24 typedef struct _parameter
26 struct _parameter *next;
30 typedef struct _parameterlist
32 parameter_t* device_config;
33 parameter_t* device_config_next;
36 typedef struct _pdf_page_info
38 int xMin, yMin, xMax, yMax;
46 typedef struct _pdf_doc_internal
48 char config_bitmap_optimizing;
49 char config_full_bitmap_optimizing;
51 parameterlist_t parameters;
61 pdf_page_info_t*pages;
72 typedef struct _pdf_page_internal
74 } pdf_page_internal_t;
76 typedef struct _dev_output_internal
78 CommonOutputDev*outputDev;
79 } dev_output_internal_t;
82 typedef struct _gfxsource_internal
84 parameterlist_t parameters;
85 } gfxsource_internal_t;
88 static const char* dirseparator()
97 static void storeDeviceParameter(parameterlist_t*i, const char*name, const char*value)
99 parameter_t*o = i->device_config;
101 if(!strcmp(name, o->name)) {
102 /* overwrite old value */
103 free((void*)o->value);
104 o->value = strdup(value);
109 parameter_t*p = new parameter_t();
110 p->name = strdup(name);
111 p->value = strdup(value);
114 if(i->device_config_next) {
115 i->device_config_next->next = p;
116 i->device_config_next = p;
118 i->device_config = p;
119 i->device_config_next = p;
125 void pdfpage_destroy(gfxpage_t*pdf_page)
127 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
128 free(pdf_page->internal);pdf_page->internal = 0;
129 free(pdf_page);pdf_page=0;
132 static void render2(gfxpage_t*page, gfxdevice_t*dev, int x,int y, int x1,int y1,int x2,int y2)
134 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
135 gfxsource_internal_t*i = (gfxsource_internal_t*)pi->parent->internal;
137 if(!pi->config_print && pi->nocopy) {msg("<fatal> PDF disallows copying");exit(0);}
138 if(pi->config_print && pi->noprint) {msg("<fatal> PDF disallows printing");exit(0);}
140 CommonOutputDev*outputDev = 0;
141 if(pi->config_full_bitmap_optimizing) {
142 FullBitmapOutputDev*d = new FullBitmapOutputDev(pi->info, pi->doc);
143 outputDev = (CommonOutputDev*)d;
144 } else if(pi->config_bitmap_optimizing) {
145 BitmapOutputDev*d = new BitmapOutputDev(pi->info, pi->doc);
146 outputDev = (CommonOutputDev*)d;
148 GFXOutputDev*d = new GFXOutputDev(pi->info, pi->doc);
149 outputDev = (CommonOutputDev*)d;
151 /* pass global parameters to PDF driver*/
152 parameter_t*p = i->parameters.device_config;
154 outputDev->setParameter(p->name, p->value);
157 p = pi->parameters.device_config;
159 outputDev->setParameter(p->name, p->value);
163 outputDev->setPageMap(pi->pagemap, pi->pagemap_pos);
164 outputDev->setMove(x,y);
165 outputDev->setClip(x1,y1,x2,y2);
167 gfxdevice_t* middev=0;
169 middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
170 gfxdevice_rescale_init(middev, 0x00000000, 0, 0, 1.0 / multiply);
171 gfxdevice_rescale_setdevice(middev, dev);
172 middev->setparameter(middev, "protect", "1");
177 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
181 if(!pi->pages[page->nr-1].has_info) {
182 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
187 dev->setparameter(dev, "protect", "1");
190 outputDev->setDevice(dev);
191 pi->doc->displayPage((OutputDev*)outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, pi->config_print);
192 pi->doc->processLinks((OutputDev*)outputDev, page->nr);
193 outputDev->finishPage();
194 outputDev->setDevice(0);
198 gfxdevice_rescale_setdevice(middev, 0x00000000);
199 middev->finish(middev);
205 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
207 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
208 render2(page, output, 0,0, 0,0,0,0);
211 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)
213 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
215 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
216 if((x1|y1|x2|y2)==0) x2++;
218 render2(page, output, (int)x*multiply,(int)y*multiply,
219 (int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
222 void pdf_doc_destroy(gfxdocument_t*gfx)
224 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
226 delete i->doc; i->doc=0;
227 free(i->pages); i->pages = 0;
232 free(i->filename);i->filename=0;
236 delete i->info;i->info=0;
239 free(gfx->internal);gfx->internal=0;
242 if(global_page_range) {
243 free(global_page_range);
244 global_page_range = 0;
247 /*globalparams_count--;
248 if(!globalparams_count) {
251 globalparams_count = 0;
255 static void add_page_to_map(gfxdocument_t*gfx, int pdfpage, int outputpage)
257 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
260 if(pdfpage >= i->pagemap_size) {
261 int oldlen = i->pagemap_size;
262 i->pagemap_size = oldlen + 1024;
263 if(pdfpage > i->pagemap_size)
264 i->pagemap_size = pdfpage+1;
267 i->pagemap = (int*)malloc(i->pagemap_size*sizeof(int));
269 i->pagemap = (int*)realloc(i->pages, i->pagemap_size*sizeof(int));
271 memset(&i->pagemap[oldlen], -1, (i->pagemap_size-oldlen)*sizeof(int));
273 i->pagemap[pdfpage] = outputpage;
274 if(pdfpage > i->pagemap_pos)
275 i->pagemap_pos = pdfpage;
278 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
280 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
281 if(!strcmp(name, "pagemap")) {
282 int pdfpage=0, outputpage=0;
283 sscanf(value,"%d:%d", &pdfpage, &outputpage);
284 add_page_to_map(gfx, pdfpage, outputpage);
285 } else if(!strcmp(name, "poly2bitmap")) {
286 i->config_bitmap_optimizing = atoi(value);
287 } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
288 i->config_full_bitmap_optimizing = atoi(value);
289 } else if(!strcmp(name, "asprint")) {
292 storeDeviceParameter(&i->parameters, name, value);
296 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
298 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
300 if(page < 1 || page > doc->num_pages)
303 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
304 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
305 memset(pi, 0, sizeof(pdf_page_internal_t));
306 pdf_page->internal = pi;
308 pdf_page->destroy = pdfpage_destroy;
309 pdf_page->render = pdfpage_render;
310 pdf_page->rendersection = pdfpage_rendersection;
311 pdf_page->width = di->pages[page-1].width;
312 pdf_page->height = di->pages[page-1].height;
314 pdf_page->parent = doc;
319 static char*getInfoString(Dict *infoDict, const char *key)
325 if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
326 s1 = obj.getString();
327 if ((s1->getChar(0) & 0xff) == 0xfe &&
328 (s1->getChar(1) & 0xff) == 0xff) {
330 for (i = 2; i < obj.getString()->getLength(); i += 2) {
331 if (s1->getChar(i) == '\0') {
332 s2->append(s1->getChar(i+1));
335 s2 = new GString("<unicode>");
339 char*ret = strdup(s2->getCString());
344 char*ret = strdup(s1->getCString());
352 static char*getInfoDate(Dict *infoDict, const char *key)
357 if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
358 s = obj.getString()->getCString();
359 if (s[0] == 'D' && s[1] == ':') {
362 char*ret = strdup(s);
369 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
371 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
372 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
373 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
374 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
375 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
376 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
377 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
378 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
379 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
380 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
381 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
382 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
383 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
384 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
385 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
386 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
387 else if(!strcmp(name, "version")) {
389 sprintf(buf, "%.1f", i->doc->getPDFVersion());
396 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
398 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
399 parameterlist_t*p = &i->parameters;
400 msg("<verbose> setting parameter %s to \"%s\"", name, value);
401 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
402 addGlobalFontDir(value);
403 } else if(!strcmp(name, "pages")) {
404 global_page_range = strdup(value);
405 } else if(!strncmp(name, "font", strlen("font")) && name[4]!='q') {
406 addGlobalFont(value);
407 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
408 addGlobalLanguageDir(value);
409 } else if(!strcmp(name, "zoom")) {
412 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
413 storeDeviceParameter(p, "jpegsubpixels", buf);
414 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
415 storeDeviceParameter(p, "ppmsubpixels", buf);
416 } else if(!strcmp(name, "jpegdpi")) {
418 jpeg_dpi = atoi(value);
419 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
420 storeDeviceParameter(p, "jpegsubpixels", buf);
421 } else if(!strcmp(name, "ppmdpi")) {
423 ppm_dpi = atoi(value);
424 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
425 storeDeviceParameter(p, "ppmsubpixels", buf);
426 } else if(!strcmp(name, "multiply")) {
427 multiply = atoi(value);
428 } else if(!strcmp(name, "help")) {
429 printf("\nPDF device global parameters:\n");
430 printf("fontdir=<dir> a directory with additional fonts\n");
431 printf("font=<filename> an additional font filename\n");
432 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
433 printf("zoom=<dpi> the resultion (default: 72)\n");
434 printf("languagedir=<dir> Add an xpdf language directory\n");
435 printf("multiply=<times> Render everything at <times> the resolution\n");
436 printf("poly2bitmap Convert graphics to bitmaps\n");
437 printf("bitmap Convert everything to bitmaps\n");
441 void pdf_doc_prepare(gfxdocument_t*doc, gfxdevice_t*dev)
443 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
444 i->info->dumpfonts(dev);
447 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
449 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
450 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
451 memset(pdf_doc, 0, sizeof(gfxdocument_t));
452 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
453 memset(i, 0, sizeof(pdf_doc_internal_t));
455 pdf_doc->internal = i;
458 i->filename = strdup(filename);
461 if((x = strchr(filename, '|'))) {
466 GString *fileName = new GString(filename);
470 if (userPassword && userPassword[0]) {
471 userPW = new GString(userPassword);
475 i->doc = new PDFDoc(fileName, userPW);
479 if (!i->doc->isOk()) {
480 printf("xpdf reports document as broken.\n");
485 i->doc->getDocInfo(&i->docinfo);
487 pdf_doc->num_pages = i->doc->getNumPages();
489 if (i->doc->isEncrypted()) {
490 if(!i->doc->okToCopy()) {
493 if(!i->doc->okToPrint()) {
496 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
500 i->info = new InfoOutputDev(i->doc->getXRef());
502 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
503 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
504 for(t=1;t<=pdf_doc->num_pages;t++) {
505 if(!global_page_range || is_in_range(t, global_page_range)) {
506 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, i->config_print);
507 i->doc->processLinks((OutputDev*)i->info, t);
508 i->pages[t-1].xMin = i->info->x1;
509 i->pages[t-1].yMin = i->info->y1;
510 i->pages[t-1].xMax = i->info->x2;
511 i->pages[t-1].yMax = i->info->y2;
512 i->pages[t-1].width = i->info->x2 - i->info->x1;
513 i->pages[t-1].height = i->info->y2 - i->info->y1;
514 i->pages[t-1].number_of_images = i->info->num_ppm_images + i->info->num_jpeg_images;
515 i->pages[t-1].number_of_links = i->info->num_links;
516 i->pages[t-1].number_of_fonts = i->info->num_fonts;
517 i->pages[t-1].has_info = 1;
522 pdf_doc->destroy = pdf_doc_destroy;
523 pdf_doc->set_parameter = pdf_doc_set_parameter;
524 pdf_doc->getinfo = pdf_doc_getinfo;
525 pdf_doc->getpage = pdf_doc_getpage;
526 pdf_doc->prepare = pdf_doc_prepare;
532 void pdf_destroy(gfxsource_t*src)
536 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
538 parameter_t*p = i->parameters.device_config;
540 parameter_t*next = p->next;
541 if(p->name) free((void*)p->name);p->name = 0;
542 if(p->value) free((void*)p->value);p->value =0;
543 p->next = 0;delete p;
546 i->parameters.device_config=i->parameters.device_config_next=0;
548 free(src->internal);src->internal=0;
550 delete globalParams;globalParams = 0;
554 gfxsource_t*gfxsource_pdf_create()
556 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
557 memset(src, 0, sizeof(gfxsource_t));
558 src->set_parameter = pdf_set_parameter;
559 src->open = pdf_open;
560 src->destroy = pdf_destroy;
561 src->internal = malloc(sizeof(gfxsource_internal_t));
562 memset(src->internal, 0, sizeof(gfxsource_internal_t));
565 globalParams = new GFXGlobalParams();
566 //globalparams_count++;