Skip to content

Commit

Permalink
Fix x position of RTL characters with trailing space
Browse files Browse the repository at this point in the history
When a line has an unwanted trailing space, the space is removed and the
textbox’ width (and its ancestor’s) is recalculated.

If the line characters have a main RTL Unicode direction (not the CSS box
direction, the text direction), the trailing space is at the left of the
box. Recalculating the width is not enough, we also need to fix the x position.

Testing would be better with Arabic characters, but none is included in
Ahem. Forcing the text direction works well too.

Fix #1111.
  • Loading branch information
liZe committed May 18, 2020
1 parent dc443ab commit d06f081
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
7 changes: 7 additions & 0 deletions weasyprint/layout/inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ def remove_last_whitespace(context, box):
assert resume is None
space_width = box.width - new_box.width
box.width = new_box.width
if new_box.pango_layout.first_line_direction % 2:
# RTL line, the trailing space is at the left of the box. We have
# to translate the box to align the stripped text with the right
# edge of the box.
box.position_x -= space_width
for ancestor in ancestors:
ancestor.position_x -= space_width
else:
space_width = box.width
box.width = 0
Expand Down
26 changes: 26 additions & 0 deletions weasyprint/tests/test_draw/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,29 @@ def test_text_overflow_ellipsis():
<div><div style="text-overflow: clip">abcde</div></div>
<div><div style="overflow: visible">abcde</div></div>
''')


def test_text_align_rtl_trailing_whitespace():
# Test text alignment for rtl text with trailing space.
# Test regression: https://github.com/Kozea/WeasyPrint/issues/1111
assert_pixels('text_overflow', 9, 9, '''
_________
_rrrrBBB_
_________
_rrrrBBB_
_________
_________
_________
_________
_________
''', '''
<style>
@font-face {src: url(AHEM____.TTF); font-family: ahem}
@page { background: white; size: 9px }
body { font-family: ahem; color: blue; font-size: 1px }
p { background: red; line-height: 1; width: 7em; margin: 1em }
</style>
<p style="direction: rtl"> abc </p>
<!-- &#8207 forces Unicode RTL direction for the following chars -->
<p style="direction: rtl"> &#8207;abc </p>
''')
6 changes: 5 additions & 1 deletion weasyprint/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@
PangoLayout *layout;
gint start_index;
gint length;
/* ... */
void *runs;
guint is_paragraph_start : 1;
guint resolved_dir : 3;
} PangoLayoutLine;
typedef struct {
Expand Down Expand Up @@ -654,6 +656,7 @@ def setup(self, context, font_size, style):

self.context = context
self.style = style
self.first_line_direction = 0

# Cairo crashes with font-size: 0 when using Win32 API
# See https://github.com/Kozea/WeasyPrint/pull/599
Expand Down Expand Up @@ -739,6 +742,7 @@ def get_first_line(self):
index = second_line.start_index
else:
index = None
self.first_line_direction = first_line.resolved_dir
return first_line, index

def set_text(self, text, justify=False):
Expand Down

0 comments on commit d06f081

Please sign in to comment.