From 5f5747738fbdc3ce250247fdcf7ef3936079bbc3 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Wed, 3 Oct 2012 22:46:27 +0800 Subject: [PATCH] working on integrating splash --- TODO | 2 ++ src/HTMLRenderer/draw.cc | 27 ++++++++++++++++++++++---- src/HTMLRenderer/general.cc | 2 +- src/include/HTMLRenderer.h | 24 ++++++++++++++++++----- src/include/SplashBackgroundRenderer.h | 15 +++++++++++++- 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 4246d7b..2a2c408 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,5 @@ +don't dump image when there is nothing + Integrate splash/cairo native support for image native support for draw diff --git a/src/HTMLRenderer/draw.cc b/src/HTMLRenderer/draw.cc index 8ed7602..a64dc2b 100644 --- a/src/HTMLRenderer/draw.cc +++ b/src/HTMLRenderer/draw.cc @@ -236,11 +236,26 @@ GBool HTMLRenderer::axialShadedFill(GfxState *state, GfxAxialShading *shading, d //TODO track state //TODO connection style -void HTMLRenderer::css_draw(GfxState *state, bool fill) +bool HTMLRenderer::css_do_path(GfxState *state, bool fill, bool test_only) { - if(!(param->css_draw)) return; + if(!(param->css_draw)) return false; GfxPath * path = state->getPath(); + /* + * capacity check + */ + for(int i = 0; i < path->getNumSubpaths(); ++i) + { + GfxSubpath * subpath = path->getSubpath(i); + if(!(is_horizontal_line(subpath) + || is_vertical_line(subpath) + || is_rectangle(subpath))) + return false; + } + + if(test_only) + return true; + for(int i = 0; i < path->getNumSubpaths(); ++i) { GfxSubpath * subpath = path->getSubpath(i); @@ -279,12 +294,11 @@ void HTMLRenderer::css_draw(GfxState *state, bool fill) } else if(is_rectangle(subpath)) { - close_text_line(); double x1 = subpath->getX(0); double x2 = subpath->getX(2); double y1 = subpath->getY(0); double y2 = subpath->getY(2); - + if(x1 > x2) swap(x1, x2); if(y1 > y2) swap(y1, y2); @@ -318,7 +332,12 @@ void HTMLRenderer::css_draw(GfxState *state, bool fill) lw, lw_count, ps, pf); } + else + { + assert(false); + } } + return true; } void HTMLRenderer::css_draw_rectangle(double x, double y, double w, double h, const double * tm, diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index eedbc70..b8c214f 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -111,7 +111,7 @@ void HTMLRenderer::process(PDFDoc *doc) BackgroundRenderer * bg_renderer = nullptr; if(param->process_nontext) { - bg_renderer = new BackgroundRenderer(param); + bg_renderer = new BackgroundRenderer(this, param); bg_renderer->startDoc(doc); } diff --git a/src/include/HTMLRenderer.h b/src/include/HTMLRenderer.h index ec7692b..989b17d 100644 --- a/src/include/HTMLRenderer.h +++ b/src/include/HTMLRenderer.h @@ -82,7 +82,7 @@ class HTMLRenderer : public OutputDev // Does this device use functionShadedFill(), axialShadedFill(), and // radialShadedFill()? If this returns false, these shaded fills // will be reduced to a series of other drawing operations. - virtual GBool useShadedFills(int type) { return type == 2; } + virtual GBool useShadedFills(int type) { return (type == 2) ? gTrue: gFalse; } // Does this device use beginType3Char/endType3Char? Otherwise, @@ -90,7 +90,7 @@ class HTMLRenderer : public OutputDev virtual GBool interpretType3Chars() { return gFalse; } // Does this device need non-text content? - virtual GBool needNonText() { return gTrue; } + virtual GBool needNonText() { return (param->process_nontext) ? gTrue: gFalse; } virtual void setDefaultCTM(double *ctm); @@ -104,6 +104,13 @@ class HTMLRenderer : public OutputDev * To optmize false alarms * We just mark as changed, and recheck if they have been changed when we are about to output a new string */ + + /* + * Ugly implementation of save/restore + */ + virtual void saveState(GfxState * state) {updateAll(state);} + virtual void restoreState(GfxState * state) {updateAll(state);} + virtual void updateAll(GfxState * state); virtual void updateRise(GfxState * state); @@ -129,12 +136,16 @@ class HTMLRenderer : public OutputDev virtual void drawImage(GfxState * state, Object * ref, Stream * str, int width, int height, GfxImageColorMap * colorMap, GBool interpolate, int *maskColors, GBool inlineImg); - virtual void stroke(GfxState *state) { css_draw(state, false); } - virtual void fill(GfxState *state) { css_draw(state, true); } + virtual void stroke(GfxState *state) { css_do_path(state, false); } + virtual void fill(GfxState *state) { css_do_path(state, true); } virtual GBool axialShadedFill(GfxState *state, GfxAxialShading *shading, double tMin, double tMax); virtual void processLink(AnnotLink * al); + /* capacity test */ + bool can_stroke(GfxState *state) { return css_do_path(state, false, true); } + bool can_fill(GfxState *state) { return css_do_path(state, true, true); } + protected: //////////////////////////////////////////////////// // misc @@ -208,7 +219,10 @@ class HTMLRenderer : public OutputDev //////////////////////////////////////////////////// // CSS drawing //////////////////////////////////////////////////// - void css_draw(GfxState *state, bool fill); + /* + * test_only is for capacity check + */ + bool css_do_path(GfxState *state, bool fill, bool test_only = false); /* * coordinates are to transformed by state->getCTM() * (x,y) should be the bottom-left corner INCLUDING border diff --git a/src/include/SplashBackgroundRenderer.h b/src/include/SplashBackgroundRenderer.h index e56bf1d..8ba0cd5 100644 --- a/src/include/SplashBackgroundRenderer.h +++ b/src/include/SplashBackgroundRenderer.h @@ -15,6 +15,7 @@ #include #include +#include "HTMLRenderer.h" #include "Param.h" namespace pdf2htmlEX { @@ -25,8 +26,9 @@ class SplashBackgroundRenderer : public SplashOutputDev public: static const SplashColor white; - SplashBackgroundRenderer(const Param * param) + SplashBackgroundRenderer(HTMLRenderer * html_renderer, const Param * param) : SplashOutputDev(splashModeRGB8, 4, gFalse, (SplashColorPtr)&white, gTrue, gTrue) + , html_renderer(html_renderer) , param(param) { } @@ -37,9 +39,20 @@ public: double originX, double originY, CharCode code, int nBytes, Unicode *u, int uLen); + virtual void stroke(GfxState *state) { + if(!html_renderer->can_stroke(state)) + SplashOutputDev::stroke(state); + } + + virtual void fill(GfxState *state) { + if(!html_renderer->can_fill(state)) + SplashOutputDev::fill(state); + } + void render_page(PDFDoc * doc, int pageno, const std::string & filename); protected: + HTMLRenderer * html_renderer; const Param * param; };