X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fpdf%2Fpdf.cc;h=10cc1906d18c3e599756b4188e6b5fff2b93f51f;hb=bcd63646450a9548aede2025b9c67c0da6a1e05c;hp=eba2a6724698ef666886102acc9a75602e071f7d;hpb=db1647cfff16c47a8744bbab4c8ddc638c2301d3;p=swftools.git diff --git a/lib/pdf/pdf.cc b/lib/pdf/pdf.cc index eba2a67..10cc190 100644 --- a/lib/pdf/pdf.cc +++ b/lib/pdf/pdf.cc @@ -5,6 +5,7 @@ #include "GlobalParams.h" #include "InfoOutputDev.h" #include "GFXOutputDev.h" +#include "BitmapOutputDev.h" #include "../mem.h" #include "pdf.h" #define NO_ARGPARSER @@ -36,7 +37,7 @@ typedef struct _pdf_doc_internal PDFDoc*doc; Object docinfo; InfoOutputDev*info; - GFXOutputDev*outputDev; + CommonOutputDev*outputDev; pdf_page_info_t*pages; } pdf_doc_internal_t; @@ -46,7 +47,7 @@ typedef struct _pdf_page_internal typedef struct _dev_output_internal { - GFXOutputDev*outputDev; + CommonOutputDev*outputDev; } dev_output_internal_t; @@ -67,7 +68,7 @@ void pdfpage_destroy(gfxpage_t*pdf_page) free(pdf_page);pdf_page=0; } -void render2(gfxpage_t*page, gfxdevice_t*output) +void render2(gfxpage_t*page, gfxdevice_t*dev) { pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal; @@ -81,43 +82,52 @@ void render2(gfxpage_t*page, gfxdevice_t*output) return; } + pi->outputDev->setDevice(dev); if(pi->protect) { - gfxdevice_t*dev = pi->outputDev->device; dev->setparameter(dev, "protect", "1"); } - pi->outputDev->setInfo(pi->info); - pi->outputDev->setXRef(pi->doc, pi->doc->getXRef()); + + /* pass global parameters to output device */ + parameter_t*p = device_config; + while(p) { + dev->setparameter(dev, p->name, p->value); + p = p->next; + } pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1); pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr); + pi->outputDev->setDevice(0); } void pdfpage_render(gfxpage_t*page, gfxdevice_t*output) { pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal; - pi->outputDev->setDevice(output); pi->outputDev->setMove(0,0); pi->outputDev->setClip(0,0,0,0); render2(page, output); - pi->outputDev->setDevice(0); } 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) { - int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2; pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal; - pi->outputDev->setDevice(output); - pi->outputDev->setMove((int)x,(int)y); + + int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2; if((x1|y1|x2|y2)==0) x2++; + + pi->outputDev->setMove((int)x,(int)y); pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2); render2(page, output); - pi->outputDev->setDevice(0); } +static int globalparams_count=0; + void pdf_doc_destroy(gfxdocument_t*gfx) { pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal; + if(i->outputDev) { + delete i->outputDev;i->outputDev=0; + } delete i->doc; i->doc=0; free(i->pages); i->pages = 0; @@ -134,18 +144,25 @@ void pdf_doc_destroy(gfxdocument_t*gfx) free(global_page_range); global_page_range = 0; } + + /*globalparams_count--; + if(!globalparams_count) { + delete globalParams; + globalParams = 0; + globalparams_count = 0; + }*/ } -void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value) +void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value) { pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal; + CommonOutputDev*o = i->outputDev; if(!strcmp(name, "pagemap")) { - GFXOutputDev*o = i->outputDev; int pdfpage=0, outputpage=0; sscanf(value,"%d:%d", &pdfpage, &outputpage); o->preparePage(pdfpage, outputpage); } else { - msg(" Ignored parameter: %s=%s", name, value); + o->setParameter(name, value); } } @@ -178,7 +195,7 @@ static char*getInfoString(Dict *infoDict, char *key) GString *s1, *s2; int i; - if (infoDict->lookup(key, &obj)->isString()) { + if (infoDict && infoDict->lookup(key, &obj)->isString()) { s1 = obj.getString(); if ((s1->getChar(0) & 0xff) == 0xfe && (s1->getChar(1) & 0xff) == 0xff) { @@ -210,7 +227,7 @@ static char*getInfoDate(Dict *infoDict, char *key) Object obj; char *s; - if (infoDict->lookup(key, &obj)->isString()) { + if (infoDict && infoDict->lookup(key, &obj)->isString()) { s = obj.getString()->getCString(); if (s[0] == 'D' && s[1] == ':') { s += 2; @@ -222,7 +239,7 @@ static char*getInfoDate(Dict *infoDict, char *key) return strdup(""); } -char* pdf_doc_getinfo(gfxdocument_t*doc, char*name) +char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name) { pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal; if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title"); @@ -234,16 +251,22 @@ char* pdf_doc_getinfo(gfxdocument_t*doc, char*name) else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate"); else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate"); else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no"); + else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no"); else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no"); else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no"); else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no"); else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no"); else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no"); + else if(!strcmp(name, "version")) { + char buf[32]; + sprintf(buf, "%.1f", i->doc->getPDFVersion()); + return strdup(buf); + } return 0; } -void storeDeviceParameter(char*name, char*value) +static void storeDeviceParameter(const char*name, const char*value) { parameter_t*p = new parameter_t(); p->name = strdup(name); @@ -258,8 +281,14 @@ void storeDeviceParameter(char*name, char*value) } } -void pdf_set_parameter(char*name, char*value) +typedef struct _gfxsource_internal +{ + int config_bitmap_optimizing; +} gfxsource_internal_t; + +static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value) { + gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal; msg(" setting parameter %s to \"%s\"", name, value); if(!strncmp(name, "fontdir", strlen("fontdir"))) { addGlobalFontDir(value); @@ -286,13 +315,22 @@ void pdf_set_parameter(char*name, char*value) ppm_dpi = atoi(value); sprintf(buf, "%f", (double)ppm_dpi/(double)zoom); storeDeviceParameter("ppmsubpixels", buf); + } else if(!strcmp(name, "poly2bitmap")) { + i->config_bitmap_optimizing = 1; + } else if(!strcmp(name, "help")) { + printf("\nPDF device global parameters:\n"); + printf("fontdir= a directory with additional fonts\n"); + printf("font= an dditional font filename\n"); + printf("pages= the range of pages to convert (example: pages=1-100,210-)\n"); + printf("zoom= the resultion (default: 72)\n"); } else { storeDeviceParameter(name,value); - } + } } -gfxdocument_t*pdf_open(char*filename) +static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename) { + gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal; gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t)); memset(pdf_doc, 0, sizeof(gfxdocument_t)); pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t)); @@ -311,10 +349,6 @@ gfxdocument_t*pdf_open(char*filename) GString *fileName = new GString(filename); GString *userPW; - // read config file - if(!globalParams) - globalParams = new GlobalParams(""); - // open PDF file if (userPassword && userPassword[0]) { userPW = new GString(userPassword); @@ -344,28 +378,41 @@ gfxdocument_t*pdf_open(char*filename) i->protect = 1; } - InfoOutputDev*io = new InfoOutputDev(); + i->info = new InfoOutputDev(i->doc->getXRef()); int t; i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages); memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages); for(t=1;t<=pdf_doc->num_pages;t++) { if(!global_page_range || is_in_range(t, global_page_range)) { - i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1); - i->doc->processLinks((OutputDev*)io, t); - i->pages[t-1].xMin = io->x1; - i->pages[t-1].yMin = io->y1; - i->pages[t-1].xMax = io->x2; - i->pages[t-1].yMax = io->y2; - i->pages[t-1].width = io->x2 - io->x1; - i->pages[t-1].height = io->y2 - io->y1; - i->pages[t-1].number_of_images = io->num_images; - i->pages[t-1].number_of_links = io->num_links; - i->pages[t-1].number_of_fonts = io->num_fonts; + i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1); + i->doc->processLinks((OutputDev*)i->info, t); + i->pages[t-1].xMin = i->info->x1; + i->pages[t-1].yMin = i->info->y1; + i->pages[t-1].xMax = i->info->x2; + i->pages[t-1].yMax = i->info->y2; + i->pages[t-1].width = i->info->x2 - i->info->x1; + i->pages[t-1].height = i->info->y2 - i->info->y1; + i->pages[t-1].number_of_images = i->info->num_images; + i->pages[t-1].number_of_links = i->info->num_links; + i->pages[t-1].number_of_fonts = i->info->num_fonts; i->pages[t-1].has_info = 1; } } - i->info = io; - i->outputDev = new GFXOutputDev(device_config); + + if(isrc->config_bitmap_optimizing) { + BitmapOutputDev*outputDev = new BitmapOutputDev(i->info, i->doc); + i->outputDev = (CommonOutputDev*)outputDev; + } else { + GFXOutputDev*outputDev = new GFXOutputDev(i->info, i->doc); + i->outputDev = (CommonOutputDev*)outputDev; + } + + /* pass global parameters to PDF driver*/ + parameter_t*p = device_config; + while(p) { + i->outputDev->setParameter(p->name, p->value); + p = p->next; + } pdf_doc->get = 0; pdf_doc->destroy = pdf_doc_destroy; @@ -384,5 +431,14 @@ gfxsource_t*gfxsource_pdf_create() memset(src, 0, sizeof(gfxsource_t)); src->set_parameter = pdf_set_parameter; src->open = pdf_open; + src->internal = malloc(sizeof(gfxsource_internal_t)); + memset(src->internal, 0, sizeof(gfxsource_internal_t)); + + if(!globalParams) { + globalParams = new GFXGlobalParams(); + //globalparams_count++; + } + + return src; }