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 int globalparams_count=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 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, /*doLinks*/(int)1);
101 pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
102 pi->outputDev->finishPage();
104 pi->outputDev->setDevice(0);
106 gfxdevice_rescale_setdevice(pi->middev, 0x00000000);
111 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
113 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
114 pi->outputDev->setMove(0,0);
115 pi->outputDev->setClip(0,0,0,0);
116 render2(page, output);
119 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)
121 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
123 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
124 if((x1|y1|x2|y2)==0) x2++;
126 pi->outputDev->setMove((int)x*multiply,(int)y*multiply);
127 pi->outputDev->setClip((int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
128 render2(page, output);
131 void pdf_doc_destroy(gfxdocument_t*gfx)
133 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
136 delete i->outputDev;i->outputDev=0;
139 gfxdevice_rescale_setdevice(i->middev, 0x00000000);
140 i->middev->finish(i->middev);
142 delete i->doc; i->doc=0;
143 free(i->pages); i->pages = 0;
148 free(i->filename);i->filename=0;
152 delete i->info;i->info=0;
155 free(gfx->internal);gfx->internal=0;
158 if(global_page_range) {
159 free(global_page_range);
160 global_page_range = 0;
163 /*globalparams_count--;
164 if(!globalparams_count) {
167 globalparams_count = 0;
171 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
173 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
174 CommonOutputDev*o = i->outputDev;
175 if(!strcmp(name, "pagemap")) {
176 int pdfpage=0, outputpage=0;
177 sscanf(value,"%d:%d", &pdfpage, &outputpage);
178 o->preparePage(pdfpage, outputpage);
180 o->setParameter(name, value);
184 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
186 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
188 if(page < 1 || page > doc->num_pages)
191 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
192 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
193 memset(pi, 0, sizeof(pdf_page_internal_t));
194 pdf_page->internal = pi;
196 pdf_page->destroy = pdfpage_destroy;
197 pdf_page->render = pdfpage_render;
198 pdf_page->rendersection = pdfpage_rendersection;
199 pdf_page->width = di->pages[page-1].width;
200 pdf_page->height = di->pages[page-1].height;
202 pdf_page->parent = doc;
207 static char*getInfoString(Dict *infoDict, char *key)
213 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
214 s1 = obj.getString();
215 if ((s1->getChar(0) & 0xff) == 0xfe &&
216 (s1->getChar(1) & 0xff) == 0xff) {
218 for (i = 2; i < obj.getString()->getLength(); i += 2) {
219 if (s1->getChar(i) == '\0') {
220 s2->append(s1->getChar(i+1));
223 s2 = new GString("<unicode>");
227 char*ret = strdup(s2->getCString());
232 char*ret = strdup(s1->getCString());
240 static char*getInfoDate(Dict *infoDict, char *key)
245 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
246 s = obj.getString()->getCString();
247 if (s[0] == 'D' && s[1] == ':') {
250 char*ret = strdup(s);
257 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
259 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
260 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
261 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
262 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
263 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
264 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
265 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
266 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
267 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
268 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
269 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
270 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
271 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
272 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
273 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
274 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
275 else if(!strcmp(name, "version")) {
277 sprintf(buf, "%.1f", i->doc->getPDFVersion());
283 typedef struct _gfxsource_internal
285 int config_bitmap_optimizing;
286 int config_full_bitmap_optimizing;
287 } gfxsource_internal_t;
289 static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
291 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
292 msg("<verbose> setting parameter %s to \"%s\"", name, value);
293 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
294 addGlobalFontDir(value);
295 } else if(!strcmp(name, "pages")) {
296 global_page_range = strdup(value);
297 } else if(!strncmp(name, "font", strlen("font")) && name[4]!='q') {
298 addGlobalFont(value);
299 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
300 addGlobalLanguageDir(value);
301 } else if(!strcmp(name, "zoom")) {
304 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
305 storeDeviceParameter("jpegsubpixels", buf);
306 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
307 storeDeviceParameter("ppmsubpixels", buf);
308 } else if(!strcmp(name, "jpegdpi")) {
310 jpeg_dpi = atoi(value);
311 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
312 storeDeviceParameter("jpegsubpixels", buf);
313 } else if(!strcmp(name, "ppmdpi")) {
315 ppm_dpi = atoi(value);
316 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
317 storeDeviceParameter("ppmsubpixels", buf);
318 } else if(!strcmp(name, "poly2bitmap")) {
319 i->config_bitmap_optimizing = atoi(value);
320 } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
321 i->config_full_bitmap_optimizing = atoi(value);
322 } else if(!strcmp(name, "multiply")) {
323 multiply = atoi(value);
324 } else if(!strcmp(name, "help")) {
325 printf("fontdir=<dir> a directory with additional fonts\n");
326 printf("font=<filename> an additional font filename\n");
327 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
328 printf("zoom=<dpi> the resultion (default: 72)\n");
329 printf("bitmap Convert everything to bitmaps\n");
330 printf("poly2bitmap Convert graphics to bitmaps\n");
331 printf("languagedir=<dir> Add an xpdf language directory\n");
332 printf("multiply=<times> Render everything at <times> the resolution\n");
333 printf("breakonwarning=0/1 Abort conversion if graphic objects are found which\n");
334 printf(" are not 100%% supported\n");
335 printf("transparent=0/1 Make output file transparent (alpha background)\n");
336 printf("extrafontdata=0/1 Store Type3 characters and capture characters\n");
337 printf("fontquality=1..100 Curve approximation quality of the fonts\n");
341 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
343 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
344 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
345 memset(pdf_doc, 0, sizeof(gfxdocument_t));
346 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
347 memset(i, 0, sizeof(pdf_doc_internal_t));
348 pdf_doc->internal = i;
351 i->filename = strdup(filename);
354 if((x = strchr(filename, '|'))) {
359 GString *fileName = new GString(filename);
363 if (userPassword && userPassword[0]) {
364 userPW = new GString(userPassword);
368 i->doc = new PDFDoc(fileName, userPW);
372 if (!i->doc->isOk()) {
373 printf("xpdf reports document as broken.\n");
378 i->doc->getDocInfo(&i->docinfo);
380 pdf_doc->num_pages = i->doc->getNumPages();
382 if (i->doc->isEncrypted()) {
383 if(!i->doc->okToCopy()) {
386 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
390 i->info = new InfoOutputDev(i->doc->getXRef());
392 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
393 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
394 for(t=1;t<=pdf_doc->num_pages;t++) {
395 if(!global_page_range || is_in_range(t, global_page_range)) {
396 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
397 i->doc->processLinks((OutputDev*)i->info, t);
398 i->pages[t-1].xMin = i->info->x1;
399 i->pages[t-1].yMin = i->info->y1;
400 i->pages[t-1].xMax = i->info->x2;
401 i->pages[t-1].yMax = i->info->y2;
402 i->pages[t-1].width = i->info->x2 - i->info->x1;
403 i->pages[t-1].height = i->info->y2 - i->info->y1;
404 i->pages[t-1].number_of_images = i->info->num_images;
405 i->pages[t-1].number_of_links = i->info->num_links;
406 i->pages[t-1].number_of_fonts = i->info->num_fonts;
407 i->pages[t-1].has_info = 1;
411 if(isrc->config_full_bitmap_optimizing) {
412 FullBitmapOutputDev*outputDev = new FullBitmapOutputDev(i->info, i->doc);
413 i->outputDev = (CommonOutputDev*)outputDev;
414 } else if(isrc->config_bitmap_optimizing) {
415 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
416 i->outputDev = (CommonOutputDev*)outputDev;
418 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
419 i->outputDev = (CommonOutputDev*)outputDev;
424 i->middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
425 gfxdevice_rescale_init(i->middev, 0x00000000, 0, 0, 1.0 / multiply);
429 pdf_doc->destroy = pdf_doc_destroy;
430 pdf_doc->set_parameter = pdf_doc_set_parameter;
431 pdf_doc->getinfo = pdf_doc_getinfo;
432 pdf_doc->getpage = pdf_doc_getpage;
439 void pdf_destroy(gfxsource_t*src)
443 gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
444 free(src->internal);src->internal=0;
446 delete globalParams;globalParams = 0;
450 gfxsource_t*gfxsource_pdf_create()
452 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
453 memset(src, 0, sizeof(gfxsource_t));
454 src->set_parameter = pdf_set_parameter;
455 src->open = pdf_open;
456 src->destroy = pdf_destroy;
457 src->internal = malloc(sizeof(gfxsource_internal_t));
458 memset(src->internal, 0, sizeof(gfxsource_internal_t));
461 globalParams = new GFXGlobalParams();
462 //globalparams_count++;