1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-08 19:00:33 +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("; 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; 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, 0, true, false, false,
nullptr, nullptr, &annot_cb, nullptr); 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); const char * fn = str_fmt("p%llx.png", i);
if(param->single_html) bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi, param->v_dpi);
add_tmp_file(fn); if(param->single_html)
add_tmp_file(fn);
}
} }
doc->displayPage(this, i, param->zoom * DEFAULT_DPI, param->zoom * DEFAULT_DPI, 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("; html_fout << "background-image:url(";
const char * fn = str_fmt("p%x.png", pageNum);
if(param->single_html)
{ {
auto path = tmp_dir / fn; const char * fn = str_fmt("p%llx.png", pageNum);
html_fout << "'data:image/png;base64," << base64stream(ifstream(path, ifstream::binary)) << "'"; if(param->single_html)
} {
else auto path = tmp_dir / fn;
{ html_fout << "'data:image/png;base64," << base64stream(ifstream(path, ifstream::binary)) << "'";
html_fout << fn; }
else
{
html_fout << fn;
}
} }
html_fout << ");background-position:0 0;background-size:" << pageWidth << "px " << pageHeight << "px;background-repeat:no-repeat;\">"; 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(); obj.streamReset();
const char * fn = str_fmt("f%x%s", fn_id, suffix.c_str());
ofstream outf; ofstream outf;
filepath = tmp_dir / fn; {
outf.open(filepath, ofstream::binary); const char * fn = str_fmt("f%llx%s", fn_id, suffix.c_str());
add_tmp_file(fn); filepath = tmp_dir / fn;
outf.open(filepath, ofstream::binary);
add_tmp_file(fn);
}
char buf[1024]; char buf[1024];
int len; 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); const char * fn = str_fmt("f%llx_.ttf", info.id);
if(param->single_html)
/*
* [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); add_tmp_file(fn);
auto tmp_path = tmp_dir / fn;
/* ff_save(tmp_path.c_str());
* [Win|Typo|HHead][Ascent|Descent] ff_close();
* Firefox & Chrome interprets the values in different ways ff_load_font(tmp_path.c_str());
* 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;
} }
// 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::flush;
using std::cerr; using std::cerr;
using std::floor; using std::floor;
using std::max;
// mute gcc warning of unused function // mute gcc warning of unused function
namespace namespace
@ -275,9 +276,9 @@ public:
va_start(vlist, format); va_start(vlist, format);
int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist);
va_end(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); va_start(vlist, format);
l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); l = vsnprintf(&buf.front(), buf.capacity(), format, vlist);
va_end(vlist); va_end(vlist);