1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 17:48:38 +00:00

optimize code; add some scope guards

This commit is contained in:
Lu Wang 2012-09-08 01:09:09 +08:00
parent ff9b079a4f
commit 296e91c951
4 changed files with 80 additions and 68 deletions

View File

@ -21,17 +21,17 @@ void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suff
{
allcss_fout << "@font-face{font-family:f" << info.id << ";src:url(";
const char * fn = str_fmt("f%llx%s", info.id, suffix.c_str());
if(param->single_html)
{
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'";
const char * fn = str_fmt("f%llx%s", info.id, suffix.c_str());
if(param->single_html)
{
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'";
}
else
{
allcss_fout << fn;
}
}
else
{
allcss_fout << fn;
}
allcss_fout << ")format(\"" << fontfileformat << "\");}.f" << info.id << "{font-family:f" << info.id << ";line-height:" << (info.ascent - info.descent) << ";}" << endl;
}

View File

@ -92,10 +92,12 @@ void HTMLRenderer::process(PDFDoc *doc)
0, true, false, false,
nullptr, nullptr, &annot_cb, nullptr);
const char * fn = str_fmt("p%x.png", i);
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi, param->v_dpi);
if(param->single_html)
add_tmp_file(fn);
{
const char * fn = str_fmt("p%llx.png", i);
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi, param->v_dpi);
if(param->single_html)
add_tmp_file(fn);
}
}
doc->displayPage(this, i, param->zoom * DEFAULT_DPI, param->zoom * DEFAULT_DPI,
@ -171,15 +173,17 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
html_fout << "background-image:url(";
const char * fn = str_fmt("p%x.png", pageNum);
if(param->single_html)
{
auto path = tmp_dir / fn;
html_fout << "'data:image/png;base64," << base64stream(ifstream(path, ifstream::binary)) << "'";
}
else
{
html_fout << fn;
const char * fn = str_fmt("p%llx.png", pageNum);
if(param->single_html)
{
auto path = tmp_dir / fn;
html_fout << "'data:image/png;base64," << base64stream(ifstream(path, ifstream::binary)) << "'";
}
else
{
html_fout << fn;
}
}
html_fout << ");background-position:0 0;background-size:" << pageWidth << "px " << pageHeight << "px;background-repeat:no-repeat;\">";

View File

@ -126,11 +126,13 @@ path HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
obj.streamReset();
const char * fn = str_fmt("f%x%s", fn_id, suffix.c_str());
ofstream outf;
filepath = tmp_dir / fn;
outf.open(filepath, ofstream::binary);
add_tmp_file(fn);
{
const char * fn = str_fmt("f%llx%s", fn_id, suffix.c_str());
filepath = tmp_dir / fn;
outf.open(filepath, ofstream::binary);
add_tmp_file(fn);
}
char buf[1024];
int len;
@ -329,51 +331,56 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
}
}
const char * fn = str_fmt("f%x%s", info.id, param->font_suffix.c_str());
auto dest = ((param->single_html ? tmp_dir : dest_dir) / fn);
if(param->single_html)
{
const char * fn = str_fmt("f%llx_.ttf", info.id);
/*
* [Win|Typo|HHead][Ascent|Descent]
* Firefox & Chrome interprets the values in different ways
* Trying to unify them
*/
// Generate an intermediate ttf font in order to retrieve the metrics
// TODO: see if we can get the values without save/load
add_tmp_file(fn);
/*
* [Win|Typo|HHead][Ascent|Descent]
* Firefox & Chrome interprets the values in different ways
* Trying to unify them
*/
// Generate an intermediate ttf font in order to retrieve the metrics
// TODO: see if we can get the values without save/load
fn = str_fmt("f%x_.ttf", info.id);
add_tmp_file(fn);
auto tmp_path = tmp_dir / fn;
ff_save(tmp_path.c_str());
ff_close();
ff_load_font(tmp_path.c_str());
int em = ff_get_em_size();
int ascent = ff_get_max_ascent();
int descent = ff_get_max_descent();
ff_set_ascent(ascent);
ff_set_descent(descent);
ff_save(dest.c_str());
ff_close();
if(em != 0)
{
info.ascent = ((double)ascent) / em;
info.descent = -((double)descent) / em;
}
else
{
info.ascent = 0;
info.descent = 0;
auto tmp_path = tmp_dir / fn;
ff_save(tmp_path.c_str());
ff_close();
ff_load_font(tmp_path.c_str());
}
// read metric
if(param->debug)
{
cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl;
// read metrics
int em = ff_get_em_size();
int ascent = ff_get_max_ascent();
int descent = ff_get_max_descent();
if(em != 0)
{
info.ascent = ((double)ascent) / em;
info.descent = -((double)descent) / em;
}
else
{
info.ascent = 0;
info.descent = 0;
}
if(param->debug)
{
cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl;
}
ff_set_ascent(ascent);
ff_set_descent(descent);
}
{
const char * fn = str_fmt("f%llx%s", info.id, param->font_suffix.c_str());
auto dest = ((param->single_html ? tmp_dir : dest_dir) / fn);
if(param->single_html)
add_tmp_file(fn);
ff_save(dest.c_str());
ff_close();
}
}

View File

@ -33,6 +33,7 @@ using std::endl;
using std::flush;
using std::cerr;
using std::floor;
using std::max;
// mute gcc warning of unused function
namespace
@ -275,9 +276,9 @@ public:
va_start(vlist, format);
int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist);
va_end(vlist);
if((l+1) > (int)buf.capacity())
if(l >= (int)buf.capacity())
{
buf.reserve(l+1);
buf.reserve(max((long)(l+1), (long)buf.capacity() * 2));
va_start(vlist, format);
l = vsnprintf(&buf.front(), buf.capacity(), format, vlist);
va_end(vlist);