1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-07 18:30:34 +00:00

fixed: cannot parse arguments with spaces; add namespace pdf2htmlEX

This commit is contained in:
Lu Wang 2012-09-11 21:52:46 +08:00
parent 64a765e90f
commit 4a908db655
16 changed files with 154 additions and 88 deletions

View File

@ -13,6 +13,8 @@
#include "ArgParser.h" #include "ArgParser.h"
namespace pdf2htmlEX {
using std::ostream; using std::ostream;
using std::cerr; using std::cerr;
using std::endl; using std::endl;
@ -22,6 +24,16 @@ using std::unordered_map;
using std::make_pair; using std::make_pair;
using std::ostringstream; 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) ArgParser::~ArgParser(void)
{ {
for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter) 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(); 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; const int ArgParser::arg_col_width = 40;
} // namespace pdf2htmlEX

View File

@ -11,6 +11,8 @@
#include "BackgroundRenderer.h" #include "BackgroundRenderer.h"
#include "util.h" #include "util.h"
using namespace pdf2htmlEX;
void BackgroundRenderer::drawChar(GfxState *state, double x, double y, void BackgroundRenderer::drawChar(GfxState *state, double x, double y,
double dx, double dy, double dx, double dy,
double originX, double originY, double originX, double originY,

View File

@ -15,6 +15,8 @@
#include "FontPreprocessor.h" #include "FontPreprocessor.h"
#include "util.h" #include "util.h"
namespace pdf2htmlEX {
FontPreprocessor::FontPreprocessor(void) FontPreprocessor::FontPreprocessor(void)
: cur_font_id(0) : cur_font_id(0)
, cur_code_map(nullptr) , 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); auto iter = code_maps.find(font_id);
return (iter == code_maps.end()) ? nullptr : (iter->second); return (iter == code_maps.end()) ? nullptr : (iter->second);
} }
} // namespace pdf2htmlEX

View File

@ -20,7 +20,7 @@
using std::fixed; using std::fixed;
using std::flush; 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 << ifstream(str_fmt("%s/%s", PDF2HTMLEX_DATA_PATH.c_str(), NECK_HTML_FILENAME.c_str()), ifstream::binary).rdbuf();
} }
html_fout << fixed << hex; fix_stream(html_fout);
allcss_fout << fixed << hex; fix_stream(allcss_fout);
allcss_fout << ifstream(str_fmt("%s/%s", PDF2HTMLEX_DATA_PATH.c_str(), CSS_FILENAME.c_str()), ifstream::binary).rdbuf(); 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(); 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) void HTMLRenderer::add_tmp_file(const string & fn)
{ {
if(!param->clean_tmp) if(!param->clean_tmp)

View File

@ -250,8 +250,8 @@ long long HTMLRenderer::install_word_space(double word_space)
long long HTMLRenderer::install_color(const GfxRGB * rgb) long long HTMLRenderer::install_color(const GfxRGB * rgb)
{ {
const GfxRGB & c = *rgb; const GfxRGB & c = *rgb;
auto iter = color_map.lower_bound(c); auto iter = color_map.find(c);
if((iter != color_map.end()) && (c == (iter->first))) if(iter != color_map.end())
return iter->second; return iter->second;
long long new_color_id = color_map.size(); long long new_color_id = color_map.size();

View File

@ -18,57 +18,81 @@
#define nullptr (NULL) #define nullptr (NULL)
#endif #endif
namespace pdf2htmlEX {
//helper
template<class T>
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<class T>
void dump_value(std::ostream & out, const T & v)
{
out << v;
}
extern void dump_value(std::ostream & out, const std::string & v);
class ArgParser 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 <class T, class Tv>
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: public:
ArgEntryBase(const char * name, const char * description, bool need_arg); ~ArgParser(void);
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;
};
template <class T, class Tv> typedef void (*ArgParserCallBack) (const char * arg);
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; * 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 <class T, class Tv>
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: private:
T * location; class ArgEntryBase
T default_value; {
ArgParserCallBack callback; 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<ArgEntryBase *> arg_entries, optional_arg_entries; template <class T, class Tv>
static const int arg_col_width; 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<ArgEntryBase *> arg_entries, optional_arg_entries;
static const int arg_col_width;
}; };
template<class T, class Tv> template<class T, class Tv>
@ -86,9 +110,9 @@ ArgParser & ArgParser::add(const char * optname, T * location, const Tv & defaul
template<class T, class Tv> template<class T, class Tv>
ArgParser::ArgEntry<T, Tv>::ArgEntry(const char * name, T * location, const Tv & default_value, ArgParserCallBack callback, const char * description) ArgParser::ArgEntry<T, Tv>::ArgEntry(const char * name, T * location, const Tv & default_value, ArgParserCallBack callback, const char * description)
: ArgEntryBase(name, description, (location != nullptr)) : ArgEntryBase(name, description, (location != nullptr))
, location(location) , location(location)
, default_value(default_value) , default_value(default_value)
, callback(callback) , callback(callback)
{ {
if(need_arg) if(need_arg)
*location = T(default_value); *location = T(default_value);
@ -102,24 +126,13 @@ void ArgParser::ArgEntry<T, Tv>::parse(const char * arg) const
if(!arg) if(!arg)
throw std::string("Missing argument of option: --") + name; throw std::string("Missing argument of option: --") + name;
std::istringstream sin(arg); read_value(arg, location);
if(!(sin >> (*location)))
throw std::string("Cannot parse argment of option: --") + name;
} }
if(callback) if(callback)
(*callback)(arg); (*callback)(arg);
} }
// helper
template<class T>
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<class T, class Tv> template<class T, class Tv>
void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const
{ {
@ -144,7 +157,7 @@ void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const
if(need_arg) if(need_arg)
{ {
sout << " <arg> (="; sout << " <arg> (=";
dump_default_value(sout, default_value); dump_value(sout, default_value);
sout << ")"; sout << ")";
} }
@ -157,4 +170,6 @@ void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const
out << " " << description << std::endl; out << " " << description << std::endl;
} }
} // namespace ArgParser
#endif //ARGPARSER_H__ #endif //ARGPARSER_H__

View File

@ -12,6 +12,8 @@
#include <SplashOutputDev.h> #include <SplashOutputDev.h>
namespace pdf2htmlEX {
// Based on BackgroundRenderer from poppler // Based on BackgroundRenderer from poppler
class BackgroundRenderer : public SplashOutputDev { class BackgroundRenderer : public SplashOutputDev {
public: public:
@ -31,7 +33,6 @@ public:
CharCode code, int nBytes, Unicode *u, int uLen); CharCode code, int nBytes, Unicode *u, int uLen);
}; };
}
#endif //BACKGROUND_RENDERER_H__ #endif //BACKGROUND_RENDERER_H__

