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

text position bug fixed

This commit is contained in:
Lu Wang 2012-08-07 19:39:47 +08:00
parent 272cf9fa02
commit 37231c9550
2 changed files with 83 additions and 49 deletions

View File

@ -127,13 +127,13 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state)
cur_fn_id = cur_fs_id = cur_tm_id = cur_color_id = 0; cur_fn_id = cur_fs_id = cur_tm_id = cur_color_id = 0;
cur_tx = cur_ty = 0; cur_tx = cur_ty = 0;
cur_line_x_offset = 0;
cur_font_size = 0; cur_font_size = 0;
memcpy(cur_ctm, id_matrix, sizeof(cur_ctm)); memcpy(cur_ctm, id_matrix, sizeof(cur_ctm));
memcpy(draw_ctm, id_matrix, sizeof(draw_ctm)); memcpy(draw_ctm, id_matrix, sizeof(draw_ctm));
draw_font_size = 0; draw_font_size = 0;
draw_scale = 1.0; draw_scale = 1.0;
draw_tx = draw_ty = 0;
cur_color.r = cur_color.g = cur_color.b = 0; cur_color.r = cur_color.g = cur_color.b = 0;
@ -189,6 +189,45 @@ void HTMLRenderer::outputUnicodes(const Unicode * u, int uLen)
} }
} }
void HTMLRenderer::updateAll(GfxState * state)
{
all_changed = true;
updateTextPos(state);
}
void HTMLRenderer::updateFont(GfxState * state)
{
font_changed = true;
}
void HTMLRenderer::updateTextMat(GfxState * state)
{
text_mat_changed = true;
}
void HTMLRenderer::updateCTM(GfxState * state, double m11, double m12, double m21, double m22, double m31, double m32)
{
ctm_changed = true;
}
void HTMLRenderer::updateTextPos(GfxState * state)
{
text_pos_changed = true;
cur_tx = state->getLineX();
cur_ty = state->getLineY();
}
void HTMLRenderer::updateTextShift(GfxState * state, double shift)
{
text_pos_changed = true;
cur_tx -= shift * 0.001 * state->getFontSize() * state->getHorizScaling();
}
void HTMLRenderer::updateFillColor(GfxState * state)
{
color_changed = true;
}
void HTMLRenderer::drawString(GfxState * state, GooString * s) void HTMLRenderer::drawString(GfxState * state, GooString * s)
{ {
if(s->getLength() == 0) if(s->getLength() == 0)
@ -206,26 +245,20 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
// if the line is still open, try to merge with it // if the line is still open, try to merge with it
if(line_opened) if(line_opened)
{ {
//debug double target = (cur_tx - draw_tx) * draw_scale;
html_fout << "<span data-x=\"" << cur_tx << "\"></span>";
double target = -cur_line_x_offset * draw_scale;
if(target > -param->h_eps) if(target > -param->h_eps)
{ {
if(target > param->h_eps) if(target > param->h_eps)
{ {
double w; double w;
auto wid = install_whitespace(target, w); auto wid = install_whitespace(target, w);
cur_line_x_offset = (w - target) / draw_scale;
html_fout << boost::format("<span class=\"w w%|1$x|\"> </span>") % wid; html_fout << boost::format("<span class=\"w w%|1$x|\"> </span>") % wid;
draw_tx += w / draw_scale;
} }
} }
else else
{ {
//debug // or can we shift left using simple tags?
html_fout << "<span data-w=\"" << target << "\"></span>";
// can we shift left using simple tags?
close_cur_line(); close_cur_line();
} }
} }
@ -266,7 +299,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
html_fout << "\""; html_fout << "\"";
double x,y; double x,y;
state->transform(state->getCurX(), state->getCurY(), &x, &y); state->transform(state->getCurX(), state->getCurY(), &x, &y);
html_fout << boost::format("data-lx=\"%5%\" data-ly=\"%6%\" data-scale=\"%4%\" data-x=\"%1%\" data-y=\"%2%\" data-hs=\"%3%") html_fout << boost::format("data-lx=\"%5%\" data-ly=\"%6%\" data-drawscale=\"%4%\" data-x=\"%1%\" data-y=\"%2%\" data-hs=\"%3%")
%x%y%(state->getHorizScaling())%draw_scale%state->getLineX()%state->getLineY(); %x%y%(state->getHorizScaling())%draw_scale%state->getLineX()%state->getLineY();
} }
@ -274,7 +307,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
line_opened = true; line_opened = true;
cur_line_x_offset = 0; draw_tx = cur_tx;
} }
@ -282,20 +315,25 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
// get the unicodes // get the unicodes
char *p = s->getCString(); char *p = s->getCString();
int len = s->getLength(); int len = s->getLength();
double dx,dy,dx1,dy1;
double dx = 0;
double dy = 0;
double dx1,dy1;
double ox, oy; double ox, oy;
int nChars = 0; int nChars = 0;
int nSpaces = 0; int nSpaces = 0;
int uLen; int uLen;
CharCode code; CharCode code;
Unicode *u = nullptr; Unicode *u = nullptr;
while (len > 0) { while (len > 0) {
auto n = font->getNextChar(p, len, &code, &u, &uLen, &dx1, &dy1, &ox, &oy); 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)))
{ {
std::cerr << "TODO: non-zero orgins" << std::endl; std::cerr << "TODO: non-zero origins" << std::endl;
} }
outputUnicodes(u, uLen); outputUnicodes(u, uLen);
@ -303,19 +341,27 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
dx += dx1; dx += dx1;
dy += dy1; dy += dy1;
if (n == 1 && *p == ' ') { if (n == 1 && *p == ' ')
{
++nSpaces; ++nSpaces;
} }
++nChars; ++nChars;
p += n; p += n;
len -= n; len -= n;
} }
cur_tx += (dx * state->getFontSize() dx = (dx * state->getFontSize()
+ nChars * state->getCharSpace() + nChars * state->getCharSpace()
+ nSpaces * state->getWordSpace()) * state->getHorizScaling(); + nSpaces * state->getWordSpace()) * state->getHorizScaling();
cur_ty += (dy * state->getFontSize()); dy *= state->getFontSize();
cur_tx += dx;
cur_ty += dy;
draw_tx += dx;
draw_ty += dy;
} }
// The font installation code is stolen from PSOutputDev.cc in poppler // The font installation code is stolen from PSOutputDev.cc in poppler
@ -792,23 +838,13 @@ void HTMLRenderer::check_state_change(GfxState * state)
{ {
bool close_line = false; bool close_line = false;
if(all_changed || line_pos_changed) if(all_changed || text_pos_changed)
{ {
double tx = state->getLineX(); if(!(std::abs(cur_ty - draw_ty) * draw_scale < param->v_eps))
double ty = state->getLineY();
// TODO: consider draw_scale, while it's actually draw_scale_x
if(!(std::abs(ty - cur_ty) < param->v_eps))
{ {
close_line = true; close_line = true;
cur_ty = ty; draw_ty = cur_ty;
cur_tx = tx; draw_tx = cur_tx;
cur_line_x_offset = 0;
}
else
{
// LineY remains unchanged
cur_line_x_offset = cur_tx - tx;
cur_tx = tx;
} }
} }
@ -866,8 +902,8 @@ void HTMLRenderer::check_state_change(GfxState * state)
if(need_rescale_font) if(need_rescale_font)
{ {
draw_scale = std::sqrt(cur_ctm[2] * cur_ctm[2] + cur_ctm[3] * cur_ctm[3]); draw_scale = std::sqrt(cur_ctm[2] * cur_ctm[2] + cur_ctm[3] * cur_ctm[3]);
double new_draw_font_size = cur_font_size; double new_draw_font_size = cur_font_size;
if(_is_positive(draw_scale)) if(_is_positive(draw_scale))
{ {
new_draw_font_size *= draw_scale; new_draw_font_size *= draw_scale;
@ -909,7 +945,7 @@ void HTMLRenderer::check_state_change(GfxState * state)
void HTMLRenderer::reset_state_track() void HTMLRenderer::reset_state_track()
{ {
all_changed = false; all_changed = false;
line_pos_changed = false; text_pos_changed = false;
ctm_changed = false; ctm_changed = false;
text_mat_changed = false; text_mat_changed = false;
font_changed = false; font_changed = false;

View File

@ -92,17 +92,13 @@ class HTMLRenderer : public OutputDev
* To optmize false alarms * To optmize false alarms
* We just mark as changed, and recheck if they have been changed when we are about to output a new string * We just mark as changed, and recheck if they have been changed when we are about to output a new string
*/ */
virtual void updateAll(GfxState * state) { all_changed = true; } virtual void updateAll(GfxState * state);
virtual void updateFont(GfxState * state) { font_changed = true; } virtual void updateFont(GfxState * state);
virtual void updateTextMat(GfxState * state) { text_mat_changed = true; } virtual void updateTextMat(GfxState * state);
virtual void updateCTM(GfxState * state, double m11, double m12, double m21, double m22, double m31, double m32) { ctm_changed = true; } virtual void updateCTM(GfxState * state, double m11, double m12, double m21, double m22, double m31, double m32);
virtual void updateTextPos(GfxState * state) { line_pos_changed = true; } virtual void updateTextPos(GfxState * state);
virtual void updateTextShift(GfxState * state, double shift) { virtual void updateTextShift(GfxState * state, double shift);
double off = shift * 0.001 * state->getFontSize() * state->getHorizScaling(); virtual void updateFillColor(GfxState * state);
cur_line_x_offset += off;
cur_tx -= off;
}
virtual void updateFillColor(GfxState * state) { color_changed = true; }
//----- text drawing //----- text drawing
virtual void drawString(GfxState * state, GooString * s); virtual void drawString(GfxState * state, GooString * s);
@ -162,9 +158,8 @@ class HTMLRenderer : public OutputDev
bool line_opened; bool line_opened;
// current position // current position
double cur_tx, cur_ty; // in text coords double cur_tx, cur_ty; // real text position, in text coords
double cur_line_x_offset; // in text coords, our position - real position bool text_pos_changed;
bool line_pos_changed;
long long cur_fn_id; long long cur_fn_id;
double cur_font_size; double cur_font_size;
@ -187,12 +182,15 @@ class HTMLRenderer : public OutputDev
// we try to render the final font size directly // we try to render the final font size directly
// to reduce the effect of ctm as much as possible // to reduce the effect of ctm as much as possible
// draw_ctm is cur_ctem scaled by 1/draw_scale, // draw_ctm is cur_ctm scaled by 1/draw_scale,
// so everything redenered should be scaled by draw_scale // so everything redenered should be multiplied by draw_scale
double draw_ctm[6]; double draw_ctm[6];
double draw_font_size; double draw_font_size;
double draw_scale; double draw_scale;
// the position of next char, in text coords
double draw_tx, draw_ty;
ofstream html_fout, allcss_fout; ofstream html_fout, allcss_fout;
class FontInfo{ class FontInfo{