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 static void pdf_set_parameter(const char*name, const char*value)
286 msg("<verbose> setting parameter %s to \"%s\"", name, value);
287 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
288 addGlobalFontDir(value);
289 } else if(!strcmp(name, "pages")) {
290 global_page_range = strdup(value);
291 } else if(!strncmp(name, "font", strlen("font"))) {
292 addGlobalFont(value);
293 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
294 addGlobalLanguageDir(value);
295 } else if(!strcmp(name, "zoom")) {
298 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
299 storeDeviceParameter("jpegsubpixels", buf);
300 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
301 storeDeviceParameter("ppmsubpixels", buf);
302 } else if(!strcmp(name, "jpegdpi")) {
304 jpeg_dpi = atoi(value);
305 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
306 storeDeviceParameter("jpegsubpixels", buf);
307 } else if(!strcmp(name, "ppmdpi")) {
309 ppm_dpi = atoi(value);
310 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
311 storeDeviceParameter("ppmsubpixels", buf);
312 } else if(!strcmp(name, "help")) {
313 printf("\nPDF device global parameters:\n");
314 printf("fontdir=<dir> a directory with additional fonts\n");
315 printf("font=<filename> an dditional font filename\n");
316 printf("pages=<range> the range of pages to convert (example: pages=1-100,210-)\n");
317 printf("zoom=<dpi> the resultion (default: 72)\n");
319 storeDeviceParameter(name,value);
323 static gfxdocument_t*pdf_open(const char*filename)
325 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
326 memset(pdf_doc, 0, sizeof(gfxdocument_t));
327 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
328 memset(i, 0, sizeof(pdf_doc_internal_t));
329 pdf_doc->internal = i;
332 filename = strdup(filename);
335 if((x = strchr(filename, '|'))) {
340 GString *fileName = new GString(filename);
344 if (userPassword && userPassword[0]) {
345 userPW = new GString(userPassword);
349 i->doc = new PDFDoc(fileName, userPW);
353 if (!i->doc->isOk()) {
354 printf("xpdf reports document as broken.\n");
359 i->doc->getDocInfo(&i->docinfo);
361 pdf_doc->num_pages = i->doc->getNumPages();
363 if (i->doc->isEncrypted()) {
364 if(!i->doc->okToCopy()) {
365 printf("PDF disallows copying.\n");
368 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
372 InfoOutputDev*io = new InfoOutputDev(i->doc->getXRef());
374 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
375 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
376 for(t=1;t<=pdf_doc->num_pages;t++) {
377 if(!global_page_range || is_in_range(t, global_page_range)) {
378 i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
379 i->doc->processLinks((OutputDev*)io, t);
380 i->pages[t-1].xMin = io->x1;
381 i->pages[t-1].yMin = io->y1;
382 i->pages[t-1].xMax = io->x2;
383 i->pages[t-1].yMax = io->y2;
384 i->pages[t-1].width = io->x2 - io->x1;
385 i->pages[t-1].height = io->y2 - io->y1;
386 i->pages[t-1].number_of_images = io->num_images;
387 i->pages[t-1].number_of_links = io->num_links;
388 i->pages[t-1].number_of_fonts = io->num_fonts;
389 i->pages[t-1].has_info = 1;
394 BitmapOutputDev*outputDev = new BitmapOutputDev(io, i->doc);
395 i->outputDev = (CommonOutputDev*)outputDev;
397 GFXOutputDev*outputDev = new GFXOutputDev(io, i->doc);
398 i->outputDev = (CommonOutputDev*)outputDev;
401 /* pass global parameters to PDF driver*/
402 parameter_t*p = device_config;
404 i->outputDev->setParameter(p->name, p->value);
409 pdf_doc->destroy = pdf_doc_destroy;
410 pdf_doc->set_parameter = pdf_doc_set_parameter;
411 pdf_doc->getinfo = pdf_doc_getinfo;
412 pdf_doc->getpage = pdf_doc_getpage;
419 gfxsource_t*gfxsource_pdf_create()
421 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
422 memset(src, 0, sizeof(gfxsource_t));
423 src->set_parameter = pdf_set_parameter;
424 src->open = pdf_open;
427 globalParams = new GFXGlobalParams();
428 //globalparams_count++;