From 99213d4e0bf3c6a7e063122d0aa2b37ae63c3376 Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Sun, 31 Mar 2013 01:21:14 +0800 Subject: [PATCH] don't add space for tiny gaps --- src/HTMLRenderer/TextLineBuffer.cc | 13 +++++++++++-- src/HTMLRenderer/TextLineBuffer.h | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/HTMLRenderer/TextLineBuffer.cc b/src/HTMLRenderer/TextLineBuffer.cc index f8b9b44..b25ed16 100644 --- a/src/HTMLRenderer/TextLineBuffer.cc +++ b/src/HTMLRenderer/TextLineBuffer.cc @@ -187,7 +187,7 @@ void HTMLRenderer::TextLineBuffer::flush(void) if(is_positive(-actual_offset)) last_text_pos_with_negative_offset = cur_text_idx; - double threshold = cur_state->draw_font_size * (cur_state->font_info->ascent - cur_state->font_info->descent) * (renderer->param->space_threshold); + double threshold = cur_state->em_size() * (renderer->param->space_threshold); out << "" << (target > (threshold - EPS) ? " " : "") << ""; @@ -268,9 +268,14 @@ void HTMLRenderer::TextLineBuffer::optimize(void) while((offset_iter != offsets.end()) && (offset_iter->start_idx < text_idx1)) ++ offset_iter; + double threshold = (state_iter1->em_size()) * (renderer->param->space_threshold); for(; (offset_iter != offsets.end()) && (offset_iter->start_idx < text_idx2); ++offset_iter) { double target = offset_iter->width; + // we don't want to add spaces for tiny gaps, or even negative shifts + if(target < threshold - EPS) + continue; + auto iter = width_map.lower_bound(target-EPS); if((iter != width_map.end()) && (abs(iter->first - target) <= EPS)) { @@ -303,7 +308,6 @@ void HTMLRenderer::TextLineBuffer::optimize(void) state_iter1->word_space = 0; double new_word_space = most_used_width - state_iter1->single_space_offset(); - // install new word_space state_iter1->ids[State::WORD_SPACE_ID] = renderer->word_space_manager.install(new_word_space, &(state_iter1->word_space)); // mark that the word_space is not free @@ -422,6 +426,11 @@ double HTMLRenderer::TextLineBuffer::State::single_space_offset(void) const return word_space + letter_space + font_info->space_width * draw_font_size; } +double HTMLRenderer::TextLineBuffer::State::em_size(void) const +{ + return draw_font_size * (font_info->ascent - font_info->descent); +} + long long HTMLRenderer::TextLineBuffer::State::umask_by_id(int id) { return (((long long)0xff) << (8*id)); diff --git a/src/HTMLRenderer/TextLineBuffer.h b/src/HTMLRenderer/TextLineBuffer.h index cf94f9c..809ccf8 100644 --- a/src/HTMLRenderer/TextLineBuffer.h +++ b/src/HTMLRenderer/TextLineBuffer.h @@ -31,6 +31,8 @@ public: int diff(const State & s) const; // the offset cause by a single ' ' char double single_space_offset(void) const; + // calculate em_size of this state + double em_size(void) const; enum { FONT_ID,