diff --git a/tests/test_text.py b/tests/test_text.py index bbfd0f7f30..f471c8f979 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -458,8 +458,16 @@ def test_text_align_justify_no_break_between_children(): assert span_3.position_x == 5 * 16 # (3 + 1) characters + 1 space +@pytest.mark.parametrize('text', ( + 'Lorem ipsum dolorsit amet', + 'Lorem ipsum dolorsit amet', + 'Lorem ipsum dolorsit amet', + 'Lorem ipsum dolorsit amet', + 'Lorem ipsum dolorsit amet', + 'Lorem ipsum dolorsit amet', +)) @assert_no_logs -def test_word_spacing(): +def test_word_spacing(text): # keep the empty - Lorem ipsum dolorsit amet''') + %s''' % text) html, = page.children body, = html.children line, = body.children strong_2, = line.children + assert strong_2.width - strong_1.width == 33 diff --git a/weasyprint/text/line_break.py b/weasyprint/text/line_break.py index f537a03365..85e8a2e395 100644 --- a/weasyprint/text/line_break.py +++ b/weasyprint/text/line_break.py @@ -182,11 +182,20 @@ def add_attr(start, end, spacing): add_attr(0, len(bytestring), letter_spacing) if word_spacing: + if bytestring == b' ': + # We need more than one space to set word spacing + self.text = ' ​' # Space + zero-width space + text, bytestring = unicode_to_char_p(self.text) + pango.pango_layout_set_text(self.layout, text, -1) + space_spacing = ( units_from_double(word_spacing) + letter_spacing) position = bytestring.find(b' ') + # Pango gives only half of word-spacing on boundaries + boundary_positions = (0, len(bytestring) - 1) while position != -1: - add_attr(position, position + 1, space_spacing) + factor = 1 + (position in boundary_positions) + add_attr(position, position + 1, factor * space_spacing) position = bytestring.find(b' ', position + 1) if word_breaking: