1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-09-19 05:40:07 +00:00
pdf2htmlEX/src/HTMLRenderer/general.cc

193 lines
4.8 KiB
C++
Raw Normal View History

2012-08-14 08:23:15 +00:00
/*
* general.cc
*
* Hanlding general stuffs
*
* TODO: better name for this file?
*
* by WangLu
* 2012.08.14
*/
2012-08-14 12:30:18 +00:00
#include <iomanip>
2012-08-14 08:23:15 +00:00
#include <splash/SplashBitmap.h>
#include "HTMLRenderer.h"
#include "BackgroundRenderer.h"
#include "config.h"
2012-08-14 09:13:29 +00:00
#include "namespace.h"
2012-08-14 08:23:15 +00:00
2012-08-14 12:30:18 +00:00
using std::flush;
2012-08-14 08:23:15 +00:00
HTMLRenderer::HTMLRenderer(const Param * param)
:line_opened(false)
,image_count(0)
,param(param)
2012-08-14 09:50:16 +00:00
,dest_dir(param->dest_dir)
,tmp_dir(TMP_DIR)
2012-08-14 08:23:15 +00:00
{
// install default font & size
install_font(nullptr);
install_font_size(0);
install_transform_matrix(id_matrix);
GfxRGB black;
black.r = black.g = black.b = 0;
install_color(&black);
}
HTMLRenderer::~HTMLRenderer()
{ }
void HTMLRenderer::process(PDFDoc *doc)
{
2012-08-14 12:30:18 +00:00
cerr << "Working: ";
2012-08-14 08:23:15 +00:00
xref = doc->getXRef();
2012-08-14 12:30:18 +00:00
BackgroundRenderer * bg_renderer = nullptr;
2012-08-14 08:23:15 +00:00
if(param->process_nontext)
{
// Render non-text objects as image
// copied from poppler
SplashColor color;
color[0] = color[1] = color[2] = 255;
2012-08-14 12:30:18 +00:00
bg_renderer = new BackgroundRenderer(splashModeRGB8, 4, gFalse, color);
2012-08-14 08:23:15 +00:00
bg_renderer->startDoc(doc);
2012-08-14 12:30:18 +00:00
}
2012-08-14 08:23:15 +00:00
2012-08-14 12:30:18 +00:00
pre_process();
for(int i = param->first_page; i <= param->last_page ; ++i)
{
if(param->process_nontext)
2012-08-14 08:23:15 +00:00
{
doc->displayPage(bg_renderer, i, param->h_dpi2, param->v_dpi2,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
2012-08-14 12:30:18 +00:00
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)(working_dir() / (format("p%|1$x|.png")%i).str()).c_str(), param->h_dpi2, param->v_dpi2);
2012-08-14 08:23:15 +00:00
}
2012-08-14 12:30:18 +00:00
doc->displayPage(this, i, param->h_dpi, param->v_dpi,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
cerr << "." << flush;
2012-08-14 08:23:15 +00:00
}
2012-08-14 12:30:18 +00:00
post_process();
if(bg_renderer)
delete bg_renderer;
cerr << endl;
2012-08-14 08:23:15 +00:00
}
2012-08-14 12:30:18 +00:00
void HTMLRenderer::pre_process()
2012-08-14 08:23:15 +00:00
{
2012-08-14 12:30:18 +00:00
html_fout.open(working_dir() / param->output_filename, ofstream::binary); // we may output utf8 characters, so use binary
allcss_fout.open(working_dir() / "all.css", ofstream::binary);
fontscript_fout.open(tmp_dir / "pdf2htmlEX.pe", ofstream::binary);
if(!param->single_html)
{
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
html_fout << "<link rel=\"stylesheet\" type=\"text/css\" href=\"all.css\"/>" << endl;
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "neck.html", ifstream::binary).rdbuf();
}
allcss_fout << ifstream(PDF2HTMLEX_LIB_PATH / "base.css", ifstream::binary).rdbuf();
2012-08-14 08:23:15 +00:00
}
2012-08-14 12:30:18 +00:00
void HTMLRenderer::post_process()
2012-08-14 08:23:15 +00:00
{
2012-08-14 12:30:18 +00:00
if(!param->single_html)
{
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
}
html_fout.close();
allcss_fout.close();
fontscript_fout.close();
if(param->single_html)
{
process_single_html();
}
2012-08-14 08:23:15 +00:00
}
void HTMLRenderer::startPage(int pageNum, GfxState *state)
{
this->pageNum = pageNum;
this->pageWidth = state->getPageWidth();
this->pageHeight = state->getPageHeight();
assert(!line_opened);
2012-08-14 09:50:16 +00:00
html_fout << format("<div id=\"page-%3%\" class=\"p\" style=\"width:%1%px;height:%2%px;") % pageWidth % pageHeight % pageNum;
2012-08-14 08:23:15 +00:00
2012-08-14 13:23:33 +00:00
html_fout << "background-image:url(";
const std::string fn = (format("p%|1$x|.png") % pageNum).str();
if(param->single_html)
{
html_fout << "'data:image/png;base64,";
copy_base64(html_fout, ifstream(tmp_dir / fn));
html_fout << "'";
}
else
{
html_fout << fn;
}
html_fout << format(");background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;") % pageWidth % pageHeight;
2012-08-14 08:23:15 +00:00
html_fout << "\">" << endl;
cur_fn_id = cur_fs_id = cur_tm_id = cur_color_id = 0;
cur_tx = cur_ty = 0;
cur_font_size = 0;
memcpy(cur_ctm, id_matrix, sizeof(cur_ctm));
memcpy(draw_ctm, id_matrix, sizeof(draw_ctm));
draw_font_size = 0;
draw_scale = 1.0;
draw_tx = draw_ty = 0;
cur_color.r = cur_color.g = cur_color.b = 0;
reset_state_track();
}
void HTMLRenderer::endPage() {
close_cur_line();
// close page
html_fout << "</div>" << endl;
}
2012-08-14 12:30:18 +00:00
void HTMLRenderer::process_single_html()
{
ofstream out (dest_dir / param->output_filename, ofstream::binary);
out << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
out << "<style type=\"text/css\">" << endl;
out << ifstream(tmp_dir / "all.css", ifstream::binary).rdbuf();
out << "</style>" << endl;
out << ifstream(PDF2HTMLEX_LIB_PATH / "neck.html", ifstream::binary).rdbuf();
2012-08-14 08:23:15 +00:00
2012-08-14 12:30:18 +00:00
out << ifstream(tmp_dir / param->output_filename, ifstream::binary).rdbuf();
out << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
}
2012-08-14 08:23:15 +00:00