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 justify non breaking spaces #2390

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions tests/draw/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,59 @@ def test_text_align_justify(assert_pixels):
<div>a c e</div>''')


def test_text_align_justify_nbsp(assert_pixels):
assert_pixels('''
_________
_RR___RR_
_RR___RR_
_RR______
_RR______
_________
''', '''
<style>
@page {
size: 9px 6px;
}
body {
color: red;
font-family: weasyprint;
font-size: 2px;
}
div {
line-height: 1;
margin: 1px;
text-align: justify;
}
</style>
<div>a&nbsp;c e</div>''')

def test_text_align_justify_nbsp_2(assert_pixels):
assert_pixels('''
_________
_RR_RR_RR
_RR_RR_RR
_________
_________
_________
''', '''
<style>
@page {
size: 9px 6px;
}
body {
color: red;
font-family: weasyprint;
font-size: 2px;
}
div {
line-height: 1;
margin: 1px;
text-align: justify;
}
</style>
<div>a\u202Fc\u202Fe</div>''')


def test_text_word_spacing(assert_pixels):
assert_pixels('''
___________________
Expand Down
3 changes: 2 additions & 1 deletion weasyprint/layout/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,8 @@ def justify_line(context, line, extra_width):
def count_spaces(box):
if isinstance(box, boxes.TextBox):
# TODO: remove trailing spaces correctly
return box.text.count(' ')
# here we count spaces and non-breaking spaces
return box.text.count(' ') + box.text.count("\u00A0")
elif isinstance(box, (boxes.LineBox, boxes.InlineBox)):
return sum(count_spaces(child) for child in box.children)
else:
Expand Down
23 changes: 20 additions & 3 deletions weasyprint/text/line_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,31 @@ def add_attr(start, end, spacing):
text, bytestring = unicode_to_char_p(self.text)
pango.pango_layout_set_text(self.layout, text, -1)

nbsp = b'\xC2\xA0' # somehow \u00A0 is converted into this
space_spacing = int(word_spacing * TO_UNITS + letter_spacing)
position = bytestring.find(b' ')

def find_next_pos(start=0):
"""
search for first occurrence of ' ' or NBSP
return index (or -1) and lenght of searched string
"""
p1 = bytestring.find(b' ', start)
p2 = bytestring.find(nbsp, start)
if p1 == -1 and p2 == -1:
return -1, None
if p2 == -1:
return p1, 1
if p1 == -1 or p2 < p1:
return p2, 2
return p1, 1

position, splen = find_next_pos()
# Pango gives only half of word-spacing on boundaries
boundary_positions = (0, len(bytestring) - 1)
while position != -1:
factor = 1 + (position in boundary_positions)
add_attr(position, position + 1, factor * space_spacing)
position = bytestring.find(b' ', position + 1)
add_attr(position, position + splen, factor * space_spacing)
position, splen = find_next_pos(position + splen)

if word_breaking:
attr = pango.pango_attr_insert_hyphens_new(False)
Expand Down