From ff9b079a4fa9aea2f4148923b3a3d23afc6cbc8c Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Sat, 8 Sep 2012 00:38:41 +0800 Subject: [PATCH] git rid of boost::format --- README.md | 2 +- src/HTMLRenderer.h | 4 +++- src/HTMLRenderer/LineBuffer.cc | 11 ++++----- src/HTMLRenderer/export.cc | 42 +++++++++++++++------------------- src/HTMLRenderer/general.cc | 12 +++++----- src/HTMLRenderer/image.cc | 1 - src/HTMLRenderer/install.cc | 8 +++---- src/HTMLRenderer/namespace.h | 3 ++- src/HTMLRenderer/text.cc | 24 +++++++++---------- src/util.h | 29 +++++++++++++++++++++++ 10 files changed, 78 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 298eaef..d3c4e0f 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Special thanks to Arthur Titeica for the [AUR Package](https://aur.archlinux.org * CMake * compilers support C++11 * libpoppler with xpdf header >= 0.20.2 (compile with --enable-xpdf-headers) -* boost c++ library (format, program options, gil, filesystem, serialization, system(which is actually required by filesystem)) +* boost c++ library (program options, gil, filesystem, serialization, system(which is actually required by filesystem)) * fontforge (with header files) #### Compiling diff --git a/src/HTMLRenderer.h b/src/HTMLRenderer.h index 40002f9..0c7f41d 100644 --- a/src/HTMLRenderer.h +++ b/src/HTMLRenderer.h @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -336,6 +335,9 @@ class HTMLRenderer : public OutputDev char ** cur_mapping2; FontPreprocessor font_preprocessor; + // for string formatting + string_formatter str_fmt; + //////////////////////////////////////////////////// // styles & resources //////////////////////////////////////////////////// diff --git a/src/HTMLRenderer/LineBuffer.cc b/src/HTMLRenderer/LineBuffer.cc index 959be83..9e08d0b 100644 --- a/src/HTMLRenderer/LineBuffer.cc +++ b/src/HTMLRenderer/LineBuffer.cc @@ -75,11 +75,7 @@ void HTMLRenderer::LineBuffer::flush(void) // TODO: class for height ? ostream & out = renderer->html_fout; - out << format("
") - % x % y - % max_ascent - % tm_id - ; + out << "
"; auto cur_state_iter = states.begin(); auto cur_offset_iter = offsets.begin(); @@ -142,7 +138,7 @@ void HTMLRenderer::LineBuffer::flush(void) auto * p = stack.back(); double threshold = p->draw_font_size * (p->ascent - p->descent) * (renderer->param->space_threshold); - out << format("%2%") % wid % (target > (threshold - EPS) ? " " : ""); + out << "" << (target > (threshold - EPS) ? " " : "") << ""; dx = target - w; @@ -203,7 +199,8 @@ void HTMLRenderer::LineBuffer::State::begin (ostream & out, const State * prev_s out << ' '; } - out << format("%1%%|2$x|") % format_str[i] % ids[i]; + // out should has set hex + out << format_str[i] << ids[i]; } if(first) diff --git a/src/HTMLRenderer/export.cc b/src/HTMLRenderer/export.cc index d6e1b2e..067762a 100644 --- a/src/HTMLRenderer/export.cc +++ b/src/HTMLRenderer/export.cc @@ -7,8 +7,9 @@ * 2012.08.14 */ +#include + #include -#include #include "HTMLRenderer.h" #include "namespace.h" @@ -18,20 +19,21 @@ using boost::algorithm::ifind_first; void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suffix, const string & fontfileformat, GfxFont * font) { - allcss_fout << format("@font-face{font-family:f%|1$x|;src:url(") % info.id; + allcss_fout << "@font-face{font-family:f" << info.id << ";src:url("; - const std::string fn = (format("f%|1$x|") % info.id).str(); + const char * fn = str_fmt("f%llx%s", info.id, suffix.c_str()); + if(param->single_html) { - allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / (fn+suffix), ifstream::binary)) << "'"; + allcss_fout << "'data:font/" << fontfileformat << ";base64," << base64stream(ifstream(tmp_dir / fn, ifstream::binary)) << "'"; } else { - allcss_fout << (fn+suffix); + allcss_fout << fn; } - allcss_fout << format(")format(\"%1%\");}.f%|2$x|{font-family:f%|2$x|;line-height:%3%;}") % fontfileformat % info.id % (info.ascent - info.descent) << endl; + allcss_fout << ")format(\"" << fontfileformat << "\");}.f" << info.id << "{font-family:f" << info.id << ";line-height:" << (info.ascent - info.descent) << ";}" << endl; } static string general_font_family(GfxFont * font) @@ -47,12 +49,12 @@ static string general_font_family(GfxFont * font) // TODO: this function is called when some font is unable to process, may use the name there as a hint void HTMLRenderer::export_remote_default_font(long long fn_id) { - allcss_fout << format(".f%|1$x|{font-family:sans-serif;color:transparent;visibility:hidden;}")%fn_id << endl; + allcss_fout << ".f" << fn_id << "{font-family:sans-serif;color:transparent;visibility:hidden;}" << endl; } void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, const string & original_font_name, const string & cssfont) { - allcss_fout << format(".f%|1$x|{") % info.id; + allcss_fout << ".f" << info.id << "{"; allcss_fout << "font-family:" << ((cssfont == "") ? (original_font_name + "," + general_font_family(font)) : cssfont) << ";"; if(font->isBold() || ifind_first(original_font_name, "bold")) @@ -70,12 +72,12 @@ void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, cons void HTMLRenderer::export_font_size (long long fs_id, double font_size) { - allcss_fout << format(".s%|1$x|{font-size:%2%px;}") % fs_id % font_size << endl; + allcss_fout << ".s" << fs_id << "{font-size:" << font_size << "px;}" << endl; } void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm) { - allcss_fout << format(".t%|1$x|{") % tm_id; + allcss_fout << ".t" << tm_id << "{"; // always ignore tm[4] and tm[5] because // we have already shifted the origin @@ -91,43 +93,37 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm) << tm[3] << ','; allcss_fout << "0,0);"; - /* - if(prefix == "-moz-") - allcss_fout << format("%1%px,%2%px);") % tm[4] % -tm[5]; - else - allcss_fout << format("%1%,%2%);") % tm[4] % -tm[5]; - */ } allcss_fout << "}" << endl; } void HTMLRenderer::export_letter_space (long long ls_id, double letter_space) { - allcss_fout << format(".l%|1$x|{letter-spacing:%2%px;}") % ls_id % letter_space << endl; + allcss_fout << ".l" << ls_id << "{letter-spacing:" << letter_space << "px;}" << endl; } void HTMLRenderer::export_word_space (long long ws_id, double word_space) { - allcss_fout << format(".w%|1$x|{word-spacing:%2%px;}") % ws_id % word_space << endl; + allcss_fout << ".w" << ws_id << "{word-spacing:" << word_space << "px;}" << endl; } void HTMLRenderer::export_color (long long color_id, const GfxRGB * rgb) { - allcss_fout << format(".c%|1$x|{color:rgb(%2%,%3%,%4%);}") - % color_id % (int)colToByte(rgb->r) % (int)colToByte(rgb->g) % (int)colToByte(rgb->b) + allcss_fout << ".c" << color_id << "{color:rgb(" + << dec << (int)colToByte(rgb->r) << "," << (int)colToByte(rgb->g) << "," << (int)colToByte(rgb->b) << ");}" << hex << endl; } void HTMLRenderer::export_whitespace (long long ws_id, double ws_width) { if(ws_width > 0) - allcss_fout << format("._%|1$x|{display:inline-block;width:%2%px;}") % ws_id % ws_width << endl; + allcss_fout << "._" << ws_id << "{display:inline-block;width:" << ws_width << "px;}" << endl; else - allcss_fout << format("._%|1$x|{display:inline;margin-left:%2%px;}") % ws_id % ws_width << endl; + allcss_fout << "._" << ws_id << "{display:inline;margin-left:" << ws_width << "px;}" << endl; } void HTMLRenderer::export_rise (long long rise_id, double rise) { - allcss_fout << format(".r%|1$x|{top:%2%px;}") % rise_id % (-rise) << endl; + allcss_fout << ".r" << rise_id << "{top:" << (-rise) << "px;}" << endl; } diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 0f6c539..ce7d2c6 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -92,7 +92,7 @@ void HTMLRenderer::process(PDFDoc *doc) 0, true, false, false, nullptr, nullptr, &annot_cb, nullptr); - string fn = (format("p%|1$x|.png")%i).str(); + const char * fn = str_fmt("p%x.png", i); bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi, param->v_dpi); if(param->single_html) add_tmp_file(fn); @@ -137,8 +137,8 @@ void HTMLRenderer::pre_process() html_fout << ifstream(PDF2HTMLEX_DATA_PATH / NECK_HTML_FILENAME, ifstream::binary).rdbuf(); } - html_fout << fixed; - allcss_fout << fixed; + html_fout << fixed << hex; + allcss_fout << fixed << hex; allcss_fout << ifstream(PDF2HTMLEX_DATA_PATH / CSS_FILENAME, ifstream::binary).rdbuf(); } @@ -167,11 +167,11 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) assert((!line_opened) && "Open line in startPage detected!"); - html_fout << format("
single_html) { auto path = tmp_dir / fn; @@ -182,7 +182,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) html_fout << fn; } - html_fout << format(");background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;\">") % pageWidth % pageHeight; + html_fout << ");background-position:0 0;background-size:" << pageWidth << "px " << pageHeight << "px;background-repeat:no-repeat;\">"; draw_scale = 1.0; diff --git a/src/HTMLRenderer/image.cc b/src/HTMLRenderer/image.cc index 71a498d..22f905c 100644 --- a/src/HTMLRenderer/image.cc +++ b/src/HTMLRenderer/image.cc @@ -8,7 +8,6 @@ */ #if 0 -#include // for gil bug const int *int_p_NULL = nullptr; #include diff --git a/src/HTMLRenderer/install.cc b/src/HTMLRenderer/install.cc index 3e504ef..0474b4c 100644 --- a/src/HTMLRenderer/install.cc +++ b/src/HTMLRenderer/install.cc @@ -11,8 +11,6 @@ #include #include -#include - #include "Param.h" #include "HTMLRenderer.h" @@ -46,7 +44,7 @@ const FontInfo * HTMLRenderer::install_font(GfxFont * font) if(param->debug) { - cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << format("f%|1$x|")%new_fn_id << endl; + cerr << "Install font: (" << (font->getID()->num) << ' ' << (font->getID()->gen) << ") -> " << "f" << hex << new_fn_id << dec << endl; } if(font->getType() == fontType3) { @@ -120,7 +118,7 @@ void HTMLRenderer::install_base_font(GfxFont * font, GfxFontLoc * font_loc, Font } else { - cerr << format("Cannot embed base font: f%|1$x| %2%") % info.id % psname << endl; + cerr << "Cannot embed base font: f" << hex << info.id << dec << ' ' << psname << endl; // fallback to exporting by name } @@ -178,7 +176,7 @@ void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info) } else { - cerr << format("Cannot embed external font: f%|1$x| %2%") % info.id % fontname << endl; + cerr << "Cannot embed external font: f" << hex << info.id << dec << ' ' << fontname << endl; // fallback to exporting by name } } diff --git a/src/HTMLRenderer/namespace.h b/src/HTMLRenderer/namespace.h index 018cc94..9f578e1 100644 --- a/src/HTMLRenderer/namespace.h +++ b/src/HTMLRenderer/namespace.h @@ -9,6 +9,8 @@ #ifndef NAMESPACE_H__ #define NAMESPACE_H__ +using std::hex; +using std::dec; using std::string; using std::cout; using std::cerr; @@ -18,7 +20,6 @@ using std::make_pair; using boost::filesystem::ifstream; using boost::filesystem::ofstream; using boost::filesystem::path; -using boost::format; #endif // NAMESPACE_H__ diff --git a/src/HTMLRenderer/text.cc b/src/HTMLRenderer/text.cc index 3f14352..7f8d9db 100644 --- a/src/HTMLRenderer/text.cc +++ b/src/HTMLRenderer/text.cc @@ -11,7 +11,6 @@ #include #include -#include #include #include @@ -127,11 +126,11 @@ path HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id) obj.streamReset(); - string fn = (format("f%|1$x|")%fn_id).str(); + const char * fn = str_fmt("f%x%s", fn_id, suffix.c_str()); ofstream outf; - filepath = tmp_dir / (fn + suffix); + filepath = tmp_dir / fn; outf.open(filepath, ofstream::binary); - add_tmp_file(fn+suffix); + add_tmp_file(fn); char buf[1024]; int len; @@ -144,7 +143,7 @@ path HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id) } catch(int) { - cerr << format("Someting wrong when trying to dump font %|1$x|") % fn_id << endl; + cerr << "Someting wrong when trying to dump font " << hex << fn_id << dec << endl; } obj2.free(); @@ -163,8 +162,6 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo & string suffix = filepath.extension().string(); to_lower(suffix); - string fn = (format("f%|1$x|") % info.id).str(); - ff_load_font(filepath.c_str()); int * code2GID = nullptr; @@ -241,7 +238,7 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo & { name_conflict_warned = true; //TODO: may be resolved using advanced font properties? - cerr << "Warning: encoding confliction detected in font: " << fn << endl; + cerr << "Warning: encoding confliction detected in font: " << hex << info.id << dec << endl; } } } @@ -332,9 +329,10 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo & } } - auto dest = ((param->single_html ? tmp_dir : dest_dir) / (fn+(param->font_suffix))); + const char * fn = str_fmt("f%x%s", info.id, param->font_suffix.c_str()); + auto dest = ((param->single_html ? tmp_dir : dest_dir) / fn); if(param->single_html) - add_tmp_file(fn+(param->font_suffix)); + add_tmp_file(fn); /* * [Win|Typo|HHead][Ascent|Descent] @@ -344,9 +342,9 @@ void HTMLRenderer::embed_font(const path & filepath, GfxFont * font, FontInfo & // Generate an intermediate ttf font in order to retrieve the metrics // TODO: see if we can get the values without save/load - string tmp_fn = fn+"_.ttf"; - add_tmp_file(tmp_fn); - auto tmp_path = tmp_dir / tmp_fn; + fn = str_fmt("f%x_.ttf", info.id); + add_tmp_file(fn); + auto tmp_path = tmp_dir / fn; ff_save(tmp_path.c_str()); ff_close(); ff_load_font(tmp_path.c_str()); diff --git a/src/util.h b/src/util.h index b1ad834..aa1b554 100644 --- a/src/util.h +++ b/src/util.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -261,4 +262,32 @@ private: static inline ostream & operator << (ostream & out, base64stream & bf) { return bf.dumpto(out); } static inline ostream & operator << (ostream & out, base64stream && bf) { return bf.dumpto(out); } +class string_formatter +{ +public: + string_formatter() { buf.reserve(64); } + /* + * Important: + * there is only one buffer, so new strings will replace old ones + */ + const char * operator () (const char * format, ...) { + va_list vlist; + va_start(vlist, format); + int l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); + va_end(vlist); + if((l+1) > (int)buf.capacity()) + { + buf.reserve(l+1); + va_start(vlist, format); + l = vsnprintf(&buf.front(), buf.capacity(), format, vlist); + va_end(vlist); + } + if(l < 0) return nullptr; + assert(l < (int)buf.capacity()); + return &buf.front(); + } +private: + std::vector buf; +}; + #endif //UTIL_H__