diff --git a/src/HTMLRenderer.h b/src/HTMLRenderer.h index 8d26ca6..ffc38bd 100644 --- a/src/HTMLRenderer.h +++ b/src/HTMLRenderer.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -269,6 +270,9 @@ class HTMLRenderer : public OutputDev double line_ascent, line_height; std::stringstream line_buf; + // for font reencoding + int32_t * cur_mapping; + //////////////////////////////////////////////////// // styles & resources //////////////////////////////////////////////////// diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 9de3d4d..3bc60f1 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -28,11 +28,13 @@ HTMLRenderer::HTMLRenderer(const Param * param) ,tmp_dir(param->tmp_dir) { ff_init(); + cur_mapping = new int32_t [0x10000]; } HTMLRenderer::~HTMLRenderer() { clean_tmp_files(); + delete [] cur_mapping; } void HTMLRenderer::process(PDFDoc *doc) diff --git a/src/HTMLRenderer/text.cc b/src/HTMLRenderer/text.cc index 1ce27a9..4543b6b 100644 --- a/src/HTMLRenderer/text.cc +++ b/src/HTMLRenderer/text.cc @@ -209,6 +209,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo & else { // move the slot such that it's consistent with the encoding seen in PDF + // TODO: build encoding directly ofstream out(tmp_dir / (fn + "_.encoding")); add_tmp_file(fn+"_.encoding"); @@ -277,31 +278,24 @@ void HTMLRenderer::embed_font(const path & 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 * - * TODO: build Encoding directly, without read/write files */ - auto ctu = font->getToUnicode(); - int cnt = 0; { - ofstream map_fout(tmp_dir / (fn + ".encoding")); - add_tmp_file(fn+".encoding"); + auto ctu = font->getToUnicode(); + memset(cur_mapping, 0, maxcode * sizeof(int32_t)); for(int i = 0; i <= maxcode; ++i) { if((suffix != ".ttf") && (font_8bit != nullptr) && (font_8bit->getCharName(i) == nullptr)) + { continue; - - ++ cnt; - map_fout << format("0x%|1$X|") % ((code2GID && (i < code2GID_len))? code2GID[i] : i); + } Unicode u, *pu=&u; - if(info.use_tounicode) { - int n = 0; - if(ctu) - n = ctu->mapToUnicode(i, &pu); + int n = ctu ? (ctu->mapToUnicode(i, &pu)) : 0; u = check_unicode(pu, n, i, font); } else @@ -309,21 +303,14 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo & u = unicode_from_font(i, font); } - map_fout << format(" 0x%|1$X|") % u; - map_fout << format(" # 0x%|1$X|") % i; - - map_fout << endl; + cur_mapping[((code2GID && (i < code2GID_len))? code2GID[i] : i)] = u; } - } - if(cnt > 0) - { - ff_load_encoding((tmp_dir / (fn+".encoding")).c_str(), fn.c_str()); - ff_reencode(fn.c_str(), 1); - } + ff_reencode_raw(cur_mapping, maxcode, 1); - if(ctu) - ctu->decRefCnt(); + if(ctu) + ctu->decRefCnt(); + } } auto dest = ((param->single_html ? tmp_dir : dest_dir) / (fn+(param->font_suffix))); diff --git a/src/ff/ff.c b/src/ff/ff.c index f8f69a9..777fbe7 100644 --- a/src/ff/ff.c +++ b/src/ff/ff.c @@ -42,6 +42,13 @@ static int max(int a, int b) return (a>b) ? a : b; } +static void dummy(const char * format, ...) +{ + va_list al; + va_start(al, format); + va_end(al); +} + void ff_init(void) { InitSimpleStuff(); @@ -49,6 +56,9 @@ void ff_init(void) default_encoding=FindOrMakeEncoding("ISO8859-1"); if ( default_encoding==NULL ) default_encoding=&custom; /* In case iconv is broken */ + + //disable error output of Fontforge + ui_interface->logwarning = &dummy; } void ff_load_font(const char * filename) { @@ -91,6 +101,28 @@ void ff_reencode(const char * encname, int force) SFReplaceEncodingBDFProps(cur_font, cur_font->fv->map); } +void ff_reencode_raw(int32 * mapping, int mapping_len, int force) +{ + Encoding * enc = calloc(1, sizeof(Encoding)); + enc->only_1byte = enc->has_1byte = true; + enc->char_cnt = mapping_len; + enc->unicode = (int32_t*)malloc(mapping_len * sizeof(int32_t)); + memcpy(enc->unicode, mapping, mapping_len * sizeof(int32_t)); + enc->enc_name = strcopy(""); + + if(force) + { + SFForceEncoding(cur_font, cur_font->fv->map, enc); + } + else + { + EncMapFree(cur_font->fv->map); + cur_font->fv->map= EncMapFromEncoding(cur_font, enc); + } + + SFReplaceEncodingBDFProps(cur_font, cur_font->fv->map); +} + void ff_cidflatten(void) { printf("cid flatten\n"); diff --git a/src/ff/ff.h b/src/ff/ff.h index c43fc04..31df702 100644 --- a/src/ff/ff.h +++ b/src/ff/ff.h @@ -10,14 +10,19 @@ * 2012.09.03 */ + #ifdef __cplusplus +#include extern "C" { +#else +#include #endif void ff_init(void); void ff_load_font(const char * filename); void ff_load_encoding(const char * filename, const char * encname); void ff_reencode(const char * encname, int force); +void ff_reencode_raw(int32_t * mapping, int mapping_len, int force); void ff_cidflatten(void); void ff_save(const char * filename); void ff_close(void);