diff --git a/src/HTMLRenderer.h b/src/HTMLRenderer.h index 5ef408c..4db6454 100644 --- a/src/HTMLRenderer.h +++ b/src/HTMLRenderer.h @@ -1,8 +1,7 @@ /* * HTMLRenderer.h * - * Created on: Mar 15, 2011 - * Author: tian + * by WangLu */ #ifndef HTMLRENDERER_H_ diff --git a/src/HTMLRenderer/state.cc b/src/HTMLRenderer/state.cc index 46e69fd..dd184a8 100644 --- a/src/HTMLRenderer/state.cc +++ b/src/HTMLRenderer/state.cc @@ -7,6 +7,12 @@ * 2012.08.14 */ +/* + * TODO + * use relative positioning for rise + * negative offset of span + */ + #include #include "HTMLRenderer.h" @@ -313,41 +319,12 @@ void HTMLRenderer::prepare_line(GfxState * state) // don't change line_status } - // open new tags when necessary if(line_status == LineStatus::NONE) - { new_line_status = LineStatus::DIV; - //resync position - draw_ty = cur_ty + cur_rise; - draw_tx = cur_tx; - } - else - { - assert(new_line_status != LineStatus::DIV); - - // horizontal position - // try to merge with the last line if possible - double target = cur_tx - draw_tx; - if(abs(target) < param->h_eps) - { - // ignore it - } - else - { - // don't close a pending span here, keep the styling - - double w; - auto wid = install_whitespace(target * draw_scale, w); - html_fout << format("%2%") % wid % (target > 0 ? " " : ""); - draw_tx += w / draw_scale; - } - } - if(new_line_status != LineStatus::NONE) { // have to open a new tag - if (new_line_status == LineStatus::DIV) { // TODO: recheck descent/ascent @@ -357,6 +334,10 @@ void HTMLRenderer::prepare_line(GfxState * state) html_fout << format("
") % y % x % cur_tm_id; + //resync position + draw_ty = cur_ty + cur_rise; + draw_tx = cur_tx; + } else if(new_line_status == LineStatus::SPAN) { @@ -370,10 +351,25 @@ void HTMLRenderer::prepare_line(GfxState * state) html_fout << format("") % cur_fn_id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id; - //line_status = new_line_status; line_status = LineStatus::SPAN; } + // align horizontal position + // try to merge with the last line if possible + double target = cur_tx - draw_tx; + if(abs(target) < param->h_eps) + { + // ignore it + } + else + { + // don't close a pending span here, keep the styling + + double w; + auto wid = install_whitespace(target * draw_scale, w); + html_fout << format("%2%") % wid % (target > 0 ? " " : ""); + draw_tx += w / draw_scale; + } } void HTMLRenderer::close_line() { diff --git a/src/HTMLRenderer/text.cc b/src/HTMLRenderer/text.cc index 8f69280..34283a9 100644 --- a/src/HTMLRenderer/text.cc +++ b/src/HTMLRenderer/text.cc @@ -167,6 +167,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) double dx = 0; double dy = 0; + double dxerr = 0; double dx1,dy1; double ox, oy; @@ -177,6 +178,11 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) CharCode code; Unicode *u = nullptr; + double fs = state->getFontSize(); + double cs = state->getCharSpace(); + double ws = state->getWordSpace(); + double hs = state->getHorizScaling(); + while (len > 0) { auto n = font->getNextChar(p, len, &code, &u, &uLen, &dx1, &dy1, &ox, &oy); @@ -193,24 +199,24 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) if((uLen > 0) && (all_of(u, u+uLen, isLegalUnicode))) { outputUnicodes(html_fout, u, uLen); - dx += dx1; - dy += dy1; } else { // should not consider hozi scaling here // will be handled by draw_ctm - double target = dx1 + state->getCharSpace(); + double target = dx1 * fs + state->getCharSpace(); if(n == 1 && *p == ' ') target += state->getWordSpace(); double w; auto wid = install_whitespace(target * draw_scale, w); html_fout << format("%2%") % wid % (target > 0 ? " " : ""); - dx += target; - dy += dy1; + + dxerr += w/draw_scale - target; } + dx += dx1; + dy += dy1; ++nChars; p += n; @@ -219,15 +225,13 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s) // horiz_scaling is merged into ctm now, // so the coordinate system is ugly - dx = (dx * state->getFontSize() - + nChars * state->getCharSpace() - + nSpaces * state->getWordSpace()) * state->getHorizScaling(); + dx = (dx * fs + nChars * cs + nSpaces * ws) * hs; - dy *= state->getFontSize(); + dy *= fs; cur_tx += dx; cur_ty += dy; - draw_tx += dx; + draw_tx += dx + dxerr * state->getFontSize() * state->getHorizScaling(); draw_ty += dy; }