1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 01:28:39 +00:00

Merge pull request #79 from jahewson/master

Automatically infer @font-face src format
This commit is contained in:
Lu Wang 2013-01-29 10:19:33 -08:00
commit d987294082
6 changed files with 57 additions and 24 deletions

View File

@ -102,8 +102,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,11 +18,38 @@
namespace pdf2htmlEX {
void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, const string & fontfileformat, GfxFont * font)
void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, GfxFont * font)
{
string mime_type, format;
if(suffix == ".ttf")
{
format = "truetype";
mime_type = "application/x-font-ttf";
}
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 << "@font-face{"
<< "font-family:f" << info.id << ";"
<< "src:url(";
<< "font-family:f" << info.id << ";"
<< "src:url(";
{
auto fn = str_fmt("f%llx%s", info.id, suffix.c_str());
@ -32,7 +59,7 @@ void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suff
ifstream fin(path, ifstream::binary);
if(!fin)
throw "Cannot locate font file: " + path;
f_css.fs << "'data:font/" + fontfileformat + ";base64," << base64stream(fin) << "'";
f_css.fs << "'data:font/" + mime_type + ";base64," << base64stream(fin) << "'";
}
else
{
@ -41,23 +68,23 @@ void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suff
}
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;
<< "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)
{
if(font -> isFixedWidth())
if(font->isFixedWidth())
return "monospace";
else if (font -> isSerif())
else if (font->isSerif())
return "serif";
else
return "sans-serif";

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

@ -54,6 +54,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")
@ -78,7 +80,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")
@ -112,6 +113,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