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

466 lines
12 KiB
C++
Raw Normal View History

2012-08-14 08:23:15 +00:00
/*
* general.cc
*
2012-09-25 11:29:59 +00:00
* Handling general stuffs
2012-08-14 08:23:15 +00:00
*
2012-10-02 12:56:40 +00:00
* Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
2012-08-14 08:23:15 +00:00
* 2012.08.14
*/
2012-09-09 17:40:37 +00:00
#include <cstdio>
2012-09-12 15:26:14 +00:00
#include <ostream>
2012-09-22 14:47:44 +00:00
#include <cmath>
2012-09-26 16:17:56 +00:00
#include <algorithm>
#include <vector>
2012-09-05 10:43:37 +00:00
2012-08-14 08:23:15 +00:00
#include "HTMLRenderer.h"
#include "BackgroundRenderer.h"
2012-08-14 09:13:29 +00:00
#include "namespace.h"
2012-09-17 12:40:10 +00:00
#include "ffw.h"
2012-09-10 05:08:47 +00:00
#include "pdf2htmlEX-config.h"
2012-08-14 08:23:15 +00:00
2012-09-12 15:26:14 +00:00
namespace pdf2htmlEX {
using std::fixed;
2012-08-14 12:30:18 +00:00
using std::flush;
2012-09-12 15:26:14 +00:00
using std::ostream;
2012-09-22 14:47:44 +00:00
using std::max;
2012-09-26 16:17:56 +00:00
using std::min_element;
using std::vector;
2012-09-26 16:25:41 +00:00
using std::abs;
2012-08-14 12:30:18 +00:00
static void dummy(void *, enum ErrorCategory, int pos, char *)
2012-09-03 13:59:39 +00:00
{
}
2012-08-14 08:23:15 +00:00
HTMLRenderer::HTMLRenderer(const Param * param)
2012-09-16 10:30:34 +00:00
:OutputDev()
,line_opened(false)
2012-09-04 15:33:15 +00:00
,line_buf(this)
2012-09-22 06:41:29 +00:00
,preprocessor(param)
2012-08-14 08:23:15 +00:00
,image_count(0)
,param(param)
{
2012-09-17 12:40:10 +00:00
if(!(param->debug))
{
//disable error function of poppler
setErrorCallback(&dummy, nullptr);
}
2012-09-03 13:59:39 +00:00
2012-09-26 16:17:56 +00:00
/*
* determine scale factors
*/
{
double zoom = 1.0;
vector<double> zoom_factors;
2012-10-02 08:06:08 +00:00
if(_is_positive(param->zoom))
2012-09-26 16:17:56 +00:00
{
zoom_factors.push_back(param->zoom);
}
2012-10-02 08:06:08 +00:00
if(_is_positive(param->fit_width))
2012-09-26 16:17:56 +00:00
{
zoom_factors.push_back((param->fit_width) / preprocessor.get_max_width());
}
2012-10-02 08:06:08 +00:00
if(_is_positive(param->fit_height))
2012-09-26 16:17:56 +00:00
{
zoom_factors.push_back((param->fit_height) / preprocessor.get_max_height());
}
if(zoom_factors.empty())
{
zoom = 1.0;
}
else
{
zoom = *min_element(zoom_factors.begin(), zoom_factors.end());
}
2012-10-02 18:19:40 +00:00
text_scale_factor1 = max<double>(zoom, param->font_size_multiplier);
2012-10-02 06:19:20 +00:00
text_scale_factor2 = zoom / text_scale_factor1;
2012-09-26 16:17:56 +00:00
}
ffw_init(param->debug);
cur_mapping = new int32_t [0x10000];
cur_mapping2 = new char* [0x100];
width_list = new int [0x10000];
}
HTMLRenderer::~HTMLRenderer()
{
ffw_finalize();
clean_tmp_files();
delete [] cur_mapping;
delete [] cur_mapping2;
delete [] width_list;
}
void HTMLRenderer::process(PDFDoc *doc)
{
cur_doc = doc;
xref = doc->getXRef();
cerr << "Preprocessing: ";
preprocessor.process(doc);
2012-09-26 16:17:56 +00:00
2012-09-06 16:58:23 +00:00
cerr << "Working: ";
BackgroundRenderer * bg_renderer = nullptr;
2012-08-14 08:23:15 +00:00
if(param->process_nontext)
{
2012-10-03 14:46:27 +00:00
bg_renderer = new BackgroundRenderer(this, param);
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();
2012-09-07 00:13:45 +00:00
2012-08-14 12:30:18 +00:00
for(int i = param->first_page; i <= param->last_page ; ++i)
{
2012-09-12 15:26:14 +00:00
if(param->split_pages)
{
2012-09-12 16:16:34 +00:00
auto page_fn = str_fmt("%s/%s%d.page", param->dest_dir.c_str(), param->output_filename.c_str(), i);
2012-09-12 15:26:14 +00:00
html_fout.open((char*)page_fn, ofstream::binary);
2012-09-17 12:07:50 +00:00
if(!html_fout)
throw string("Cannot open ") + (char*)page_fn + " for writing";
2012-09-12 15:26:14 +00:00
fix_stream(html_fout);
}
2012-08-14 12:30:18 +00:00
if(param->process_nontext)
2012-08-14 08:23:15 +00:00
{
2012-10-02 13:11:00 +00:00
auto fn = str_fmt("%s/p%x.png", (param->single_html ? param->tmp_dir : param->dest_dir).c_str(), i);
if(param->single_html)
add_tmp_file((char*)fn);
bg_renderer->render_page(doc, i, (char*)fn);
2012-08-14 08:23:15 +00:00
}
2012-08-14 12:30:18 +00:00
2012-10-02 06:19:20 +00:00
doc->displayPage(this, i,
text_zoom_factor() * DEFAULT_DPI, text_zoom_factor() * DEFAULT_DPI,
2012-08-14 12:30:18 +00:00
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
2012-09-12 15:26:14 +00:00
if(param->split_pages)
{
html_fout.close();
}
2012-08-14 12:30:18 +00:00
cerr << "." << flush;
2012-08-14 08:23:15 +00:00
}
2012-09-12 15:26:14 +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-09-16 10:30:34 +00:00
void HTMLRenderer::setDefaultCTM(double *ctm)
{
memcpy(default_ctm, ctm, sizeof(default_ctm));
}
void HTMLRenderer::startPage(int pageNum, GfxState *state)
{
this->pageNum = pageNum;
this->pageWidth = state->getPageWidth();
this->pageHeight = state->getPageHeight();
assert((!line_opened) && "Open line in startPage detected!");
html_fout
<< "<div class=\"d\" style=\"width:"
2012-09-24 17:12:28 +00:00
<< (pageWidth) << "px;height:"
<< (pageHeight) << "px;\">"
<< "<div id=\"p" << pageNum << "\" data-page-no=\"" << pageNum << "\" class=\"p\">"
<< "<div class=\"b\" style=\"";
2012-09-16 10:30:34 +00:00
2012-09-17 07:28:52 +00:00
if(param->process_nontext)
2012-09-16 10:30:34 +00:00
{
2012-09-17 07:28:52 +00:00
html_fout << "background-image:url(";
2012-09-16 10:30:34 +00:00
{
2012-09-17 07:28:52 +00:00
if(param->single_html)
{
auto path = str_fmt("%s/p%x.png", param->tmp_dir.c_str(), pageNum);
ifstream fin((char*)path, ifstream::binary);
if(!fin)
throw string("Cannot read background image ") + (char*)path;
html_fout << "'data:image/png;base64," << base64stream(fin) << "'";
}
else
{
html_fout << str_fmt("p%x.png", pageNum);
}
2012-09-16 10:30:34 +00:00
}
2012-09-17 07:28:52 +00:00
html_fout << ");background-position:0 0;background-size:" << pageWidth << "px " << pageHeight << "px;background-repeat:no-repeat;";
2012-09-16 10:30:34 +00:00
}
2012-09-17 07:28:52 +00:00
html_fout << "\">";
2012-10-02 06:19:20 +00:00
draw_text_scale = 1.0;
2012-09-16 10:30:34 +00:00
cur_font_info = install_font(nullptr);
cur_font_size = draw_font_size = 0;
cur_fs_id = install_font_size(cur_font_size);
2012-10-01 17:59:04 +00:00
memcpy(cur_text_tm, id_matrix, sizeof(cur_text_tm));
memcpy(draw_text_tm, id_matrix, sizeof(draw_text_tm));
cur_ttm_id = install_transform_matrix(draw_text_tm);
2012-09-16 10:30:34 +00:00
cur_letter_space = cur_word_space = 0;
cur_ls_id = install_letter_space(cur_letter_space);
cur_ws_id = install_word_space(cur_word_space);
cur_color.r = cur_color.g = cur_color.b = 0;
cur_color_id = install_color(&cur_color);
cur_rise = 0;
cur_rise_id = install_rise(cur_rise);
cur_tx = cur_ty = 0;
draw_tx = draw_ty = 0;
reset_state_change();
all_changed = true;
}
void HTMLRenderer::endPage() {
2012-10-01 17:59:04 +00:00
close_text_line();
2012-09-16 10:30:34 +00:00
// process links before the page is closed
cur_doc->processLinks(this, pageNum);
2012-09-12 15:26:14 +00:00
2012-09-25 11:29:59 +00:00
// close box
html_fout << "</div>";
2012-09-16 12:47:42 +00:00
2012-09-25 11:29:59 +00:00
// dump info for js
// TODO: create a function for this
// BE CAREFUL WITH ESCAPES
html_fout << "<div class=\"j\" data-data='{";
//default CTM
html_fout << "\"ctm\":[";
for(int i = 0; i < 6; ++i)
2012-09-16 12:47:42 +00:00
{
2012-09-25 11:29:59 +00:00
if(i > 0) html_fout << ",";
html_fout << _round(default_ctm[i]);
2012-09-16 12:47:42 +00:00
}
2012-09-25 11:29:59 +00:00
html_fout << "]";
2012-09-16 12:47:42 +00:00
2012-09-25 11:29:59 +00:00
html_fout << "}'></div>";
// close page
html_fout << "</div></div>" << endl;
2012-08-14 08:23:15 +00:00
}
void HTMLRenderer::pre_process()
{
// we may output utf8 characters, so always use binary
{
/*
* If single-html && !split-pages
* we have to keep the generated css file into a temporary place
* and embed it into the main html later
*
*
* If single-html && split-page
* as there's no place to embed the css file, just leave it alone (into param->dest_dir)
*
* If !single-html
* leave it in param->dest_dir
*/
auto fn = (param->single_html && (!param->split_pages))
? str_fmt("%s/__css", param->tmp_dir.c_str())
: str_fmt("%s/%s", param->dest_dir.c_str(), param->css_filename.c_str());
if(param->single_html && (!param->split_pages))
add_tmp_file((char*)fn);
css_path = (char*)fn,
css_fout.open(css_path, ofstream::binary);
2012-09-17 12:07:50 +00:00
if(!css_fout)
throw string("Cannot open ") + (char*)fn + " for writing";
fix_stream(css_fout);
}
// if split-pages is specified, open & close the file in the process loop
// if not, open the file here:
if(!param->split_pages)
{
/*
* If single-html
* we have to keep the html file (for page) into a temporary place
* because we'll have to embed css before it
*
* Otherwise just generate it
*/
auto fn = str_fmt("%s/__pages", param->tmp_dir.c_str());
add_tmp_file((char*)fn);
html_path = (char*)fn;
html_fout.open(html_path, ofstream::binary);
2012-09-17 12:07:50 +00:00
if(!html_fout)
throw string("Cannot open ") + (char*)fn + " for writing";
fix_stream(html_fout);
}
}
void HTMLRenderer::post_process()
{
// close files
html_fout.close();
css_fout.close();
//only when split-page, do we have some work left to do
if(param->split_pages)
return;
2012-09-17 12:07:50 +00:00
ofstream output;
{
auto fn = str_fmt("%s/%s", param->dest_dir.c_str(), param->output_filename.c_str());
output.open((char*)fn, ofstream::binary);
if(!output)
throw string("Cannot open ") + (char*)fn + " for writing";
fix_stream(output);
}
// apply manifest
2012-09-17 12:07:50 +00:00
ifstream manifest_fin((char*)str_fmt("%s/%s", param->data_dir.c_str(), MANIFEST_FILENAME.c_str()), ifstream::binary);
if(!manifest_fin)
throw "Cannot open the manifest file";
bool embed_string = false;
string line;
while(getline(manifest_fin, line))
{
if(line == "\"\"\"")
{
embed_string = !embed_string;
continue;
}
if(embed_string)
{
output << line << endl;
continue;
}
if(line.empty() || line[0] == '#')
continue;
if(line[0] == '@')
{
embed_file(output, param->data_dir + "/" + line.substr(1), "", true);
continue;
}
if(line[0] == '$')
{
if(line == "$css")
{
embed_file(output, css_path, ".css", false);
}
else if (line == "$pages")
{
ifstream fin(html_path, ifstream::binary);
if(!fin)
throw "Cannot open read the pages";
output << fin.rdbuf();
}
else
{
cerr << "Warning: unknown line in manifest: " << line << endl;
}
continue;
}
cerr << "Warning: unknown line in manifest: " << line << endl;
}
}
void HTMLRenderer::fix_stream (std::ostream & out)
{
2012-10-01 17:59:04 +00:00
// we output all ID's in hex
// browsers are not happy with scientific notations
out << hex << fixed;
}
2012-08-15 04:27:41 +00:00
void HTMLRenderer::add_tmp_file(const string & fn)
{
2012-08-15 07:43:49 +00:00
if(!param->clean_tmp)
return;
2012-08-15 04:27:41 +00:00
if(tmp_files.insert(fn).second && param->debug)
cerr << "Add new temporary file: " << fn << endl;
}
2012-08-14 08:23:15 +00:00
2012-08-15 04:27:41 +00:00
void HTMLRenderer::clean_tmp_files()
{
2012-08-15 07:43:49 +00:00
if(!param->clean_tmp)
return;
2012-09-09 06:48:10 +00:00
for(auto iter = tmp_files.begin(); iter != tmp_files.end(); ++iter)
2012-08-15 04:27:41 +00:00
{
2012-09-09 17:40:37 +00:00
const string & fn = *iter;
remove(fn.c_str());
2012-08-15 04:27:41 +00:00
if(param->debug)
2012-09-09 17:40:37 +00:00
cerr << "Remove temporary file: " << fn << endl;
2012-08-15 04:27:41 +00:00
}
2012-09-09 17:40:37 +00:00
2012-09-12 16:16:34 +00:00
remove(param->tmp_dir.c_str());
2012-09-09 17:40:37 +00:00
if(param->debug)
2012-09-12 16:16:34 +00:00
cerr << "Remove temporary directory: " << param->tmp_dir << endl;
2012-08-15 04:27:41 +00:00
}
void HTMLRenderer::embed_file(ostream & out, const string & path, const string & type, bool copy)
2012-09-12 15:26:14 +00:00
{
string fn = get_filename(path);
string suffix = (type == "") ? get_suffix(fn) : type;
2012-09-12 15:26:14 +00:00
auto iter = EMBED_STRING_MAP.find(make_pair(suffix, (bool)param->single_html));
2012-09-12 15:26:14 +00:00
if(iter == EMBED_STRING_MAP.end())
{
cerr << "Warning: unknown suffix: " << suffix << endl;
2012-09-12 15:26:14 +00:00
return;
}
if(param->single_html)
{
ifstream fin(path, ifstream::binary);
if(!fin)
2012-09-17 12:07:50 +00:00
throw string("Cannot open file ") + path + " for embedding";
out << iter->second.first << endl
<< fin.rdbuf()
2012-09-12 15:26:14 +00:00
<< iter->second.second << endl;
}
else
{
out << iter->second.first
2012-09-12 15:26:14 +00:00
<< fn
<< iter->second.second << endl;
if(copy)
{
ifstream fin(path, ifstream::binary);
if(!fin)
throw string("Cannot copy file: ") + path;
2012-09-17 12:07:50 +00:00
auto out_path = param->dest_dir + "/" + fn;
ofstream out(out_path, ofstream::binary);
if(!out)
throw string("Cannot open file ") + path + " for embedding";
out << fin.rdbuf();
}
2012-09-12 15:26:14 +00:00
}
}
const std::string HTMLRenderer::MANIFEST_FILENAME = "manifest";
}// namespace pdf2htmlEX