X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fpdf%2FGFXOutputDev.cc;h=43aa743f5242533d6196aa0bddcf2e9001de4022;hb=5342d19e08458c762289507835e2b0deb45e463c;hp=24a8f05d0fbf6ee5dd7b992baefe81e1d30fa337;hpb=1f75b5311e88f8a6d71146cf2e709f1f5666dfc8;p=swftools.git diff --git a/lib/pdf/GFXOutputDev.cc b/lib/pdf/GFXOutputDev.cc index 24a8f05..43aa743 100644 --- a/lib/pdf/GFXOutputDev.cc +++ b/lib/pdf/GFXOutputDev.cc @@ -22,7 +22,9 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif #include "../../config.h" #include "../os.h" #ifdef HAVE_DIRENT_H @@ -31,9 +33,6 @@ #ifdef HAVE_SYS_STAT_H #include #endif -#ifdef HAVE_FONTCONFIG -#include -#endif //xpdf header files #include "config.h" #include "gfile.h" @@ -60,7 +59,7 @@ #include "GHash.h" #include "GFXOutputDev.h" -//swftools header files +// swftools header files #include "../log.h" #include "../gfxdevice.h" #include "../gfxtools.h" @@ -71,7 +70,7 @@ #include "../devices/render.h" #include "../art/libart.h" -#include "../devices/artsutils.c" +#include "../devices/artsutils.h" #include "../png.h" #include "fonts.h" @@ -81,11 +80,16 @@ typedef struct _fontfile { const char*filename; + int len; // basename length int used; + struct _fontfile*next; } fontfile_t; // for pdfswf_addfont -static fontfile_t fonts[2048]; + +static fontfile_t* global_fonts = 0; +static fontfile_t* global_fonts_next = 0; + static int fontnum = 0; /* config */ @@ -152,7 +156,7 @@ typedef struct _feature } feature_t; feature_t*featurewarnings = 0; -void GFXOutputDev::showfeature(const char*feature,char fully, char warn) +void GFXOutputDev::showfeature(const char*feature, char fully, char warn) { feature_t*f = featurewarnings; while(f) { @@ -328,11 +332,34 @@ DisplayFontParam *GFXGlobalParams::getDisplayFont(GString *fontName) return dfp; } } + + int bestlen = 0x7fffffff; + const char*bestfilename = 0; + + fontfile_t*f = 0; + while(f) { + if(strstr(f->filename, name)) { + if(f->len < bestlen) { + bestlen = f->len; + bestfilename = f->filename; + } + } + f = f->next; + } + if(bestfilename) { + DisplayFontParam *dfp = new DisplayFontParam(new GString(fontName), displayFontT1); + dfp->t1.fileName = new GString(bestfilename); + return dfp; + } return GlobalParams::getDisplayFont(fontName); } -GFXOutputDev::GFXOutputDev(parameter_t*p) +GFXOutputDev::GFXOutputDev(InfoOutputDev*info, PDFDoc*doc) { + this->info = info; + this->doc = doc; + this->xref = doc->getXRef(); + this->jpeginfo = 0; this->textmodeinfo = 0; this->linkinfo = 0; @@ -356,52 +383,45 @@ GFXOutputDev::GFXOutputDev(parameter_t*p) this->pages = 0; this->pagebuflen = 0; this->pagepos = 0; - this->config_use_fontconfig=1; this->config_break_on_warning=0; this->config_remapunicode=0; this->config_transparent=0; this->config_extrafontdata = 0; + this->config_fontquality = 10; - this->parameters = p; - this->gfxfontlist = gfxfontlist_create(); memset(states, 0, sizeof(states)); - - /* configure device */ - while(p) { - setParameter(p->name, p->value); - p = p->next; - } }; void GFXOutputDev::setParameter(const char*key, const char*value) { if(!strcmp(key,"breakonwarning")) { this->config_break_on_warning = atoi(value); - } else if(!strcmp(key,"fontconfig")) { - this->config_use_fontconfig = atoi(value); } else if(!strcmp(key,"remapunicode")) { this->config_remapunicode = atoi(value); } else if(!strcmp(key,"transparent")) { this->config_transparent = atoi(value); } else if(!strcmp(key,"extrafontdata")) { this->config_extrafontdata = atoi(value); + } else if(!strcmp(key,"fontquality")) { + this->config_fontquality = atof(value); + if(this->config_fontquality<=1) + this->config_fontquality=1; + } else if(!strcmp(key,"help")) { + printf("\nPDF layer options:\n"); + printf("breakonwarning=0/1 Abort conversion if graphic objects are found which\n"); + printf(" are not 100%% supported\n"); + printf("transparent=0/1 Make PDF transparent (alpha background)\n"); + printf("extrafontdata=0/1 Store Type3 characters and capture characters\n"); + printf("fontquality=1..100 Curve approximation quality of the fonts\n"); } + } void GFXOutputDev::setDevice(gfxdevice_t*dev) { - parameter_t*p = this->parameters; - - /* pass parameters to output device */ this->device = dev; - if(this->device) { - while(p) { - this->device->setparameter(this->device, p->name, p->value); - p = p->next; - } - } } void GFXOutputDev::setMove(int x,int y) @@ -649,7 +669,11 @@ GBool GFXOutputDev::needNonText() } void GFXOutputDev::endPage() { - msg(" endPage"); + msg(" endPage (GfxOutputDev)"); + if(outer_clip_box) { + device->endclip(device); + outer_clip_box = 0; + } } #define STROKE_FILL 1 @@ -696,7 +720,7 @@ void GFXOutputDev::strokeGfxline(GfxState *state, gfxline_t*line, int flags) msg(" %d dashes", dashnum); msg(" | phase: %f", dashphase); for(t=0;t | d%-3d: %f", t, ldash[t]); } dash[dashnum] = -1; @@ -704,7 +728,7 @@ void GFXOutputDev::strokeGfxline(GfxState *state, gfxline_t*line, int flags) dump_outline(line); } - line2 = gfxtool_dash_line(line, dash, dashphase); + line2 = gfxtool_dash_line(line, dash, (float)dashphase); line = line2; free(dash); msg(" After dashing:"); @@ -819,14 +843,6 @@ void GFXOutputDev::clipToStrokePath(GfxState *state) gfxline_free(line); } -void GFXOutputDev::endframe() -{ - if(outer_clip_box) { - device->endclip(device); - outer_clip_box = 0; - } -} - void GFXOutputDev::finish() { if(outer_clip_box) { @@ -845,7 +861,7 @@ GFXOutputDev::~GFXOutputDev() free(this->pages); this->pages = 0; } - gfxfontlist_free(this->gfxfontlist); + gfxfontlist_free(this->gfxfontlist, 1);this->gfxfontlist = 0; }; GBool GFXOutputDev::upsideDown() { @@ -859,12 +875,6 @@ GBool GFXOutputDev::useDrawChar() const char*renderModeDesc[]= {"fill", "stroke", "fill+stroke", "invisible", "clip+fill", "stroke+clip", "fill+stroke+clip", "clip"}; -#define RENDER_FILL 0 -#define RENDER_STROKE 1 -#define RENDER_FILLSTROKE 2 -#define RENDER_INVISIBLE 3 -#define RENDER_CLIP 4 - static char tmp_printstr[4096]; char* makeStringPrintable(char*str) { @@ -893,15 +903,25 @@ char* makeStringPrintable(char*str) #define INTERNAL_FONT_SIZE 1024.0 void GFXOutputDev::updateFontMatrix(GfxState*state) { - double m11,m21,m12,m22; - state->getFontTransMat(&m11, &m12, &m21, &m22); - m11 *= state->getHorizScaling(); - m21 *= state->getHorizScaling(); + double* ctm = state->getCTM(); + double fontSize = state->getFontSize(); + double*textMat = state->getTextMat(); - this->current_font_matrix.m00 = m11 / INTERNAL_FONT_SIZE; - this->current_font_matrix.m01 = m12 / INTERNAL_FONT_SIZE; - this->current_font_matrix.m10 = -m21 / INTERNAL_FONT_SIZE; - this->current_font_matrix.m11 = -m22 / INTERNAL_FONT_SIZE; + /* taking the absolute value of horizScaling seems to be required for + some italic fonts. FIXME: SplashOutputDev doesn't need this- why? */ + double hscale = fabs(state->getHorizScaling()); + + // from xpdf-3.02/SplashOutputDev:updateFont + double mm11 = textMat[0] * fontSize * hscale; + double mm12 = textMat[1] * fontSize * hscale; + double mm21 = textMat[2] * fontSize; + double mm22 = textMat[3] * fontSize; + + // multiply with ctm, like state->getFontTransMat() does + this->current_font_matrix.m00 = (ctm[0]*mm11 + ctm[2]*mm12) / INTERNAL_FONT_SIZE; + this->current_font_matrix.m01 = (ctm[1]*mm11 + ctm[3]*mm12) / INTERNAL_FONT_SIZE; + this->current_font_matrix.m10 = (ctm[0]*mm21 + ctm[2]*mm22) / INTERNAL_FONT_SIZE; + this->current_font_matrix.m11 = (ctm[1]*mm21 + ctm[3]*mm22) / INTERNAL_FONT_SIZE; this->current_font_matrix.tx = 0; this->current_font_matrix.ty = 0; } @@ -914,7 +934,6 @@ void GFXOutputDev::beginString(GfxState *state, GString *s) } msg(" beginString(%s) render=%d", makeStringPrintable(s->getCString()), render); - updateFontMatrix(state); } static gfxline_t* mkEmptyGfxShape(double x, double y) @@ -937,10 +956,10 @@ void GFXOutputDev::drawChar(GfxState *state, double x, double y, CharCode charid, int nBytes, Unicode *_u, int uLen) { if(!current_fontinfo || (unsigned)charid >= current_fontinfo->num_glyphs || !current_fontinfo->glyphs[charid]) { - msg(" Invalid charid %d for font %s", charid, current_font_id); + msg(" Invalid charid %d for font (%d characters)", charid, current_fontinfo?current_fontinfo->num_glyphs:0); return; } - + CharCode glyphid = current_fontinfo->glyphs[charid]->glyphid; int render = state->getRender(); @@ -949,6 +968,8 @@ void GFXOutputDev::drawChar(GfxState *state, double x, double y, // check for invisible text -- this is used by Acrobat Capture if (render == RENDER_INVISIBLE) { col.a = 0; + if(!config_extrafontdata) + return; } GfxFont*font = state->getFont(); @@ -1066,7 +1087,7 @@ GBool GFXOutputDev::beginType3Char(GfxState *state, double x, double y, double d m.ty += user_movey + clipmovey; if(!current_fontinfo || (unsigned)charid >= current_fontinfo->num_glyphs || !current_fontinfo->glyphs[charid]) { - msg(" Invalid charid %d for font %s", charid, current_font_id); + msg(" Invalid charid %d for font", charid); return gFalse; } gfxcolor_t col={0,0,0,0}; @@ -1090,29 +1111,16 @@ void GFXOutputDev::endType3Char(GfxState *state) msg(" endType3Char"); } -void GFXOutputDev::startFrame(int width, int height) -{ - if(outer_clip_box) { - device->endclip(device); - outer_clip_box = 0; - } - - device->startpage(device, width, height); - this->width = width; - this->height = height; -} - void GFXOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2) { this->currentpage = pageNum; double x1,y1,x2,y2; int rot = doc->getPageRotate(1); - gfxcolor_t white; + gfxcolor_t white = {255,255,255,255}; + gfxcolor_t black = {255,0,0,0}; laststate = state; gfxline_t clippath[5]; - white.r = white.g = white.b = white.a = 255; - /* state->transform(state->getX1(),state->getY1(),&x1,&y1); state->transform(state->getX2(),state->getY2(),&x2,&y2); Use CropBox, not MediaBox, as page size @@ -1160,7 +1168,7 @@ void GFXOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, doubl void GFXOutputDev::processLink(Link *link, Catalog *catalog) { - double x1, y1, x2, y2, w; + double x1, y1, x2, y2; gfxline_t points[5]; int x, y; @@ -1198,6 +1206,10 @@ void GFXOutputDev::processLink(Link *link, Catalog *catalog) points[1].x, points[1].y, points[2].x, points[2].y, points[3].x, points[3].y); + + if(getLogLevel() >= LOGLEVEL_TRACE) { + dump_outline(points); + } LinkAction*action=link->getAction(); char buf[128]; @@ -1434,13 +1446,8 @@ void GFXOutputDev::updateStrokeColor(GfxState *state) state->getStrokeRGB(&rgb); } -void GFXOutputDev::setXRef(PDFDoc*doc, XRef *xref) -{ - this->doc = doc; - this->xref = xref; -} -gfxfont_t* createGfxFont(GfxFont*xpdffont, FontInfo*src) +gfxfont_t* createGfxFont(GfxFont*xpdffont, FontInfo*src, double config_fontquality) { gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t)); memset(font, 0, sizeof(gfxfont_t)); @@ -1449,7 +1456,8 @@ gfxfont_t* createGfxFont(GfxFont*xpdffont, FontInfo*src) memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs); font->id = strdup(getFontID(xpdffont)); int t; - double quality = (INTERNAL_FONT_SIZE * 0.05) / src->max_size; + + double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size; double scale = 1; //printf("%d glyphs\n", font->num_glyphs); font->num_glyphs = 0; @@ -1472,7 +1480,7 @@ gfxfont_t* createGfxFont(GfxFont*xpdffont, FontInfo*src) Guchar f; double x, y; path->getPoint(s, &x, &y, &f); - if(x > xmax) + if(!s || x > xmax) xmax = x; if(f&splashPathFirst) { drawer.moveTo(&drawer, x*scale, y*scale); @@ -1531,14 +1539,19 @@ void GFXOutputDev::updateFont(GfxState *state) } this->current_fontinfo = this->info->getFont(id); + if(!this->current_fontinfo) { + msg(" Internal Error: no fontinfo for font %s\n", id); + return; + } if(!this->current_fontinfo->seen) { dumpFontInfo("", gfxFont); } gfxfont_t*font = gfxfontlist_findfont(this->gfxfontlist,id); if(!font) { - font = createGfxFont(gfxFont, current_fontinfo); - gfxfontlist_addfont(this->gfxfontlist, font); + font = createGfxFont(gfxFont, current_fontinfo, this->config_fontquality); + font->id = strdup(id); + this->gfxfontlist = gfxfontlist_addfont(this->gfxfontlist, font); device->addfont(device, font); } current_gfxfont = font; @@ -1557,7 +1570,6 @@ unsigned char* antialize(unsigned char*data, int width, int height, int newwidth unsigned char*newdata; int x,y; newdata= (unsigned char*)malloc(newwidth*newheight); - int t; double fx = (double)(width)/newwidth; double fy = (double)(height)/newheight; double px = 0; @@ -1630,9 +1642,6 @@ static void drawimage(gfxdevice_t*dev, gfxcolor_t* data, int sizex,int sizey, p5.y = (int)(p5.y*20)/20.0; } - float m00,m10,tx; - float m01,m11,ty; - gfxmatrix_t m; m.m00 = (p4.x-p1.x)/sizex; m.m10 = (p2.x-p1.x)/sizey; m.m01 = (p4.y-p1.y)/sizex; m.m11 = (p2.y-p1.y)/sizey; @@ -1723,7 +1732,7 @@ void GFXOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str, imgStr = new ImageStream(str, width, ncomps,bits); imgStr->reset(); - if(!width || !height || (height<=1 && width<=1)) + if(!width || !height || (height<=1 && width<=1 && maskWidth<=1 && maskHeight<=1)) { msg(" Ignoring %d by %d image", width, height); unsigned char buf[8]; @@ -1757,7 +1766,6 @@ void GFXOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str, } if(mask) { - int i,j; unsigned char buf[8]; int x,y; unsigned char*pic = new unsigned char[width*height]; @@ -1786,7 +1794,7 @@ void GFXOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str, /* the size of the drawn image is added to the identifier as the same image may require different bitmaps if displayed at different sizes (due to antialiasing): */ - int t,found = -1; + int found = -1; if(type3active) { unsigned char*pic2 = 0; numpalette = 16; @@ -1794,7 +1802,7 @@ void GFXOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str, pic2 = antialize(pic,width,height,realwidth,realheight,numpalette); if(!pic2) { - delete pic; + delete[] pic; delete imgStr; return; } @@ -1806,7 +1814,7 @@ void GFXOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str, /* make a black/white palette */ - float r = 255/(numpalette-1); + float r = 255./(float)(numpalette-1); int t; for(t=0;tgetPixel(pixBuf); pic[width*y+x] = pal[pixBuf[0]]; - if(maskbitmap) { - pic[width*y+x].a = maskbitmap[(y*maskHeight/height)*maskWidth+(x*maskWidth/width)]; - } } } + if(maskbitmap) { + if(maskWidth < width && maskHeight < height) { + for(y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + pic[width*y+x].a = maskbitmap[(y*maskHeight/height)*maskWidth+(x*maskWidth/width)]; + } + } + } else { + msg(" resampling %dx%d to mask size (%dx%d)", width, height, maskWidth, maskHeight); + gfxcolor_t*newpic=new gfxcolor_t[maskWidth*maskHeight]; + double dx = width / maskWidth; + double dy = height / maskHeight; + double yy = 0; + for(y = 0; y < maskHeight; y++) { + double xx = 0; + for (x = 0; x < maskWidth; x++) { + newpic[maskWidth*y+x] = pic[int(yy)*width+int(xx)]; + newpic[maskWidth*y+x].a = maskbitmap[maskWidth*y+x]; + xx += dx; + } + yy += dy; + } + delete[] pic; + pic = newpic; + width = maskWidth; + height = maskHeight; + } + } drawimagelossless(device, pic, width, height, x1,y1,x2,y2,x3,y3,x4,y4); - delete pic; + delete[] pic; delete imgStr; if(maskbitmap) free(maskbitmap); return; @@ -2017,15 +2050,25 @@ static const char* dirseparator() void addGlobalFont(const char*filename) { - fontfile_t f; - memset(&f, 0, sizeof(fontfile_t)); - f.filename = filename; - if(fontnum < sizeof(fonts)/sizeof(fonts[0])) { - msg(" Adding font \"%s\".", filename); - msg(" External fonts are not supported with this version. Ignoring font %s", filename); - fonts[fontnum++] = f; + fontfile_t* f = (fontfile_t*)malloc(sizeof(fontfile_t)); + memset(f, 0, sizeof(fontfile_t)); + f->filename = filename; + int len = strlen(filename); + char*r1 = strrchr(filename, '/'); + char*r2 = strrchr(filename, '\\'); + if(r2>r1) + r1 = r2; + if(r1) { + len = strlen(r1+1); + } + f->len = len; + + msg(" Adding font \"%s\".", filename); + if(global_fonts_next) { + global_fonts_next->next = f; + global_fonts_next = global_fonts_next->next; } else { - msg(" Too many external fonts. Not adding font file \"%s\".", filename); + global_fonts_next = global_fonts = f; } } @@ -2033,7 +2076,6 @@ void addGlobalLanguageDir(const char*dir) { msg(" Adding %s to language pack directories", dir); - int l; FILE*fi = 0; char* config_file = (char*)malloc(strlen(dir) + 1 + sizeof("add-to-xpdfrc") + 1); strcpy(config_file, dir); @@ -2051,8 +2093,6 @@ void addGlobalLanguageDir(const char*dir) void addGlobalFontDir(const char*dirname) { - msg(" External fonts are not supported with this version. Ignoring directory %s", dirname); - return; #ifdef HAVE_DIRENT_H msg(" Adding %s to font directories", dirname); lastfontdir = strdup(dirname); @@ -2079,8 +2119,7 @@ void addGlobalFontDir(const char*dirname) type=3; if(!strncasecmp(&name[l-4], ".ttf", 4)) type=2; - if(type) - { + if(type) { char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2); strcpy(fontname, dirname); strcat(fontname, dirseparator()); @@ -2090,7 +2129,7 @@ void addGlobalFontDir(const char*dirname) } closedir(dir); #else - msg(" No dirent.h- unable to add font dir %s", dir); + msg(" No dirent.h- unable to add font dir %s", dirname); #endif } @@ -2204,16 +2243,15 @@ void GFXOutputDev::beginTransparencyGroup(GfxState *state, double *bbox, GBool forSoftMask) { const char*colormodename = ""; - BBox rect = mkBBox(state, bbox, this->width, this->height); if(blendingColorSpace) { colormodename = GfxColorSpace::getColorSpaceModeName(blendingColorSpace->getMode()); } dbg("beginTransparencyGroup %.1f/%.1f/%.1f/%.1f %s isolated=%d knockout=%d forsoftmask=%d", bbox[0],bbox[1],bbox[2],bbox[3], colormodename, isolated, knockout, forSoftMask); - dbg("using clipping rect %f/%f/%f/%f\n", rect.posx,rect.posy,rect.width,rect.height); msg(" beginTransparencyGroup %.1f/%.1f/%.1f/%.1f %s isolated=%d knockout=%d forsoftmask=%d", bbox[0],bbox[1],bbox[2],bbox[3], colormodename, isolated, knockout, forSoftMask); - states[statepos].createsoftmask |= forSoftMask; + //states[statepos].createsoftmask |= forSoftMask; + states[statepos].createsoftmask = forSoftMask; states[statepos].transparencygroup = !forSoftMask; states[statepos].isolated = isolated; @@ -2238,10 +2276,11 @@ void GFXOutputDev::endTransparencyGroup(GfxState *state) this->device = states[statepos].olddevice; + gfxresult_t*recording = r->finish(r); if(states[statepos].createsoftmask) { - states[statepos-1].softmaskrecording = r->finish(r); + states[statepos-1].softmaskrecording = recording; } else { - states[statepos-1].grouprecording = r->finish(r); + states[statepos-1].grouprecording = recording; } states[statepos].createsoftmask = 0;