View File

@ -15,6 +15,8 @@
#include <OutputDev.h> #include <OutputDev.h>
namespace pdf2htmlEX {
class FontPreprocessor : public OutputDev { class FontPreprocessor : public OutputDev {
public: public:
FontPreprocessor(void); FontPreprocessor(void);
@ -38,5 +40,6 @@ protected:
std::unordered_map<long long, char*> code_maps; std::unordered_map<long long, char*> code_maps;
}; };
} // namespace pdf2htmlEX
#endif //FONTPREPROCESSOR_H__ #endif //FONTPREPROCESSOR_H__

View File

@ -53,6 +53,8 @@
* *
*/ */
namespace pdf2htmlEX {
class HTMLRenderer : public OutputDev class HTMLRenderer : public OutputDev
{ {
public: public:
@ -123,6 +125,9 @@ class HTMLRenderer : public OutputDev
// misc // misc
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
// set flags
void fix_stream (std::ostream & out);
void add_tmp_file (const std::string & fn); void add_tmp_file (const std::string & fn);
void clean_tmp_files (); void clean_tmp_files ();
std::string dump_embedded_font (GfxFont * font, long long fn_id); std::string dump_embedded_font (GfxFont * font, long long fn_id);
@ -346,7 +351,7 @@ class HTMLRenderer : public OutputDev
std::map<TM, long long> transform_matrix_map; std::map<TM, long long> transform_matrix_map;
std::map<double, long long> letter_space_map; std::map<double, long long> letter_space_map;
std::map<double, long long> word_space_map; std::map<double, long long> word_space_map;
std::map<GfxRGB, long long> color_map; std::unordered_map<GfxRGB, long long, GfxRGB_hash, GfxRGB_equal> color_map;
std::map<double, long long> whitespace_map; std::map<double, long long> whitespace_map;
std::map<double, long long> rise_map; std::map<double, long long> rise_map;
@ -363,4 +368,6 @@ class HTMLRenderer : public OutputDev
static const std::string CSS_FILENAME; static const std::string CSS_FILENAME;
}; };
} //namespace pdf2htmlEX
#endif /* HTMLRENDERER_H_ */ #endif /* HTMLRENDERER_H_ */

View File

