From 039b0a680d431de53f9f6f2f900f023278dc5285 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Fri, 12 Apr 2019 16:04:25 +0200 Subject: [PATCH] Don't rely on iter_get_index to get current line break index 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. --- weasyprint/tests/test_text.py | 24 ++++++++++++++++++++++-- weasyprint/text.py | 4 +++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/weasyprint/tests/test_text.py b/weasyprint/tests/test_text.py index 8f9d305d5..3970f1a76 100644 --- a/weasyprint/tests/test_text.py +++ b/weasyprint/tests/test_text.py @@ -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 diff --git a/weasyprint/text.py b/weasyprint/text.py index 035074e93..f1a69d4e7 100644 --- a/weasyprint/text.py +++ b/weasyprint/text.py @@ -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