From 7534466d75526c07d98675bc0a0c64d0ff24b2d0 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 16 Sep 2013 16:30:00 +0800 Subject: [PATCH] do not embed empty bg image --- .../SplashBackgroundRenderer.cc | 57 +++++++++++++++++-- .../SplashBackgroundRenderer.h | 7 ++- src/HTMLRenderer/HTMLRenderer.h | 9 +++ src/HTMLRenderer/general.cc | 30 +++------- 4 files changed, 75 insertions(+), 28 deletions(-) diff --git a/src/BackgroundRenderer/SplashBackgroundRenderer.cc b/src/BackgroundRenderer/SplashBackgroundRenderer.cc index 357917a..f57d821 100644 --- a/src/BackgroundRenderer/SplashBackgroundRenderer.cc +++ b/src/BackgroundRenderer/SplashBackgroundRenderer.cc @@ -4,16 +4,32 @@ * Copyright (C) 2012 Lu Wang */ +#include + #include +#include "Base64Stream.h" + #include "SplashBackgroundRenderer.h" namespace pdf2htmlEX { using std::string; +using std::ifstream; const SplashColor SplashBackgroundRenderer::white = {255,255,255}; +/* + * SplashOutputDev::startPage would paint the whole page with the background color + * And thus have modified region set to the whole page area + * We do not want that. + */ +void SplashBackgroundRenderer::startPage(int pageNum, GfxState *state, XRef *xrefA) +{ + SplashOutputDev::startPage(pageNum, state, xrefA); + clearModRegion(); +} + void SplashBackgroundRenderer::drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, @@ -40,17 +56,50 @@ static GBool annot_cb(Annot *, void *) { return false; }; -void SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno, const string & filename) +void SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno) { doc->displayPage(this, pageno, param.h_dpi, param.v_dpi, 0, (!(param.use_cropbox)), false, false, nullptr, nullptr, &annot_cb, nullptr); +} - getBitmap()->writeImgFile(splashFormatPng, - (char*)filename.c_str(), - param.h_dpi, param.v_dpi); +void SplashBackgroundRenderer::embed_image(int pageno) +{ + int xmin, xmax, ymin, ymax; + getModRegion(&xmin, &ymin, &xmax, &ymax); + + // dump the background image only when it is not empty + if((xmin <= xmax) && (ymin <= ymax)) + { + { + auto fn = html_renderer->str_fmt("%s/bg%x.png", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno); + if(param.embed_image) + html_renderer->tmp_files.add((char*)fn); + + getBitmap()->writeImgFile(splashFormatPng, + (char*)fn, + param.h_dpi, param.v_dpi); + } + + auto & f_page = *(html_renderer->f_curpage); + f_page << "\"\"str_fmt("%s/bg%x.png", param.tmp_dir.c_str(), pageno); + ifstream fin((char*)path, ifstream::binary); + if(!fin) + throw string("Cannot read background image ") + (char*)path; + f_page << "data:image/png;base64," << Base64Stream(fin); + } + else + { + f_page << (char*)html_renderer->str_fmt("bg%x.png", pageno); + } + f_page << "\"/>"; + } } } // namespace pdf2htmlEX diff --git a/src/BackgroundRenderer/SplashBackgroundRenderer.h b/src/BackgroundRenderer/SplashBackgroundRenderer.h index f82e6dc..e44c58a 100644 --- a/src/BackgroundRenderer/SplashBackgroundRenderer.h +++ b/src/BackgroundRenderer/SplashBackgroundRenderer.h @@ -27,12 +27,14 @@ public: static const SplashColor white; SplashBackgroundRenderer(HTMLRenderer * html_renderer, const Param & param) - : SplashOutputDev(splashModeRGB8, 4, gFalse, (SplashColorPtr)&white, gTrue, gTrue) + : SplashOutputDev(splashModeRGB8, 4, gFalse, (SplashColorPtr)(&white), gTrue, gTrue) , html_renderer(html_renderer) , param(param) { } virtual ~SplashBackgroundRenderer() { } + + virtual void startPage(int pageNum, GfxState *state, XRef *xrefA); virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, @@ -49,7 +51,8 @@ public: SplashOutputDev::fill(state); } - void render_page(PDFDoc * doc, int pageno, const std::string & filename); + void render_page(PDFDoc * doc, int pageno); + void embed_image(int pageno); protected: HTMLRenderer * html_renderer; diff --git a/src/HTMLRenderer/HTMLRenderer.h b/src/HTMLRenderer/HTMLRenderer.h index 4d07f7d..e8e6b37 100644 --- a/src/HTMLRenderer/HTMLRenderer.h +++ b/src/HTMLRenderer/HTMLRenderer.h @@ -28,6 +28,8 @@ #include "StateManager.h" #include "HTMLTextPage.h" +#include "BackgroundRenderer/BackgroundRenderer_forward.h" + #include "util/const.h" #include "util/misc.h" @@ -307,11 +309,18 @@ protected: int * width_list; Preprocessor preprocessor; + + // manage temporary files TmpFiles tmp_files; // for string formatting StringFormatter str_fmt; + // render background image + friend BackgroundRenderer; + BackgroundRenderer * bg_renderer; + + struct { std::ofstream fs; std::string path; diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 7918abe..6608176 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -17,9 +17,10 @@ #include "pdf2htmlEX-config.h" #include "HTMLRenderer.h" #include "HTMLTextLine.h" -#include "BackgroundRenderer/BackgroundRenderer.h" #include "Base64Stream.h" +#include "BackgroundRenderer/BackgroundRenderer.h" + #include "util/namespace.h" #include "util/ffw.h" #include "util/math.h" @@ -96,7 +97,7 @@ void HTMLRenderer::process(PDFDoc *doc) /////////////////// // Process pages - BackgroundRenderer * bg_renderer = nullptr; + bg_renderer = nullptr; if(param.process_nontext) { bg_renderer = new BackgroundRenderer(this, param); @@ -122,11 +123,7 @@ void HTMLRenderer::process(PDFDoc *doc) if(param.process_nontext) { - auto fn = str_fmt("%s/bg%x.png", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), i); - if(param.embed_image) - tmp_files.add((char*)fn); - - bg_renderer->render_page(doc, i, (char*)fn); + bg_renderer->render_page(doc, i); } doc->displayPage(this, i, @@ -155,7 +152,10 @@ void HTMLRenderer::process(PDFDoc *doc) post_process(); if(bg_renderer) + { delete bg_renderer; + bg_renderer = nullptr; + } cerr << endl; } @@ -215,21 +215,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state, XRef * xref) if(param.process_nontext) { - (*f_curpage) << "\"\""; + bg_renderer->embed_image(pageNum); } reset_state();