1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-09-17 21:16:03 +00:00
pdf2htmlEX/src/HTMLRenderer/general.cc

143 lines
3.5 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
*/
#include <iostream>
#include <boost/format.hpp>
#include <boost/filesystem/fstream.hpp>
#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
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)
,html_fout(dest_dir / param->output_filename, ofstream::binary) // we may output utf8 characters, so use binary
,allcss_fout(dest_dir / "all.css", ofstream::binary)
,fontscript_fout(tmp_dir / "convert.pe", ofstream::binary)
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 09:13:29 +00:00
cerr << "Processing Text: ";
2012-08-14 08:23:15 +00:00
write_html_head();
xref = doc->getXRef();
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);
2012-08-14 09:13:29 +00:00
cerr << ".";
cerr.flush();
2012-08-14 08:23:15 +00:00
}
write_html_tail();
2012-08-14 09:13:29 +00:00
cerr << endl;
2012-08-14 08:23:15 +00:00
if(param->process_nontext)
{
// Render non-text objects as image
2012-08-14 09:13:29 +00:00
cerr << "Processing Others: ";
2012-08-14 08:23:15 +00:00
// 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, param->h_dpi2, param->v_dpi2,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
2012-08-14 10:12:58 +00:00
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)(dest_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 09:13:29 +00:00
cerr << ".";
cerr.flush();
2012-08-14 08:23:15 +00:00
}
delete bg_renderer;
2012-08-14 09:13:29 +00:00
cerr << endl;
2012-08-14 08:23:15 +00:00
}
}
void HTMLRenderer::write_html_head()
{
2012-08-14 09:13:29 +00:00
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
2012-08-14 08:23:15 +00:00
}
void HTMLRenderer::write_html_tail()
{
2012-08-14 09:13:29 +00:00
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
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 09:50:16 +00:00
html_fout << format("background-image:url(p%|3$x|.png);background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;") % pageWidth % pageHeight % pageNum;
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;
}