1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
5 #include "GlobalParams.h"
6 #include "InfoOutputDev.h"
7 #include "GFXOutputDev.h"
8 #include "BitmapOutputDev.h"
14 static parameter_t* device_config = 0;
15 static parameter_t* device_config_next = 0;
20 static double zoom = 72; /* xpdf: 86 */
22 static char* global_page_range = 0;
24 typedef struct _pdf_page_info
26 int xMin, yMin, xMax, yMax;
34 typedef struct _pdf_doc_internal
40 CommonOutputDev*outputDev;
41 pdf_page_info_t*pages;
44 typedef struct _pdf_page_internal
46 } pdf_page_internal_t;
48 typedef struct _dev_output_internal
50 CommonOutputDev*outputDev;
51 } dev_output_internal_t;
54 static char* dirseparator()
64 void pdfpage_destroy(gfxpage_t*pdf_page)
66 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
67 free(pdf_page->internal);pdf_page->internal = 0;
68 free(pdf_page);pdf_page=0;
71 void render2(gfxpage_t*page, gfxdevice_t*dev)
73 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
76 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
80 if(!pi->pages[page->nr-1].has_info) {
81 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
85 pi->outputDev->setDevice(dev);
87 dev->setparameter(dev, "protect", "1");
90 /* pass global parameters to output device */
91 parameter_t*p = device_config;
93 dev->setparameter(dev, p->name, p->value);
96 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
97 pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
98 pi->outputDev->setDevice(0);
102 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
104 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
105 pi->outputDev->setMove(0,0);
106 pi->outputDev->setClip(0,0,0,0);
107 render2(page, output);
110 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)
112 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
114 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
115 if((x1|y1|x2|y2)==0) x2++;
117 pi->outputDev->setMove((int)x,(int)y);
118 pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
119 render2(page, output);
122 static int globalparams_count=0;
124 void pdf_doc_destroy(gfxdocument_t*gfx)
126 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
129 delete i->outputDev;i->outputDev=0;
131 delete i->doc; i->doc=0;
132 free(i->pages); i->pages = 0;
137 delete i->info;i->info=0;
140 free(gfx->internal);gfx->internal=0;
143 if(global_page_range) {
144 free(global_page_range);
145 global_page_range = 0;
148 /*globalparams_count--;
149 if(!globalparams_count) {
152 globalparams_count = 0;
156 void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
158 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
159 CommonOutputDev*o = i->outputDev;
160 if(!strcmp(name, "pagemap")) {
161 int pdfpage=0, outputpage=0;
162 sscanf(value,"%d:%d", &pdfpage, &outputpage);
163 o->preparePage(pdfpage, outputpage);
165 o->setParameter(name, value);
169 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
171 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
173 if(page < 1 || page > doc->num_pages)
176 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
177 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
178 memset(pi, 0, sizeof(pdf_page_internal_t));
179 pdf_page->internal = pi;
181 pdf_page->destroy = pdfpage_destroy;
182 pdf_page->render = pdfpage_render;
183 pdf_page->rendersection = pdfpage_rendersection;
184 pdf_page->width = di->pages[page-1].width;
185 pdf_page->height = di->pages[page-1].height;
187 pdf_page->parent = doc;
192 static char*getInfoString(Dict *infoDict, char *key)
198 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
199 s1 = obj.getString();
200 if ((s1->getChar(0) & 0xff) == 0xfe &&
201 (s1->getChar(1) & 0xff) == 0xff) {
203 for (i = 2; i < obj.getString()->getLength(); i += 2) {
204 if (s1->getChar(i) == '\0') {
205 s2->append(s1->getChar(i+1));
208 s2 = new GString("<unicode>");
212 char*ret = strdup(s2->getCString());
217 char*ret = strdup(s1->getCString());
225 static char*getInfoDate(Dict *infoDict, char *key)
230 if (infoDict && infoDict->lookup(key, &obj)->isString()) {
231 s = obj.getString()->getCString();
232 if (s[0] == 'D' && s[1] == ':') {
235 char*ret = strdup(s);
242 char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
244 pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
245 if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
246 else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
247 else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
248 else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
249 else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
250 else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
251 else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
252 else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
253 else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
254 else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
255 else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
256 else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
257 else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
258 else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
259 else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
260 else if(!strcmp(name, "version")) {
262 sprintf(buf, "%.1f", i->doc->getPDFVersion());
269 static void storeDeviceParameter(const char*name, const char*value)
271 parameter_t*p = new parameter_t();
272 p->name = strdup(name);
273 p->value = strdup(value);
275 if(device_config_next) {
276 device_config_next->next = p;
277 device_config_next = p;
280 device_config_next = p;
284 typedef struct _gfxsource_internal
286 int config_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"))) {
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 = 1;
320 } else if(!strcmp(name, "help")) {
321 printf("\nPDF device global parameters:\n");
322 printf("fontdir=<dir> a directory with additional fonts\n");
323 printf("font=<filename> an dditional font filename\n");
324 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
325 printf("zoom=<dpi> the resultion (default: 72)\n");
327 storeDeviceParameter(name,value);
330 static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
332 gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
333 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
334 memset(pdf_doc, 0, sizeof(gfxdocument_t));
335 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
336 memset(i, 0, sizeof(pdf_doc_internal_t));
337 pdf_doc->internal = i;
340 filename = strdup(filename);
343 if((x = strchr(filename, '|'))) {
348 GString *fileName = new GString(filename);
352 if (userPassword && userPassword[0]) {
353 userPW = new GString(userPassword);
357 i->doc = new PDFDoc(fileName, userPW);
361 if (!i->doc->isOk()) {
362 printf("xpdf reports document as broken.\n");
367 i->doc->getDocInfo(&i->docinfo);
369 pdf_doc->num_pages = i->doc->getNumPages();
371 if (i->doc->isEncrypted()) {
372 if(!i->doc->okToCopy()) {
373 printf("PDF disallows copying.\n");
376 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
380 i->info = new InfoOutputDev(i->doc->getXRef());
382 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
383 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
384 for(t=1;t<=pdf_doc->num_pages;t++) {
385 if(!global_page_range || is_in_range(t, global_page_range)) {
386 i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
387 i->doc->processLinks((OutputDev*)i->info, t);
388 i->pages[t-1].xMin = i->info->x1;
389 i->pages[t-1].yMin = i->info->y1;
390 i->pages[t-1].xMax = i->info->x2;
391 i->pages[t-1].yMax = i->info->y2;
392 i->pages[t-1].width = i->info->x2 - i->info->x1;
393 i->pages[t-1].height = i->info->y2 - i->info->y1;
394 i->pages[t-1].number_of_images = i->info->num_images;
395 i->pages[t-1].number_of_links = i->info->num_links;
396 i->pages[t-1].number_of_fonts = i->info->num_fonts;
397 i->pages[t-1].has_info = 1;
401 if(isrc->config_bitmap_optimizing) {
402 BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc);
403 i->outputDev = (CommonOutputDev*)outputDev;
405 GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc);
406 i->outputDev = (CommonOutputDev*)outputDev;
409 /* pass global parameters to PDF driver*/
410 parameter_t*p = device_config;
412 i->outputDev->setParameter(p->name, p->value);
417 pdf_doc->destroy = pdf_doc_destroy;
418 pdf_doc->set_parameter = pdf_doc_set_parameter;
419 pdf_doc->getinfo = pdf_doc_getinfo;
420 pdf_doc->getpage = pdf_doc_getpage;
427 gfxsource_t*gfxsource_pdf_create()
429 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
430 memset(src, 0, sizeof(gfxsource_t));
431 src->set_parameter = pdf_set_parameter;
432 src->open = pdf_open;
433 src->internal = malloc(sizeof(gfxsource_internal_t));
434 memset(src->internal, 0, sizeof(gfxsource_internal_t));
437 globalParams = new GFXGlobalParams();
438 //globalparams_count++;