+void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
+ int width, int height, GBool invert,
+ GBool inlineImg)
+{
+ if(states[statepos].textRender & 4) //clipped
+ return;
+ msg("<verbose> drawImageMask %dx%d, invert=%d inline=%d", width, height, invert, inlineImg);
+ drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1, 0, 0,0,0,0, 0);
+}
+
+void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
+ int width, int height, GfxImageColorMap *colorMap,
+ int *maskColors, GBool inlineImg)
+{
+ if(states[statepos].textRender & 4) //clipped
+ return;
+
+ msg("<verbose> drawImage %dx%d, %s, %s, inline=%d", width, height,
+ colorMap?"colorMap":"no colorMap",
+ maskColors?"maskColors":"no maskColors",
+ inlineImg);
+ if(colorMap)
+ msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
+ colorMap->getBits(),colorMap->getColorSpace()->getMode());
+ drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0,maskColors, 0,0,0,0, 0);
+}
+
+void SWFOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
+ int width, int height,
+ GfxImageColorMap *colorMap,
+ Stream *maskStr, int maskWidth, int maskHeight,
+ GBool maskInvert)
+{
+ if(states[statepos].textRender & 4) //clipped
+ return;
+
+ msg("<verbose> drawMaskedImage %dx%d, %s, %dx%d mask", width, height,
+ colorMap?"colorMap":"no colorMap",
+ maskWidth, maskHeight);
+ if(colorMap)
+ msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
+ colorMap->getBits(),colorMap->getColorSpace()->getMode());
+ drawGeneralImage(state,ref,str,width,height,colorMap,0,0,0,0, maskStr, maskWidth, maskHeight, maskInvert, 0);
+}
+
+void SWFOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
+ int width, int height,
+ GfxImageColorMap *colorMap,
+ Stream *maskStr,
+ int maskWidth, int maskHeight,
+ GfxImageColorMap *maskColorMap)
+{
+ if(states[statepos].textRender & 4) //clipped
+ return;
+
+ msg("<verbose> drawSoftMaskedImage %dx%d, %s, %dx%d mask", width, height,
+ colorMap?"colorMap":"no colorMap",
+ maskWidth, maskHeight);
+ if(colorMap)
+ msg("<verbose> colorMap pixcomps:%d bits:%d mode:%d\n", colorMap->getNumPixelComps(),
+ colorMap->getBits(),colorMap->getColorSpace()->getMode());
+ drawGeneralImage(state,ref,str,width,height,colorMap,0,0,0,0, maskStr, maskWidth, maskHeight, 0, maskColorMap);
+}
+
+//SWFOutputDev*output = 0;
+
+static void printInfoString(Dict *infoDict, char *key, char *fmt) {
+ Object obj;
+ GString *s1, *s2;
+ int i;
+
+ if (infoDict->lookup(key, &obj)->isString()) {
+ s1 = obj.getString();
+ if ((s1->getChar(0) & 0xff) == 0xfe &&
+ (s1->getChar(1) & 0xff) == 0xff) {
+ s2 = new GString();
+ for (i = 2; i < obj.getString()->getLength(); i += 2) {
+ if (s1->getChar(i) == '\0') {
+ s2->append(s1->getChar(i+1));
+ } else {
+ delete s2;
+ s2 = new GString("<unicode>");
+ break;
+ }
+ }
+ printf(fmt, s2->getCString());
+ delete s2;
+ } else {
+ printf(fmt, s1->getCString());
+ }
+ }
+ obj.free();
+}
+
+static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
+ Object obj;
+ char *s;
+
+ if (infoDict->lookup(key, &obj)->isString()) {
+ s = obj.getString()->getCString();
+ if (s[0] == 'D' && s[1] == ':') {
+ s += 2;
+ }
+ printf(fmt, s);
+ }
+ obj.free();
+}
+
+int jpeg_dpi = 0;
+int ppm_dpi = 0;
+
+void storeDeviceParameter(char*name, char*value)
+{
+ parameter_t*p = new parameter_t();
+ p->name = strdup(name);
+ p->value = strdup(value);
+ p->next = 0;
+ if(device_config_next) {
+ device_config_next->next = p;
+ device_config_next = p;
+ } else {
+ device_config = p;
+ device_config_next = p;
+ }
+}
+
+void pdfswf_setparameter(char*name, char*value)
+{
+ msg("<verbose> setting parameter %s to \"%s\"", name, value);
+ if(!strcmp(name, "caplinewidth")) {
+ caplinewidth = atof(value);
+ } else if(!strcmp(name, "zoom")) {
+ char buf[80];
+ zoom = atof(value);
+ sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
+ storeDeviceParameter("jpegsubpixels", buf);
+ sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
+ storeDeviceParameter("ppmsubpixels", buf);
+ } else if(!strcmp(name, "jpegdpi")) {
+ char buf[80];
+ jpeg_dpi = atoi(value);
+ sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
+ storeDeviceParameter("jpegsubpixels", buf);
+ } else if(!strcmp(name, "ppmdpi")) {
+ char buf[80];
+ ppm_dpi = atoi(value);
+ sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
+ storeDeviceParameter("ppmsubpixels", buf);
+ } else if(!strcmp(name, "forceType0Fonts")) {
+ forceType0Fonts = atoi(value);
+ } else if(!strncmp(name, "fontdir", strlen("fontdir"))) {
+ pdfswf_addfontdir(value);
+ } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
+ pdfswf_addlanguagedir(value);
+ } else if(!strcmp(name, "fontconfig")) {
+ config_use_fontconfig = atoi(value);
+ } else {
+ storeDeviceParameter(name,value);
+ }
+}
+void pdfswf_addfont(char*filename)
+{
+ fontfile_t f;
+ memset(&f, 0, sizeof(fontfile_t));
+ f.filename = filename;
+ if(fontnum < sizeof(fonts)/sizeof(fonts[0])) {
+ fonts[fontnum++] = f;
+ } else {
+ msg("<error> Too many external fonts. Not adding font file \"%s\".", filename);
+ }
+}
+
+static char* dirseparator()
+{
+#ifdef WIN32
+ return "\\";
+#else
+ return "/";
+#endif
+}
+
+void pdfswf_addlanguagedir(char*dir)
+{
+ if(!globalParams)
+ globalParams = new GlobalParams("");
+
+ msg("<notice> Adding %s to language pack directories", dir);
+
+ int l;
+ FILE*fi = 0;
+ char* config_file = (char*)malloc(strlen(dir) + 1 + sizeof("add-to-xpdfrc"));
+ strcpy(config_file, dir);
+ strcat(config_file, dirseparator());
+ strcat(config_file, "add-to-xpdfrc");
+
+ fi = fopen(config_file, "rb");
+ if(!fi) {
+ msg("<error> Could not open %s", config_file);
+ return;
+ }
+ globalParams->parseFile(new GString(config_file), fi);
+ fclose(fi);
+}
+
+void pdfswf_addfontdir(char*dirname)
+{
+#ifdef HAVE_DIRENT_H
+ msg("<notice> Adding %s to font directories", dirname);
+ lastfontdir = strdup(dirname);
+ DIR*dir = opendir(dirname);
+ if(!dir) {
+ msg("<warning> Couldn't open directory %s\n", dirname);
+ return;
+ }
+ struct dirent*ent;
+ while(1) {
+ ent = readdir (dir);
+ if (!ent)
+ break;
+ int l;
+ char*name = ent->d_name;
+ char type = 0;
+ if(!name) continue;
+ l=strlen(name);
+ if(l<4)
+ continue;
+ if(!strncasecmp(&name[l-4], ".pfa", 4))
+ type=1;
+ if(!strncasecmp(&name[l-4], ".pfb", 4))
+ type=3;
+ if(!strncasecmp(&name[l-4], ".ttf", 4))
+ type=2;
+ if(type)
+ {
+ char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
+ strcpy(fontname, dirname);
+ strcat(fontname, dirseparator());
+ strcat(fontname, name);
+ msg("<verbose> Adding %s to fonts", fontname);
+ pdfswf_addfont(fontname);
+ }
+ }
+ closedir(dir);
+#else
+ msg("<warning> No dirent.h- unable to add font dir %s", dir);
+#endif
+}
+
+
+typedef struct _pdf_doc_internal
+{
+ int protect;
+ PDFDoc*doc;
+ InfoOutputDev*info;
+} pdf_doc_internal_t;
+typedef struct _pdf_page_internal
+{
+} pdf_page_internal_t;
+typedef struct _swf_output_internal
+{
+ SWFOutputDev*outputDev;
+} swf_output_internal_t;
+
+pdf_doc_t* pdf_init(char*filename, char*userPassword)
+{
+ pdf_doc_t*pdf_doc = (pdf_doc_t*)malloc(sizeof(pdf_doc_t));
+ memset(pdf_doc, 0, sizeof(pdf_doc_t));
+ pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
+ memset(i, 0, sizeof(pdf_doc_internal_t));
+ pdf_doc->internal = i;
+
+ GString *fileName = new GString(filename);
+ GString *userPW;
+ Object info;
+
+ // read config file
+ if(!globalParams)
+ globalParams = new GlobalParams("");
+
+ // open PDF file
+ if (userPassword && userPassword[0]) {
+ userPW = new GString(userPassword);
+ } else {
+ userPW = NULL;
+ }
+ i->doc = new PDFDoc(fileName, userPW);
+ if (userPW) {
+ delete userPW;
+ }
+ if (!i->doc->isOk()) {
+ return 0;
+ }
+
+ // print doc info
+ i->doc->getDocInfo(&info);
+ if (info.isDict() &&
+ (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
+ printInfoString(info.getDict(), "Title", "Title: %s\n");
+ printInfoString(info.getDict(), "Subject", "Subject: %s\n");
+ printInfoString(info.getDict(), "Keywords", "Keywords: %s\n");
+ printInfoString(info.getDict(), "Author", "Author: %s\n");
+ printInfoString(info.getDict(), "Creator", "Creator: %s\n");
+ printInfoString(info.getDict(), "Producer", "Producer: %s\n");
+ printInfoDate(info.getDict(), "CreationDate", "CreationDate: %s\n");
+ printInfoDate(info.getDict(), "ModDate", "ModDate: %s\n");
+ printf("Pages: %d\n", i->doc->getNumPages());
+ printf("Linearized: %s\n", i->doc->isLinearized() ? "yes" : "no");
+ printf("Encrypted: ");
+ if (i->doc->isEncrypted()) {
+ printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
+ i->doc->okToPrint() ? "yes" : "no",
+ i->doc->okToCopy() ? "yes" : "no",
+ i->doc->okToChange() ? "yes" : "no",
+ i->doc->okToAddNotes() ? "yes" : "no");
+ } else {
+ printf("no\n");
+ }
+ }
+ info.free();
+
+ pdf_doc->num_pages = i->doc->getNumPages();
+ i->protect = 0;
+ if (i->doc->isEncrypted()) {
+ if(!i->doc->okToCopy()) {
+ printf("PDF disallows copying.\n");
+ return 0;
+ }
+ if(!i->doc->okToChange() || !i->doc->okToAddNotes())
+ i->protect = 1;
+ }
+
+ InfoOutputDev*io = new InfoOutputDev();
+ int t;
+ for(t=1;t<=pdf_doc->num_pages;t++) {
+ i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
+ }
+ i->info = io;
+
+ return pdf_doc;
+}
+
+class MemCheck
+{
+ public: ~MemCheck()
+ {
+ delete globalParams;globalParams=0;
+ Object::memCheck(stderr);
+ gMemReport(stderr);
+ }
+} myMemCheck;
+
+void pdf_destroy(pdf_doc_t*pdf_doc)
+{
+ pdf_doc_internal_t*i= (pdf_doc_internal_t*)pdf_doc->internal;
+
+ delete i->doc; i->doc=0;
+
+ if(i->info) {
+ delete i->info;i->info=0;
+ }
+
+ free(pdf_doc->internal);pdf_doc->internal=0;
+ free(pdf_doc);pdf_doc=0;
+}
+
+pdf_page_t* pdf_getpage(pdf_doc_t*pdf_doc, int page)
+{
+ pdf_doc_internal_t*di= (pdf_doc_internal_t*)pdf_doc->internal;
+
+ if(page < 1 || page > pdf_doc->num_pages)
+ return 0;
+
+ pdf_page_t* pdf_page = (pdf_page_t*)malloc(sizeof(pdf_page_t));
+ pdf_page_internal_t*pi= (pdf_page_internal_t*)malloc(sizeof(pdf_page_internal_t));
+ memset(pi, 0, sizeof(pdf_page_internal_t));
+ pdf_page->internal = pi;
+
+ pdf_page->parent = pdf_doc;
+ pdf_page->nr = page;
+ return pdf_page;
+}
+
+void pdf_page_destroy(pdf_page_t*pdf_page)
+{
+ pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal;
+ free(pdf_page->internal);pdf_page->internal = 0;
+ free(pdf_page);pdf_page=0;
+}
+
+swf_output_t* swf_output_init()
+{
+ swf_output_t*swf_output = (swf_output_t*)malloc(sizeof(swf_output_t));
+ memset(swf_output, 0, sizeof(swf_output_t));
+ swf_output_internal_t*i= (swf_output_internal_t*)malloc(sizeof(swf_output_internal_t));
+ memset(i, 0, sizeof(swf_output_internal_t));
+ swf_output->internal = i;
+
+ i->outputDev = new SWFOutputDev();
+ return swf_output;
+}
+
+void swf_output_setparameter(swf_output_t*swf, char*name, char*value)
+{
+ pdfswf_setparameter(name, value);
+}
+
+void swf_output_startframe(swf_output_t*swf, int width, int height)
+{
+ swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+ i->outputDev->startFrame(width, height);
+}
+
+void swf_output_endframe(swf_output_t*swf)
+{
+ swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+ i->outputDev->endframe();
+}
+
+void swf_output_preparepage(swf_output_t*swf, int pdfpage, int outputpage)
+{
+ swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+ SWFOutputDev*o = i->outputDev;
+
+ if(pdfpage < 0)
+ return;
+
+ if(!o->pages) {
+ o->pagebuflen = 1024;
+ o->pages = (int*)malloc(o->pagebuflen*sizeof(int));
+ memset(o->pages, -1, o->pagebuflen*sizeof(int));
+ } else {
+ while(pdfpage >= o->pagebuflen)
+ {
+ int oldlen = o->pagebuflen;
+ o->pagebuflen+=1024;
+ o->pages = (int*)realloc(o->pages, o->pagebuflen*sizeof(int));
+ memset(&o->pages[oldlen], -1, (o->pagebuflen-oldlen)*sizeof(int));
+ }
+ }
+ o->pages[pdfpage] = outputpage;
+ if(pdfpage>o->pagepos)
+ o->pagepos = pdfpage;
+}
+
+int swf_output_save(swf_output_t*swf, char*filename)
+{
+ swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+ int ret = i->outputDev->save(filename);
+ return ret;
+}
+
+void* swf_output_get(swf_output_t*swf,char*name)
+{
+ swf_output_internal_t*i= (swf_output_internal_t*)swf->internal;
+ void* ret = i->outputDev->get(name);
+ return ret;
+}
+
+void swf_output_destroy(swf_output_t*output)
+{
+ swf_output_internal_t*i = (swf_output_internal_t*)output->internal;
+ delete i->outputDev; i->outputDev=0;
+ free(output->internal);output->internal=0;
+ free(output);
+}
+
+void pdf_page_render2(pdf_page_t*page, swf_output_t*swf)
+{
+ pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
+ swf_output_internal_t*si = (swf_output_internal_t*)swf->internal;
+
+ if(!pi) {
+ msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
+ return;
+ }
+
+ if(pi->protect) {
+ gfxdevice_t*dev = si->outputDev->output;
+ dev->setparameter(dev, "protect", "1");
+ }
+ si->outputDev->setInfo(pi->info);
+ si->outputDev->setXRef(pi->doc, pi->doc->getXRef());
+ pi->doc->displayPage((OutputDev*)si->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
+}
+
+void pdf_page_rendersection(pdf_page_t*page, swf_output_t*output, int x, int y, int x1, int y1, int x2, int y2)
+{
+ pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
+ swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
+
+ si->outputDev->setMove(x,y);
+ if((x1|y1|x2|y2)==0) x2++;
+ si->outputDev->setClip(x1,y1,x2,y2);
+
+ pdf_page_render2(page, output);
+}
+void pdf_page_render(pdf_page_t*page, swf_output_t*output)
+{
+ pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
+ swf_output_internal_t*si = (swf_output_internal_t*)output->internal;
+
+ si->outputDev->setMove(0,0);
+ si->outputDev->setClip(0,0,0,0);
+
+ pdf_page_render2(page, output);
+}
+
+
+pdf_page_info_t* pdf_page_getinfo(pdf_page_t*page)
+{
+ pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
+ pdf_page_internal_t*i= (pdf_page_internal_t*)page->internal;
+ pdf_page_info_t*info = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t));
+ memset(info, 0, sizeof(pdf_page_info_t));
+
+ InfoOutputDev*output = new InfoOutputDev;
+
+ pi->doc->displayPage((OutputDev*)output, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
+
+ info->xMin = output->x1;
+ info->yMin = output->y1;
+ info->xMax = output->x2;
+ info->yMax = output->y2;
+ info->number_of_images = output->num_images;
+ info->number_of_links = output->num_links;
+ info->number_of_fonts = output->num_fonts;
+
+ delete output;
+
+ return info;
+}
+
+void pdf_page_info_destroy(pdf_page_info_t*info)
+{
+ free(info);
+
+}