1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 13:00:08 +00:00

--fit-width/height

This commit is contained in:
Lu Wang 2012-09-27 00:17:56 +08:00
parent 1cda6dddbc
commit 6e03e6d12a
10 changed files with 93 additions and 25 deletions

View File

@ -47,8 +47,12 @@ Specify the first page to process
.B -l, --last-page <num> (Default: last page)
Specify the last page to process
.TP
.B --zoom <ratio> (Default: 1.0)
Specify the zoom ratio of the HTML file
.B --zoom <ratio>, --fit-width <width>, --fit-height <height>
--zoom specifies the zoom factor directly, --fit-width/height specifies the maximum width/height of a page.
If multiple values are specified, the minimum one will be used.
If none is specified, pages will be rendered as 72DPI.
.TP
.B --hpdi <dpi>, --vpdi <dpi> (Default: 144)
Specify the horizontal and vertical DPI for images

View File

@ -51,7 +51,7 @@ ArgParser::~ArgParser(void)
ArgParser & ArgParser::add(const char * optname, const char * description, ArgParserCallBack callback)
{
return add<char>(optname, nullptr, 0, description, callback);
return add<char>(optname, nullptr, 0, description, callback, true);
}
void ArgParser::parse(int argc, char ** argv) const

View File

@ -10,6 +10,8 @@
#include <cstdio>
#include <ostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <splash/SplashBitmap.h>
@ -25,6 +27,8 @@ using std::fixed;
using std::flush;
using std::ostream;
using std::max;
using std::min_element;
using std::vector;
static void dummy(void *, enum ErrorCategory, int pos, char *)
{
@ -48,12 +52,6 @@ HTMLRenderer::HTMLRenderer(const Param * param)
cur_mapping = new int32_t [0x10000];
cur_mapping2 = new char* [0x100];
width_list = new int [0x10000];
/*
* determine scale factors
*/
scale_factor1 = max(param->zoom, param->font_size_multiplier);
scale_factor2 = (param->zoom) / scale_factor1;
}
HTMLRenderer::~HTMLRenderer()
@ -77,6 +75,43 @@ void HTMLRenderer::process(PDFDoc *doc)
cerr << "Preprocessing: ";
preprocessor.process(doc);
/*
* determine scale factors
*/
{
double zoom = 1.0;
vector<double> zoom_factors;
if(abs(param->zoom) > EPS)
{
zoom_factors.push_back(param->zoom);
}
if(abs(param->fit_width) > EPS)
{
zoom_factors.push_back((param->fit_width) / preprocessor.get_max_width());
}
if(abs(param->fit_height) > EPS)
{
zoom_factors.push_back((param->fit_height) / preprocessor.get_max_height());
}
if(zoom_factors.empty())
{
zoom = 1.0;
}
else
{
zoom = *min_element(zoom_factors.begin(), zoom_factors.end());
}
scale_factor1 = max(zoom, param->font_size_multiplier);
scale_factor2 = zoom / scale_factor1;
}
cerr << "Working: ";
BackgroundRenderer * bg_renderer = nullptr;
if(param->process_nontext)
@ -120,7 +155,7 @@ void HTMLRenderer::process(PDFDoc *doc)
}
}
doc->displayPage(this, i, (param->zoom) * DEFAULT_DPI, (param->zoom) * DEFAULT_DPI,
doc->displayPage(this, i, zoom_factor() * DEFAULT_DPI, zoom_factor() * DEFAULT_DPI,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);

View File

@ -250,7 +250,7 @@ void HTMLRenderer::processLink(AnnotLink * al)
auto * border = al->getBorder();
if(border)
{
border_width = border->getWidth() * (param->zoom);
border_width = border->getWidth() * zoom_factor();
if(border_width > 0)
{
{

View File

@ -9,6 +9,7 @@
#include <cstring>
#include <iostream>
#include <algorithm>
#include <GfxState.h>
#include <GfxFont.h>
@ -21,10 +22,13 @@ namespace pdf2htmlEX {
using std::cerr;
using std::endl;
using std::flush;
using std::max;
Preprocessor::Preprocessor(const Param * param)
: OutputDev()
, param(param)
, max_width(0)
, max_height(0)
, cur_font_id(0)
, cur_code_map(nullptr)
{ }
@ -39,7 +43,7 @@ void Preprocessor::process(PDFDoc * doc)
{
for(int i = param->first_page; i <= param->last_page ; ++i)
{
doc->displayPage(this, i, param->h_dpi, param->v_dpi,
doc->displayPage(this, i, DEFAULT_DPI, DEFAULT_DPI,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
@ -76,6 +80,12 @@ void Preprocessor::drawChar(GfxState *state, double x, double y,
cur_code_map[code] = 1;
}
void Preprocessor::startPage(int pageNum, GfxState *state)
{
max_width = max(max_width, state->getPageWidth());
max_height = max(max_height, state->getPageHeight());
}
const char * Preprocessor::get_code_map (long long font_id) const
{
auto iter = code_maps.find(font_id);

View File

@ -55,7 +55,7 @@ class ArgParser
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);
ArgParser & add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback = nullptr, bool dont_show_default = false);
void parse(int argc, char ** argv) const;
void show_usage(std::ostream & out) const;
@ -78,7 +78,11 @@ class ArgParser
class ArgEntry : public ArgEntryBase
{
public:
ArgEntry(const char * name, T * location, const Tv & deafult_value, ArgParserCallBack callback, const char * description);
ArgEntry(const char * name,
T * location, const Tv & deafult_value,
ArgParserCallBack callback,
const char * description, bool dont_show_default);
virtual void parse (const char * arg) const;
virtual void show_usage (std::ostream & out) const;
@ -87,6 +91,7 @@ class ArgParser
T * location;
T default_value;
ArgParserCallBack callback;
bool dont_show_default;
};
std::vector<ArgEntryBase *> arg_entries, optional_arg_entries;
@ -94,23 +99,24 @@ class ArgParser
};
template<class T, class Tv>
ArgParser & ArgParser::add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback)
ArgParser & ArgParser::add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback, bool dont_show_default)
{
// use "" in case nullptr is provided
if((!optname) || (!optname[0]))
optional_arg_entries.push_back(new ArgEntry<T, Tv>("", location, default_value, callback, ""));
optional_arg_entries.push_back(new ArgEntry<T, Tv>("", location, default_value, callback, "", dont_show_default));
else
arg_entries.push_back(new ArgEntry<T, Tv>(optname, location, default_value, callback, description));
arg_entries.push_back(new ArgEntry<T, Tv>(optname, location, default_value, callback, description, dont_show_default));
return *this;
}
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, bool dont_show_default)
: ArgEntryBase(name, description, (location != nullptr))
, location(location)
, default_value(default_value)
, callback(callback)
, dont_show_default(dont_show_default)
{
if(need_arg)
*location = T(default_value);
@ -155,9 +161,13 @@ void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const
if(need_arg)
{
sout << " <arg> (=";
dump_value(sout, default_value);
sout << ")";
sout << " <arg>";
if(!dont_show_default)
{
sout << " (=";
dump_value(sout, default_value);
sout << ")";
}
}
std::string s = sout.str();

View File

@ -216,6 +216,7 @@ class HTMLRenderer : public OutputDev
* factor1 & factor 2 are determined according to zoom and font-size-multiplier
*
*/
double zoom_factor (void) const { return scale_factor1 * scale_factor2; }
double scale_factor1;
double scale_factor2;

View File

@ -26,6 +26,7 @@ struct Param
int first_page, last_page;
double zoom;
double fit_width, fit_height;
double h_dpi, v_dpi;
int process_nontext;

View File

@ -19,7 +19,6 @@
#include <OutputDev.h>
#include <PDFDoc.h>
#include <Annot.h>
#include "Param.h"
namespace pdf2htmlEX {
@ -41,11 +40,17 @@ public:
double originX, double originY,
CharCode code, int nBytes, Unicode *u, int uLen);
virtual void startPage(int pageNum, GfxState *state);
const char * get_code_map (long long font_id) const;
double get_max_width (void) const { return max_width; }
double get_max_height (void) const { return max_height; }
protected:
const Param * param;
double max_width, max_height;
long long cur_font_id;
char * cur_code_map;

View File

@ -50,8 +50,8 @@ void parse_options (int argc, char **argv)
.add("help,h", "show all options", &show_usage_and_exit)
.add("version,v", "show copyright and version info", &show_usage_and_exit)
.add("owner-password,o", &param.owner_password, "", "owner password (for encrypted files)")
.add("user-password,u", &param.user_password, "", "user password (for encrypted files)")
.add("owner-password,o", &param.owner_password, "", "owner password (for encrypted files)", nullptr, true)
.add("user-password,u", &param.user_password, "", "user password (for encrypted files)", nullptr, true)
.add("dest-dir", &param.dest_dir, ".", "specify destination directory")
.add("data-dir", &param.data_dir, PDF2HTMLEX_DATA_PATH, "specify data directory")
@ -59,7 +59,9 @@ void parse_options (int argc, char **argv)
.add("first-page,f", &param.first_page, 1, "first page to process")
.add("last-page,l", &param.last_page, numeric_limits<int>::max(), "last page to process")
.add("zoom", &param.zoom, 1.0, "zoom ratio")
.add("zoom", &param.zoom, 0, "zoom ratio", nullptr, true)
.add("fit-width", &param.fit_width, 0, "fit width", nullptr, true)
.add("fit-height", &param.fit_height, 0, "fit height", nullptr, true)
.add("hdpi", &param.h_dpi, 144.0, "horizontal DPI for non-text")
.add("vdpi", &param.v_dpi, 144.0, "vertical DPI for non-text")