From 245df826983814f775091f86f93a183419acab9f Mon Sep 17 00:00:00 2001 From: Ryan Fox Date: Mon, 24 Jun 2013 15:54:21 -0400 Subject: [PATCH 3/4] Add support for stroke and fill colours in pdftohtml, as well as transparency in the colour. --- utils/HtmlFonts.cc | 73 ++++++++++++++++++++++-------------------------- utils/HtmlFonts.h | 25 ++++++++++------- utils/HtmlOutputDev.cc | 9 ++++-- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/utils/HtmlFonts.cc b/utils/HtmlFonts.cc index 4b551b2..472c9ef 100644 --- a/utils/HtmlFonts.cc +++ b/utils/HtmlFonts.cc @@ -69,47 +69,27 @@ extern GBool fontFullName; GooString* HtmlFont::DefaultFont=new GooString("Times"); // Arial,Helvetica,sans-serif -HtmlFontColor::HtmlFontColor(GfxRGB rgb){ +HtmlFontColor::HtmlFontColor(GfxRGB rgb, double transparency){ + trans = transparency; r=static_cast(rgb.r/65535.0*255.0); g=static_cast(rgb.g/65535.0*255.0); b=static_cast(rgb.b/65535.0*255.0); - if (!(Ok(r)&&Ok(b)&&Ok(g))) { - if (!globalParams->getErrQuiet()) fprintf(stderr, "Error : Bad color (%d,%d,%d) reset to (0,0,0)\n", r, g, b); - r=0;g=0;b=0; + if (!(Ok(r)&&Ok(b)&&Ok(g)&&Ok(trans))) { + if (!globalParams->getErrQuiet()) fprintf(stderr, "Error : Bad color (%d,%d,%d,%g) reset to (0,0,0,1.0)\n", r, g, b, trans); + r=0;g=0;b=0;trans=1.0; } } -GooString *HtmlFontColor::convtoX(unsigned int xcol) const{ - GooString *xret=new GooString(); - char tmp; - unsigned int k; - k = (xcol/16); - if (k<10) tmp=(char) ('0'+k); else tmp=(char)('a'+k-10); - xret->append(tmp); - k = (xcol%16); - if (k<10) tmp=(char) ('0'+k); else tmp=(char)('a'+k-10); - xret->append(tmp); - return xret; -} - GooString *HtmlFontColor::toString() const{ - GooString *tmp=new GooString("#"); - GooString *tmpr=convtoX(r); - GooString *tmpg=convtoX(g); - GooString *tmpb=convtoX(b); - tmp->append(tmpr); - tmp->append(tmpg); - tmp->append(tmpb); - delete tmpr; - delete tmpg; - delete tmpb; - return tmp; + return GooString::format("rgba({0:ud}, {1:ud}, {2:ud}, {3:.2f})", r, g, b, trans); } -HtmlFont::HtmlFont(GfxFont *font, int _size, GfxRGB rgb){ +HtmlFont::HtmlFont(GfxFont *font, int _size, GfxRGB stroke, double strokeTrans, GfxRGB fill, double fillTrans, int _render){ //if (col) color=HtmlFontColor(col); //else color=HtmlFontColor(); - color=HtmlFontColor(rgb); + strokeColor=HtmlFontColor(stroke, strokeTrans); + fillColor=HtmlFontColor(fill, fillTrans); + render = _render; GooString* ftname=font->getName(); if (!ftname) ftname = getDefaultFont(); @@ -164,7 +144,9 @@ HtmlFont::HtmlFont(const HtmlFont& x){ italic=x.italic; bold=x.bold; pos=x.pos; - color=x.color; + fillColor=x.fillColor; + render=x.render; + strokeColor=x.strokeColor; if (x.FontName) FontName=new GooString(x.FontName); rotOrSkewed = x.rotOrSkewed; memcpy(rotSkewMat, x.rotSkewMat, sizeof(rotSkewMat)); @@ -182,7 +164,9 @@ HtmlFont& HtmlFont::operator=(const HtmlFont& x){ italic=x.italic; bold=x.bold; pos=x.pos; - color=x.color; + fillColor=x.fillColor; + render=x.render; + strokeColor=x.strokeColor; if (FontName) delete FontName; if (x.FontName) FontName=new GooString(x.FontName); return *this; @@ -203,7 +187,8 @@ GBool HtmlFont::isEqual(const HtmlFont& x) const{ return (size==x.size) && (lineSize==x.lineSize) && (pos==x.pos) && (bold==x.bold) && (italic==x.italic) && - (color.isEqual(x.getColor())) && isRotOrSkewed() == x.isRotOrSkewed() && + (strokeColor.isEqual(x.getStrokeColor())) && (fillColor.isEqual(x.getFillColor())) && + isRotOrSkewed() == x.isRotOrSkewed() && render == x.render && (!isRotOrSkewed() || rot_matrices_equal(getRotMat(), x.getRotMat())); } @@ -213,8 +198,8 @@ GBool HtmlFont::isEqual(const HtmlFont& x) const{ */ GBool HtmlFont::isEqualIgnoreBold(const HtmlFont& x) const{ return ((size==x.size) && - (!strcmp(fonts[pos].name, fonts[x.pos].name)) && - (color.isEqual(x.getColor()))); + (!strcmp(fonts[pos].name, fonts[x.pos].name)) && render == x.render && + (fillColor.isEqual(x.getFillColor())) && (strokeColor.isEqual(x.getStrokeColor()))); } GooString* HtmlFont::getFontName(){ @@ -320,7 +305,9 @@ GooString* HtmlFontAccu::CSStyle(int i, int j){ g+=i; HtmlFont font=*g; GooString *Size=GooString::fromInt(font.getSize()); - GooString *colorStr=font.getColor().toString(); + GooString *fillColorStr=font.getFillColor().toString(); + GooString *strokeColorStr=font.getStrokeColor().toString(); + int render = font.getRender(); GooString *fontName=(fontFullName ? font.getFullName() : font.getFontName()); GooString *lSize; @@ -347,7 +334,7 @@ GooString* HtmlFontAccu::CSStyle(int i, int j){ tmp->append("px;font-family:"); tmp->append(fontName); //font.getFontName()); tmp->append(";color:"); - tmp->append(colorStr); + tmp->append(fillColorStr); // if there is rotation or skew, include the matrix if (font.isRotOrSkewed()) { tmp->append(";-moz-transform: "); @@ -376,8 +363,14 @@ GooString* HtmlFontAccu::CSStyle(int i, int j){ tmp->append(Size); tmp->append("\" family=\""); tmp->append(fontName); //font.getFontName()); - tmp->append("\" color=\""); - tmp->append(colorStr); + if (!(render & 1)) { + tmp->append("\" fill-color=\""); + tmp->append(fillColorStr); + } + if ((render & 3) == 1 || (render & 3) == 2) { + tmp->append("\" stroke-color=\""); + tmp->append(strokeColorStr); + } if (font.isRotOrSkewed()) { tmp->append("\" transform=\""); tmp->append(&matrix_str); @@ -386,7 +379,7 @@ GooString* HtmlFontAccu::CSStyle(int i, int j){ } delete fontName; - delete colorStr; + delete fillColorStr; delete jStr; delete iStr; delete Size; diff --git a/utils/HtmlFonts.h b/utils/HtmlFonts.h index 7993c78..c150955 100644 --- a/utils/HtmlFonts.h +++ b/utils/HtmlFonts.h @@ -40,20 +40,21 @@ class HtmlFontColor{ unsigned int r; unsigned int g; unsigned int b; - GBool Ok(unsigned int xcol){ return xcol<=255;} - GooString *convtoX(unsigned int xcol) const; + double trans; + GBool Ok(unsigned int xcol){ return xcol<=255; } + GBool Ok(double xcol){ return xcol>=0 && xcol<=1.0; } public: - HtmlFontColor():r(0),g(0),b(0){} - HtmlFontColor(GfxRGB rgb); - HtmlFontColor(const HtmlFontColor& x){r=x.r;g=x.g;b=x.b;} + HtmlFontColor():r(0),g(0),b(0),trans(1.0){} + HtmlFontColor(GfxRGB rgb, double transparency); + HtmlFontColor(const HtmlFontColor& x){r=x.r;g=x.g;b=x.b;trans=x.trans;} HtmlFontColor& operator=(const HtmlFontColor &x){ - r=x.r;g=x.g;b=x.b; + r=x.r;g=x.g;b=x.b;trans=x.trans; return *this; } ~HtmlFontColor(){}; GooString* toString() const; GBool isEqual(const HtmlFontColor& col) const{ - return ((r==col.r)&&(g==col.g)&&(b==col.b)); + return ((r==col.r)&&(g==col.g)&&(b==col.b)&&(trans==col.trans)); } } ; @@ -68,21 +69,25 @@ class HtmlFont{ int pos; // position of the font name in the fonts array static GooString *DefaultFont; GooString *FontName; - HtmlFontColor color; + HtmlFontColor strokeColor; + HtmlFontColor fillColor; + int render; double rotSkewMat[4]; // only four values needed for rotation and skew public: HtmlFont(){FontName=NULL; rotOrSkewed = gFalse;} - HtmlFont(GfxFont *font,int _size, GfxRGB rgb); + HtmlFont(GfxFont *font, int _size, GfxRGB stroke, double strokeTrans, GfxRGB fill, double fillTrans, int _render); HtmlFont(const HtmlFont& x); HtmlFont& operator=(const HtmlFont& x); - HtmlFontColor getColor() const {return color;} + HtmlFontColor getFillColor() const {return fillColor;} + HtmlFontColor getStrokeColor() const {return strokeColor;} ~HtmlFont(); static void clear(); GooString* getFullName(); GBool isItalic() const {return italic;} GBool isBold() const {return bold;} GBool isRotOrSkewed() const { return rotOrSkewed; } + int getRender() const {return render;} unsigned int getSize() const {return size;} int getLineSize() const {return lineSize;} void setLineSize(int _lineSize) { lineSize = _lineSize; } diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc index 23d8b6e..d644cdf 100644 --- a/utils/HtmlOutputDev.cc +++ b/utils/HtmlOutputDev.cc @@ -180,9 +180,12 @@ HtmlString::HtmlString(GfxState *state, double fontSize, HtmlFontAccu* _fonts) : } yMin = y - ascent * fontSize; yMax = y - descent * fontSize; - GfxRGB rgb; - state->getFillRGB(&rgb); - HtmlFont hfont=HtmlFont(font, static_cast(fontSize-1), rgb); + GfxRGB fill, stroke; + state->getFillRGB(&fill); + state->getStrokeRGB(&stroke); + double fillTrans = state->getFillOpacity(); + double strokeTrans = state->getStrokeOpacity(); + HtmlFont hfont=HtmlFont(font, fontSize, stroke, strokeTrans, fill, fillTrans, state->getRender()); if (isMatRotOrSkew(state->getTextMat())) { double normalizedMatrix[4]; memcpy(normalizedMatrix, state->getTextMat(), sizeof(normalizedMatrix)); -- 1.7.9.5