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->setDevice(0);
113 gfxdevice_rescale_setdevice(pi->middev, 0x00000000);
118 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
120 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
121 pi->outputDev->setMove(0,0);
122 pi->outputDev->setClip(0,0,0,0);
123 render2(page, output);
126 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)
128 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
130 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
131 if((x1|y1|x2|y2)==0) x2++;
133 pi->outputDev->setMove((int)x*multiply,(int)y*multiply);
134 pi->outputDev->setClip((int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
135 render2(page, output);
138 void pdf_doc_destroy(gfxdocument_t*gfx)
140 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
143 delete i->outputDev;i->outputDev=0;
146 gfxdevice_rescale_setdevice(i->middev, 0x00000000);
147 i->middev->finish(i->middev);
149 delete i->doc; i->doc=0;
150 free(i->pages); i->pages = 0;
155 free(i->filename);i->filename=0;
159 delete i->info;i->info=0;
162 free(gfx->internal);gfx->internal=0;
165 if(global_page_range) {
166 free(global_page_range);
167 global_page_range = 0;
170 /*globalparams_count--;
171 if(!globalparams_count) {
174 globalparams_count = 0;
178 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
180 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
181 CommonOutputDev*o = i->outputDev;
182 if(!strcmp(name, "pagemap")) {
183 int pdfpage=0, outputpage=0;
184 sscanf(value,"%d:%d", &pdfpage, &outputpage);
185 o->preparePage(pdfpage, outputpage);
187 o->setParameter(name, value);
191 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
193 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
195 if(page < 1 || page > doc->num_pages)
198 msg("<error> PDF disallows copying.");
202 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
203 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
204 memset(pi, 0, sizeof(pdf_page_internal_t));
205 pdf_page->internal = pi;
207 pdf_page->destroy = pdfpage_destroy;
208 pdf_page->render = pdfpage_render;
209 pdf_page->rendersection = pdfpage_rendersection;
210 pdf_page->width = di->pages[page-1].width;
211 pdf_page->height = di->pages[page-1].height;
213 pdf_page->parent = doc;
218 static char*getInfoString(Dict *infoDict, char *key)
224 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
225 s1 = obj.getString();
226 if ((s1->getChar(0) & 0xff) == 0xfe &&
227 (s1->getChar(1) & 0xff) == 0xff) {
229 for (i = 2; i < obj.getString()->getLength(); i += 2) {
230 if (s1->getChar(i) == '\0') {
231 s2->append(s1->getChar(i+1));
234 s2 = new GString("<unicode>");
238 char*ret = strdup(s2->getCString());
243 char*ret = strdup(s1->getCString());
251 static char*getInfoDate(Dict *infoDict, char *key)
256 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
257 s = obj.getString()->getCString();
258 if (s[0] == 'D' && s[1] == ':') {
261 char*ret = strdup(s);
268 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
270 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
271 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
272 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
273 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
274 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
275 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
276 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
277 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
278 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
279 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
280 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
281 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
282 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
283 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
284 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
285 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
286 else if(!strcmp(name, "version")) {
288 sprintf(buf, "%.1f", i->doc->getPDFVersion());
295 static void storeDeviceParameter(const char*name, const char*value)
297 parameter_t*o = device_config;
299 if(!strcmp(name, o->name)) {
300 /* overwrite old value */
302 o->value = strdup(value);
307 parameter_t*p = new parameter_t();
308 p->name = strdup(name);
309 p->value = strdup(value);
312 if(device_config_next) {
313 device_config_next->next = p;
314 device_config_next = p;
317 device_config_next = p;
321 typedef struct _gfxsource_internal
323 int config_bitmap_optimizing;
324 int config_full_bitmap_optimizing;
325 } gfxsource_internal_t;
327 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
329 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
330 msg("<verbose> setting parameter %s to \"%s\"", name, value);
331 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
332 addGlobalFontDir(value);
333 } else if(!strcmp(name, "pages")) {
334 global_page_range = strdup(value);
335 } else if(!strncmp(name, "font", strlen("font"))) {
336 addGlobalFont(value);
337 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
338 addGlobalLanguageDir(value);
339 } else if(!strcmp(name, "zoom")) {
342 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
343 storeDeviceParameter("jpegsubpixels", buf);
344 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
345 storeDeviceParameter("ppmsubpixels", buf);
346 } else if(!strcmp(name, "jpegdpi")) {
348 jpeg_dpi = atoi(value);
349 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
350 storeDeviceParameter("jpegsubpixels", buf);
351 } else if(!strcmp(name, "ppmdpi")) {
353 ppm_dpi = atoi(value);
354 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
355 storeDeviceParameter("ppmsubpixels", buf);
356 } else if(!strcmp(name, "poly2bitmap")) {
357 i->config_bitmap_optimizing = atoi(value);
358 } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
359 i->config_full_bitmap_optimizing = atoi(value);
360 } else if(!strcmp(name, "multiply")) {
361 multiply = atoi(value);
362 } else if(!strcmp(name, "help")) {
363 printf("\nPDF device global parameters:\n");
364 printf("fontdir=<dir> a directory with additional fonts\n");
365 printf("font=<filename> an dditional font filename\n");
366 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
367 printf("zoom=<dpi> the resultion (default: 72)\n");
369 storeDeviceParameter(name,value);
372 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
374 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
375 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
376 memset(pdf_doc, 0, sizeof(gfxdocument_t));
377 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
378 memset(i, 0, sizeof(pdf_doc_internal_t));
379 pdf_doc->internal = i;
382 i->filename = strdup(filename);
385 if((x = strchr(filename, '|'))) {
390 GString *fileName = new GString(filename);
394 if (userPassword && userPassword[0]) {
395 userPW = new GString(userPassword);
399 i->doc = new PDFDoc(fileName, userPW);
403 if (!i->doc->isOk()) {
404 printf("xpdf reports document as broken.\n");
409 i->doc->getDocInfo(&i->docinfo);
411 pdf_doc->num_pages = i->doc->getNumPages();
413 if (i->doc->isEncrypted()) {
414 if(!i->doc->okToCopy()) {
417 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
421 i->info = new InfoOutputDev(i->doc->getXRef());
423 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
424 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
425 for(t=1;t<=pdf_doc->num_pages;t++) {
426 if(!global_page_range || is_in_range(t, global_page_range)) {
427 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
428 i->doc->processLinks((OutputDev*)i->info, t);
429 i->pages[t-1].xMin = i->info->x1;
430 i->pages[t-1].yMin = i->info->y1;
431 i->pages[t-1].xMax = i->info->x2;
432 i->pages[t-1].yMax = i->info->y2;
433 i->pages[t-1].width = i->info->x2 - i->info->x1;
434 i->pages[t-1].height = i->info->y2 - i->info->y1;
435 i->pages[t-1].number_of_images = i->info->num_images;
436 i->pages[t-1].number_of_links = i->info->num_links;
437 i->pages[t-1].number_of_fonts = i->info->num_fonts;
438 i->pages[t-1].has_info = 1;
442 if(isrc->config_full_bitmap_optimizing) {
443 FullBitmapOutputDev*outputDev = new FullBitmapOutputDev(i->info, i->doc);
444 i->outputDev = (CommonOutputDev*)outputDev;
445 } else if(isrc->config_bitmap_optimizing) {
446 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
447 i->outputDev = (CommonOutputDev*)outputDev;
449 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
450 i->outputDev = (CommonOutputDev*)outputDev;
453 /* pass global parameters to PDF driver*/
454 parameter_t*p = device_config;
456 i->outputDev->setParameter(p->name, p->value);
462 i->middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
463 gfxdevice_rescale_init(i->middev, 0x00000000, 0, 0, 1.0 / multiply);
467 pdf_doc->destroy = pdf_doc_destroy;
468 pdf_doc->set_parameter = pdf_doc_set_parameter;
469 pdf_doc->getinfo = pdf_doc_getinfo;
470 pdf_doc->getpage = pdf_doc_getpage;
477 void pdf_destroy(gfxsource_t*src)
481 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
482 free(src->internal);src->internal=0;
484 parameter_t*p = device_config;
486 parameter_t*next = p->next;
490 delete globalParams;globalParams = 0;
493 gfxsource_t*gfxsource_pdf_create()
495 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
496 memset(src, 0, sizeof(gfxsource_t));
497 src->set_parameter = pdf_set_parameter;
498 src->open = pdf_open;
499 src->destroy = pdf_destroy;
500 src->internal = malloc(sizeof(gfxsource_internal_t));
501 memset(src->internal, 0, sizeof(gfxsource_internal_t));
504 globalParams = new GFXGlobalParams();
505 //globalparams_count++;