1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 04:50:09 +00:00

clean code

This commit is contained in:
Lu Wang 2013-02-02 20:32:09 +08:00
parent a44a383094
commit 59b89510cc
3 changed files with 96 additions and 88 deletions

View File

@ -29,6 +29,7 @@
#include "util/const.h" #include "util/const.h"
#include "util/StringFormatter.h" #include "util/StringFormatter.h"
#include "util/TmpFiles.h" #include "util/TmpFiles.h"
#include "util/misc.h"
/* /*
* Naming Convention * Naming Convention
@ -75,44 +76,6 @@ public:
bool is_type3; bool is_type3;
}; };
class GfxRGB_hash
{
public:
size_t operator () (const GfxRGB & rgb) const
{
return ( (((size_t)colToByte(rgb.r)) << 16)
| (((size_t)colToByte(rgb.g)) << 8)
| ((size_t)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 == rgb2.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 class HTMLRenderer : public OutputDev
{ {
public: public:
@ -380,11 +343,17 @@ class HTMLRenderer : public OutputDev
double cur_word_space; double cur_word_space;
bool word_space_changed; bool word_space_changed;
// text color // fill color
long long cur_fill_color_id, cur_stroke_color_id; long long cur_fill_color_id;
GfxRGB cur_fill_color, cur_stroke_color; GfxRGB cur_fill_color;
bool cur_has_fill, cur_has_stroke; bool cur_has_fill;
bool fill_color_changed, stroke_color_changed; bool fill_color_changed;
// stroke color
long long cur_stroke_color_id;
GfxRGB cur_stroke_color;
bool cur_has_stroke;
bool stroke_color_changed;
// rise // rise
long long cur_rise_id; long long cur_rise_id;

View File

@ -110,11 +110,12 @@ void HTMLRenderer::reset_state()
cur_ws_id = install_word_space(cur_word_space); cur_ws_id = install_word_space(cur_word_space);
cur_fill_color.r = cur_fill_color.g = cur_fill_color.b = 0; cur_fill_color.r = cur_fill_color.g = cur_fill_color.b = 0;
cur_stroke_color.r = cur_stroke_color.g = cur_stroke_color.b = 0; cur_fill_color_id = install_fill_color(&cur_fill_color);
cur_fill_color_id = install_fill_color(&cur_fill_color); cur_has_fill = true;
cur_stroke_color_id = install_stroke_color(&cur_stroke_color);
cur_has_stroke = false; cur_stroke_color.r = cur_stroke_color.g = cur_stroke_color.b = 0;
cur_has_fill = true; cur_stroke_color_id = install_stroke_color(&cur_stroke_color);
cur_has_stroke = false;
cur_rise = 0; cur_rise = 0;
cur_rise_id = install_rise(cur_rise); cur_rise_id = install_rise(cur_rise);
@ -359,61 +360,58 @@ void HTMLRenderer::check_state_change(GfxState * state)
if(all_changed || fill_color_changed || stroke_color_changed) if(all_changed || fill_color_changed || stroke_color_changed)
{ {
/* /*
* Render modes 0 2 4 6 fill text (stroke or not) * PDF Spec. Table 106 Text rendering modes
* Render modes 1 5 stroke only
* Render modes 3 7 hidden (but ok, we won't even draw text)
*/ */
static const char FILL[8] = { true, false, true, false, true, false, true, false };
static const char STROKE[8] = { false, true, true, false, false, true, true, false };
// PDF Spec. Table 106 Text rendering modes int idx = state->getRender();
bool is_filled, is_stroked; assert((idx >= 0) && (idx < 8));
switch (state->getRender()) { bool is_filled = FILL[idx];
case 0: is_filled = true; is_stroked = false; break; bool is_stroked = STROKE[idx];
case 1: is_filled = false; is_stroked = true; break;
case 2: is_filled = true; is_stroked = true; break;
case 3: is_filled = false; is_stroked = false; break;
case 4: is_filled = true; is_stroked = false; break;
case 5: is_filled = false; is_stroked = true; break;
case 6: is_filled = true; is_stroked = true; break;
case 7: is_filled = false; is_stroked = false; break;
}
GfxRGB new_color;
// fill // fill
state->getFillRGB(&new_color); if(is_filled)
if(cur_has_fill != is_filled ||
!((new_color.r == cur_fill_color.r) &&
(new_color.g == cur_fill_color.g) &&
(new_color.b == cur_fill_color.b)))
{ {
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN); GfxRGB new_color;
cur_fill_color = new_color; state->getFillRGB(&new_color);
cur_fill_color_id = install_fill_color(&new_color);
}
if (!is_filled) { if(!cur_has_fill
|| (!GfxRGB_equal()(new_color, cur_fill_color))
)
{
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
cur_fill_color = new_color;
cur_fill_color_id = install_fill_color(&new_color);
}
}
else
{
cur_fill_color_id = install_fill_color(nullptr); cur_fill_color_id = install_fill_color(nullptr);
} }
cur_has_fill = is_filled;
// stroke // stroke
state->getStrokeRGB(&new_color); if(is_stroked)
if(cur_has_stroke != is_stroked ||
!((new_color.r == cur_stroke_color.r) &&
(new_color.g == cur_stroke_color.g) &&
(new_color.b == cur_stroke_color.b)))
{ {
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN); GfxRGB new_color;
cur_stroke_color = new_color; state->getStrokeRGB(&new_color);
cur_stroke_color_id = install_stroke_color(&new_color);
if(!cur_has_stroke
|| (!GfxRGB_equal()(new_color, cur_stroke_color))
)
{
new_line_state = max<NewLineState>(new_line_state, NLS_SPAN);
cur_stroke_color = new_color;
cur_stroke_color_id = install_stroke_color(&new_color);
}
} }
else
if (!is_stroked) { {
cur_stroke_color_id = install_stroke_color(nullptr); cur_stroke_color_id = install_stroke_color(nullptr);
} }
cur_has_fill = is_filled;
cur_has_stroke = is_stroked; cur_has_stroke = is_stroked;
} }

View File

@ -13,6 +13,8 @@
#include <GfxState.h> #include <GfxState.h>
#include "util/const.h"
namespace pdf2htmlEX { namespace pdf2htmlEX {
static inline long long hash_ref(const Ref * id) static inline long long hash_ref(const Ref * id)
@ -32,6 +34,45 @@ void css_fix_rectangle_border_width(double x1, double y1, double x2, double y2,
std::ostream & operator << (std::ostream & out, const GfxRGB & rgb); std::ostream & operator << (std::ostream & out, const GfxRGB & rgb);
class GfxRGB_hash
{
public:
size_t operator () (const GfxRGB & rgb) const
{
return ( (((size_t)colToByte(rgb.r)) << 16)
| (((size_t)colToByte(rgb.g)) << 8)
| ((size_t)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 == rgb2.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;
}
};
} // namespace pdf2htmlEX } // namespace pdf2htmlEX
#endif //UTIL_H__ #endif //UTIL_H__