From cde35df1a10a0ff9a0c02524641100c7d199f0d1 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Fri, 24 Aug 2012 19:40:43 +0200 Subject: [PATCH] add a class for rise --- share/all.css | 3 ++- src/HTMLRenderer.h | 12 +++++++++--- src/HTMLRenderer/export.cc | 5 +++++ src/HTMLRenderer/general.cc | 5 +++-- src/HTMLRenderer/install.cc | 13 +++++++++++++ src/HTMLRenderer/state.cc | 33 +++++++++++++++++---------------- 6 files changed, 49 insertions(+), 22 deletions(-) diff --git a/share/all.css b/share/all.css index fcb171d..7bf8eba 100644 --- a/share/all.css +++ b/share/all.css @@ -28,8 +28,9 @@ -o-transform-origin:0% 100%; } #pdf-main span { - vertical-align: baseline; + position:relative; line-height:0; + vertical-align: baseline; } #pdf-main ._ { color:transparent; diff --git a/src/HTMLRenderer.h b/src/HTMLRenderer.h index 4db6454..2abc92e 100644 --- a/src/HTMLRenderer.h +++ b/src/HTMLRenderer.h @@ -49,6 +49,7 @@ * w - Word spacing * c - Color * _ - white space + * r - Rise * */ @@ -139,6 +140,7 @@ class HTMLRenderer : public OutputDev long long install_word_space(double word_space); long long install_color(const GfxRGB * rgb); long long install_whitespace(double ws_width, double & actual_width); + long long install_rise(double rise); //////////////////////////////////////////////////// // export css styles @@ -156,6 +158,7 @@ class HTMLRenderer : public OutputDev void export_word_space(long long ws_id, double word_space); void export_color(long long color_id, const GfxRGB * rgb); void export_whitespace(long long ws_id, double ws_width); + void export_rise(long long rise_id, double rise); //////////////////////////////////////////////////// // state tracking @@ -199,9 +202,6 @@ class HTMLRenderer : public OutputDev // The order is according to the appearance in check_state_change // any state changed bool all_changed; - // rise - double cur_rise; - bool rise_changed; // current position double cur_tx, cur_ty; // real text position, in text coords bool text_pos_changed; @@ -239,6 +239,11 @@ class HTMLRenderer : public OutputDev GfxRGB cur_color; bool color_changed; + // rise + long long cur_rise_id; + double cur_rise; + bool rise_changed; + // optimize for web // we try to render the final font size directly // to reduce the effect of ctm as much as possible @@ -267,6 +272,7 @@ class HTMLRenderer : public OutputDev std::map color_map; std::map whitespace_map; + std::map rise_map; int image_count; diff --git a/src/HTMLRenderer/export.cc b/src/HTMLRenderer/export.cc index 8b313fc..d448b9b 100644 --- a/src/HTMLRenderer/export.cc +++ b/src/HTMLRenderer/export.cc @@ -125,3 +125,8 @@ void HTMLRenderer::export_whitespace (long long ws_id, double ws_width) allcss_fout << format("._%|1$x|{display:inline;margin-left:%2%px;}") % ws_id % ws_width << endl; } +void HTMLRenderer::export_rise (long long rise_id, double rise) +{ + allcss_fout << format(".r%|1$x|{top:%2%px;}") % rise_id % (-rise) << endl; +} + diff --git a/src/HTMLRenderer/general.cc b/src/HTMLRenderer/general.cc index 5f7750b..5fa45f8 100644 --- a/src/HTMLRenderer/general.cc +++ b/src/HTMLRenderer/general.cc @@ -153,8 +153,6 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) html_fout << format(");background-position:0 0;background-size:%1%px %2%px;background-repeat:no-repeat;\">") % pageWidth % pageHeight; - cur_rise = 0; - draw_scale = 1.0; cur_fn_id = install_font(nullptr); @@ -172,6 +170,9 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state) cur_color.r = cur_color.g = cur_color.b = 0; cur_color_id = install_color(&cur_color); + cur_rise = 0; + cur_rise_id = install_rise(cur_rise); + cur_tx = cur_ty = 0; draw_tx = draw_ty = 0; diff --git a/src/HTMLRenderer/install.cc b/src/HTMLRenderer/install.cc index d0c381d..8754560 100644 --- a/src/HTMLRenderer/install.cc +++ b/src/HTMLRenderer/install.cc @@ -331,3 +331,16 @@ long long HTMLRenderer::install_whitespace(double ws_width, double & actual_widt return new_ws_id; } +long long HTMLRenderer::install_rise(double rise) +{ + auto iter = rise_map.lower_bound(rise - param->v_eps); + if((iter != rise_map.end()) && (abs(iter->first - rise) < param->v_eps)) + { + return iter->second; + } + + long long new_rise_id = rise_map.size(); + rise_map.insert(make_pair(rise, new_rise_id)); + export_rise(new_rise_id, rise); + return new_rise_id; +} diff --git a/src/HTMLRenderer/state.cc b/src/HTMLRenderer/state.cc index e4c8ace..b00bae8 100644 --- a/src/HTMLRenderer/state.cc +++ b/src/HTMLRenderer/state.cc @@ -9,7 +9,7 @@ /* * TODO - * use relative positioning for rise + * optimize lines using nested (reuse classes) */ #include @@ -82,17 +82,6 @@ void HTMLRenderer::check_state_change(GfxState * state) bool need_recheck_position = false; bool need_rescale_font = false; - // rise - if(all_changed || rise_changed) - { - double new_rise = state->getRise(); - if(!_equal(cur_rise, new_rise)) - { - need_recheck_position = true; - cur_rise = new_rise; - } - } - // text position // we've been tracking the text position positively in the update*** functions if(all_changed || text_pos_changed) @@ -200,7 +189,7 @@ void HTMLRenderer::check_state_change(GfxState * state) * TODO, writing mode, set dx and solve dy */ - double dy = cur_ty + cur_rise - draw_ty; + double dy = cur_ty - draw_ty; double tdx = old_ctm[4] - cur_ctm[4] - cur_ctm[2] * dy; double tdy = old_ctm[5] - cur_ctm[5] - cur_ctm[3] * dy; @@ -277,6 +266,18 @@ void HTMLRenderer::check_state_change(GfxState * state) } } + // rise + if(all_changed || rise_changed) + { + double new_rise = state->getRise(); + if(!_equal(cur_rise, new_rise)) + { + new_line_status = max(new_line_status, LineStatus::SPAN); + cur_rise = new_rise; + cur_rise_id = install_rise(new_rise * draw_scale); + } + } + reset_state_change(); } @@ -355,7 +356,7 @@ void HTMLRenderer::prepare_line(GfxState * state) % y % x % cur_tm_id; //resync position - draw_ty = cur_ty + cur_rise; + draw_ty = cur_ty; draw_tx = cur_tx; } @@ -368,8 +369,8 @@ void HTMLRenderer::prepare_line(GfxState * state) assert(false && "Bad value of new_line_status"); } - html_fout << format("") - % cur_fn_id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id; + html_fout << format("") + % cur_fn_id % cur_fs_id % cur_color_id % cur_ls_id % cur_ws_id % cur_rise_id; line_status = LineStatus::SPAN; }