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 msg("<error> PDF disallows copying.");
204 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
205 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
206 memset(pi, 0, sizeof(pdf_page_internal_t));
207 pdf_page->internal = pi;
209 pdf_page->destroy = pdfpage_destroy;
210 pdf_page->render = pdfpage_render;
211 pdf_page->rendersection = pdfpage_rendersection;
212 pdf_page->width = di->pages[page-1].width;
213 pdf_page->height = di->pages[page-1].height;
215 pdf_page->parent = doc;
220 static char*getInfoString(Dict *infoDict, char *key)
226 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
227 s1 = obj.getString();
228 if ((s1->getChar(0) & 0xff) == 0xfe &&
229 (s1->getChar(1) & 0xff) == 0xff) {
231 for (i = 2; i < obj.getString()->getLength(); i += 2) {
232 if (s1->getChar(i) == '\0') {
233 s2->append(s1->getChar(i+1));
236 s2 = new GString("<unicode>");
240 char*ret = strdup(s2->getCString());
245 char*ret = strdup(s1->getCString());
253 static char*getInfoDate(Dict *infoDict, char *key)
258 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
259 s = obj.getString()->getCString();
260 if (s[0] == 'D' && s[1] == ':') {
263 char*ret = strdup(s);
270 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
272 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
273 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
274 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
275 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
276 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
277 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
278 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
279 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
280 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
281 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
282 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
283 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
284 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
285 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
286 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
287 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
288 else if(!strcmp(name, "version")) {
290 sprintf(buf, "%.1f", i->doc->getPDFVersion());
297 static void storeDeviceParameter(const char*name, const char*value)
299 parameter_t*o = device_config;
301 if(!strcmp(name, o->name)) {
302 /* overwrite old value */
304 o->value = strdup(value);
309 parameter_t*p = new parameter_t();
310 p->name = strdup(name);
311 p->value = strdup(value);
314 if(device_config_next) {
315 device_config_next->next = p;
316 device_config_next = p;
319 device_config_next = p;
323 typedef struct _gfxsource_internal
325 int config_bitmap_optimizing;
326 int config_full_bitmap_optimizing;
327 } gfxsource_internal_t;
329 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
331 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
332 msg("<verbose> setting parameter %s to \"%s\"", name, value);
333 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
334 addGlobalFontDir(value);
335 } else if(!strcmp(name, "pages")) {
336 global_page_range = strdup(value);
337 } else if(!strncmp(name, "font", strlen("font"))) {
338 addGlobalFont(value);
339 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
340 addGlobalLanguageDir(value);
341 } else if(!strcmp(name, "zoom")) {
344 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
345 storeDeviceParameter("jpegsubpixels", buf);
346 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
347 storeDeviceParameter("ppmsubpixels", buf);
348 } else if(!strcmp(name, "jpegdpi")) {
350 jpeg_dpi = atoi(value);
351 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
352 storeDeviceParameter("jpegsubpixels", buf);
353 } else if(!strcmp(name, "ppmdpi")) {
355 ppm_dpi = atoi(value);
356 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
357 storeDeviceParameter("ppmsubpixels", buf);
358 } else if(!strcmp(name, "poly2bitmap")) {
359 i->config_bitmap_optimizing = atoi(value);
360 } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
361 i->config_full_bitmap_optimizing = atoi(value);
362 } else if(!strcmp(name, "multiply")) {
363 multiply = atoi(value);
364 } else if(!strcmp(name, "help")) {
365 printf("\nPDF device global parameters:\n");
366 printf("fontdir=<dir> a directory with additional fonts\n");
367 printf("font=<filename> an additional font filename\n");
368 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
369 printf("zoom=<dpi> the resultion (default: 72)\n");
370 printf("languagedir=<dir> Add an xpdf language directory\n");
371 printf("multiply=<times> Render everything at <times> the resolution\n");
372 printf("poly2bitmap Convert graphics to bitmaps\n");
373 printf("bitmap Convert everything to bitmaps\n");
375 storeDeviceParameter(name,value);
378 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
380 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
381 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
382 memset(pdf_doc, 0, sizeof(gfxdocument_t));
383 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
384 memset(i, 0, sizeof(pdf_doc_internal_t));
385 pdf_doc->internal = i;
388 i->filename = strdup(filename);
391 if((x = strchr(filename, '|'))) {
396 GString *fileName = new GString(filename);
400 if (userPassword && userPassword[0]) {
401 userPW = new GString(userPassword);
405 i->doc = new PDFDoc(fileName, userPW);
409 if (!i->doc->isOk()) {
410 printf("xpdf reports document as broken.\n");
415 i->doc->getDocInfo(&i->docinfo);
417 pdf_doc->num_pages = i->doc->getNumPages();
419 if (i->doc->isEncrypted()) {
420 if(!i->doc->okToCopy()) {
423 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
427 i->info = new InfoOutputDev(i->doc->getXRef());
429 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
430 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
431 for(t=1;t<=pdf_doc->num_pages;t++) {
432 if(!global_page_range || is_in_range(t, global_page_range)) {
433 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
434 i->doc->processLinks((OutputDev*)i->info, t);
435 i->pages[t-1].xMin = i->info->x1;
436 i->pages[t-1].yMin = i->info->y1;
437 i->pages[t-1].xMax = i->info->x2;
438 i->pages[t-1].yMax = i->info->y2;
439 i->pages[t-1].width = i->info->x2 - i->info->x1;
440 i->pages[t-1].height = i->info->y2 - i->info->y1;
441 i->pages[t-1].number_of_images = i->info->num_images;
442 i->pages[t-1].number_of_links = i->info->num_links;
443 i->pages[t-1].number_of_fonts = i->info->num_fonts;
444 i->pages[t-1].has_info = 1;
448 if(isrc->config_full_bitmap_optimizing) {
449 FullBitmapOutputDev*outputDev = new FullBitmapOutputDev(i->info, i->doc);
450 i->outputDev = (CommonOutputDev*)outputDev;
451 } else if(isrc->config_bitmap_optimizing) {
452 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
453 i->outputDev = (CommonOutputDev*)outputDev;
455 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
456 i->outputDev = (CommonOutputDev*)outputDev;
459 /* pass global parameters to PDF driver*/
460 parameter_t*p = device_config;
462 i->outputDev->setParameter(p->name, p->value);
468 i->middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
469 gfxdevice_rescale_init(i->middev, 0x00000000, 0, 0, 1.0 / multiply);
473 pdf_doc->destroy = pdf_doc_destroy;
474 pdf_doc->set_parameter = pdf_doc_set_parameter;
475 pdf_doc->getinfo = pdf_doc_getinfo;
476 pdf_doc->getpage = pdf_doc_getpage;
483 void pdf_destroy(gfxsource_t*src)
487 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
488 free(src->internal);src->internal=0;
490 parameter_t*p = device_config;
492 parameter_t*next = p->next;
493 if(p->name) free(p->name);p->name = 0;
494 if(p->value) free(p->value);p->value =0;
495 p->next = 0;delete p;
498 delete globalParams;globalParams = 0;
502 gfxsource_t*gfxsource_pdf_create()
504 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
505 memset(src, 0, sizeof(gfxsource_t));
506 src->set_parameter = pdf_set_parameter;
507 src->open = pdf_open;
508 src->destroy = pdf_destroy;
509 src->internal = malloc(sizeof(gfxsource_internal_t));
510 memset(src->internal, 0, sizeof(gfxsource_internal_t));
513 globalParams = new GFXGlobalParams();
514 //globalparams_count++;