1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
4 #include "GlobalParams.h"
5 #include "InfoOutputDev.h"
6 #include "GFXOutputDev.h"
10 static parameter_t* device_config = 0;
11 static parameter_t* device_config_next = 0;
16 static double zoom = 72; /* xpdf: 86 */
18 typedef struct _pdf_page_info
20 int xMin, yMin, xMax, yMax;
27 typedef struct _pdf_doc_internal
32 GFXOutputDev*outputDev;
33 pdf_page_info_t*pages;
36 typedef struct _pdf_page_internal
38 } pdf_page_internal_t;
40 typedef struct _dev_output_internal
42 GFXOutputDev*outputDev;
43 } dev_output_internal_t;
46 static char* dirseparator()
56 void pdfpage_destroy(gfxpage_t*pdf_page)
58 pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
59 free(pdf_page->internal);pdf_page->internal = 0;
60 free(pdf_page);pdf_page=0;
63 void render2(gfxpage_t*page, gfxdevice_t*output)
65 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
68 msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
73 gfxdevice_t*dev = pi->outputDev->device;
74 dev->setparameter(dev, "protect", "1");
76 pi->outputDev->setInfo(pi->info);
77 pi->outputDev->setXRef(pi->doc, pi->doc->getXRef());
78 pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
82 void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
84 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
85 pi->outputDev->setDevice(output);
86 pi->outputDev->setMove(0,0);
87 pi->outputDev->setClip(0,0,0,0);
88 render2(page, output);
89 pi->outputDev->setDevice(0);
92 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)
94 int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
95 pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
96 pi->outputDev->setDevice(output);
97 pi->outputDev->setMove((int)x,(int)y);
98 if((x1|y1|x2|y2)==0) x2++;
99 pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
100 render2(page, output);
101 pi->outputDev->setDevice(0);
104 void pdf_doc_destroy(gfxdocument_t*gfx)
106 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
108 delete i->doc; i->doc=0;
109 free(i->pages); i->pages = 0;
112 delete i->info;i->info=0;
115 free(gfx->internal);gfx->internal=0;
119 void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
121 pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
122 if(!strcmp(name, "pagemap")) {
123 GFXOutputDev*o = i->outputDev;
124 int pdfpage=0, outputpage=0;
125 sscanf(value,"%d:%d", &pdfpage, &outputpage);
126 o->preparePage(pdfpage, outputpage);
128 msg("<warning> Ignored parameter: %s=%s", name, value);
132 gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
134 pdf_doc_internal_t*di= (pdf_doc_internal_t*)doc->internal;
136 if(page < 1 || page > doc->num_pages)
139 gfxpage_t* pdf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
140 pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
141 memset(pi, 0, sizeof(pdf_page_internal_t));
142 pdf_page->internal = pi;
144 pdf_page->destroy = pdfpage_destroy;
145 pdf_page->render = pdfpage_render;
146 pdf_page->rendersection = pdfpage_rendersection;
147 pdf_page->width = di->pages[page-1].width;
148 pdf_page->height = di->pages[page-1].height;
150 pdf_page->parent = doc;
155 void storeDeviceParameter(char*name, char*value)
157 parameter_t*p = new parameter_t();
158 p->name = strdup(name);
159 p->value = strdup(value);
161 if(device_config_next) {
162 device_config_next->next = p;
163 device_config_next = p;
166 device_config_next = p;
170 void pdf_set_parameter(char*name, char*value)
172 msg("<verbose> setting parameter %s to \"%s\"", name, value);
173 if(!strncmp(name, "fontdir", strlen("fontdir"))) {
174 addGlobalFontDir(value);
175 } else if(!strncmp(name, "font", strlen("font"))) {
176 addGlobalFont(value);
177 } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
178 addGlobalLanguageDir(value);
179 } else if(!strcmp(name, "zoom")) {
182 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
183 storeDeviceParameter("jpegsubpixels", buf);
184 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
185 storeDeviceParameter("ppmsubpixels", buf);
186 } else if(!strcmp(name, "jpegdpi")) {
188 jpeg_dpi = atoi(value);
189 sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
190 storeDeviceParameter("jpegsubpixels", buf);
191 } else if(!strcmp(name, "ppmdpi")) {
193 ppm_dpi = atoi(value);
194 sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
195 storeDeviceParameter("ppmsubpixels", buf);
197 storeDeviceParameter(name,value);
201 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
206 if (infoDict->lookup(key, &obj)->isString()) {
207 s1 = obj.getString();
208 if ((s1->getChar(0) & 0xff) == 0xfe &&
209 (s1->getChar(1) & 0xff) == 0xff) {
211 for (i = 2; i < obj.getString()->getLength(); i += 2) {
212 if (s1->getChar(i) == '\0') {
213 s2->append(s1->getChar(i+1));
216 s2 = new GString("<unicode>");
220 printf(fmt, s2->getCString());
223 printf(fmt, s1->getCString());
229 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
233 if (infoDict->lookup(key, &obj)->isString()) {
234 s = obj.getString()->getCString();
235 if (s[0] == 'D' && s[1] == ':') {
245 gfxdocument_t*pdf_open(char*filename)
247 gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
248 memset(pdf_doc, 0, sizeof(gfxdocument_t));
249 pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
250 memset(i, 0, sizeof(pdf_doc_internal_t));
251 pdf_doc->internal = i;
254 filename = strdup(filename);
257 if((x = strchr(filename, '|'))) {
262 GString *fileName = new GString(filename);
268 globalParams = new GlobalParams("");
271 if (userPassword && userPassword[0]) {
272 userPW = new GString(userPassword);
276 i->doc = new PDFDoc(fileName, userPW);
280 if (!i->doc->isOk()) {
285 i->doc->getDocInfo(&info);
287 (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
288 printInfoString(info.getDict(), "Title", "Title: %s\n");
289 printInfoString(info.getDict(), "Subject", "Subject: %s\n");
290 printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
291 printInfoString(info.getDict(), "Author", "Author: %s\n");
292 printInfoString(info.getDict(), "Creator", "Creator: %s\n");
293 printInfoString(info.getDict(), "Producer", "Producer: %s\n");
294 printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
295 printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
296 printf("Pages: %d\n", i->doc->getNumPages());
297 printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
298 printf("Encrypted: ");
299 if (i->doc->isEncrypted()) {
300 printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
301 i->doc->okToPrint() ? "yes" : "no",
302 i->doc->okToCopy() ? "yes" : "no",
303 i->doc->okToChange() ? "yes" : "no",
304 i->doc->okToAddNotes() ? "yes" : "no");
311 pdf_doc->num_pages = i->doc->getNumPages();
313 if (i->doc->isEncrypted()) {
314 if(!i->doc->okToCopy()) {
315 printf("PDF disallows copying.\n");
318 if(!i->doc->okToChange() || !i->doc->okToAddNotes())
322 InfoOutputDev*io = new InfoOutputDev();
324 i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
325 for(t=1;t<=pdf_doc->num_pages;t++) {
326 i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
327 i->pages[t-1].xMin = io->x1;
328 i->pages[t-1].yMin = io->y1;
329 i->pages[t-1].xMax = io->x2;
330 i->pages[t-1].yMax = io->y2;
331 i->pages[t-1].width = io->x2 - io->x1;
332 i->pages[t-1].height = io->y2 - io->y1;
333 i->pages[t-1].number_of_images = io->num_images;
334 i->pages[t-1].number_of_links = io->num_links;
335 i->pages[t-1].number_of_fonts = io->num_fonts;
338 i->outputDev = new GFXOutputDev(device_config);
341 pdf_doc->destroy = pdf_doc_destroy;
342 pdf_doc->set_parameter = pdf_doc_set_parameter;
343 pdf_doc->getpage = pdf_doc_getpage;
350 gfxsource_t*gfxsource_pdf_create()
352 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
353 memset(src, 0, sizeof(gfxsource_t));
354 src->set_parameter = pdf_set_parameter;
355 src->open = pdf_open;