diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 1a906b8..a37f36b 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -45,6 +45,7 @@ HTMLRenderer::HTMLRenderer(const Param * param) ffw_init(param->debug); cur_mapping = new int32_t [0x10000]; cur_mapping2 = new char* [0x100]; + width_list = new int [0x10000]; } HTMLRenderer::~HTMLRenderer() @@ -53,6 +54,7 @@ HTMLRenderer::~HTMLRenderer() clean_tmp_files(); delete [] cur_mapping; delete [] cur_mapping2; + delete [] width_list; } static GBool annot_cb(Annot *, void *) { diff --git a/src/HTMLRenderer/text.cc b/src/HTMLRenderer/text.cc index 7ddc7e0..de91d8f 100644 --- a/src/HTMLRenderer/text.cc +++ b/src/HTMLRenderer/text.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,7 @@ namespace pdf2htmlEX { using std::unordered_set; using std::min; using std::all_of; +using std::round; string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id) { @@ -165,6 +167,7 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo int maxcode = 0; Gfx8BitFont * font_8bit = nullptr; + GfxCIDFont * font_cid = nullptr; string suffix = get_suffix(filepath); for(auto iter = suffix.begin(); iter != suffix.end(); ++iter) @@ -175,6 +178,13 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo const char * used_map = nullptr; + ffw_metric(&info.ascent, &info.descent, &info.em_size); + + if(param->debug) + { + cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl; + } + if(!get_metric_only) { used_map = font_preprocessor.get_code_map(hash_ref(font->getID())); @@ -250,6 +260,7 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo } else { + font_cid = dynamic_cast(font); maxcode = 0xffff; if(is_truetype_suffix(suffix)) @@ -278,6 +289,8 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo * - For 8bit nonTruetype fonts: * Try to calculate the correct Unicode value from the glyph names, unless param->always_apply_tounicode is set * + * + * Also fill in the width_list, and set widths accordingly */ @@ -286,18 +299,27 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo bool name_conflict_warned = false; auto ctu = font->getToUnicode(); - memset(cur_mapping, 0, 0x10000 * sizeof(int32_t)); + memset(cur_mapping, -1, 0x10000 * sizeof(*cur_mapping)); + memset(width_list, -1, 0x1000 * sizeof(*width_list)); if(code2GID) maxcode = min(maxcode, code2GID_len - 1); + bool is_truetype = is_truetype_suffix(suffix); int max_key = maxcode; + /* + * Traverse all possible codes + */ for(int i = 0; i <= maxcode; ++i) { if(!used_map[i]) continue; - if(is_truetype_suffix(suffix) && (font_8bit != nullptr) && (font_8bit->getCharName(i) == nullptr)) + /* + * Skip glyphs without names (only for non-ttf fonts) + */ + if(!is_truetype && (font_8bit != nullptr) + && (font_8bit->getCharName(i) == nullptr)) { continue; } @@ -338,9 +360,22 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo cerr << "Warning: encoding confliction detected in font: " << hex << info.id << dec << endl; } } + + if(font_8bit) + { + width_list[k] = (int)round(font_8bit->getWidth(i) * info.em_size); + } + else + { + char buf[2]; + buf[0] = (i >> 8) & 0xff; + buf[1] = (i & 0xff); + width_list[k] = (int)round(font_cid->getWidth(buf, 2) * info.em_size); + } } ffw_reencode_raw(cur_mapping, max_key + 1, 1); + ffw_set_widths(width_list, max_key + 1); if(ctu) ctu->decRefCnt(); @@ -357,21 +392,15 @@ void HTMLRenderer::embed_font(const string & filepath, GfxFont * font, FontInfo // TODO: see if we can get the values without save/load + /* auto fn = str_fmt("%s/f%llx_.ttf", param->tmp_dir.c_str(), info.id); add_tmp_file((char*)fn); ffw_save((char*)fn); ffw_close(); ffw_load_font((char*)fn); + */ } - - ffw_metric(&info.ascent, &info.descent); - - if(param->debug) - { - cerr << "Ascent: " << info.ascent << " Descent: " << info.descent << endl; - } - { auto fn = str_fmt("%s/f%llx%s", (param->single_html ? param->tmp_dir : param->dest_dir).c_str(), diff --git a/src/ffw.c b/src/ffw.c index 5e228c1..f372517 100644 --- a/src/ffw.c +++ b/src/ffw.c @@ -18,6 +18,11 @@ #include "ffw.h" +static inline int min(int a, int b) +{ + return (asf; + + DBounds bb; SplineFontFindBounds(sf, &bb); + struct pfminfo * info = &sf->pfminfo; + *em_size = sf->ascent + sf->descent; + /* //debug printf("bb %lf %lf\n", bb.maxy, bb.miny); @@ -267,3 +276,29 @@ void ffw_metric(double * ascent, double * descent) sf->changed = true; } +/* + * TODO:bitmap, reference have not been considered in this function + */ +void ffw_set_widths(int * width_list, int mapping_len) +{ + SplineFont * sf = cur_fv->sf; + + EncMap * map = cur_fv->map; + int i; + int imax = min(mapping_len, map->enccount); + for(i = 0; i < imax; ++i) + { + // TODO why need this + // when width_list[i] == -1, the code itself should be unused. + // but might be reference within ttf etc + if(width_list[i] == -1) continue; + + int j = map->map[i]; + if(j == -1) continue; + + SplineChar * sc = sf->glyphs[j]; + if(sc == NULL) continue; + + sc->width = width_list[i]; + } +} diff --git a/src/include/HTMLRenderer.h b/src/include/HTMLRenderer.h index bf50747..8d97b11 100644 --- a/src/include/HTMLRenderer.h +++ b/src/include/HTMLRenderer.h @@ -357,6 +357,7 @@ class HTMLRenderer : public OutputDev // for font reencoding int32_t * cur_mapping; char ** cur_mapping2; + int * width_list; FontPreprocessor font_preprocessor; // for string formatting diff --git a/src/include/ffw.h b/src/include/ffw.h index da01a26..4dd9248 100644 --- a/src/include/ffw.h +++ b/src/include/ffw.h @@ -31,7 +31,9 @@ void ffw_save(const char * filename); void ffw_close(void); // fix metrics and get them -void ffw_metric(double * ascent, double * descent); +void ffw_metric(double * ascent, double * descent, int * em_size); + +void ffw_set_widths(int * width_list, int mapping_len); #ifdef __cplusplus } diff --git a/src/include/util.h b/src/include/util.h index 18cd4b4..a8745dd 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -109,6 +109,7 @@ class FontInfo public: long long id; bool use_tounicode; + int em_size; double ascent, descent; bool has_space; // whether space is included in the font };