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

working on util.h

This commit is contained in:
Lu Wang 2012-11-29 18:16:05 +08:00
parent ab28c44034
commit d179b50147
20 changed files with 293 additions and 227 deletions

View File

@ -171,7 +171,11 @@ add_executable(pdf2htmlEX
src/util/const.cc
src/util/ffw.h
src/util/ffw.c
src/util/math.h
src/util/math.cc
src/util/namespace.h
src/util/path.h
src/util/path.cc
src/util/string_formatter.h
src/util/TmpFiles.h
src/util/TmpFiles.cc

View File

@ -62,6 +62,50 @@
namespace pdf2htmlEX {
// we may need more info of a font in the future
class FontInfo
{
public:
long long id;
bool use_tounicode;
int em_size;
double ascent, descent;
};
class GfxRGB_hash
{
public:
size_t operator () (const GfxRGB & rgb) const
{
return (colToByte(rgb.r) << 16) | (colToByte(rgb.g) << 8) | (colToByte(rgb.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));
}
};
class Matrix_less
{
public:
bool operator () (const Matrix & m1, const Matrix & m2) const
{
// Note that we only care about the first 4 elements
for(int i = 0; i < 4; ++i)
{
if(m1.m[i] < m2.m[i] - EPS)
return true;
if(m1.m[i] > m2.m[i] + EPS)
return false;
}
return false;
}
};
class HTMLRenderer : public OutputDev
{
public:

View File

@ -81,8 +81,8 @@ void HTMLRenderer::LineBuffer::flush(void)
ostream & out = renderer->html_fout;
out << "<div style=\"left:"
<< _round(x) << "px;bottom:"
<< _round(y) << "px;"
<< round(x) << "px;bottom:"
<< round(y) << "px;"
<< "\""
<< " class=\"l t" << tm_id
<< " h" << renderer->install_height(max_ascent)

View File

@ -14,7 +14,7 @@
#include <iostream>
#include "HTMLRenderer.h"
#include "util/util.h"
#include "util/math.h"
#include "util/namespace.h"
namespace pdf2htmlEX {
@ -33,36 +33,36 @@ static bool is_horizontal_line(GfxSubpath * path)
{
return ((path->getNumPoints() == 2)
&& (!path->getCurve(1))
&& (_equal(path->getY(0), path->getY(1))));
&& (equal(path->getY(0), path->getY(1))));
}
static bool is_vertical_line(GfxSubpath * path)
{
return ((path->getNumPoints() == 2)
&& (!path->getCurve(1))
&& (_equal(path->getX(0), path->getX(1))));
&& (equal(path->getX(0), path->getX(1))));
}
static bool is_rectangle(GfxSubpath * path)
{
if (!(((path->getNumPoints() != 4) && (path->isClosed()))
|| ((path->getNumPoints() == 5)
&& _equal(path->getX(0), path->getX(4))
&& _equal(path->getY(0), path->getY(4)))))
&& equal(path->getX(0), path->getX(4))
&& equal(path->getY(0), path->getY(4)))))
return false;
for(int i = 1; i < path->getNumPoints(); ++i)
if(path->getCurve(i))
return false;
return (_equal(path->getY(0), path->getY(1))
&& _equal(path->getX(1), path->getX(2))
&& _equal(path->getY(2), path->getY(3))
&& _equal(path->getX(3), path->getX(0)))
|| (_equal(path->getX(0), path->getX(1))
&& _equal(path->getY(1), path->getY(2))
&& _equal(path->getX(2), path->getX(3))
&& _equal(path->getY(3), path->getY(0)));
return (equal(path->getY(0), path->getY(1))
&& equal(path->getX(1), path->getX(2))
&& equal(path->getY(2), path->getY(3))
&& equal(path->getX(3), path->getX(0)))
|| (equal(path->getX(0), path->getX(1))
&& equal(path->getY(1), path->getY(2))
&& equal(path->getX(2), path->getX(3))
&& equal(path->getY(3), path->getY(0)));
}
static void get_shading_bbox(GfxState * state, GfxShading * shading,
@ -105,7 +105,7 @@ static void get_shading_bbox(GfxState * state, GfxShading * shading,
*/
static double get_angle(double dx, double dy)
{
double r = _hypot(dx, dy);
double r = hypot(dx, dy);
/*
* acos always returns [0, pi]
@ -208,10 +208,10 @@ void LinearGradient::dumpto (ostream & out)
auto prefixes = {"", "-ms-", "-moz-", "-webkit-", "-o-"};
for(auto iter = prefixes.begin(); iter != prefixes.end(); ++iter)
{
out << "background-image:" << (*iter) << "linear-gradient(" << _round(angle) << "rad";
out << "background-image:" << (*iter) << "linear-gradient(" << round(angle) << "rad";
for(auto iter2 = stops.begin(); iter2 != stops.end(); ++iter2)
{
out << "," << (iter2->rgb) << " " << _round((iter2->pos) * 100) << "%";
out << "," << (iter2->rgb) << " " << round((iter2->pos) * 100) << "%";
}
out << ");";
}
@ -318,7 +318,7 @@ bool HTMLRenderer::css_do_path(GfxState *state, bool fill, bool test_only)
GfxRGB * ps = fill ? nullptr : (&stroke_color);
GfxRGB * pf = fill ? (&fill_color) : nullptr;
if(_equal(h, 0) || _equal(w, 0))
if(equal(h, 0) || equal(w, 0))
{
// orthogonal line
@ -351,7 +351,7 @@ void HTMLRenderer::css_draw_rectangle(double x, double y, double w, double h, co
double new_tm[6];
memcpy(new_tm, tm, sizeof(new_tm));
_tm_transform(new_tm, x, y);
tm_transform(new_tm, x, y);
double scale = 1.0;
{
@ -359,8 +359,8 @@ void HTMLRenderer::css_draw_rectangle(double x, double y, double w, double h, co
double i1 = (new_tm[0] + new_tm[2]) / sqrt2;
double i2 = (new_tm[1] + new_tm[3]) / sqrt2;
scale = _hypot(i1, i2);
if(_is_positive(scale))
scale = hypot(i1, i2);
if(is_positive(scale))
{
for(int i = 0; i < 4; ++i)
new_tm[i] /= scale;
@ -383,8 +383,8 @@ void HTMLRenderer::css_draw_rectangle(double x, double y, double w, double h, co
if(i > 0) html_fout << ' ';
double lw = line_width_array[i] * scale;
html_fout << _round(lw);
if(_is_positive(lw)) html_fout << "px";
html_fout << round(lw);
if(is_positive(lw)) html_fout << "px";
}
html_fout << ";";
}
@ -407,10 +407,10 @@ void HTMLRenderer::css_draw_rectangle(double x, double y, double w, double h, co
style_function(style_function_data, html_fout);
}
html_fout << "bottom:" << _round(y) << "px;"
<< "left:" << _round(x) << "px;"
<< "width:" << _round(w * scale) << "px;"
<< "height:" << _round(h * scale) << "px;";
html_fout << "bottom:" << round(y) << "px;"
<< "left:" << round(x) << "px;"
<< "width:" << round(w * scale) << "px;"
<< "height:" << round(h * scale) << "px;";
html_fout << "\"></div>";
}

View File

@ -13,6 +13,7 @@
#include "HTMLRenderer.h"
#include "util/namespace.h"
#include "util/base64.h"
#include "util/math.h"
namespace pdf2htmlEX {
@ -39,7 +40,7 @@ void HTMLRenderer::export_remote_font(const FontInfo & info, const string & suff
css_fout << ")format(\"" << fontfileformat
<< "\");}.f" << info.id
<< "{font-family:f" << info.id
<< ";line-height:" << _round(info.ascent - info.descent)
<< ";line-height:" << round(info.ascent - info.descent)
<< ";font-style:normal;font-weight:normal;}";
css_fout << endl;
@ -82,14 +83,14 @@ void HTMLRenderer::export_local_font(const FontInfo & info, GfxFont * font, cons
else
css_fout << "font-style:normal;";
css_fout << "line-height:" << _round(info.ascent - info.descent) << ";";
css_fout << "line-height:" << round(info.ascent - info.descent) << ";";
css_fout << "}" << endl;
}
void HTMLRenderer::export_font_size (long long fs_id, double font_size)
{
css_fout << ".s" << fs_id << "{font-size:" << _round(font_size) << "px;}" << endl;
css_fout << ".s" << fs_id << "{font-size:" << round(font_size) << "px;}" << endl;
}
void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
@ -100,7 +101,7 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
// we have already shifted the origin
// TODO: recognize common matices
if(_tm_equal(tm, ID_MATRIX, 4))
if(tm_equal(tm, ID_MATRIX, 4))
{
auto prefixes = {"", "-ms-", "-moz-", "-webkit-", "-o-"};
for(auto iter = prefixes.begin(); iter != prefixes.end(); ++iter)
@ -113,10 +114,10 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
{
// PDF use a different coordinate system from Web
css_fout << *iter << "transform:matrix("
<< _round(tm[0]) << ','
<< _round(-tm[1]) << ','
<< _round(-tm[2]) << ','
<< _round(tm[3]) << ',';
<< round(tm[0]) << ','
<< round(-tm[1]) << ','
<< round(-tm[2]) << ','
<< round(tm[3]) << ',';
css_fout << "0,0);";
}
@ -126,12 +127,12 @@ void HTMLRenderer::export_transform_matrix (long long tm_id, const double * tm)
void HTMLRenderer::export_letter_space (long long ls_id, double letter_space)
{
css_fout << ".l" << ls_id << "{letter-spacing:" << _round(letter_space) << "px;}" << endl;
css_fout << ".l" << ls_id << "{letter-spacing:" << round(letter_space) << "px;}" << endl;
}
void HTMLRenderer::export_word_space (long long ws_id, double word_space)
{
css_fout << ".w" << ws_id << "{word-spacing:" << _round(word_space) << "px;}" << endl;
css_fout << ".w" << ws_id << "{word-spacing:" << round(word_space) << "px;}" << endl;
}
void HTMLRenderer::export_color (long long color_id, const GfxRGB * rgb)
@ -142,19 +143,19 @@ void HTMLRenderer::export_color (long long color_id, const GfxRGB * rgb)
void HTMLRenderer::export_whitespace (long long ws_id, double ws_width)
{
if(ws_width > 0)
css_fout << "._" << ws_id << "{display:inline-block;width:" << _round(ws_width) << "px;}" << endl;
css_fout << "._" << ws_id << "{display:inline-block;width:" << round(ws_width) << "px;}" << endl;
else
css_fout << "._" << ws_id << "{display:inline;margin-left:" << _round(ws_width) << "px;}" << endl;
css_fout << "._" << ws_id << "{display:inline;margin-left:" << round(ws_width) << "px;}" << endl;
}
void HTMLRenderer::export_rise (long long rise_id, double rise)
{
css_fout << ".r" << rise_id << "{top:" << _round(-rise) << "px;}" << endl;
css_fout << ".r" << rise_id << "{top:" << round(-rise) << "px;}" << endl;
}
void HTMLRenderer::export_height (long long height_id, double height)
{
css_fout << ".h" << height_id << "{height:" << _round(height) << "px;}" << endl;
css_fout << ".h" << height_id << "{height:" << round(height) << "px;}" << endl;
}
}

View File

@ -19,6 +19,8 @@
#include "util/namespace.h"
#include "util/ffw.h"
#include "util/base64.h"
#include "util/math.h"
#include "util/path.h"
namespace pdf2htmlEX {
@ -211,7 +213,7 @@ void HTMLRenderer::endPage() {
for(int i = 0; i < 6; ++i)
{
if(i > 0) html_fout << ",";
html_fout << _round(default_ctm[i]);
html_fout << round(default_ctm[i]);
}
html_fout << "]";
@ -233,17 +235,17 @@ void HTMLRenderer::pre_process(PDFDoc * doc)
vector<double> zoom_factors;
if(_is_positive(param->zoom))
if(is_positive(param->zoom))
{
zoom_factors.push_back(param->zoom);
}
if(_is_positive(param->fit_width))
if(is_positive(param->fit_width))
{
zoom_factors.push_back((param->fit_width) / preprocessor.get_max_width());
}
if(_is_positive(param->fit_height))
if(is_positive(param->fit_height))
{
zoom_factors.push_back((param->fit_height) / preprocessor.get_max_height());
}

View File

@ -16,7 +16,7 @@
#include "Param.h"
#include "HTMLRenderer.h"
#include "util/namespace.h"
#include "util/util.h"
#include "util/math.h"
namespace pdf2htmlEX {
@ -204,7 +204,7 @@ void HTMLRenderer::install_external_font(GfxFont * font, FontInfo & info)
long long HTMLRenderer::install_font_size(double font_size)
{
auto iter = font_size_map.lower_bound(font_size - EPS);
if((iter != font_size_map.end()) && (_equal(iter->first, font_size)))
if((iter != font_size_map.end()) && (equal(iter->first, font_size)))
return iter->second;
long long new_fs_id = font_size_map.size();
@ -219,7 +219,7 @@ long long HTMLRenderer::install_transform_matrix(const double * tm)
memcpy(m.m, tm, sizeof(m.m));
auto iter = transform_matrix_map.lower_bound(m);
if((iter != transform_matrix_map.end()) && (_tm_equal(m.m, iter->first.m, 4)))
if((iter != transform_matrix_map.end()) && (tm_equal(m.m, iter->first.m, 4)))
return iter->second;
long long new_tm_id = transform_matrix_map.size();
@ -231,7 +231,7 @@ long long HTMLRenderer::install_transform_matrix(const double * tm)
long long HTMLRenderer::install_letter_space(double letter_space)
{
auto iter = letter_space_map.lower_bound(letter_space - EPS);
if((iter != letter_space_map.end()) && (_equal(iter->first, letter_space)))
if((iter != letter_space_map.end()) && (equal(iter->first, letter_space)))
return iter->second;
long long new_ls_id = letter_space_map.size();
@ -243,7 +243,7 @@ long long HTMLRenderer::install_letter_space(double letter_space)
long long HTMLRenderer::install_word_space(double word_space)
{
auto iter = word_space_map.lower_bound(word_space - EPS);
if((iter != word_space_map.end()) && (_equal(iter->first, word_space)))
if((iter != word_space_map.end()) && (equal(iter->first, word_space)))
return iter->second;
long long new_ws_id = word_space_map.size();

View File

@ -15,6 +15,7 @@
#include "HTMLRenderer.h"
#include "util/namespace.h"
#include "util/math.h"
namespace pdf2htmlEX {
@ -211,9 +212,9 @@ void HTMLRenderer::processLink(AnnotLink * al)
border_top_bottom_width, border_left_right_width);
if(abs(border_top_bottom_width - border_left_right_width) < EPS)
html_fout << "border-width:" << _round(border_top_bottom_width) << "px;";
html_fout << "border-width:" << round(border_top_bottom_width) << "px;";
else
html_fout << "border-width:" << _round(border_top_bottom_width) << "px " << _round(border_left_right_width) << "px;";
html_fout << "border-width:" << round(border_top_bottom_width) << "px " << round(border_left_right_width) << "px;";
}
auto style = border->getStyle();
switch(style)
@ -267,13 +268,13 @@ void HTMLRenderer::processLink(AnnotLink * al)
html_fout << "border-style:none;";
}
_tm_transform(default_ctm, x, y);
tm_transform(default_ctm, x, y);
html_fout << "position:absolute;"
<< "left:" << _round(x) << "px;"
<< "bottom:" << _round(y) << "px;"
<< "width:" << _round(w) << "px;"
<< "height:" << _round(h) << "px;";
<< "left:" << round(x) << "px;"
<< "bottom:" << round(y) << "px;"
<< "width:" << round(w) << "px;"
<< "height:" << round(h) << "px;";
// fix for IE
html_fout << "background-color:rgba(255,255,255,0.000001);";

View File

@ -17,7 +17,7 @@
#include "HTMLRenderer.h"
#include "util/namespace.h"
#include "util/util.h"
#include "util/math.h"
namespace pdf2htmlEX {
@ -104,7 +104,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
}
double new_font_size = state->getFontSize();
if(!_equal(cur_font_size, new_font_size))
if(!equal(cur_font_size, new_font_size))
{
need_rescale_font = true;
cur_font_size = new_font_size;
@ -132,7 +132,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
new_ctm[5] = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
//new_ctm[4] = new_ctm[5] = 0;
if(!_tm_equal(new_ctm, cur_text_tm))
if(!tm_equal(new_ctm, cur_text_tm))
{
need_recheck_position = true;
need_rescale_font = true;
@ -147,10 +147,10 @@ void HTMLRenderer::check_state_change(GfxState * state)
double new_draw_text_tm[6];
memcpy(new_draw_text_tm, cur_text_tm, sizeof(new_draw_text_tm));
double new_draw_text_scale = 1.0/text_scale_factor2 * _hypot(new_draw_text_tm[2], new_draw_text_tm[3]);
double new_draw_text_scale = 1.0/text_scale_factor2 * hypot(new_draw_text_tm[2], new_draw_text_tm[3]);
double new_draw_font_size = cur_font_size;
if(_is_positive(new_draw_text_scale))
if(is_positive(new_draw_text_scale))
{
new_draw_font_size *= new_draw_text_scale;
for(int i = 0; i < 4; ++i)
@ -161,19 +161,19 @@ void HTMLRenderer::check_state_change(GfxState * state)
new_draw_text_scale = 1.0;
}
if(!(_equal(new_draw_text_scale, draw_text_scale)))
if(!(equal(new_draw_text_scale, draw_text_scale)))
{
draw_text_scale_changed = true;
draw_text_scale = new_draw_text_scale;
}
if(!(_equal(new_draw_font_size, draw_font_size)))
if(!(equal(new_draw_font_size, draw_font_size)))
{
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
draw_font_size = new_draw_font_size;
cur_fs_id = install_font_size(draw_font_size);
}
if(!(_tm_equal(new_draw_text_tm, draw_text_tm, 4)))
if(!(tm_equal(new_draw_text_tm, draw_text_tm, 4)))
{
new_line_state = max<NewLineState>(new_line_state, NLS_DIV);
memcpy(draw_text_tm, new_draw_text_tm, sizeof(draw_text_tm));
@ -199,21 +199,21 @@ void HTMLRenderer::check_state_change(GfxState * state)
*/
bool merged = false;
if(_tm_equal(old_ctm, cur_text_tm, 4))
if(tm_equal(old_ctm, cur_text_tm, 4))
{
double dy = cur_ty - draw_ty;
double tdx = old_ctm[4] - cur_text_tm[4] - cur_text_tm[2] * dy;
double tdy = old_ctm[5] - cur_text_tm[5] - cur_text_tm[3] * dy;
if(_equal(cur_text_tm[0] * tdy, cur_text_tm[1] * tdx))
if(equal(cur_text_tm[0] * tdy, cur_text_tm[1] * tdx))
{
if(_is_positive(cur_text_tm[0]))
if(is_positive(cur_text_tm[0]))
{
draw_tx += tdx / cur_text_tm[0];
draw_ty += dy;
merged = true;
}
else if (_is_positive(cur_text_tm[1]))
else if (is_positive(cur_text_tm[1]))
{
draw_tx += tdy / cur_text_tm[1];
draw_ty += dy;
@ -221,7 +221,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
}
else
{
if((_equal(tdx,0)) && (_equal(tdy,0)))
if((equal(tdx,0)) && (equal(tdy,0)))
{
// free
draw_tx = cur_tx;
@ -246,7 +246,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
if(all_changed || letter_space_changed || draw_text_scale_changed)
{
double new_letter_space = state->getCharSpace();
if(!_equal(cur_letter_space, new_letter_space))
if(!equal(cur_letter_space, new_letter_space))
{
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
cur_letter_space = new_letter_space;
@ -259,7 +259,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
if(all_changed || word_space_changed || draw_text_scale_changed)
{
double new_word_space = state->getWordSpace();
if(!_equal(cur_word_space, new_word_space))
if(!equal(cur_word_space, new_word_space))
{
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
cur_word_space = new_word_space;
@ -294,7 +294,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
if(all_changed || rise_changed || draw_text_scale_changed)
{
double new_rise = state->getRise();
if(!_equal(cur_rise, new_rise))
if(!equal(cur_rise, new_rise))
{
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
cur_rise = new_rise;

View File

@ -19,6 +19,8 @@
#include "util/ffw.h"
#include "util/namespace.h"
#include "util/unicode.h"
#include "util/path.h"
#include "util/math.h"
namespace pdf2htmlEX {
@ -542,7 +544,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
while (len > 0) {
auto n = font->getNextChar(p, len, &code, &u, &uLen, &dx1, &dy1, &ox, &oy);
if(!(_equal(ox, 0) && _equal(oy, 0)))
if(!(equal(ox, 0) && equal(oy, 0)))
{
cerr << "TODO: non-zero origins" << endl;
}

View File

@ -23,6 +23,7 @@
#include "pdf2htmlEX-config.h"
#include "HTMLRenderer/HTMLRenderer.h"
#include "util/ArgParser.h"
#include "util/path.h"
using namespace std;
using namespace pdf2htmlEX;

View File

@ -1,3 +1,7 @@
#include "base64.h"
namespace pdf2htmlEX {
const char * base64stream::base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}

View File

@ -10,6 +10,8 @@
#include <iostream>
namespace pdf2htmlEX {
class base64stream
{
public:
@ -58,5 +60,5 @@ private:
static inline std::ostream & operator << (std::ostream & out, base64stream & bf) { return bf.dumpto(out); }
static inline std::ostream & operator << (std::ostream & out, base64stream && bf) { return bf.dumpto(out); }
} //namespace pdf2htmlEX
#endif //BASE64_H__

32
src/util/math.cc Normal file
View File

@ -0,0 +1,32 @@
#include <cstring>
#include "math.h"
namespace pdf2htmlEX {
void tm_transform(const double * tm, double & x, double & y, bool is_delta)
{
double xx = x, yy = y;
x = tm[0] * xx + tm[2] * yy;
y = tm[1] * xx + tm[3] * yy;
if(!is_delta)
{
x += tm[4];
y += tm[5];
}
}
void tm_multiply(double * tm_left, const double * tm_right)
{
double old[4];
memcpy(old, tm_left, sizeof(old));
tm_left[0] = old[0] * tm_right[0] + old[2] * tm_right[1];
tm_left[1] = old[1] * tm_right[0] + old[3] * tm_right[1];
tm_left[2] = old[0] * tm_right[2] + old[2] * tm_right[3];
tm_left[3] = old[1] * tm_right[2] + old[3] * tm_right[3];
tm_left[4] += old[0] * tm_right[4] + old[2] * tm_right[5];
tm_left[5] += old[1] * tm_right[4] + old[3] * tm_right[5];
}
} //namespace pdf2htmlEX

33
src/util/math.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Math functions
*
* by WangLu
* 2012.11.29
*/
#ifndef MATH_H__
#define MATH_H__
#include <cmath>
#include "const.h"
namespace pdf2htmlEX {
static inline double round(double x) { return (std::fabs(x) > EPS) ? x : 0.0; }
static inline bool equal(double x, double y) { return std::abs(x-y) < EPS; }
static inline bool is_positive(double x) { return x > EPS; }
static inline bool tm_equal(const double * tm1, const double * tm2, int size = 6)
{
for(int i = 0; i < size; ++i)
if(!equal(tm1[i], tm2[i]))
return false;
return true;
}
static inline double hypot(double x, double y) { return std::sqrt(x*x+y*y); }
void tm_transform(const double * tm, double & x, double & y, bool is_delta = false);
void tm_multiply(double * tm_left, const double * tm_right);
} //namespace pdf2htmlEX
#endif //MATH_H__

65
src/util/path.cc Normal file
View File

@ -0,0 +1,65 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "path.h"
using std::string;
namespace pdf2htmlEX {
void create_directories(string path)
{
if(path.empty()) return;
size_t idx = path.rfind('/');
if(idx != string::npos)
{
create_directories(path.substr(0, idx));
}
int r = mkdir(path.c_str(), S_IRWXU);
if(r != 0)
{
if(errno == EEXIST)
{
struct stat stat_buf;
if((stat(path.c_str(), &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
return;
}
throw string("Cannot create directory: ") + path;
}
}
bool is_truetype_suffix(const string & suffix)
{
return (suffix == ".ttf") || (suffix == ".ttc") || (suffix == ".otf");
}
string get_filename (const string & path)
{
size_t idx = path.rfind('/');
if(idx == string::npos)
return path;
else if (idx == path.size() - 1)
return "";
return path.substr(idx + 1);
}
string get_suffix(const string & path)
{
string fn = get_filename(path);
size_t idx = fn.rfind('.');
if(idx == string::npos)
return "";
else
{
string s = fn.substr(idx);
for(auto iter = s.begin(); iter != s.end(); ++iter)
*iter = tolower(*iter);
return s;
}
}
} //namespace pdf2htmlEX

23
src/util/path.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Function handling filenames and paths
*
* by WangLu
* 2012.11.29
*/
#ifndef PATH_H__
#define PATH_H__
#include <string>
namespace pdf2htmlEX {
void create_directories(std::string path);
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 pdf2htmlEX
#endif //PATH_H__

View File

@ -8,6 +8,8 @@
#ifndef STRING_FORMATTER_H__
#define STRING_FORMATTER_H__
namespace pdf2htmlEX {
class string_formatter
{
public:
@ -50,4 +52,5 @@ private:
int buf_cnt;
};
} //namespace pdf2htmlEX
#endif //STRING_FORMATTER_H__

View File

@ -15,10 +15,6 @@
#include <GlobalParams.h>
#include <Object.h>
// for mkdir
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
using std::cerr;
@ -29,85 +25,6 @@ using std::ostream;
namespace pdf2htmlEX {
void _tm_transform(const double * tm, double & x, double & y, bool is_delta)
{
double xx = x, yy = y;
x = tm[0] * xx + tm[2] * yy;
y = tm[1] * xx + tm[3] * yy;
if(!is_delta)
{
x += tm[4];
y += tm[5];
}
}
void _tm_multiply(double * tm_left, const double * tm_right)
{
double old[4];
memcpy(old, tm_left, sizeof(old));
tm_left[0] = old[0] * tm_right[0] + old[2] * tm_right[1];
tm_left[1] = old[1] * tm_right[0] + old[3] * tm_right[1];
tm_left[2] = old[0] * tm_right[2] + old[2] * tm_right[3];
tm_left[3] = old[1] * tm_right[2] + old[3] * tm_right[3];
tm_left[4] += old[0] * tm_right[4] + old[2] * tm_right[5];
tm_left[5] += old[1] * tm_right[4] + old[3] * tm_right[5];
}
void create_directories(string path)
{
if(path.empty()) return;
size_t idx = path.rfind('/');
if(idx != string::npos)
{
create_directories(path.substr(0, idx));
}
int r = mkdir(path.c_str(), S_IRWXU);
if(r != 0)
{
if(errno == EEXIST)
{
struct stat stat_buf;
if((stat(path.c_str(), &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
return;
}
throw string("Cannot create directory: ") + path;
}
}
bool is_truetype_suffix(const string & suffix)
{
return (suffix == ".ttf") || (suffix == ".ttc") || (suffix == ".otf");
}
string get_filename (const string & path)
{
size_t idx = path.rfind('/');
if(idx == string::npos)
return path;
else if (idx == path.size() - 1)
return "";
return path.substr(idx + 1);
}
string get_suffix(const string & path)
{
string fn = get_filename(path);
size_t idx = fn.rfind('.');
if(idx == string::npos)
return "";
else
{
string s = fn.substr(idx);
for(auto iter = s.begin(); iter != s.end(); ++iter)
*iter = tolower(*iter);
return s;
}
}
void css_fix_rectangle_border_width(double x1, double y1,
double x2, double y2,
double border_width,

View File

@ -17,8 +17,6 @@
#include <string>
#include <map>
#include <GfxState.h>
#include "const.h"
#ifndef nullptr
@ -27,77 +25,11 @@
namespace pdf2htmlEX {
static inline double _round(double x) { return (std::abs(x) > EPS) ? x : 0.0; }
static inline bool _equal(double x, double y) { return std::abs(x-y) < EPS; }
static inline bool _is_positive(double x) { return x > EPS; }
static inline bool _tm_equal(const double * tm1, const double * tm2, int size = 6)
{
for(int i = 0; i < size; ++i)
if(!_equal(tm1[i], tm2[i]))
return false;
return true;
}
static inline double _hypot(double x, double y) { return std::sqrt(x*x+y*y); }
void _tm_transform(const double * tm, double & x, double & y, bool is_delta = false);
void _tm_multiply(double * tm_left, const double * tm_right);
static inline long long hash_ref(const Ref * id)
{
return (((long long)(id->num)) << (sizeof(id->gen)*8)) | (id->gen);
}
class GfxRGB_hash
{
public:
size_t operator () (const GfxRGB & rgb) const
{
return (colToByte(rgb.r) << 16) | (colToByte(rgb.g) << 8) | (colToByte(rgb.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
{
public:
long long id;
bool use_tounicode;
int em_size;
double ascent, descent;
};
class Matrix_less
{
public:
bool operator () (const Matrix & m1, const Matrix & m2) const
{
// Note that we only care about the first 4 elements
for(int i = 0; i < 4; ++i)
{
if(m1.m[i] < m2.m[i] - EPS)
return true;
if(m1.m[i] > m2.m[i] + EPS)
return false;
}
return false;
}
};
void create_directories(std::string path);
bool is_truetype_suffix(const std::string & suffix);
std::string get_filename(const std::string & path);
std::string get_suffix(const std::string & path);
/*
* In PDF, edges of the rectangle are in the middle of the borders