From fcf5c2755ba9b48363a268248ef7baa15f9a4ecf Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 6 Aug 2012 14:21:37 +0800 Subject: [PATCH] add background image --- CMakeLists.txt | 2 +- src/BackgroundRenderer.cc | 22 ++++++++++++++++++++++ src/BackgroundRenderer.h | 35 +++++++++++++++++++++++++++++++++++ src/HTMLRenderer.cc | 38 +++++++++++++++++++++++++++++++------- 4 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 src/BackgroundRenderer.cc create mode 100644 src/BackgroundRenderer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f994109..8a64c7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb") -add_executable(pdftohtmlEX src/pdftohtmlEX.cc src/HTMLRenderer.cc src/HTMLRenderer.h) +add_executable(pdftohtmlEX src/pdftohtmlEX.cc src/HTMLRenderer.cc src/HTMLRenderer.h src/BackgroundRenderer.cc src/BackgroundRenderer.h) target_link_libraries(pdftohtmlEX poppler boost_program_options) diff --git a/src/BackgroundRenderer.cc b/src/BackgroundRenderer.cc new file mode 100644 index 0000000..2913947 --- /dev/null +++ b/src/BackgroundRenderer.cc @@ -0,0 +1,22 @@ +/* + * BackgroundRenderer.cc + * + * Copyright (C) 2012 by Lu Wang coolwanglugmail.com + */ + +#include "GfxFont.h" + +#include "BackgroundRenderer.h" + +void BackgroundRenderer::drawChar(GfxState *state, double x, double y, + double dx, double dy, + double originX, double originY, + CharCode code, int nBytes, Unicode *u, int uLen) +{ + auto font = state->getFont(); + if((font->getType() == fontType3) || (font->getWMode())) + { + SplashOutputDev::drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen); + } +} + diff --git a/src/BackgroundRenderer.h b/src/BackgroundRenderer.h new file mode 100644 index 0000000..b207c54 --- /dev/null +++ b/src/BackgroundRenderer.h @@ -0,0 +1,35 @@ +/* + * Background renderer + * Render all those things not supported as Image + * + * by WangLu + * 2012.08.06 + */ + + +#ifndef BACKGROUND_RENDERER_H__ +#define BACKGROUND_RENDERER_H__ + +#include + +// Based on BackgroundRenderer from poppler +class BackgroundRenderer : public SplashOutputDev { +public: + BackgroundRenderer(SplashColorMode colorModeA, int bitmapRowPadA, + GBool reverseVideoA, SplashColorPtr paperColorA, + GBool bitmapTopDownA = gTrue, + GBool allowAntialiasA = gTrue) : SplashOutputDev(colorModeA, + bitmapRowPadA, reverseVideoA, paperColorA, bitmapTopDownA, + allowAntialiasA) { } + virtual ~BackgroundRenderer() { } + + void drawChar(GfxState *state, double x, double y, + double dx, double dy, + double originX, double originY, + CharCode code, int nBytes, Unicode *u, int uLen); +}; + + + + +#endif //BACKGROUND_RENDERER_H__ diff --git a/src/HTMLRenderer.cc b/src/HTMLRenderer.cc index 9c8622b..b27d3de 100644 --- a/src/HTMLRenderer.cc +++ b/src/HTMLRenderer.cc @@ -28,8 +28,10 @@ #include #include #include +#include #include "HTMLRenderer.h" +#include "BackgroundRenderer.h" /* * CSS classes @@ -185,11 +187,29 @@ HTMLRenderer::~HTMLRenderer() void HTMLRenderer::process(PDFDoc *doc) { xref = doc->getXRef(); - for(int i = param->first_page; i <= param->last_page ; ++i) { + for(int i = param->first_page; i <= param->last_page ; ++i) + { doc->displayPage(this, i, param->h_dpi, param->v_dpi, 0, true, false, false, nullptr, nullptr, nullptr, nullptr); } + + // Render non-text objects as image + // copied from poppler + SplashColor color; + color[0] = color[1] = color[2] = 255; + + auto bg_renderer = new BackgroundRenderer(splashModeRGB8, 4, gFalse, color); + bg_renderer->startDoc(doc); + + for(int i = param->first_page; i <= param->last_page ; ++i) + { + doc->displayPage(bg_renderer, i, 4*param->h_dpi, 4*param->v_dpi, + 0, true, false, false, + nullptr, nullptr, nullptr, nullptr); + bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)(boost::format("p%|1$x|.png")%i).str().c_str(), 4*param->h_dpi, 4*param->v_dpi); + } + delete bg_renderer; } void HTMLRenderer::startPage(int pageNum, GfxState *state) @@ -203,8 +223,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) html_fout << boost::format("
"; if(param->readable) html_fout << endl; @@ -263,7 +282,7 @@ void HTMLRenderer::close_cur_line() { if(cur_line != nullptr) { - html_fout << "
"; + html_fout << "

"; if(param->readable) html_fout << endl; delete cur_line; @@ -415,7 +434,7 @@ void HTMLRenderer::endString(GfxState *state) { // TODO: optimize text matrix search/install // TODO: position might not be accurate - html_fout << boost::format("
getY() << "px;" << "left:" << cur_string->getX() << "px;" << "top:" << (pageHeight - cur_string->getY() - cur_state->getFont()->getAscent() * cur_state->getFontSize()) << "px;" @@ -544,7 +563,12 @@ long long HTMLRenderer::install_font(GfxFont * font) string new_fn = (boost::format("f%|1$x|") % new_fn_id).str(); if(font->getType() == fontType3) { - std::cerr << "TODO: Type 3 font unsupported" << std::endl; + std::cerr << "Type 3 fonts are unsupported and will be rendered as Image" << std::endl; + export_remote_default_font(new_fn_id); + return new_fn_id; + } + if(font->getWMode()) { + std::cerr << "Writing mode is unsupported and will be rendered as Image" << std::endl; export_remote_default_font(new_fn_id); return new_fn_id; } @@ -866,7 +890,7 @@ void HTMLRenderer::export_remote_font(long long fn_id, const string & suffix) void HTMLRenderer::export_remote_default_font(long long fn_id) { - allcss_fout << boost::format(".f%|1$x|{font-family:sans-serif;color:red;}")%fn_id; + allcss_fout << boost::format(".f%|1$x|{font-family:sans-serif;color:transparent;}")%fn_id; if(param->readable) allcss_fout << endl; }