1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 13:00:08 +00:00

do not embed empty bg image

This commit is contained in:
Lu Wang 2013-09-16 16:30:00 +08:00
parent b32892b01b
commit 7534466d75
4 changed files with 75 additions and 28 deletions

View File

@ -4,16 +4,32 @@
* Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com> * Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
*/ */
#include <fstream>
#include <PDFDoc.h> #include <PDFDoc.h>
#include "Base64Stream.h"
#include "SplashBackgroundRenderer.h" #include "SplashBackgroundRenderer.h"
namespace pdf2htmlEX { namespace pdf2htmlEX {
using std::string; using std::string;
using std::ifstream;
const SplashColor SplashBackgroundRenderer::white = {255,255,255}; 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, void SplashBackgroundRenderer::drawChar(GfxState *state, double x, double y,
double dx, double dy, double dx, double dy,
double originX, double originY, double originX, double originY,
@ -40,17 +56,50 @@ static GBool annot_cb(Annot *, void *) {
return false; 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, doc->displayPage(this, pageno, param.h_dpi, param.v_dpi,
0, 0,
(!(param.use_cropbox)), (!(param.use_cropbox)),
false, false, false, false,
nullptr, nullptr, &annot_cb, nullptr); nullptr, nullptr, &annot_cb, nullptr);
}
getBitmap()->writeImgFile(splashFormatPng, void SplashBackgroundRenderer::embed_image(int pageno)
(char*)filename.c_str(), {
param.h_dpi, param.v_dpi); 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 << "<img class=\"" << CSS::BACKGROUND_IMAGE_CN
<< "\" alt=\"\" src=\"";
if(param.embed_image)
{
auto path = html_renderer->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 } // namespace pdf2htmlEX

View File

@ -27,12 +27,14 @@ public:
static const SplashColor white; static const SplashColor white;
SplashBackgroundRenderer(HTMLRenderer * html_renderer, const Param & param) 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) , html_renderer(html_renderer)
, param(param) , param(param)
{ } { }
virtual ~SplashBackgroundRenderer() { } virtual ~SplashBackgroundRenderer() { }
virtual void startPage(int pageNum, GfxState *state, XRef *xrefA);
virtual void drawChar(GfxState *state, double x, double y, virtual void drawChar(GfxState *state, double x, double y,
double dx, double dy, double dx, double dy,
@ -49,7 +51,8 @@ public:
SplashOutputDev::fill(state); 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: protected:
HTMLRenderer * html_renderer; HTMLRenderer * html_renderer;

View File

@ -28,6 +28,8 @@
#include "StateManager.h" #include "StateManager.h"
#include "HTMLTextPage.h" #include "HTMLTextPage.h"
#include "BackgroundRenderer/BackgroundRenderer_forward.h"
#include "util/const.h" #include "util/const.h"
#include "util/misc.h" #include "util/misc.h"
@ -307,11 +309,18 @@ protected:
int * width_list; int * width_list;
Preprocessor preprocessor; Preprocessor preprocessor;
// manage temporary files
TmpFiles tmp_files; TmpFiles tmp_files;
// for string formatting // for string formatting
StringFormatter str_fmt; StringFormatter str_fmt;
// render background image
friend BackgroundRenderer;
BackgroundRenderer * bg_renderer;
struct { struct {
std::ofstream fs; std::ofstream fs;
std::string path; std::string path;

View File

@ -17,9 +17,10 @@
#include "pdf2htmlEX-config.h" #include "pdf2htmlEX-config.h"
#include "HTMLRenderer.h" #include "HTMLRenderer.h"
#include "HTMLTextLine.h" #include "HTMLTextLine.h"
#include "BackgroundRenderer/BackgroundRenderer.h"
#include "Base64Stream.h" #include "Base64Stream.h"
#include "BackgroundRenderer/BackgroundRenderer.h"
#include "util/namespace.h" #include "util/namespace.h"
#include "util/ffw.h" #include "util/ffw.h"
#include "util/math.h" #include "util/math.h"
@ -96,7 +97,7 @@ void HTMLRenderer::process(PDFDoc *doc)
/////////////////// ///////////////////
// Process pages // Process pages
BackgroundRenderer * bg_renderer = nullptr; bg_renderer = nullptr;
if(param.process_nontext) if(param.process_nontext)
{ {
bg_renderer = new BackgroundRenderer(this, param); bg_renderer = new BackgroundRenderer(this, param);
@ -122,11 +123,7 @@ void HTMLRenderer::process(PDFDoc *doc)
if(param.process_nontext) if(param.process_nontext)
{ {
auto fn = str_fmt("%s/bg%x.png", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), i); bg_renderer->render_page(doc, i);
if(param.embed_image)
tmp_files.add((char*)fn);
bg_renderer->render_page(doc, i, (char*)fn);
} }
doc->displayPage(this, i, doc->displayPage(this, i,
@ -155,7 +152,10 @@ void HTMLRenderer::process(PDFDoc *doc)
post_process(); post_process();
if(bg_renderer) if(bg_renderer)
{
delete bg_renderer; delete bg_renderer;
bg_renderer = nullptr;
}
cerr << endl; cerr << endl;
} }
@ -215,21 +215,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state, XRef * xref)
if(param.process_nontext) if(param.process_nontext)
{ {
(*f_curpage) << "<img class=\"" << CSS::BACKGROUND_IMAGE_CN bg_renderer->embed_image(pageNum);
<< "\" alt=\"\" src=\"";
if(param.embed_image)
{
auto path = str_fmt("%s/bg%x.png", param.tmp_dir.c_str(), pageNum);
ifstream fin((char*)path, ifstream::binary);
if(!fin)
throw string("Cannot read background image ") + (char*)path;
(*f_curpage) << "data:image/png;base64," << Base64Stream(fin);
}
else
{
(*f_curpage) << (char*)str_fmt("bg%x.png", pageNum);
}
(*f_curpage) << "\"/>";
} }
reset_state(); reset_state();