3 #include "../gfxdevice.h"
4 #include "../gfxsource.h"
5 #include "../devices/rescale.h"
8 #include "GlobalParams.h"
9 #include "InfoOutputDev.h"
10 #include "GFXOutputDev.h"
11 #include "FullBitmapOutputDev.h"
12 #include "BitmapOutputDev.h"
18 static double zoom = 72; /* xpdf: 86 */
19 static int zoomtowidth = 0;
20 static int multiply = 1;
21 static char* global_page_range = 0;
23 static int globalparams_count=0;
25 typedef struct _pdf_page_info
27 int xMin, yMin, xMax, yMax;
35 typedef struct _pdf_doc_internal
37 char config_bitmap_optimizing;
38 char config_full_bitmap_optimizing;
40 gfxparams_t* parameters;
50 pdf_page_info_t*pages;
61 typedef struct _pdf_page_internal
63 } pdf_page_internal_t;
65 typedef struct _dev_output_internal
67 CommonOutputDev*outputDev;
68 } dev_output_internal_t;
71 typedef struct _gfxsource_internal
73 gfxparams_t* parameters;
74 } gfxsource_internal_t;
77 static const char* dirseparator()
86 void pdfpage_destroy(gfxpage_t*pdf_page)
88 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
89 free(pdf_page->internal);pdf_page->internal = 0;
90 free(pdf_page);pdf_page=0;
93 static void render2(gfxpage_t*page, gfxdevice_t*dev, int x,int y, int x1,int y1,int x2,int y2)
95 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
96 gfxsource_internal_t*i = (gfxsource_internal_t*)pi->parent->internal;
98 if(!pi->config_print && pi->nocopy) {msg("<fatal> PDF disallows copying");exit(0);}
99 if(pi->config_print && pi->noprint) {msg("<fatal> PDF disallows printing");exit(0);}
101 CommonOutputDev*outputDev = 0;
102 if(pi->config_full_bitmap_optimizing) {
103 FullBitmapOutputDev*d = new FullBitmapOutputDev(pi->info, pi->doc);
104 outputDev = (CommonOutputDev*)d;
105 } else if(pi->config_bitmap_optimizing) {
106 BitmapOutputDev*d = new BitmapOutputDev(pi->info, pi->doc);
107 outputDev = (CommonOutputDev*)d;
109 GFXOutputDev*d = new GFXOutputDev(pi->info, pi->doc);
110 outputDev = (CommonOutputDev*)d;
112 /* pass global parameters to PDF driver*/
113 gfxparam_t*p = i->parameters->params;
115 outputDev->setParameter(p->key, p->value);
118 p = pi->parameters->params;
120 outputDev->setParameter(p->key, p->value);
124 outputDev->setPageMap(pi->pagemap, pi->pagemap_pos);
125 outputDev->setMove(x,y);
126 outputDev->setClip(x1,y1,x2,y2);
128 gfxdevice_t* middev=0;
130 middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
131 gfxdevice_rescale_init(middev, 0x00000000, 0, 0, 1.0 / multiply);
132 gfxdevice_rescale_setdevice(middev, dev);
133 middev->setparameter(middev, "protect", "1");
138 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
142 if(!pi->pages[page->nr-1].has_info) {
143 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
148 dev->setparameter(dev, "protect", "1");
151 outputDev->setDevice(dev);
152 pi->doc->displayPage((OutputDev*)outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, pi->config_print);
153 pi->doc->processLinks((OutputDev*)outputDev, page->nr);
154 outputDev->finishPage();
155 outputDev->setDevice(0);
159 gfxdevice_rescale_setdevice(middev, 0x00000000);
160 middev->finish(middev);
166 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
168 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
169 render2(page, output, 0,0, 0,0,0,0);
172 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)
174 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
176 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
177 if((x1|y1|x2|y2)==0) x2++;
179 render2(page, output, (int)x*multiply,(int)y*multiply,
180 (int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
183 void pdf_doc_destroy(gfxdocument_t*gfx)
185 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
187 delete i->doc; i->doc=0;
188 free(i->pages); i->pages = 0;
193 free(i->filename);i->filename=0;
197 delete i->info;i->info=0;
200 free(gfx->internal);gfx->internal=0;
203 if(global_page_range) {
204 free(global_page_range);
205 global_page_range = 0;
208 /*globalparams_count--;
209 if(!globalparams_count) {
212 globalparams_count = 0;
216 static void add_page_to_map(gfxdocument_t*gfx, int pdfpage, int outputpage)
218 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
221 if(pdfpage >= i->pagemap_size) {
222 int oldlen = i->pagemap_size;
223 i->pagemap_size = oldlen + 1024;
224 if(pdfpage > i->pagemap_size)
225 i->pagemap_size = pdfpage+1;
228 i->pagemap = (int*)malloc(i->pagemap_size*sizeof(int));
230 i->pagemap = (int*)realloc(i->pages, i->pagemap_size*sizeof(int));
232 memset(&i->pagemap[oldlen], -1, (i->pagemap_size-oldlen)*sizeof(int));
234 i->pagemap[pdfpage] = outputpage;
235 if(pdfpage > i->pagemap_pos)
236 i->pagemap_pos = pdfpage;
239 void pdf_doc_setparameter(gfxdocument_t*gfx, const char*name, const char*value)
241 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
242 if(!strcmp(name, "pagemap")) {
243 int pdfpage=0, outputpage=0;
244 sscanf(value,"%d:%d", &pdfpage, &outputpage);
245 add_page_to_map(gfx, pdfpage, outputpage);
246 } else if(!strcmp(name, "poly2bitmap")) {
247 i->config_bitmap_optimizing = atoi(value);
248 } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
249 i->config_full_bitmap_optimizing = atoi(value);
250 } else if(!strcmp(name, "asprint")) {
253 gfxparams_store(i->parameters, name, value);
257 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
259 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
261 if(page < 1 || page > doc->num_pages)
264 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
265 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
266 memset(pi, 0, sizeof(pdf_page_internal_t));
267 pdf_page->internal = pi;
269 pdf_page->destroy = pdfpage_destroy;
270 pdf_page->render = pdfpage_render;
271 pdf_page->rendersection = pdfpage_rendersection;
272 pdf_page->width = di->pages[page-1].width;
273 pdf_page->height = di->pages[page-1].height;
275 pdf_page->parent = doc;
280 static char*getInfoString(Dict *infoDict, const char *key)
286 if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
287 s1 = obj.getString();
288 if ((s1->getChar(0) & 0xff) == 0xfe &&
289 (s1->getChar(1) & 0xff) == 0xff) {
291 for (i = 2; i < obj.getString()->getLength(); i += 2) {
292 if (s1->getChar(i) == '\0') {
293 s2->append(s1->getChar(i+1));
296 s2 = new GString("<unicode>");
300 char*ret = strdup(s2->getCString());
305 char*ret = strdup(s1->getCString());
313 static char*getInfoDate(Dict *infoDict, const char *key)
318 if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
319 s = obj.getString()->getCString();
320 if (s[0] == 'D' && s[1] == ':') {
323 char*ret = strdup(s);
330 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
332 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
333 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
334 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
335 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
336 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
337 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
338 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
339 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
340 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
341 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
342 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
343 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
344 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
345 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
346 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
347 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
348 else if(!strcmp(name, "version")) {
350 sprintf(buf, "%.1f", i->doc->getPDFVersion());
357 /* shortcut to InfoOutputDev.cc */
358 extern int config_addspace;
359 extern int config_fontquality;
360 extern int config_bigchar;
362 static void pdf_setparameter(gfxsource_t*src, const char*name, const char*value)
364 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
366 gfxparams_store(i->parameters, name, value);
368 msg("<verbose> setting parameter %s to \"%s\"", name, value);
369 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
370 addGlobalFontDir(value);
371 } else if(!strcmp(name, "addspacechars")) {
372 config_addspace = atoi(value);
373 gfxparams_store(i->parameters, "detectspaces", 0);
374 } else if(!strcmp(name, "detectspaces")) {
375 config_addspace = atoi(value);
376 } else if(!strcmp(name, "fontquality")) {
377 config_fontquality = atoi(value);
378 } else if(!strcmp(name, "bigchar")) {
379 config_bigchar = atoi(value);
380 } else if(!strcmp(name, "pages")) {
381 global_page_range = strdup(value);
382 } else if(!strncmp(name, "font", strlen("font")) && name[4]!='q') {
383 addGlobalFont(value);
384 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
385 addGlobalLanguageDir(value);
386 } else if(!strcmp(name, "zoomtowidth")) {
387 zoomtowidth = atoi(value);
388 } else if(!strcmp(name, "zoom")) {
391 } else if(!strcmp(name, "jpegdpi") || !strcmp(name, "ppmdpi")) {
392 msg("<error> %s not supported anymore. Please use jpegsubpixels/ppmsubpixels");
393 } else if(!strcmp(name, "multiply")) {
394 multiply = atoi(value);
395 } else if(!strcmp(name, "help")) {
396 printf("\nPDF device global parameters:\n");
397 printf("fontdir=<dir> a directory with additional fonts\n");
398 printf("font=<filename> an additional font filename\n");
399 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
400 printf("zoom=<dpi> the resultion (default: 72)\n");
401 printf("languagedir=<dir> Add an xpdf language directory\n");
402 printf("multiply=<times> Render everything at <times> the resolution\n");
403 printf("poly2bitmap Convert graphics to bitmaps\n");
404 printf("bitmap Convert everything to bitmaps\n");
408 void pdf_doc_prepare(gfxdocument_t*doc, gfxdevice_t*dev)
410 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
411 i->info->dumpfonts(dev);
414 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
416 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
417 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
418 memset(pdf_doc, 0, sizeof(gfxdocument_t));
419 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
420 memset(i, 0, sizeof(pdf_doc_internal_t));
422 i->parameters = gfxparams_new();
423 pdf_doc->internal = i;
426 i->filename = strdup(filename);
429 if((x = strchr((char*)filename, '|'))) {
434 GString *fileName = new GString(filename);
438 if (userPassword && userPassword[0]) {
439 userPW = new GString(userPassword);
443 i->doc = new PDFDoc(fileName, userPW);
447 if (!i->doc->isOk()) {
452 i->doc->getDocInfo(&i->docinfo);
454 pdf_doc->num_pages = i->doc->getNumPages();
456 if (i->doc->isEncrypted()) {
457 if(!i->doc->okToCopy()) {
460 if(!i->doc->okToPrint()) {
463 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
467 if(zoomtowidth && i->doc->getNumPages()) {
468 Page*page = i->doc->getCatalog()->getPage(1);
469 PDFRectangle *r = page->getCropBox();
470 double width_before = r->x2 - r->x1;
471 zoom = 72.0 * zoomtowidth / width_before;
472 msg("<notice> Rendering at %f DPI. (Page width at 72 DPI: %f, target width: %d)", zoom, width_before, zoomtowidth);
475 i->info = new InfoOutputDev(i->doc->getXRef());
477 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
478 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
479 for(t=1;t<=pdf_doc->num_pages;t++) {
480 if(!global_page_range || is_in_range(t, global_page_range)) {
481 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, i->config_print);
482 i->doc->processLinks((OutputDev*)i->info, t);
483 i->pages[t-1].xMin = i->info->x1;
484 i->pages[t-1].yMin = i->info->y1;
485 i->pages[t-1].xMax = i->info->x2;
486 i->pages[t-1].yMax = i->info->y2;
487 i->pages[t-1].width = i->info->x2 - i->info->x1;
488 i->pages[t-1].height = i->info->y2 - i->info->y1;
489 i->pages[t-1].number_of_images = i->info->num_ppm_images + i->info->num_jpeg_images;
490 i->pages[t-1].number_of_links = i->info->num_links;
491 i->pages[t-1].number_of_fonts = i->info->num_fonts;
492 i->pages[t-1].has_info = 1;
497 pdf_doc->destroy = pdf_doc_destroy;
498 pdf_doc->setparameter = pdf_doc_setparameter;
499 pdf_doc->getinfo = pdf_doc_getinfo;
500 pdf_doc->getpage = pdf_doc_getpage;
501 pdf_doc->prepare = pdf_doc_prepare;
503 /* pass global parameters to PDF driver*/
504 gfxparam_t*p = isrc->parameters->params;
506 pdf_doc->setparameter(pdf_doc, p->key, p->value);
513 void pdf_destroy(gfxsource_t*src)
517 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
519 gfxparams_free(i->parameters);
522 free(src->internal);src->internal=0;
524 delete globalParams;globalParams = 0;
528 gfxsource_t*gfxsource_pdf_create()
530 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
531 memset(src, 0, sizeof(gfxsource_t));
532 src->setparameter = pdf_setparameter;
533 src->open = pdf_open;
534 src->destroy = pdf_destroy;
535 gfxsource_internal_t*i = (gfxsource_internal_t*)rfx_calloc(sizeof(gfxsource_internal_t));
536 src->internal = (void*)i;
537 i->parameters = gfxparams_new();
540 globalParams = new GFXGlobalParams();
541 //globalparams_count++;