1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
4 #include "GlobalParams.h"
5 #include "InfoOutputDev.h"
6 #include "GFXOutputDev.h"
12 static parameter_t* device_config = 0;
13 static parameter_t* device_config_next = 0;
18 static double zoom = 72; /* xpdf: 86 */
20 static char* global_page_range = 0;
22 typedef struct _pdf_page_info
24 int xMin, yMin, xMax, yMax;
32 typedef struct _pdf_doc_internal
37 GFXOutputDev*outputDev;
38 pdf_page_info_t*pages;
41 typedef struct _pdf_page_internal
43 } pdf_page_internal_t;
45 typedef struct _dev_output_internal
47 GFXOutputDev*outputDev;
48 } dev_output_internal_t;
51 static char* dirseparator()
61 void pdfpage_destroy(gfxpage_t*pdf_page)
63 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
64 free(pdf_page->internal);pdf_page->internal = 0;
65 free(pdf_page);pdf_page=0;
68 void render2(gfxpage_t*page, gfxdevice_t*output)
70 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
73 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
77 if(!pi->pages[page->nr-1].has_info) {
78 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
83 gfxdevice_t*dev = pi->outputDev->device;
84 dev->setparameter(dev, "protect", "1");
86 pi->outputDev->setInfo(pi->info);
87 pi->outputDev->setXRef(pi->doc, pi->doc->getXRef());
88 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
92 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
94 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
95 pi->outputDev->setDevice(output);
96 pi->outputDev->setMove(0,0);
97 pi->outputDev->setClip(0,0,0,0);
98 render2(page, output);
99 pi->outputDev->setDevice(0);
102 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)
104 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
105 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
106 pi->outputDev->setDevice(output);
107 pi->outputDev->setMove((int)x,(int)y);
108 if((x1|y1|x2|y2)==0) x2++;
109 pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
110 render2(page, output);
111 pi->outputDev->setDevice(0);
114 void pdf_doc_destroy(gfxdocument_t*gfx)
116 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
118 delete i->doc; i->doc=0;
119 free(i->pages); i->pages = 0;
122 delete i->info;i->info=0;
125 free(gfx->internal);gfx->internal=0;
128 if(global_page_range) {
129 free(global_page_range);
130 global_page_range = 0;
134 void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
136 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
137 if(!strcmp(name, "pagemap")) {
138 GFXOutputDev*o = i->outputDev;
139 int pdfpage=0, outputpage=0;
140 sscanf(value,"%d:%d", &pdfpage, &outputpage);
141 o->preparePage(pdfpage, outputpage);
143 msg("<warning> Ignored parameter: %s=%s", name, value);
147 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
149 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
151 if(page < 1 || page > doc->num_pages)
154 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
155 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
156 memset(pi, 0, sizeof(pdf_page_internal_t));
157 pdf_page->internal = pi;
159 pdf_page->destroy = pdfpage_destroy;
160 pdf_page->render = pdfpage_render;
161 pdf_page->rendersection = pdfpage_rendersection;
162 pdf_page->width = di->pages[page-1].width;
163 pdf_page->height = di->pages[page-1].height;
165 pdf_page->parent = doc;
170 void storeDeviceParameter(char*name, char*value)
172 parameter_t*p = new parameter_t();
173 p->name = strdup(name);
174 p->value = strdup(value);
176 if(device_config_next) {
177 device_config_next->next = p;
178 device_config_next = p;
181 device_config_next = p;
185 void pdf_set_parameter(char*name, char*value)
187 msg("<verbose> setting parameter %s to \"%s\"", name, value);
188 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
189 addGlobalFontDir(value);
190 } else if(!strcmp(name, "pages")) {
191 global_page_range = strdup(value);
192 } else if(!strncmp(name, "font", strlen("font"))) {
193 addGlobalFont(value);
194 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
195 addGlobalLanguageDir(value);
196 } else if(!strcmp(name, "zoom")) {
199 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
200 storeDeviceParameter("jpegsubpixels", buf);
201 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
202 storeDeviceParameter("ppmsubpixels", buf);
203 } else if(!strcmp(name, "jpegdpi")) {
205 jpeg_dpi = atoi(value);
206 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
207 storeDeviceParameter("jpegsubpixels", buf);
208 } else if(!strcmp(name, "ppmdpi")) {
210 ppm_dpi = atoi(value);
211 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
212 storeDeviceParameter("ppmsubpixels", buf);
214 storeDeviceParameter(name,value);
218 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
223 if (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 printf(fmt, s2->getCString());
240 printf(fmt, s1->getCString());
246 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
250 if (infoDict->lookup(key, &obj)->isString()) {
251 s = obj.getString()->getCString();
252 if (s[0] == 'D' && s[1] == ':') {
262 gfxdocument_t*pdf_open(char*filename)
264 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
265 memset(pdf_doc, 0, sizeof(gfxdocument_t));
266 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
267 memset(i, 0, sizeof(pdf_doc_internal_t));
268 pdf_doc->internal = i;
271 filename = strdup(filename);
274 if((x = strchr(filename, '|'))) {
279 GString *fileName = new GString(filename);
285 globalParams = new GlobalParams("");
288 if (userPassword && userPassword[0]) {
289 userPW = new GString(userPassword);
293 i->doc = new PDFDoc(fileName, userPW);
297 if (!i->doc->isOk()) {
302 i->doc->getDocInfo(&info);
304 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
305 printInfoString(info.getDict(), "Title", "Title: %s\n");
306 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
307 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
308 printInfoString(info.getDict(), "Author", "Author: %s\n");
309 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
310 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
311 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
312 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
313 printf("Pages: %d\n", i->doc->getNumPages());
314 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
315 printf("Encrypted: ");
316 if (i->doc->isEncrypted()) {
317 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
318 i->doc->okToPrint() ? "yes" : "no",
319 i->doc->okToCopy() ? "yes" : "no",
320 i->doc->okToChange() ? "yes" : "no",
321 i->doc->okToAddNotes() ? "yes" : "no");
328 pdf_doc->num_pages = i->doc->getNumPages();
330 if (i->doc->isEncrypted()) {
331 if(!i->doc->okToCopy()) {
332 printf("PDF disallows copying.\n");
335 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
339 InfoOutputDev*io = new InfoOutputDev();
341 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
342 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
343 for(t=1;t<=pdf_doc->num_pages;t++) {
344 if(!global_page_range || is_in_range(t, global_page_range)) {
345 i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
346 i->pages[t-1].xMin = io->x1;
347 i->pages[t-1].yMin = io->y1;
348 i->pages[t-1].xMax = io->x2;
349 i->pages[t-1].yMax = io->y2;
350 i->pages[t-1].width = io->x2 - io->x1;
351 i->pages[t-1].height = io->y2 - io->y1;
352 i->pages[t-1].number_of_images = io->num_images;
353 i->pages[t-1].number_of_links = io->num_links;
354 i->pages[t-1].number_of_fonts = io->num_fonts;
355 i->pages[t-1].has_info = 1;
359 i->outputDev = new GFXOutputDev(device_config);
362 pdf_doc->destroy = pdf_doc_destroy;
363 pdf_doc->set_parameter = pdf_doc_set_parameter;
364 pdf_doc->getpage = pdf_doc_getpage;
371 gfxsource_t*gfxsource_pdf_create()
373 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
374 memset(src, 0, sizeof(gfxsource_t));
375 src->set_parameter = pdf_set_parameter;
376 src->open = pdf_open;