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 parameter_t* device_config = 0;
23 static parameter_t* device_config_next = 0;
25 static int globalparams_count=0;
27 typedef struct _pdf_page_info
29 int xMin, yMin, xMax, yMax;
37 typedef struct _pdf_doc_internal
44 CommonOutputDev*outputDev;
45 pdf_page_info_t*pages;
50 typedef struct _pdf_page_internal
52 } pdf_page_internal_t;
54 typedef struct _dev_output_internal
56 CommonOutputDev*outputDev;
57 } dev_output_internal_t;
60 static char* dirseparator()
70 void pdfpage_destroy(gfxpage_t*pdf_page)
72 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
73 free(pdf_page->internal);pdf_page->internal = 0;
74 free(pdf_page);pdf_page=0;
77 void render2(gfxpage_t*page, gfxdevice_t*dev)
79 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
82 gfxdevice_rescale_setdevice(pi->middev, dev);
83 pi->middev->setparameter(pi->middev, "protect", "1");
88 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
92 if(!pi->pages[page->nr-1].has_info) {
93 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
97 pi->outputDev->setDevice(dev);
100 dev->setparameter(dev, "protect", "1");
103 /* pass global parameters to output device */
104 parameter_t*p = device_config;
106 dev->setparameter(dev, p->name, p->value);
109 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, /*doLinks*/(int)1);
110 pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
111 pi->outputDev->finishPage();
113 pi->outputDev->setDevice(0);
115 gfxdevice_rescale_setdevice(pi->middev, 0x00000000);
120 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
122 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
123 pi->outputDev->setMove(0,0);
124 pi->outputDev->setClip(0,0,0,0);
125 render2(page, output);
128 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)
130 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
132 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
133 if((x1|y1|x2|y2)==0) x2++;
135 pi->outputDev->setMove((int)x*multiply,(int)y*multiply);
136 pi->outputDev->setClip((int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
137 render2(page, output);
140 void pdf_doc_destroy(gfxdocument_t*gfx)
142 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
145 delete i->outputDev;i->outputDev=0;
148 gfxdevice_rescale_setdevice(i->middev, 0x00000000);
149 i->middev->finish(i->middev);
151 delete i->doc; i->doc=0;
152 free(i->pages); i->pages = 0;
157 free(i->filename);i->filename=0;
161 delete i->info;i->info=0;
164 free(gfx->internal);gfx->internal=0;
167 if(global_page_range) {
168 free(global_page_range);
169 global_page_range = 0;
172 /*globalparams_count--;
173 if(!globalparams_count) {
176 globalparams_count = 0;
180 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
182 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
183 CommonOutputDev*o = i->outputDev;
184 if(!strcmp(name, "pagemap")) {
185 int pdfpage=0, outputpage=0;
186 sscanf(value,"%d:%d", &pdfpage, &outputpage);
187 o->preparePage(pdfpage, outputpage);
189 o->setParameter(name, value);
193 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
195 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
197 if(page < 1 || page > doc->num_pages)
200 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
201 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
202 memset(pi, 0, sizeof(pdf_page_internal_t));
203 pdf_page->internal = pi;
205 pdf_page->destroy = pdfpage_destroy;
206 pdf_page->render = pdfpage_render;
207 pdf_page->rendersection = pdfpage_rendersection;
208 pdf_page->width = di->pages[page-1].width;
209 pdf_page->height = di->pages[page-1].height;
211 pdf_page->parent = doc;
216 static char*getInfoString(Dict *infoDict, char *key)
222 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
223 s1 = obj.getString();
224 if ((s1->getChar(0) & 0xff) == 0xfe &&
225 (s1->getChar(1) & 0xff) == 0xff) {
227 for (i = 2; i < obj.getString()->getLength(); i += 2) {
228 if (s1->getChar(i) == '\0') {
229 s2->append(s1->getChar(i+1));
232 s2 = new GString("<unicode>");
236 char*ret = strdup(s2->getCString());
241 char*ret = strdup(s1->getCString());
249 static char*getInfoDate(Dict *infoDict, char *key)
254 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
255 s = obj.getString()->getCString();
256 if (s[0] == 'D' && s[1] == ':') {
259 char*ret = strdup(s);
266 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
268 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
269 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
270 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
271 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
272 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
273 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
274 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
275 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
276 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
277 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
278 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
279 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
280 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
281 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
282 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
283 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
284 else if(!strcmp(name, "version")) {
286 sprintf(buf, "%.1f", i->doc->getPDFVersion());
293 static void storeDeviceParameter(const char*name, const char*value)
295 parameter_t*o = device_config;
297 if(!strcmp(name, o->name)) {
298 /* overwrite old value */
300 o->value = strdup(value);
305 parameter_t*p = new parameter_t();
306 p->name = strdup(name);
307 p->value = strdup(value);
310 if(device_config_next) {
311 device_config_next->next = p;
312 device_config_next = p;
315 device_config_next = p;
319 typedef struct _gfxsource_internal
321 int config_bitmap_optimizing;
322 int config_full_bitmap_optimizing;
323 } gfxsource_internal_t;
325 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
327 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
328 msg("<verbose> setting parameter %s to \"%s\"", name, value);
329 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
330 addGlobalFontDir(value);
331 } else if(!strcmp(name, "pages")) {
332 global_page_range = strdup(value);
333 } else if(!strncmp(name, "font", strlen("font")) && name[4]!='q') {
334 addGlobalFont(value);
335 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
336 addGlobalLanguageDir(value);
337 } else if(!strcmp(name, "zoom")) {
340 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
341 storeDeviceParameter("jpegsubpixels", buf);
342 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
343 storeDeviceParameter("ppmsubpixels", buf);
344 } else if(!strcmp(name, "jpegdpi")) {
346 jpeg_dpi = atoi(value);
347 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
348 storeDeviceParameter("jpegsubpixels", buf);
349 } else if(!strcmp(name, "ppmdpi")) {
351 ppm_dpi = atoi(value);
352 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
353 storeDeviceParameter("ppmsubpixels", buf);
354 } else if(!strcmp(name, "poly2bitmap")) {
355 i->config_bitmap_optimizing = atoi(value);
356 } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
357 i->config_full_bitmap_optimizing = atoi(value);
358 } else if(!strcmp(name, "multiply")) {
359 multiply = atoi(value);
360 } else if(!strcmp(name, "help")) {
361 printf("\nPDF device global parameters:\n");
362 printf("fontdir=<dir> a directory with additional fonts\n");
363 printf("font=<filename> an additional font filename\n");
364 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
365 printf("zoom=<dpi> the resultion (default: 72)\n");
366 printf("languagedir=<dir> Add an xpdf language directory\n");
367 printf("multiply=<times> Render everything at <times> the resolution\n");
368 printf("poly2bitmap Convert graphics to bitmaps\n");
369 printf("bitmap Convert everything to bitmaps\n");
371 storeDeviceParameter(name,value);
374 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
376 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
377 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
378 memset(pdf_doc, 0, sizeof(gfxdocument_t));
379 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
380 memset(i, 0, sizeof(pdf_doc_internal_t));
381 pdf_doc->internal = i;
384 i->filename = strdup(filename);
387 if((x = strchr(filename, '|'))) {
392 GString *fileName = new GString(filename);
396 if (userPassword && userPassword[0]) {
397 userPW = new GString(userPassword);
401 i->doc = new PDFDoc(fileName, userPW);
405 if (!i->doc->isOk()) {
406 printf("xpdf reports document as broken.\n");
411 i->doc->getDocInfo(&i->docinfo);
413 pdf_doc->num_pages = i->doc->getNumPages();
415 if (i->doc->isEncrypted()) {
416 if(!i->doc->okToCopy()) {
419 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
423 i->info = new InfoOutputDev(i->doc->getXRef());
425 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
426 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
427 for(t=1;t<=pdf_doc->num_pages;t++) {
428 if(!global_page_range || is_in_range(t, global_page_range)) {
429 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
430 i->doc->processLinks((OutputDev*)i->info, t);
431 i->pages[t-1].xMin = i->info->x1;
432 i->pages[t-1].yMin = i->info->y1;
433 i->pages[t-1].xMax = i->info->x2;
434 i->pages[t-1].yMax = i->info->y2;
435 i->pages[t-1].width = i->info->x2 - i->info->x1;
436 i->pages[t-1].height = i->info->y2 - i->info->y1;
437 i->pages[t-1].number_of_images = i->info->num_images;
438 i->pages[t-1].number_of_links = i->info->num_links;
439 i->pages[t-1].number_of_fonts = i->info->num_fonts;
440 i->pages[t-1].has_info = 1;
444 if(isrc->config_full_bitmap_optimizing) {
445 FullBitmapOutputDev*outputDev = new FullBitmapOutputDev(i->info, i->doc);
446 i->outputDev = (CommonOutputDev*)outputDev;
447 } else if(isrc->config_bitmap_optimizing) {
448 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
449 i->outputDev = (CommonOutputDev*)outputDev;
451 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
452 i->outputDev = (CommonOutputDev*)outputDev;
455 /* pass global parameters to PDF driver*/
456 parameter_t*p = device_config;
458 i->outputDev->setParameter(p->name, p->value);
464 i->middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
465 gfxdevice_rescale_init(i->middev, 0x00000000, 0, 0, 1.0 / multiply);
469 pdf_doc->destroy = pdf_doc_destroy;
470 pdf_doc->set_parameter = pdf_doc_set_parameter;
471 pdf_doc->getinfo = pdf_doc_getinfo;
472 pdf_doc->getpage = pdf_doc_getpage;
479 void pdf_destroy(gfxsource_t*src)
483 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
484 free(src->internal);src->internal=0;
486 parameter_t*p = device_config;
488 parameter_t*next = p->next;
489 if(p->name) free(p->name);p->name = 0;
490 if(p->value) free(p->value);p->value =0;
491 p->next = 0;delete p;
494 delete globalParams;globalParams = 0;
498 gfxsource_t*gfxsource_pdf_create()
500 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
501 memset(src, 0, sizeof(gfxsource_t));
502 src->set_parameter = pdf_set_parameter;
503 src->open = pdf_open;
504 src->destroy = pdf_destroy;
505 src->internal = malloc(sizeof(gfxsource_internal_t));
506 memset(src->internal, 0, sizeof(gfxsource_internal_t));
509 globalParams = new GFXGlobalParams();
510 //globalparams_count++;