1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-10-06 12:01:39 +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,8 +21,8 @@ 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()); const char * fn = str_fmt("f%llx%s", info.id, suffix.c_str());
if(param->single_html) if(param->single_html)
{ {
allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'"; allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'";
@ -31,7 +31,7 @@ void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suff
{ {
allcss_fout << fn; 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,11 +92,13 @@ 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); {
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); 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) if(param->single_html)
add_tmp_file(fn); 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,
0, true, false, false, 0, true, false, false,
@ -171,7 +173,8 @@ 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); {
const char * fn = str_fmt("p%llx.png", pageNum);
if(param->single_html) if(param->single_html)
{ {
auto path = tmp_dir / fn; auto path = tmp_dir / fn;
@ -181,6 +184,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
{ {
html_fout << fn; 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;
{
const char * fn = str_fmt("f%llx%s", fn_id, suffix.c_str());
filepath = tmp_dir / fn; filepath = tmp_dir / fn;
outf.open(filepath, ofstream::binary); outf.open(filepath, ofstream::binary);
add_tmp_file(fn); add_tmp_file(fn);
}
char buf[1024]; char buf[1024];
int len; int len;
@ -329,10 +331,8 @@ 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)
add_tmp_file(fn);
/* /*
* [Win|Typo|HHead][Ascent|Descent] * [Win|Typo|HHead][Ascent|Descent]
@ -342,22 +342,18 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
// Generate an intermediate ttf font in order to retrieve the metrics // Generate an intermediate ttf font in order to retrieve the metrics
// TODO: see if we can get the values without save/load // TODO: see if we can get the values without save/load
fn = str_fmt("f%x_.ttf", info.id);
add_tmp_file(fn); add_tmp_file(fn);
auto tmp_path = tmp_dir / fn; auto tmp_path = tmp_dir / fn;
ff_save(tmp_path.c_str()); ff_save(tmp_path.c_str());
ff_close(); ff_close();
ff_load_font(tmp_path.c_str()); ff_load_font(tmp_path.c_str());
}
{
// read metrics
int em = ff_get_em_size(); int em = ff_get_em_size();
int ascent = ff_get_max_ascent(); int ascent = ff_get_max_ascent();
int descent = ff_get_max_descent(); int descent = ff_get_max_descent();
ff_set_ascent(ascent);
ff_set_descent(descent);
ff_save(dest.c_str());
ff_close();
if(em != 0) if(em != 0)
{ {
info.ascent = ((double)ascent) / em; info.ascent = ((double)ascent) / em;
@ -368,13 +364,24 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo &
info.ascent = 0; info.ascent = 0;
info.descent = 0; info.descent = 0;
} }
// read metric
if(param->debug) if(param->debug)
{ {
cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl; 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();
}
} }
void HTMLRenderer::drawString(GfxState * state, GooString * s) void HTMLRenderer::drawString(GfxState * state, GooString * s)

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);