@ -11,6 +11,8 @@
#include <string> #include <string>
namespace pdf2htmlEX {
struct Param struct Param
{ {
// PDF stuff // PDF stuff
@ -45,5 +47,6 @@ struct Param
int clean_tmp; int clean_tmp;
}; };
} // namespace pdf2htmlEX
#endif //PARAM_h__ #endif //PARAM_h__

View File

@ -13,11 +13,13 @@
#ifdef __cplusplus #ifdef __cplusplus
#include <cstdint> #include <cstdint>
namespace pdf2htmlEX {
extern "C" { extern "C" {
#else #else
#include <stdint.h> #include <stdint.h>
#endif #endif
void ff_init(void); void ff_init(void);
void ff_fin(void); void ff_fin(void);
void ff_load_font(const char * filename); void ff_load_font(const char * filename);
@ -35,4 +37,5 @@ void ff_set_descent(int d);
#ifdef __cplusplus #ifdef __cplusplus
} }
}
#endif #endif

View File

@ -19,5 +19,7 @@ using std::make_pair;
using std::ifstream; using std::ifstream;
using std::ofstream; using std::ofstream;
using namespace pdf2htmlEX;
#endif // NAMESPACE_H__ #endif // NAMESPACE_H__

View File

@ -11,9 +11,12 @@
#include <string> #include <string>
namespace pdf2htmlEX {
static const std::string PDF2HTMLEX_VERSION = "@PDF2HTMLEX_VERSION@"; static const std::string PDF2HTMLEX_VERSION = "@PDF2HTMLEX_VERSION@";
static const std::string PDF2HTMLEX_PREFIX = "@CMAKE_INSTALL_PREFIX@"; static const std::string PDF2HTMLEX_PREFIX = "@CMAKE_INSTALL_PREFIX@";
static const std::string PDF2HTMLEX_DATA_PATH = "@CMAKE_INSTALL_PREFIX@""/share/pdf2htmlEX"; static const std::string PDF2HTMLEX_DATA_PATH = "@CMAKE_INSTALL_PREFIX@""/share/pdf2htmlEX";
} // namespace pdf2htmlEX
#endif //PDF2HTMLEX_CONFIG_H__ #endif //PDF2HTMLEX_CONFIG_H__

View File

@ -24,6 +24,8 @@
#define nullptr (NULL) #define nullptr (NULL)
#endif #endif
namespace pdf2htmlEX {
static const double EPS = 1e-6; static const double EPS = 1e-6;
extern const double id_matrix[6]; extern const double id_matrix[6];
@ -36,7 +38,7 @@ extern const std::map<std::string, std::string> GB_ENCODED_FONT_NAME_MAP;
namespace namespace
{ {
template <class T> template <class T>
void dummy(){ void _(){
auto _1 = &mapUCS2; auto _1 = &mapUCS2;
auto _2 = &mapUTF8; 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); 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; public:
if(rgb1.r > rgb2.r) return false; size_t operator () (const GfxRGB & rgb) const
if(rgb1.g < rgb2.g) return true; {
if(rgb1.g > rgb2.g) return false; return (colToByte(rgb.r) << 16) | (colToByte(rgb.g) << 8) | (colToByte(rgb.b));
return (rgb1.b < rgb2.b); }
} };
static inline bool operator == (const GfxRGB & rgb1, const GfxRGB & rgb2) class GfxRGB_equal
{ {
return ((rgb1.r == rgb2.r) && (rgb1.g == rgb2.g) && (rgb1.b == rgb1.b)); 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 // we may need more info of a font in the future
class FontInfo 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_filename(const std::string & path);
std::string get_suffix(const std::string & path); std::string get_suffix(const std::string & path);
} // namespace util
#endif //UTIL_H__ #endif //UTIL_H__

View File

@ -25,6 +25,7 @@
#include "ArgParser.h" #include "ArgParser.h"
using namespace std; using namespace std;
using namespace pdf2htmlEX;
Param param; Param param;
ArgParser argparser; ArgParser argparser;

View File

@ -26,6 +26,8 @@ using std::string;
using std::map; using std::map;
using std::ostream; using std::ostream;
namespace pdf2htmlEX {
const double id_matrix[6] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0}; const double id_matrix[6] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
const map<string, string> BASE_14_FONT_CSS_FONT_MAP({ const map<string, string> BASE_14_FONT_CSS_FONT_MAP({
@ -195,3 +197,4 @@ string get_suffix(const string & path)
return fn.substr(idx); return fn.substr(idx);
} }
} // namespace pdf2htmlEX