Skip to content

Commit

Permalink
draft delayed background rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Jan 1, 2025
1 parent 3eccf47 commit f7aee77
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 57 deletions.
94 changes: 41 additions & 53 deletions pdf2htmlEX/src/BackgroundRenderer/SplashBackgroundRenderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,66 +111,54 @@ bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
(!(param.use_cropbox)),
false, false,
nullptr, nullptr, &annot_cb, &process_annotation);

auto * bitmap = getBitmap();

auto fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
if(param.embed_image)
html_renderer->tmp_files.add((const char *)fn);

SplashImageFileFormat splashImageFileFormat;
if(format == "png")
splashImageFileFormat = splashFormatPng;
else if(format == "jpg")
splashImageFileFormat = splashFormatJpeg;
else
throw string("Image format not supported: ") + format;

SplashError e = bitmap->writeImgFile(splashImageFileFormat, (const char *)fn, param.actual_dpi, param.actual_dpi);
if (e != splashOk)
throw string("Cannot write background image. SplashErrorCode: ") + std::to_string(e);

return true;
}

void SplashBackgroundRenderer::embed_image(int pageno)
{
auto * bitmap = getBitmap();
// dump the background image only when it is not empty
if(bitmap->getWidth() >= 0 && bitmap->getHeight() >= 0)
auto & f_page = *(html_renderer->f_curpage);

f_page << "<img class=\"" << CSS::FULL_BACKGROUND_IMAGE_CN
<< "\" alt=\"\" src=\"";

if(param.embed_image)
{
auto path = html_renderer->str_fmt("%s/bg%x.%s", param.tmp_dir.c_str(), pageno, format.c_str());
ifstream fin((char*)path, ifstream::binary);
if(!fin)
throw string("Cannot read background image ") + (char*)path;

auto iter = FORMAT_MIME_TYPE_MAP.find(format);
if(iter == FORMAT_MIME_TYPE_MAP.end())
throw string("Image format not supported: ") + format;

string mime_type = iter->second;
f_page << "data:" << mime_type << ";base64," << Base64Stream(fin);
}
else
{
{
auto fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
if(param.embed_image)
html_renderer->tmp_files.add((const char *)fn);

SplashImageFileFormat splashImageFileFormat;
if(format == "png")
splashImageFileFormat = splashFormatPng;
else if(format == "jpg")
splashImageFileFormat = splashFormatJpeg;
else
throw string("Image format not supported: ") + format;

SplashError e = bitmap->writeImgFile(splashImageFileFormat, (const char *)fn, param.actual_dpi, param.actual_dpi);
if (e != splashOk)
throw string("Cannot write background image. SplashErrorCode: ") + std::to_string(e);
}

double h_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.actual_dpi;
double v_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.actual_dpi;

auto & f_page = *(html_renderer->f_curpage);
auto & all_manager = html_renderer->all_manager;

f_page << "<img class=\"" << CSS::BACKGROUND_IMAGE_CN
<< " " << CSS::LEFT_CN << all_manager.left.install(0.0L)
<< " " << CSS::BOTTOM_CN << all_manager.bottom.install(0.0L)
<< " " << CSS::WIDTH_CN << all_manager.width.install(h_scale * bitmap->getWidth())
<< " " << CSS::HEIGHT_CN << all_manager.height.install(v_scale * bitmap->getHeight())
<< "\" alt=\"\" src=\"";

if(param.embed_image)
{
auto path = html_renderer->str_fmt("%s/bg%x.%s", param.tmp_dir.c_str(), pageno, format.c_str());
ifstream fin((char*)path, ifstream::binary);
if(!fin)
throw string("Cannot read background image ") + (char*)path;

auto iter = FORMAT_MIME_TYPE_MAP.find(format);
if(iter == FORMAT_MIME_TYPE_MAP.end())
throw string("Image format not supported: ") + format;

string mime_type = iter->second;
f_page << "data:" << mime_type << ";base64," << Base64Stream(fin);
}
else
{
f_page << (char*)html_renderer->str_fmt("bg%x.%s", pageno, format.c_str());
}
f_page << "\"/>";
f_page << (char*)html_renderer->str_fmt("bg%x.%s", pageno, format.c_str());
}
f_page << "\"/>";
}

} // namespace pdf2htmlEX
2 changes: 2 additions & 0 deletions pdf2htmlEX/src/HTMLRenderer/HTMLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ struct HTMLRenderer : OutputDev

void process(PDFDoc * doc);

bool renderPage(PDFDoc * doc, int pageno);

////////////////////////////////////////////////////
// OutputDev interface
////////////////////////////////////////////////////
Expand Down
30 changes: 26 additions & 4 deletions pdf2htmlEX/src/HTMLRenderer/general.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ void HTMLRenderer::process(PDFDoc *doc)
cerr << endl;
}

bool HTMLRenderer::renderPage(PDFDoc *doc, int pageno)
{
if (bg_renderer->render_page(cur_doc, pageNum))
{
return true;
}
else if (fallback_bg_renderer)
{
if (fallback_bg_renderer->render_page(cur_doc, pageNum))
return true;
}

return false;
}

void HTMLRenderer::setDefaultCTM(const double *ctm)
{
memcpy(default_ctm, ctm, sizeof(default_ctm));
Expand Down Expand Up @@ -243,14 +258,21 @@ void HTMLRenderer::endPage() {

if(param.process_nontext)
{
if (bg_renderer->render_page(cur_doc, pageNum))
if (param.delay_background)
{
bg_renderer->embed_image(pageNum);
}
else if (fallback_bg_renderer)
else
{
if (fallback_bg_renderer->render_page(cur_doc, pageNum))
fallback_bg_renderer->embed_image(pageNum);
if (bg_renderer->render_page(cur_doc, pageNum))
{
bg_renderer->embed_image(pageNum);
}
else if (fallback_bg_renderer)
{
if (fallback_bg_renderer->render_page(cur_doc, pageNum))
fallback_bg_renderer->embed_image(pageNum);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions pdf2htmlEX/src/Param.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct Param
int printing;
int fallback;
int tmp_file_size_limit;
int delay_background;

// fonts
int embed_external_font;
Expand Down

0 comments on commit f7aee77

Please sign in to comment.