From 4a908db655e2ebf076b2fb629bce41c3ae3dbc55 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Tue, 11 Sep 2012 21:52:46 +0800 Subject: [PATCH] fixed: cannot parse arguments with spaces; add namespace pdf2htmlEX --- src/ArgParser.cc | 25 ++++-- src/BackgroundRenderer.cc | 2 + src/FontPreprocessor.cc | 4 + src/HTMLRenderer/general.cc | 11 ++- src/HTMLRenderer/install.cc | 4 +- src/include/ArgParser.h | 133 ++++++++++++++++------------- src/include/BackgroundRenderer.h | 5 +- src/include/FontPreprocessor.h | 3 + src/include/HTMLRenderer.h | 9 +- src/include/Param.h | 3 + src/include/ff.h | 3 + src/include/namespace.h | 2 + src/include/pdf2htmlEX-config.h.in | 3 + src/include/util.h | 31 ++++--- src/pdf2htmlEX.cc | 1 + src/util.cc | 3 + 16 files changed, 154 insertions(+), 88 deletions(-) diff --git a/src/ArgParser.cc b/src/ArgParser.cc index a61bf2b..81ff714 100644 --- a/src/ArgParser.cc +++ b/src/ArgParser.cc @@ -13,6 +13,8 @@ #include "ArgParser.h" +namespace pdf2htmlEX { + using std::ostream; using std::cerr; using std::endl; @@ -22,6 +24,16 @@ using std::unordered_map; using std::make_pair; using std::ostringstream; +void read_value(const char * arg, std::string * location) +{ + *location = std::string(arg); +} + +void dump_value(std::ostream & out, const std::string & v) +{ + out << '"' << v << '"'; +} + ArgParser::~ArgParser(void) { for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter) @@ -111,11 +123,10 @@ void ArgParser::parse(int argc, char ** argv) const } { - int i = optind; auto iter = optional_arg_entries.begin(); - while((i < argc) && (iter != optional_arg_entries.end())) + while((optind < argc) && (iter != optional_arg_entries.end())) { - (*(iter++))->parse(argv[i++]); + (*(iter++))->parse(argv[optind++]); } } } @@ -146,10 +157,6 @@ ArgParser::ArgEntryBase::ArgEntryBase(const char * name, const char * descriptio } } - -void dump_default_value(std::ostream & out, const std::string & v) -{ - out << '"' << v << '"'; -} - const int ArgParser::arg_col_width = 40; + +} // namespace pdf2htmlEX diff --git a/src/BackgroundRenderer.cc b/src/BackgroundRenderer.cc index 0ce5a8b..6956408 100644 --- a/src/BackgroundRenderer.cc +++ b/src/BackgroundRenderer.cc @@ -11,6 +11,8 @@ #include "BackgroundRenderer.h" #include "util.h" +using namespace pdf2htmlEX; + void BackgroundRenderer::drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, diff --git a/src/FontPreprocessor.cc b/src/FontPreprocessor.cc index 6c79f72..b5ffc1b 100644 --- a/src/FontPreprocessor.cc +++ b/src/FontPreprocessor.cc @@ -15,6 +15,8 @@ #include "FontPreprocessor.h" #include "util.h" +namespace pdf2htmlEX { + FontPreprocessor::FontPreprocessor(void) : cur_font_id(0) , cur_code_map(nullptr) @@ -59,3 +61,5 @@ const char * FontPreprocessor::get_code_map (long long font_id) const auto iter = code_maps.find(font_id); return (iter == code_maps.end()) ? nullptr : (iter->second); } + +} // namespace pdf2htmlEX diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 29ab895..5715876 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -20,7 +20,7 @@ using std::fixed; using std::flush; -static void dummy(void *, ErrorCategory, int pos, char *) +static void dummy(void *, enum ErrorCategory, int pos, char *) { } @@ -144,8 +144,8 @@ void HTMLRenderer::pre_process() html_fout << ifstream(str_fmt("%s/%s", PDF2HTMLEX_DATA_PATH.c_str(), NECK_HTML_FILENAME.c_str()), ifstream::binary).rdbuf(); } - html_fout << fixed << hex; - allcss_fout << fixed << hex; + fix_stream(html_fout); + fix_stream(allcss_fout); allcss_fout << ifstream(str_fmt("%s/%s", PDF2HTMLEX_DATA_PATH.c_str(), CSS_FILENAME.c_str()), ifstream::binary).rdbuf(); } @@ -241,6 +241,11 @@ void HTMLRenderer::process_single_html() out << ifstream(PDF2HTMLEX_DATA_PATH + "/" + TAIL_HTML_FILENAME, ifstream::binary).rdbuf(); } +void HTMLRenderer::fix_stream (std::ostream & out) +{ + out << fixed << hex; +} + void HTMLRenderer::add_tmp_file(const string & fn) { if(!param->clean_tmp) diff --git a/src/HTMLRenderer/install.cc b/src/HTMLRenderer/install.cc index 3301f43..c1011e8 100644 --- a/src/HTMLRenderer/install.cc +++ b/src/HTMLRenderer/install.cc @@ -250,8 +250,8 @@ long long HTMLRenderer::install_word_space(double word_space) long long HTMLRenderer::install_color(const GfxRGB * rgb) { const GfxRGB & c = *rgb; - auto iter = color_map.lower_bound(c); - if((iter != color_map.end()) && (c == (iter->first))) + auto iter = color_map.find(c); + if(iter != color_map.end()) return iter->second; long long new_color_id = color_map.size(); diff --git a/src/include/ArgParser.h b/src/include/ArgParser.h index 9d242fe..3bfff92 100644 --- a/src/include/ArgParser.h +++ b/src/include/ArgParser.h @@ -18,57 +18,81 @@ #define nullptr (NULL) #endif +namespace pdf2htmlEX { + +//helper +template +void read_value(const char * arg, T * location) +{ + std::istringstream sin(arg); + if((sin >> (*location)) && (sin.eof())) + return; + + throw std::string("Invalid argument: ") + arg; +} + +extern void read_value(const char * arg, std::string * location); + +template +void dump_value(std::ostream & out, const T & v) +{ + out << v; +} + +extern void dump_value(std::ostream & out, const std::string & v); + + class ArgParser { -public: - ~ArgParser(void); - - typedef void (*ArgParserCallBack) (const char * arg); - - /* - * optname: name of the argment, should be provided as --optname - * description: if description is "", the argument won't be shown in show_usage() - */ - - ArgParser & add(const char * optname, const char * description, ArgParserCallBack callback = nullptr); - - template - ArgParser & add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback = nullptr); - - void parse(int argc, char ** argv) const; - void show_usage(std::ostream & out) const; - -private: - class ArgEntryBase - { public: - ArgEntryBase(const char * name, const char * description, bool need_arg); - virtual ~ArgEntryBase() { } - char shortname; - std::string name; - std::string description; - bool need_arg; - virtual void parse (const char * arg) const = 0; - virtual void show_usage (std::ostream & out) const = 0; - }; + ~ArgParser(void); - template - class ArgEntry : public ArgEntryBase - { - public: - ArgEntry(const char * name, T * location, const Tv & deafult_value, ArgParserCallBack callback, const char * description); + typedef void (*ArgParserCallBack) (const char * arg); - virtual void parse (const char * arg) const; - virtual void show_usage (std::ostream & out) const; + /* + * optname: name of the argment, should be provided as --optname + * description: if description is "", the argument won't be shown in show_usage() + */ + + ArgParser & add(const char * optname, const char * description, ArgParserCallBack callback = nullptr); + + template + ArgParser & add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback = nullptr); + + void parse(int argc, char ** argv) const; + void show_usage(std::ostream & out) const; private: - T * location; - T default_value; - ArgParserCallBack callback; - }; + class ArgEntryBase + { + public: + ArgEntryBase(const char * name, const char * description, bool need_arg); + virtual ~ArgEntryBase() { } + char shortname; + std::string name; + std::string description; + bool need_arg; + virtual void parse (const char * arg) const = 0; + virtual void show_usage (std::ostream & out) const = 0; + }; - std::vector arg_entries, optional_arg_entries; - static const int arg_col_width; + template + class ArgEntry : public ArgEntryBase + { + public: + ArgEntry(const char * name, T * location, const Tv & deafult_value, ArgParserCallBack callback, const char * description); + + virtual void parse (const char * arg) const; + virtual void show_usage (std::ostream & out) const; + + private: + T * location; + T default_value; + ArgParserCallBack callback; + }; + + std::vector arg_entries, optional_arg_entries; + static const int arg_col_width; }; template @@ -86,9 +110,9 @@ ArgParser & ArgParser::add(const char * optname, T * location, const Tv & defaul template ArgParser::ArgEntry::ArgEntry(const char * name, T * location, const Tv & default_value, ArgParserCallBack callback, const char * description) : ArgEntryBase(name, description, (location != nullptr)) - , location(location) - , default_value(default_value) - , callback(callback) + , location(location) + , default_value(default_value) + , callback(callback) { if(need_arg) *location = T(default_value); @@ -102,24 +126,13 @@ void ArgParser::ArgEntry::parse(const char * arg) const if(!arg) throw std::string("Missing argument of option: --") + name; - std::istringstream sin(arg); - if(!(sin >> (*location))) - throw std::string("Cannot parse argment of option: --") + name; + read_value(arg, location); } if(callback) (*callback)(arg); } -// helper -template -void dump_default_value(std::ostream & out, const T & v) -{ - out << v; -} - -extern void dump_default_value(std::ostream & out, const std::string & v); - template void ArgParser::ArgEntry::show_usage(std::ostream & out) const { @@ -144,7 +157,7 @@ void ArgParser::ArgEntry::show_usage(std::ostream & out) const if(need_arg) { sout << " (="; - dump_default_value(sout, default_value); + dump_value(sout, default_value); sout << ")"; } @@ -157,4 +170,6 @@ void ArgParser::ArgEntry::show_usage(std::ostream & out) const out << " " << description << std::endl; } +} // namespace ArgParser + #endif //ARGPARSER_H__ diff --git a/src/include/BackgroundRenderer.h b/src/include/BackgroundRenderer.h index 335184f..39cbd07 100644 --- a/src/include/BackgroundRenderer.h +++ b/src/include/BackgroundRenderer.h @@ -12,6 +12,8 @@ #include +namespace pdf2htmlEX { + // Based on BackgroundRenderer from poppler class BackgroundRenderer : public SplashOutputDev { public: @@ -31,7 +33,6 @@ public: CharCode code, int nBytes, Unicode *u, int uLen); }; - - +} #endif //BACKGROUND_RENDERER_H__ diff --git a/src/include/FontPreprocessor.h b/src/include/FontPreprocessor.h index 96bc8e5..bfdf4fd 100644 --- a/src/include/FontPreprocessor.h +++ b/src/include/FontPreprocessor.h @@ -15,6 +15,8 @@ #include +namespace pdf2htmlEX { + class FontPreprocessor : public OutputDev { public: FontPreprocessor(void); @@ -38,5 +40,6 @@ protected: std::unordered_map code_maps; }; +} // namespace pdf2htmlEX #endif //FONTPREPROCESSOR_H__ diff --git a/src/include/HTMLRenderer.h b/src/include/HTMLRenderer.h index a451db9..59f66c6 100644 --- a/src/include/HTMLRenderer.h +++ b/src/include/HTMLRenderer.h @@ -53,6 +53,8 @@ * */ +namespace pdf2htmlEX { + class HTMLRenderer : public OutputDev { public: @@ -123,6 +125,9 @@ class HTMLRenderer : public OutputDev // misc //////////////////////////////////////////////////// + // set flags + void fix_stream (std::ostream & out); + void add_tmp_file (const std::string & fn); void clean_tmp_files (); std::string dump_embedded_font (GfxFont * font, long long fn_id); @@ -346,7 +351,7 @@ class HTMLRenderer : public OutputDev std::map transform_matrix_map; std::map letter_space_map; std::map word_space_map; - std::map color_map; + std::unordered_map color_map; std::map whitespace_map; std::map rise_map; @@ -363,4 +368,6 @@ class HTMLRenderer : public OutputDev static const std::string CSS_FILENAME; }; +} //namespace pdf2htmlEX + #endif /* HTMLRENDERER_H_ */ diff --git a/src/include/Param.h b/src/include/Param.h index 0359846..ab3bbb8 100644 --- a/src/include/Param.h +++ b/src/include/Param.h @@ -11,6 +11,8 @@ #include +namespace pdf2htmlEX { + struct Param { // PDF stuff @@ -45,5 +47,6 @@ struct Param int clean_tmp; }; +} // namespace pdf2htmlEX #endif //PARAM_h__ diff --git a/src/include/ff.h b/src/include/ff.h index df8eb33..f368ea6 100644 --- a/src/include/ff.h +++ b/src/include/ff.h @@ -13,11 +13,13 @@ #ifdef __cplusplus #include +namespace pdf2htmlEX { extern "C" { #else #include #endif + void ff_init(void); void ff_fin(void); void ff_load_font(const char * filename); @@ -35,4 +37,5 @@ void ff_set_descent(int d); #ifdef __cplusplus } +} #endif diff --git a/src/include/namespace.h b/src/include/namespace.h index a74f936..57b64da 100644 --- a/src/include/namespace.h +++ b/src/include/namespace.h @@ -19,5 +19,7 @@ using std::make_pair; using std::ifstream; using std::ofstream; +using namespace pdf2htmlEX; + #endif // NAMESPACE_H__ diff --git a/src/include/pdf2htmlEX-config.h.in b/src/include/pdf2htmlEX-config.h.in index 836d34c..7a7ef62 100644 --- a/src/include/pdf2htmlEX-config.h.in +++ b/src/include/pdf2htmlEX-config.h.in @@ -11,9 +11,12 @@ #include +namespace pdf2htmlEX { + static const std::string PDF2HTMLEX_VERSION = "@PDF2HTMLEX_VERSION@"; static const std::string PDF2HTMLEX_PREFIX = "@CMAKE_INSTALL_PREFIX@"; static const std::string PDF2HTMLEX_DATA_PATH = "@CMAKE_INSTALL_PREFIX@""/share/pdf2htmlEX"; +} // namespace pdf2htmlEX #endif //PDF2HTMLEX_CONFIG_H__ diff --git a/src/include/util.h b/src/include/util.h index 2cfae36..53342f2 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -24,6 +24,8 @@ #define nullptr (NULL) #endif +namespace pdf2htmlEX { + static const double EPS = 1e-6; extern const double id_matrix[6]; @@ -36,7 +38,7 @@ extern const std::map GB_ENCODED_FONT_NAME_MAP; namespace { template - void dummy(){ + void _(){ auto _1 = &mapUCS2; auto _2 = &mapUTF8; } @@ -78,19 +80,23 @@ Unicode check_unicode(Unicode * u, int len, CharCode code, GfxFont * font); void outputUnicodes(std::ostream & out, const Unicode * u, int uLen); -static inline bool operator < (const GfxRGB & rgb1, const GfxRGB & rgb2) +class GfxRGB_hash { - if(rgb1.r < rgb2.r) return true; - if(rgb1.r > rgb2.r) return false; - if(rgb1.g < rgb2.g) return true; - if(rgb1.g > rgb2.g) return false; - return (rgb1.b < rgb2.b); -} +public: + size_t operator () (const GfxRGB & rgb) const + { + return (colToByte(rgb.r) << 16) | (colToByte(rgb.g) << 8) | (colToByte(rgb.b)); + } +}; -static inline bool operator == (const GfxRGB & rgb1, const GfxRGB & rgb2) -{ - return ((rgb1.r == rgb2.r) && (rgb1.g == rgb2.g) && (rgb1.b == rgb1.b)); -} +class GfxRGB_equal +{ +public: + bool operator ()(const GfxRGB & rgb1, const GfxRGB & rgb2) const + { + return ((rgb1.r == rgb2.r) && (rgb1.g == rgb2.g) && (rgb1.b == rgb1.b)); + } +}; // we may need more info of a font in the future class FontInfo @@ -222,4 +228,5 @@ bool is_truetype_suffix(const std::string & suffix); std::string get_filename(const std::string & path); std::string get_suffix(const std::string & path); +} // namespace util #endif //UTIL_H__ diff --git a/src/pdf2htmlEX.cc b/src/pdf2htmlEX.cc index 3289bb7..4406893 100644 --- a/src/pdf2htmlEX.cc +++ b/src/pdf2htmlEX.cc @@ -25,6 +25,7 @@ #include "ArgParser.h" using namespace std; +using namespace pdf2htmlEX; Param param; ArgParser argparser; diff --git a/src/util.cc b/src/util.cc index 0b07c5b..5495557 100644 --- a/src/util.cc +++ b/src/util.cc @@ -26,6 +26,8 @@ using std::string; using std::map; using std::ostream; +namespace pdf2htmlEX { + const double id_matrix[6] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0}; const map BASE_14_FONT_CSS_FONT_MAP({ @@ -195,3 +197,4 @@ string get_suffix(const string & path) return fn.substr(idx); } +} // namespace pdf2htmlEX