1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-08 19:00:33 +00:00

working on new positioning

This commit is contained in:
Lu Wang 2012-08-23 23:07:01 +02:00
commit 17c44e8cb0
3 changed files with 41 additions and 42 deletions

View File

@ -1,8 +1,7 @@
/* /*
* HTMLRenderer.h * HTMLRenderer.h
* *
* Created on: Mar 15, 2011 * by WangLu
* Author: tian
*/ */
#ifndef HTMLRENDERER_H_ #ifndef HTMLRENDERER_H_

View File

@ -7,6 +7,12 @@
* 2012.08.14 * 2012.08.14
*/ */
/*
* TODO
* use relative positioning for rise
* negative offset of span
*/
#include <algorithm> #include <algorithm>
#include "HTMLRenderer.h" #include "HTMLRenderer.h"
@ -313,41 +319,12 @@ void HTMLRenderer::prepare_line(GfxState * state)
// don't change line_status // don't change line_status
} }
// open new tags when necessary
if(line_status == LineStatus::NONE) if(line_status == LineStatus::NONE)
{
new_line_status = LineStatus::DIV; 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("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > 0 ? " " : "");
draw_tx += w / draw_scale;
}
}
if(new_line_status != LineStatus::NONE) if(new_line_status != LineStatus::NONE)
{ {
// have to open a new tag // have to open a new tag
if (new_line_status == LineStatus::DIV) if (new_line_status == LineStatus::DIV)
{ {
// TODO: recheck descent/ascent // TODO: recheck descent/ascent
@ -357,6 +334,10 @@ void HTMLRenderer::prepare_line(GfxState * state)
html_fout << format("<div style=\"bottom:%1%px;left:%2%px;\" class=\"l t%|3$x|\"><em></em>") html_fout << format("<div style=\"bottom:%1%px;left:%2%px;\" class=\"l t%|3$x|\"><em></em>")
% y % x % cur_tm_id; % y % x % cur_tm_id;
//resync position
draw_ty = cur_ty + cur_rise;
draw_tx = cur_tx;
} }
else if(new_line_status == LineStatus::SPAN) else if(new_line_status == LineStatus::SPAN)
{ {
@ -370,10 +351,25 @@ void HTMLRenderer::prepare_line(GfxState * state)
html_fout << format("<span class=\"f%|1$x| s%|2$x| c%|3$x| l%|4$x| w%|5$x|\">") html_fout << format("<span class=\"f%|1$x| s%|2$x| c%|3$x| l%|4$x| w%|5$x|\">")
% cur_fn_id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id; % cur_fn_id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id;
//line_status = new_line_status;
line_status = LineStatus::SPAN; 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("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > 0 ? " " : "");
draw_tx += w / draw_scale;
}
} }
void HTMLRenderer::close_line() void HTMLRenderer::close_line()
{ {

View File

@ -167,6 +167,7 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
double dx = 0; double dx = 0;
double dy = 0; double dy = 0;
double dxerr = 0;
double dx1,dy1; double dx1,dy1;
double ox, oy; double ox, oy;
@ -177,6 +178,11 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
CharCode code; CharCode code;
Unicode *u = nullptr; Unicode *u = nullptr;
double fs = state->getFontSize();
double cs = state->getCharSpace();
double ws = state->getWordSpace();
double hs = state->getHorizScaling();
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);
@ -193,24 +199,24 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
if((uLen > 0) && (all_of(u, u+uLen, isLegalUnicode))) if((uLen > 0) && (all_of(u, u+uLen, isLegalUnicode)))
{ {
outputUnicodes(html_fout, u, uLen); outputUnicodes(html_fout, u, uLen);
dx += dx1;
dy += dy1;
} }
else else
{ {
// should not consider hozi scaling here // should not consider hozi scaling here
// will be handled by draw_ctm // will be handled by draw_ctm
double target = dx1 + state->getCharSpace(); double target = dx1 * fs + state->getCharSpace();
if(n == 1 && *p == ' ') if(n == 1 && *p == ' ')
target += state->getWordSpace(); target += state->getWordSpace();
double w; double w;
auto wid = install_whitespace(target * draw_scale, w); auto wid = install_whitespace(target * draw_scale, w);
html_fout << format("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > 0 ? " " : ""); html_fout << format("<span class=\"_ _%|1$x|\">%2%</span>") % wid % (target > 0 ? " " : "");
dx += target;
dy += dy1; dxerr += w/draw_scale - target;
} }
dx += dx1;
dy += dy1;
++nChars; ++nChars;
p += n; p += n;
@ -219,15 +225,13 @@ void HTMLRenderer::drawString(GfxState * state, GooString * s)
// horiz_scaling is merged into ctm now, // horiz_scaling is merged into ctm now,
// so the coordinate system is ugly // so the coordinate system is ugly
dx = (dx * state->getFontSize() dx = (dx * fs + nChars * cs + nSpaces * ws) * hs;
+ nChars * state->getCharSpace()
+ nSpaces * state->getWordSpace()) * state->getHorizScaling();
dy *= state->getFontSize(); dy *= fs;
cur_tx += dx; cur_tx += dx;
cur_ty += dy; cur_ty += dy;
draw_tx += dx; draw_tx += dx + dxerr * state->getFontSize() * state->getHorizScaling();
draw_ty += dy; draw_ty += dy;
} }