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 "BitmapOutputDev.h"
15 static double zoom = 72; /* xpdf: 86 */
16 static int jpeg_dpi = 0;
17 static int ppm_dpi = 0;
18 static int multiply = 1;
19 static char* global_page_range = 0;
21 static parameter_t* device_config = 0;
22 static parameter_t* device_config_next = 0;
24 typedef struct _pdf_page_info
26 int xMin, yMin, xMax, yMax;
34 typedef struct _pdf_doc_internal
41 CommonOutputDev*outputDev;
42 pdf_page_info_t*pages;
47 typedef struct _pdf_page_internal
49 } pdf_page_internal_t;
51 typedef struct _dev_output_internal
53 CommonOutputDev*outputDev;
54 } dev_output_internal_t;
57 static char* dirseparator()
67 void pdfpage_destroy(gfxpage_t*pdf_page)
69 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
70 free(pdf_page->internal);pdf_page->internal = 0;
71 free(pdf_page);pdf_page=0;
74 void render2(gfxpage_t*page, gfxdevice_t*dev)
76 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
79 gfxdevice_rescale_setdevice(pi->middev, dev);
80 pi->middev->setparameter(pi->middev, "protect", "1");
85 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
89 if(!pi->pages[page->nr-1].has_info) {
90 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
94 pi->outputDev->setDevice(dev);
97 dev->setparameter(dev, "protect", "1");
100 /* pass global parameters to output device */
101 parameter_t*p = device_config;
103 dev->setparameter(dev, p->name, p->value);
106 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, /*doLinks*/(int)1);
107 pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
108 pi->outputDev->setDevice(0);
110 gfxdevice_rescale_setdevice(pi->middev, 0x00000000);
115 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
117 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
118 pi->outputDev->setMove(0,0);
119 pi->outputDev->setClip(0,0,0,0);
120 render2(page, output);
123 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)
125 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
127 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
128 if((x1|y1|x2|y2)==0) x2++;
130 pi->outputDev->setMove((int)x*multiply,(int)y*multiply);
131 pi->outputDev->setClip((int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
132 render2(page, output);
135 static int globalparams_count=0;
137 void pdf_doc_destroy(gfxdocument_t*gfx)
139 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
142 delete i->outputDev;i->outputDev=0;
145 gfxdevice_rescale_setdevice(i->middev, 0x00000000);
146 i->middev->finish(i->middev);
148 delete i->doc; i->doc=0;
149 free(i->pages); i->pages = 0;
154 free(i->filename);i->filename=0;
158 delete i->info;i->info=0;
161 free(gfx->internal);gfx->internal=0;
164 if(global_page_range) {
165 free(global_page_range);
166 global_page_range = 0;
169 /*globalparams_count--;
170 if(!globalparams_count) {
173 globalparams_count = 0;
177 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
179 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
180 CommonOutputDev*o = i->outputDev;
181 if(!strcmp(name, "pagemap")) {
182 int pdfpage=0, outputpage=0;
183 sscanf(value,"%d:%d", &pdfpage, &outputpage);
184 o->preparePage(pdfpage, outputpage);
186 o->setParameter(name, value);
190 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
192 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
194 if(page < 1 || page > doc->num_pages)
197 msg("<error> PDF disallows copying.");
201 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
202 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
203 memset(pi, 0, sizeof(pdf_page_internal_t));
204 pdf_page->internal = pi;
206 pdf_page->destroy = pdfpage_destroy;
207 pdf_page->render = pdfpage_render;
208 pdf_page->rendersection = pdfpage_rendersection;
209 pdf_page->width = di->pages[page-1].width;
210 pdf_page->height = di->pages[page-1].height;
212 pdf_page->parent = doc;
217 static char*getInfoString(Dict *infoDict, char *key)
223 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
224 s1 = obj.getString();
225 if ((s1->getChar(0) & 0xff) == 0xfe &&
226 (s1->getChar(1) & 0xff) == 0xff) {
228 for (i = 2; i < obj.getString()->getLength(); i += 2) {
229 if (s1->getChar(i) == '\0') {
230 s2->append(s1->getChar(i+1));
233 s2 = new GString("<unicode>");
237 char*ret = strdup(s2->getCString());
242 char*ret = strdup(s1->getCString());
250 static char*getInfoDate(Dict *infoDict, char *key)
255 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
256 s = obj.getString()->getCString();
257 if (s[0] == 'D' && s[1] == ':') {
260 char*ret = strdup(s);
267 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
269 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
270 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
271 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
272 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
273 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
274 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
275 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
276 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
277 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
278 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
279 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
280 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
281 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
282 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
283 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
284 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
285 else if(!strcmp(name, "version")) {
287 sprintf(buf, "%.1f", i->doc->getPDFVersion());
294 static void storeDeviceParameter(const char*name, const char*value)
296 parameter_t*o = device_config;
298 if(!strcmp(name, o->name)) {
299 /* overwrite old value */
301 o->value = strdup(value);
306 parameter_t*p = new parameter_t();
307 p->name = strdup(name);
308 p->value = strdup(value);
311 if(device_config_next) {
312 device_config_next->next = p;
313 device_config_next = p;
316 device_config_next = p;
320 typedef struct _gfxsource_internal
322 int config_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"))) {
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, "multiply")) {
357 multiply = atoi(value);
358 } else if(!strcmp(name, "help")) {
359 printf("\nPDF device global parameters:\n");
360 printf("fontdir=<dir> a directory with additional fonts\n");
361 printf("font=<filename> an dditional font filename\n");
362 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
363 printf("zoom=<dpi> the resultion (default: 72)\n");
365 storeDeviceParameter(name,value);
368 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
370 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
371 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
372 memset(pdf_doc, 0, sizeof(gfxdocument_t));
373 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
374 memset(i, 0, sizeof(pdf_doc_internal_t));
375 pdf_doc->internal = i;
378 i->filename = strdup(filename);
381 if((x = strchr(filename, '|'))) {
386 GString *fileName = new GString(filename);
390 if (userPassword && userPassword[0]) {
391 userPW = new GString(userPassword);
395 i->doc = new PDFDoc(fileName, userPW);
399 if (!i->doc->isOk()) {
400 printf("xpdf reports document as broken.\n");
405 i->doc->getDocInfo(&i->docinfo);
407 pdf_doc->num_pages = i->doc->getNumPages();
409 if (i->doc->isEncrypted()) {
410 if(!i->doc->okToCopy()) {
413 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
417 i->info = new InfoOutputDev(i->doc->getXRef());
419 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
420 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
421 for(t=1;t<=pdf_doc->num_pages;t++) {
422 if(!global_page_range || is_in_range(t, global_page_range)) {
423 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
424 i->doc->processLinks((OutputDev*)i->info, t);
425 i->pages[t-1].xMin = i->info->x1;
426 i->pages[t-1].yMin = i->info->y1;
427 i->pages[t-1].xMax = i->info->x2;
428 i->pages[t-1].yMax = i->info->y2;
429 i->pages[t-1].width = i->info->x2 - i->info->x1;
430 i->pages[t-1].height = i->info->y2 - i->info->y1;
431 i->pages[t-1].number_of_images = i->info->num_images;
432 i->pages[t-1].number_of_links = i->info->num_links;
433 i->pages[t-1].number_of_fonts = i->info->num_fonts;
434 i->pages[t-1].has_info = 1;
438 if(isrc->config_bitmap_optimizing) {
439 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
440 i->outputDev = (CommonOutputDev*)outputDev;
442 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
443 i->outputDev = (CommonOutputDev*)outputDev;
445 /* pass global parameters to PDF driver*/
446 parameter_t*p = device_config;
448 i->outputDev->setParameter(p->name, p->value);
454 i->middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
455 gfxdevice_rescale_init(i->middev, 0x00000000, 0, 0, 1.0 / multiply);
459 pdf_doc->destroy = pdf_doc_destroy;
460 pdf_doc->set_parameter = pdf_doc_set_parameter;
461 pdf_doc->getinfo = pdf_doc_getinfo;
462 pdf_doc->getpage = pdf_doc_getpage;
469 void pdf_destroy(gfxsource_t*src)
473 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
474 free(src->internal);src->internal=0;
477 gfxsource_t*gfxsource_pdf_create()
479 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
480 memset(src, 0, sizeof(gfxsource_t));
481 src->set_parameter = pdf_set_parameter;
482 src->open = pdf_open;
483 src->destroy = pdf_destroy;
484 src->internal = malloc(sizeof(gfxsource_internal_t));
485 memset(src->internal, 0, sizeof(gfxsource_internal_t));
488 globalParams = new GFXGlobalParams();
489 //globalparams_count++;