Skip to content

Commit

Permalink
Only enable letter- and word-spacing when needed
Browse files Browse the repository at this point in the history
Enabling letter-spacing with a value of 0 breaks ligatures, and probably other
typography features.

Fix #1469.
  • Loading branch information
liZe committed Oct 13, 2021
1 parent ab3ff79 commit 0524421
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 15 additions & 0 deletions tests/test_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
body { font-family: weasyprint; word-spacing: 1em; width: 10em }
</style>
aa liga aa''')
html, = page.children
body, = html.children
assert len(body.children) == 1


@assert_no_logs
def test_kerning_deactivate():
# Deactivate kerning
Expand Down
20 changes: 12 additions & 8 deletions weasyprint/text/line_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ def set_text(self, text, justify=False):
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
Expand All @@ -226,11 +224,17 @@ def add_attr(start, end, spacing):
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)

Expand Down

0 comments on commit 0524421

Please sign in to comment.