From ea4fb47f04e3789763fa4d1a18edd846429d45e3 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Thu, 19 Sep 2013 02:41:00 +0800 Subject: [PATCH] working on dumping type 3 glyphs --- src/HTMLRenderer/HTMLRenderer.h | 3 +- src/HTMLRenderer/font.cc | 92 ++++++++++++++++++++++++++++++++- src/HTMLRenderer/text.cc | 2 +- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/HTMLRenderer/HTMLRenderer.h b/src/HTMLRenderer/HTMLRenderer.h index 98d641e..317d6c8 100644 --- a/src/HTMLRenderer/HTMLRenderer.h +++ b/src/HTMLRenderer/HTMLRenderer.h @@ -166,7 +166,8 @@ protected: * local font: to be substituted with a local (client side) font */ //////////////////////////////////////////////////// - std::string dump_embedded_font (GfxFont * font, long long fn_id); + std::string dump_embedded_font(GfxFont * font, long long fn_id); + std::string dump_type3_font(GfxFont * font, long long fn_id); void embed_font(const std::string & filepath, GfxFont * font, FontInfo & info, bool get_metric_only = false); const FontInfo * install_font(GfxFont * font); void install_embedded_font(GfxFont * font, FontInfo & info); diff --git a/src/HTMLRenderer/font.cc b/src/HTMLRenderer/font.cc index b668e8e..c275e69 100644 --- a/src/HTMLRenderer/font.cc +++ b/src/HTMLRenderer/font.cc @@ -21,6 +21,8 @@ #include "HTMLRenderer.h" #include "Base64Stream.h" +#include "pdf2htmlEX-config.h" + #include "util/namespace.h" #include "util/math.h" #include "util/misc.h" @@ -29,6 +31,13 @@ #include "util/unicode.h" #include "util/css_const.h" +#if ENABLE_SVG +#include +#include +#include +#include "BackgroundRenderer/CairoOutputDev/CairoFontEngine.h" +#endif + namespace pdf2htmlEX { using std::min; @@ -166,6 +175,83 @@ string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id) return filepath; } +string HTMLRenderer::dump_type3_font (GfxFont * font, long long fn_id) +{ + assert(font->getFontType() == fontType3); + +#if ENABLE_SVG + FT_Library ft_lib; + FT_Init_FreeType(&ft_lib); + CairoFontEngine font_engine(ft_lib); + auto * cur_font = font_engine.getFont(font, cur_doc, true, xref); + auto used_map = preprocessor.get_code_map(hash_ref(font->getID())); + + double * font_bbox = font->getFontBBox(); + double glyph_width = font_bbox[2] - font_bbox[0]; + double glyph_height = font_bbox[3] - font_bbox[1]; + + glyph_width /= 10; + glyph_height /= 10; + + for(int i = 0; i < 256; ++i) + { + if(!used_map[i]) continue; + + cairo_glyph_t glyph; + glyph.index = cur_font->getGlyph(i, nullptr, 0); + glyph.x = 0; + glyph.y = glyph_height; + + cairo_surface_t * surface = nullptr; + { + auto fn = str_fmt("/tmp/pdf2htmlEX/f%x-%x.svg", fn_id, i); + surface = cairo_svg_surface_create((char*)fn, glyph_height, glyph_width); + } + cairo_svg_surface_restrict_to_version(surface, CAIRO_SVG_VERSION_1_2); + cairo_surface_set_fallback_resolution(surface, param.h_dpi, param.v_dpi); + cairo_t * cr = cairo_create(surface); + + // zoom the image to prevent CairoOutputDev from rounding/increasing thin borders + //cairo_matrix_t matrix; + /* + double * font_matrix = font->getFontMatrix(); + cairo_matrix_init(&matrix, font_matrix[0], font_matrix[1], font_matrix[2], font_matrix[3], font_matrix[4], font_matrix[5]); + cairo_set_font_matrix(cr, &matrix); + */ + + /* + cairo_matrix_init_identity(&matrix); +// cairo_matrix_scale(&matrix, 10, 10); + cairo_transform(cr, &matrix); + */ + + cairo_set_font_size(cr, 100); + + cairo_set_source_rgb(cr, 0., 0., 0.); + + cairo_set_font_face(cr, cur_font->getFontFace()); + cairo_show_glyphs(cr, &glyph, 1); + { + auto status = cairo_status(cr); + cairo_destroy(cr); + if(status) + throw string("Cairo error: ") + cairo_status_to_string(status); + } + cairo_surface_finish(surface); + { + auto status = cairo_surface_status(surface); + cairo_surface_destroy(surface); + surface = nullptr; + if(status) + throw string("Error in cairo: ") + cairo_status_to_string(status); + } + } + return ""; +#else + return ""; +#endif +} + void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo & info, bool get_metric_only) { if(param.debug) @@ -631,7 +717,11 @@ const FontInfo * HTMLRenderer::install_font(GfxFont * font) << endl; } - if(font->getType() == fontType3) { + if(font->getType() == fontType3) + { + //test + dump_type3_font(font, new_fn_id); + cerr << "Type 3 fonts are unsupported and will be rendered as Image" << endl; export_remote_default_font(new_fn_id); return &new_font_info; diff --git a/src/HTMLRenderer/text.cc b/src/HTMLRenderer/text.cc index 1626ce6..323b209 100644 --- a/src/HTMLRenderer/text.cc +++ b/src/HTMLRenderer/text.cc @@ -35,7 +35,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) // For type 3 fonts, due to the font matrix, still it's hard to show it on HTML if( (font == nullptr) || (font->getWMode()) - || (font->getType() == fontType3) +// || (font->getType() == fontType3) ) { return;