1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 13:00:08 +00:00

automatically infer @font-face src format

This commit is contained in:
John Hewson 2013-01-29 12:07:51 +00:00
parent 2f1c8170bf
commit 6fda07bec3
6 changed files with 66 additions and 38 deletions

View File

@ -109,8 +109,8 @@ If this switch is on, local matched font will be used and embedded; otherwise on
Similar as above but for non-base fonts.
.TP
.B --font-suffix <suffix> (Default: .ttf), --font-format <format> (Default: truetype)
Specify the suffix and format of fonts extracted from the PDF file. They should be consistent.
.B --font-suffix <suffix> (Default: .ttf)
Specify the suffix of fonts extracted from the PDF file.
.TP
.B --decompose-ligature <0|1> (Default: 0)

View File

@ -248,7 +248,7 @@ class HTMLRenderer : public OutputDev
* remote font: to be retrieved from the web server
* local font: to be substituted with a local (client side) font
*/
void export_remote_font(const FontInfo & info, const std::string & suffix, const std::string & fontfileformat, GfxFont * font);
void export_remote_font(const FontInfo & info, const std::string & suffix, GfxFont * font);
void export_remote_default_font(long long fn_id);
void export_local_font(const FontInfo & info, GfxFont * font, const std::string & original_font_name, const std::string & cssfont);

View File

@ -18,40 +18,62 @@
namespace pdf2htmlEX {
void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, const string & fontfileformat, GfxFont * font)
{
f_css.fs << "@font-face{"
<< "font-family:f" << info.id << ";"
<< "src:url(";
void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, GfxFont * font)
{
auto fn = str_fmt("f%llx%s", info.id, suffix.c_str());
if(param->single_html)
{
auto path = param->tmp_dir + "/" + (char*)fn;
ifstream fin(path, ifstream::binary);
if(!fin)
throw "Cannot locate font file: " + path;
f_css.fs << "'data:font/" + fontfileformat + ";base64," << base64stream(fin) << "'";
string mime_type, format;
if (suffix == ".ttf") {
format = "truetype";
mime_type = "application/x-font-ttf";
}
else
{
f_css.fs << (char*)fn;
else if (suffix == ".otf") {
format = "opentype";
mime_type = "application/x-font-otf";
}
else if (suffix == ".woff") {
format = "woff";
mime_type = "application/font-woff";
}
else if (suffix == ".eot") {
format = "embedded-opentype";
mime_type = "application/vnd.ms-fontobject";
}
else if (suffix == ".svg") {
format = "svg";
mime_type = "image/svg+xml";
}
}
f_css.fs << ")"
<< "format(\"" << fontfileformat << "\");"
<< "}" // end of @font-face
<< ".f" << info.id << "{"
<< "font-family:f" << info.id << ";"
<< "line-height:" << round(info.ascent - info.descent) << ";"
<< "font-style:normal;"
<< "font-weight:normal;"
<< "visibility:visible;"
<< "}" // end of .f
<< endl;
}
f_css.fs << "@font-face{"
<< "font-family:f" << info.id << ";"
<< "src:url(";
{
auto fn = str_fmt("f%llx%s", info.id, suffix.c_str());
if(param->single_html)
{
auto path = param->tmp_dir + "/" + (char*)fn;
ifstream fin(path, ifstream::binary);
if(!fin)
throw "Cannot locate font file: " + path;
f_css.fs << "'data:font/" + mime_type + ";base64," << base64stream(fin) << "'";
}
else
{
f_css.fs << (char*)fn;
}
}
f_css.fs << ")"
<< "format(\"" << format << "\");"
<< "}" // end of @font-face
<< ".f" << info.id << "{"
<< "font-family:f" << info.id << ";"
<< "line-height:" << round(info.ascent - info.descent) << ";"
<< "font-style:normal;"
<< "font-weight:normal;"
<< "visibility:visible;"
<< "}" // end of .f
<< endl;
}
static string general_font_family(GfxFont * font)
{

View File

@ -110,7 +110,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, FontInfo & info)
if(path != "")
{
embed_font(path, font, info);
export_remote_font(info, param->font_suffix, param->font_format, font);
export_remote_font(info, param->font_suffix, font);
}
else
{
@ -129,7 +129,7 @@ void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, Font
if(localfontloc != nullptr)
{
embed_font(localfontloc->path->getCString(), font, info);
export_remote_font(info, param->font_suffix, param->font_format, font);
export_remote_font(info, param->font_suffix, font);
delete localfontloc;
return;
}
@ -186,7 +186,7 @@ void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info)
if(localfontloc != nullptr)
{
embed_font(string(localfontloc->path->getCString()), font, info);
export_remote_font(info, param->font_suffix, param->font_format, font);
export_remote_font(info, param->font_suffix, font);
delete localfontloc;
return;
}

View File

@ -34,7 +34,7 @@ struct Param
// fonts
int embed_base_font;
int embed_external_font;
std::string font_suffix, font_format;
std::string font_suffix;
int decompose_ligature;
int remove_unused_glyph;
int auto_hint;

View File

@ -53,6 +53,8 @@ void show_version_and_exit(const char * dummy = nullptr)
void parse_options (int argc, char **argv)
{
string deprecated_string;
argparser
// pages
.add("first-page,f", &param.first_page, 1, "first page to convert")
@ -77,7 +79,6 @@ void parse_options (int argc, char **argv)
.add("embed-base-font", &param.embed_base_font, 0, "embed local match for standard 14 fonts")
.add("embed-external-font", &param.embed_external_font, 0, "embed local match for external fonts")
.add("font-suffix", &param.font_suffix, ".ttf", "suffix for embedded font files (.ttf,.otf,.woff,.svg)")
.add("font-format", &param.font_format, "opentype", "CSS @font-face format for embedded fonts")
.add("decompose-ligature", &param.decompose_ligature, 0, "decompose ligatures, such as \uFB01 -> fi")
.add("remove-unused-glyph", &param.remove_unused_glyph, 1, "remove unused glyphs in embedded fonts")
.add("auto-hint", &param.auto_hint, 0, "use fontforge autohint on fonts without hints")
@ -111,6 +112,11 @@ void parse_options (int argc, char **argv)
.add("", &param.input_filename, "", "")
.add("", &param.output_filename, "", "")
// deprecated
.add("font-format", &deprecated_string, "", "", [] (const char*) {
cerr << "warning: --font-format is deprecated, @font-face format is inferred from --font-suffix" << endl;
})
;
try