diff --git a/share/base.css.in b/share/base.css.in index 9a94b6c..a0311be 100644 --- a/share/base.css.in +++ b/share/base.css.in @@ -91,10 +91,6 @@ } .@CSS_BACKGROUND_IMAGE_CN@ { position:absolute; - left:0; - top:0; - width:100%; - height:100%; -ms-user-select:none; -moz-user-select:none; -webkit-user-select:none; diff --git a/src/BackgroundRenderer/SplashBackgroundRenderer.cc b/src/BackgroundRenderer/SplashBackgroundRenderer.cc index f57d821..a982149 100644 --- a/src/BackgroundRenderer/SplashBackgroundRenderer.cc +++ b/src/BackgroundRenderer/SplashBackgroundRenderer.cc @@ -5,8 +5,10 @@ */ #include +#include #include +#include #include "Base64Stream.h" @@ -16,6 +18,7 @@ namespace pdf2htmlEX { using std::string; using std::ifstream; +using std::vector; const SplashColor SplashBackgroundRenderer::white = {255,255,255}; @@ -67,6 +70,7 @@ void SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno) void SplashBackgroundRenderer::embed_image(int pageno) { + // xmin->xmax is top->bottom int xmin, xmax, ymin, ymax; getModRegion(&xmin, &ymin, &xmax, &ymax); @@ -78,14 +82,22 @@ void SplashBackgroundRenderer::embed_image(int pageno) if(param.embed_image) html_renderer->tmp_files.add((char*)fn); - getBitmap()->writeImgFile(splashFormatPng, - (char*)fn, - param.h_dpi, param.v_dpi); + dump_image((char*)fn, xmin, ymin, xmax, ymax); } + double h_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.h_dpi; + double v_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.v_dpi; + auto & f_page = *(html_renderer->f_curpage); + auto & all_manager = html_renderer->all_manager; + f_page << "\"\"str_fmt("%s/bg%x.png", param.tmp_dir.c_str(), pageno); @@ -102,4 +114,43 @@ void SplashBackgroundRenderer::embed_image(int pageno) } } +// There will be mem leak when exception is thrown ! +void SplashBackgroundRenderer::dump_image(const char * filename, int x1, int y1, int x2, int y2) +{ + int width = x2 - x1 + 1; + int height = y2 - y1 + 1; + if((width <= 0) || (height <= 0)) + throw "Bad metric for background image"; + + FILE * f = fopen(filename, "wb"); + if(!f) + throw string("Cannot open file for background image " ) + filename; + + ImgWriter * writer = new PNGWriter(); + if(!writer->init(f, width, height, param.h_dpi, param.v_dpi)) + throw "Cannot initialize PNGWriter"; + + auto * bitmap = getBitmap(); + assert(bitmap->getMode() == splashModeRGB8); + + SplashColorPtr data = bitmap->getDataPtr(); + int row_size = bitmap->getRowSize(); + + vector pointers; + pointers.reserve(height); + SplashColorPtr p = data + y1 * row_size + x1 * 3; + for(int i = 0; i < height; ++i) + { + pointers.push_back(p); + p += row_size; + } + + if(!writer->writePointers(pointers.data(), height)) { + throw "Cannot write background image"; + } + + delete writer; + fclose(f); +} + } // namespace pdf2htmlEX diff --git a/src/BackgroundRenderer/SplashBackgroundRenderer.h b/src/BackgroundRenderer/SplashBackgroundRenderer.h index e44c58a..1c5be7e 100644 --- a/src/BackgroundRenderer/SplashBackgroundRenderer.h +++ b/src/BackgroundRenderer/SplashBackgroundRenderer.h @@ -53,6 +53,7 @@ public: void render_page(PDFDoc * doc, int pageno); void embed_image(int pageno); + void dump_image(const char * filename, int x1, int y1, int x2, int y2); protected: HTMLRenderer * html_renderer;