mirror of
https://github.com/pdf2htmlEX/pdf2htmlEX.git
synced 2024-07-04 17:18:40 +00:00
fixed: cannot parse arguments with spaces; add namespace pdf2htmlEX
This commit is contained in:
parent
64a765e90f
commit
4a908db655
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -18,57 +18,81 @@
|
|||
#define nullptr (NULL)
|
||||
#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
|
||||
{
|
||||
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:
|
||||
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 T, class Tv>
|
||||
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 <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:
|
||||
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<ArgEntryBase *> arg_entries, optional_arg_entries;
|
||||
static const int arg_col_width;
|
||||
template <class T, class Tv>
|
||||
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>
|
||||
|
@ -86,9 +110,9 @@ ArgParser & ArgParser::add(const char * optname, T * location, const Tv & defaul
|
|||
template<class T, class Tv>
|
||||
ArgParser::ArgEntry<T, Tv>::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<T, Tv>::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<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>
|
||||
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)
|
||||
{
|
||||
sout << " <arg> (=";
|
||||
dump_default_value(sout, default_value);
|
||||
dump_value(sout, default_value);
|
||||
sout << ")";
|
||||
}
|
||||
|
||||
|
@ -157,4 +170,6 @@ void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const
|
|||
out << " " << description << std::endl;
|
||||
}
|
||||
|
||||
} // namespace ArgParser
|
||||
|
||||
#endif //ARGPARSER_H__
|
||||
|
|
|
@ -12,6 +12,8 @@
|
|||
|
||||
#include <SplashOutputDev.h>
|
||||
|
||||
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__
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#include <OutputDev.h>
|
||||
|
||||
namespace pdf2htmlEX {
|
||||
|
||||
class FontPreprocessor : public OutputDev {
|
||||
public:
|
||||
FontPreprocessor(void);
|
||||
|
@ -38,5 +40,6 @@ protected:
|
|||
std::unordered_map<long long, char*> code_maps;
|
||||
};
|
||||
|
||||
} // namespace pdf2htmlEX
|
||||
|
||||
#endif //FONTPREPROCESSOR_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<TM, long long> transform_matrix_map;
|
||||
std::map<double, long long> letter_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> rise_map;
|
||||
|
||||
|
@ -363,4 +368,6 @@ class HTMLRenderer : public OutputDev
|
|||
static const std::string CSS_FILENAME;
|
||||
};
|
||||
|
||||
} //namespace pdf2htmlEX
|
||||
|
||||
#endif /* HTMLRENDERER_H_ */
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
namespace pdf2htmlEX {
|
||||
|
||||
struct Param
|
||||
{
|
||||
// PDF stuff
|
||||
|
@ -45,5 +47,6 @@ struct Param
|
|||
int clean_tmp;
|
||||
};
|
||||
|
||||
} // namespace pdf2htmlEX
|
||||
|
||||
#endif //PARAM_h__
|
||||
|
|
|
@ -13,11 +13,13 @@
|
|||
|
||||
#ifdef __cplusplus
|
||||
#include <cstdint>
|
||||
namespace pdf2htmlEX {
|
||||
extern "C" {
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#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
|
||||
|
|
|
@ -19,5 +19,7 @@ using std::make_pair;
|
|||
using std::ifstream;
|
||||
using std::ofstream;
|
||||
|
||||
using namespace pdf2htmlEX;
|
||||
|
||||
#endif // NAMESPACE_H__
|
||||
|
||||
|
|
|
@ -11,9 +11,12 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
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__
|
||||
|
|
|
@ -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<std::string, std::string> GB_ENCODED_FONT_NAME_MAP;
|
|||
namespace
|
||||
{
|
||||
template <class T>
|
||||
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__
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "ArgParser.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace pdf2htmlEX;
|
||||
|
||||
Param param;
|
||||
ArgParser argparser;
|
||||
|
|
|
@ -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<string, string> BASE_14_FONT_CSS_FONT_MAP({
|
||||
|
@ -195,3 +197,4 @@ string get_suffix(const string & path)
|
|||
return fn.substr(idx);
|
||||
}
|
||||
|
||||
} // namespace pdf2htmlEX
|
||||
|
|
Loading…
Reference in New Issue
Block a user