1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
5 #include "GlobalParams.h"
6 #include "InfoOutputDev.h"
7 #include "GFXOutputDev.h"
13 static parameter_t* device_config = 0;
14 static parameter_t* device_config_next = 0;
19 static double zoom = 72; /* xpdf: 86 */
21 static char* global_page_range = 0;
23 typedef struct _pdf_page_info
25 int xMin, yMin, xMax, yMax;
33 typedef struct _pdf_doc_internal
38 GFXOutputDev*outputDev;
39 pdf_page_info_t*pages;
42 typedef struct _pdf_page_internal
44 } pdf_page_internal_t;
46 typedef struct _dev_output_internal
48 GFXOutputDev*outputDev;
49 } dev_output_internal_t;
52 static char* dirseparator()
62 void pdfpage_destroy(gfxpage_t*pdf_page)
64 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
65 free(pdf_page->internal);pdf_page->internal = 0;
66 free(pdf_page);pdf_page=0;
69 void render2(gfxpage_t*page, gfxdevice_t*output)
71 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
74 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
78 if(!pi->pages[page->nr-1].has_info) {
79 msg("<fatal> pdf_page_render: page %d was previously set as not-to-render via the \"pages\" option", page->nr);
84 gfxdevice_t*dev = pi->outputDev->device;
85 dev->setparameter(dev, "protect", "1");
87 pi->outputDev->setInfo(pi->info);
88 pi->outputDev->setXRef(pi->doc, pi->doc->getXRef());
89 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
90 pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
94 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
96 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
97 pi->outputDev->setDevice(output);
98 pi->outputDev->setMove(0,0);
99 pi->outputDev->setClip(0,0,0,0);
100 render2(page, output);
101 pi->outputDev->setDevice(0);
104 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)
106 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
107 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
108 pi->outputDev->setDevice(output);
109 pi->outputDev->setMove((int)x,(int)y);
110 if((x1|y1|x2|y2)==0) x2++;
111 pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
112 render2(page, output);
113 pi->outputDev->setDevice(0);
116 void pdf_doc_destroy(gfxdocument_t*gfx)
118 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
120 delete i->doc; i->doc=0;
121 free(i->pages); i->pages = 0;
124 delete i->info;i->info=0;
127 free(gfx->internal);gfx->internal=0;
130 if(global_page_range) {
131 free(global_page_range);
132 global_page_range = 0;
136 void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
138 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
139 if(!strcmp(name, "pagemap")) {
140 GFXOutputDev*o = i->outputDev;
141 int pdfpage=0, outputpage=0;
142 sscanf(value,"%d:%d", &pdfpage, &outputpage);
143 o->preparePage(pdfpage, outputpage);
145 msg("<warning> Ignored parameter: %s=%s", name, value);
149 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
151 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
153 if(page < 1 || page > doc->num_pages)
156 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
157 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
158 memset(pi, 0, sizeof(pdf_page_internal_t));
159 pdf_page->internal = pi;
161 pdf_page->destroy = pdfpage_destroy;
162 pdf_page->render = pdfpage_render;
163 pdf_page->rendersection = pdfpage_rendersection;
164 pdf_page->width = di->pages[page-1].width;
165 pdf_page->height = di->pages[page-1].height;
167 pdf_page->parent = doc;
172 void storeDeviceParameter(char*name, char*value)
174 parameter_t*p = new parameter_t();
175 p->name = strdup(name);
176 p->value = strdup(value);
178 if(device_config_next) {
179 device_config_next->next = p;
180 device_config_next = p;
183 device_config_next = p;
187 void pdf_set_parameter(char*name, char*value)
189 msg("<verbose> setting parameter %s to \"%s\"", name, value);
190 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
191 addGlobalFontDir(value);
192 } else if(!strcmp(name, "pages")) {
193 global_page_range = strdup(value);
194 } else if(!strncmp(name, "font", strlen("font"))) {
195 addGlobalFont(value);
196 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
197 addGlobalLanguageDir(value);
198 } else if(!strcmp(name, "zoom")) {
201 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
202 storeDeviceParameter("jpegsubpixels", buf);
203 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
204 storeDeviceParameter("ppmsubpixels", buf);
205 } else if(!strcmp(name, "jpegdpi")) {
207 jpeg_dpi = atoi(value);
208 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
209 storeDeviceParameter("jpegsubpixels", buf);
210 } else if(!strcmp(name, "ppmdpi")) {
212 ppm_dpi = atoi(value);
213 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
214 storeDeviceParameter("ppmsubpixels", buf);
216 storeDeviceParameter(name,value);
220 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
225 if (infoDict->lookup(key, &obj)->isString()) {
226 s1 = obj.getString();
227 if ((s1->getChar(0) & 0xff) == 0xfe &&
228 (s1->getChar(1) & 0xff) == 0xff) {
230 for (i = 2; i < obj.getString()->getLength(); i += 2) {
231 if (s1->getChar(i) == '\0') {
232 s2->append(s1->getChar(i+1));
235 s2 = new GString("<unicode>");
239 printf(fmt, s2->getCString());
242 printf(fmt, s1->getCString());
248 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
252 if (infoDict->lookup(key, &obj)->isString()) {
253 s = obj.getString()->getCString();
254 if (s[0] == 'D' && s[1] == ':') {
264 gfxdocument_t*pdf_open(char*filename)
266 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
267 memset(pdf_doc, 0, sizeof(gfxdocument_t));
268 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
269 memset(i, 0, sizeof(pdf_doc_internal_t));
270 pdf_doc->internal = i;
273 filename = strdup(filename);
276 if((x = strchr(filename, '|'))) {
281 GString *fileName = new GString(filename);
287 globalParams = new GlobalParams("");
290 if (userPassword && userPassword[0]) {
291 userPW = new GString(userPassword);
295 i->doc = new PDFDoc(fileName, userPW);
299 if (!i->doc->isOk()) {
300 printf("xpdf reports document as broken.\n");
305 i->doc->getDocInfo(&info);
307 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
308 printInfoString(info.getDict(), "Title", "Title: %s\n");
309 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
310 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
311 printInfoString(info.getDict(), "Author", "Author: %s\n");
312 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
313 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
314 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
315 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
316 printf("Pages: %d\n", i->doc->getNumPages());
317 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
318 printf("Encrypted: ");
319 if (i->doc->isEncrypted()) {
320 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
321 i->doc->okToPrint() ? "yes" : "no",
322 i->doc->okToCopy() ? "yes" : "no",
323 i->doc->okToChange() ? "yes" : "no",
324 i->doc->okToAddNotes() ? "yes" : "no");
331 pdf_doc->num_pages = i->doc->getNumPages();
333 if (i->doc->isEncrypted()) {
334 if(!i->doc->okToCopy()) {
335 printf("PDF disallows copying.\n");
338 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
342 InfoOutputDev*io = new InfoOutputDev();
344 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
345 memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
346 for(t=1;t<=pdf_doc->num_pages;t++) {
347 if(!global_page_range || is_in_range(t, global_page_range)) {
348 i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
349 #if xpdfUpdateVersion >= 16
350 i->doc->processLinks((OutputDev*)io, t);
352 i->pages[t-1].xMin = io->x1;
353 i->pages[t-1].yMin = io->y1;
354 i->pages[t-1].xMax = io->x2;
355 i->pages[t-1].yMax = io->y2;
356 i->pages[t-1].width = io->x2 - io->x1;
357 i->pages[t-1].height = io->y2 - io->y1;
358 i->pages[t-1].number_of_images = io->num_images;
359 i->pages[t-1].number_of_links = io->num_links;
360 i->pages[t-1].number_of_fonts = io->num_fonts;
361 i->pages[t-1].has_info = 1;
365 i->outputDev = new GFXOutputDev(device_config);
368 pdf_doc->destroy = pdf_doc_destroy;
369 pdf_doc->set_parameter = pdf_doc_set_parameter;
370 pdf_doc->getpage = pdf_doc_getpage;
377 gfxsource_t*gfxsource_pdf_create()
379 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
380 memset(src, 0, sizeof(gfxsource_t));
381 src->set_parameter = pdf_set_parameter;
382 src->open = pdf_open;