Skip to content

Commit

Permalink
Don't rely on iter_get_index to get current line break index
Browse files Browse the repository at this point in the history
pango_layout_iter_get_index gives the visual order, not the logical order, so
we can't rely on this to fix the line break index.

Fix #828.
  • Loading branch information
liZe committed Apr 12, 2019
1 parent 5ceddcb commit 039b0a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 22 additions & 2 deletions weasyprint/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,31 @@ def test_line_breaking():
assert resume_at is None

_, _, resume_at, _, _, _ = make_text(string, 90, font_size=100)
assert string[resume_at:] == 'is a text for test'
assert string.encode('utf-8')[resume_at:].decode('utf-8') == (
'is a text for test')

_, _, resume_at, _, _, _ = make_text(
string, 100, font_family=SANS_FONTS.split(','), font_size=19)
assert string[resume_at:] == 'text for test'
assert string.encode('utf-8')[resume_at:].decode('utf-8') == (
'text for test')


@assert_no_logs
def test_line_breaking_rtl():
string = 'لوريم ايبسوم دولا'

# These two tests do not really rely on installed fonts
_, _, resume_at, _, _, _ = make_text(string, 90, font_size=1)
assert resume_at is None

_, _, resume_at, _, _, _ = make_text(string, 90, font_size=100)
assert string.encode('utf-8')[resume_at:].decode('utf-8') == (
'ايبسوم دولا')

_, _, resume_at, _, _, _ = make_text(
string, 100, font_family=SANS_FONTS.split(','), font_size=16)
assert string.encode('utf-8')[resume_at:].decode('utf-8') == (
'دولا')


@assert_no_logs
Expand Down
4 changes: 3 additions & 1 deletion weasyprint/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,9 @@ def get_first_line(self):
pango.pango_layout_iter_free)
first_line = pango.pango_layout_iter_get_line_readonly(layout_iter)
if pango.pango_layout_iter_next_line(layout_iter):
index = pango.pango_layout_iter_get_index(layout_iter)
second_line = pango.pango_layout_iter_get_line_readonly(
layout_iter)
index = second_line.start_index
else:
index = None
return first_line, index
Expand Down

0 comments on commit 039b0a6

Please sign in to comment.