From 6fda07bec3fb2c111abf866372c61477831887bc Mon Sep 17 00:00:00 2001 From: John Hewson Date: Tue, 29 Jan 2013 12:07:51 +0000 Subject: [PATCH 1/3] automatically infer @font-face src format --- pdf2htmlEX.1.in | 4 +- src/HTMLRenderer/HTMLRenderer.h | 2 +- src/HTMLRenderer/export.cc | 82 +++++++++++++++++++++------------ src/HTMLRenderer/install.cc | 6 +-- src/Param.h | 2 +- src/pdf2htmlEX.cc | 8 +++- 6 files changed, 66 insertions(+), 38 deletions(-) diff --git a/pdf2htmlEX.1.in b/pdf2htmlEX.1.in index 630ddb6..57dc1ee 100644 --- a/pdf2htmlEX.1.in +++ b/pdf2htmlEX.1.in @@ -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 (Default: .ttf), --font-format (Default: truetype) -Specify the suffix and format of fonts extracted from the PDF file. They should be consistent. +.B --font-suffix (Default: .ttf) +Specify the suffix of fonts extracted from the PDF file. .TP .B --decompose-ligature <0|1> (Default: 0) diff --git a/src/HTMLRenderer/HTMLRenderer.h b/src/HTMLRenderer/HTMLRenderer.h index 532eedb..f94e013 100644 --- a/src/HTMLRenderer/HTMLRenderer.h +++ b/src/HTMLRenderer/HTMLRenderer.h @@ -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); diff --git a/src/HTMLRenderer/export.cc b/src/HTMLRenderer/export.cc index 148f741..a83709b 100644 --- a/src/HTMLRenderer/export.cc +++ b/src/HTMLRenderer/export.cc @@ -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) { diff --git a/src/HTMLRenderer/install.cc b/src/HTMLRenderer/install.cc index 4dac8d2..1ec80ca 100644 --- a/src/HTMLRenderer/install.cc +++ b/src/HTMLRenderer/install.cc @@ -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; } diff --git a/src/Param.h b/src/Param.h index fdabc8c..e8d6b90 100644 --- a/src/Param.h +++ b/src/Param.h @@ -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; diff --git a/src/pdf2htmlEX.cc b/src/pdf2htmlEX.cc index ba3eaec..20d0cee 100644 --- a/src/pdf2htmlEX.cc +++ b/src/pdf2htmlEX.cc @@ -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", ¶m.first_page, 1, "first page to convert") @@ -77,7 +79,6 @@ void parse_options (int argc, char **argv) .add("embed-base-font", ¶m.embed_base_font, 0, "embed local match for standard 14 fonts") .add("embed-external-font", ¶m.embed_external_font, 0, "embed local match for external fonts") .add("font-suffix", ¶m.font_suffix, ".ttf", "suffix for embedded font files (.ttf,.otf,.woff,.svg)") - .add("font-format", ¶m.font_format, "opentype", "CSS @font-face format for embedded fonts") .add("decompose-ligature", ¶m.decompose_ligature, 0, "decompose ligatures, such as \uFB01 -> fi") .add("remove-unused-glyph", ¶m.remove_unused_glyph, 1, "remove unused glyphs in embedded fonts") .add("auto-hint", ¶m.auto_hint, 0, "use fontforge autohint on fonts without hints") @@ -111,6 +112,11 @@ void parse_options (int argc, char **argv) .add("", ¶m.input_filename, "", "") .add("", ¶m.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 From b2d28e2490c3c032983454f7817584d1eae64c18 Mon Sep 17 00:00:00 2001 From: John Hewson Date: Tue, 29 Jan 2013 13:38:34 +0000 Subject: [PATCH 2/3] fixed formatting --- src/HTMLRenderer/export.cc | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/HTMLRenderer/export.cc b/src/HTMLRenderer/export.cc index a83709b..ff76fc6 100644 --- a/src/HTMLRenderer/export.cc +++ b/src/HTMLRenderer/export.cc @@ -43,8 +43,8 @@ namespace pdf2htmlEX { } 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()); @@ -63,23 +63,23 @@ namespace pdf2htmlEX { } 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; + << "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"; From b9763d10727c4b7e5c082fc55dc2e2903cdb7912 Mon Sep 17 00:00:00 2001 From: John Hewson Date: Tue, 29 Jan 2013 18:13:48 +0000 Subject: [PATCH 3/3] cosmetic changes --- src/HTMLRenderer/export.cc | 113 +++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/src/HTMLRenderer/export.cc b/src/HTMLRenderer/export.cc index ff76fc6..c9e1516 100644 --- a/src/HTMLRenderer/export.cc +++ b/src/HTMLRenderer/export.cc @@ -18,62 +18,67 @@ namespace pdf2htmlEX { - void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, GfxFont * font) +void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, GfxFont * font) +{ + string mime_type, format; + if(suffix == ".ttf") { - 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("; - - { - 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; + 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("; + + { + 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) {