Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix word-spacing #1832

Merged
merged 2 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 dolor<em>sit amet</em>',
'Lorem ipsum <em>dolorsit</em> amet',
'Lorem ipsum <em></em>dolorsit amet',
'Lorem ipsum<em> </em>dolorsit amet',
'Lorem ipsum<em> dolorsit</em> amet',
'Lorem ipsum <em>dolorsit </em>amet',
))
@assert_no_logs
def test_word_spacing():
def test_word_spacing(text):
# keep the empty <style> as a regression test: element.text is None
# (Not a string.)
page, = render_pages('''
Expand All @@ -470,15 +478,14 @@ def test_word_spacing():
line, = body.children
strong_1, = line.children

# TODO: Pango gives only half of word-spacing to a space at the end
# of a TextBox. Is this what we want?
page, = render_pages('''
<style>strong { word-spacing: 11px }</style>
<body><strong>Lorem ipsum dolor<em>sit amet</em></strong>''')
<body><strong>%s</strong>''' % text)
html, = page.children
body, = html.children
line, = body.children
strong_2, = line.children

assert strong_2.width - strong_1.width == 33


Expand Down
11 changes: 10 additions & 1 deletion weasyprint/text/line_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

@bot-jonas bot-jonas Mar 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better if the space + zero-width space were written as ' \u200b'?

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:
Expand Down