From 0524421ec1f3600676ff2cae47e795664f730eab Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 13 Oct 2021 22:11:58 +0200 Subject: [PATCH] Only enable letter- and word-spacing when needed Enabling letter-spacing with a value of 0 breaks ligatures, and probably other typography features. Fix #1469. --- tests/test_fonts.py | 15 +++++++++++++++ weasyprint/text/line_break.py | 20 ++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/tests/test_fonts.py b/tests/test_fonts.py index 798f43b6..78d8566d 100644 --- a/tests/test_fonts.py +++ b/tests/test_fonts.py @@ -40,6 +40,21 @@ def test_kerning_default(): assert span2.width == 1.5 * 16 +@assert_no_logs +def test_ligatures_word_space(): + # Kerning and ligatures are on for text with increased word spacing + # https://github.com/Kozea/WeasyPrint/issues/1469 + page, = render_pages(''' + + aa liga aa''') + html, = page.children + body, = html.children + assert len(body.children) == 1 + + @assert_no_logs def test_kerning_deactivate(): # Deactivate kerning diff --git a/weasyprint/text/line_break.py b/weasyprint/text/line_break.py index ed17d0fe..a3a0d36f 100644 --- a/weasyprint/text/line_break.py +++ b/weasyprint/text/line_break.py @@ -212,9 +212,7 @@ class Layout: if letter_spacing == 'normal': letter_spacing = 0 - if text and (word_spacing != 0 or letter_spacing != 0): - letter_spacing = units_from_double(letter_spacing) - space_spacing = units_from_double(word_spacing) + letter_spacing + if self.text and (word_spacing or letter_spacing): attr_list = pango.pango_layout_get_attributes(self.layout) if not attr_list: # TODO: list should be freed @@ -226,11 +224,17 @@ class Layout: attr.start_index, attr.end_index = start, end pango.pango_attr_list_change(attr_list, attr) - add_attr(0, len(bytestring), letter_spacing) - position = bytestring.find(b' ') - while position != -1: - add_attr(position, position + 1, space_spacing) - position = bytestring.find(b' ', position + 1) + if letter_spacing: + letter_spacing = units_from_double(letter_spacing) + add_attr(0, len(bytestring), letter_spacing) + + if word_spacing: + space_spacing = ( + units_from_double(word_spacing) + letter_spacing) + position = bytestring.find(b' ') + while position != -1: + add_attr(position, position + 1, space_spacing) + position = bytestring.find(b' ', position + 1) pango.pango_layout_set_attributes(self.layout, attr_list)