draw565: Improve line wrapping
Currently the final word of wrapped text will always appear as a single word on its own line. Fix this by rearranging the break cases to avoid searching for the most recent space when we get to the end of the text. Fixes: #230 Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
parent
586507753b
commit
0831f79a10
2 changed files with 17 additions and 3 deletions
|
@ -57,3 +57,16 @@ def test_font_width(draw):
|
|||
|
||||
if f.max_ch() >= 90:
|
||||
assert draw.bounding_box('IIII')[0] < draw.bounding_box('WWWW')[0]
|
||||
|
||||
@pytest.mark.parametrize("input,expected", (
|
||||
('abc', [0, 3]),
|
||||
('one.two', [0, 7]),
|
||||
('one two', [0, 7]),
|
||||
('one two three', [0, 13]),
|
||||
('abcdefghijklmnopqrstuvwxyz', [0, 17, 26]),
|
||||
('abcdefghijklm nopqrstuvwxyz', [0, 14, 27]),
|
||||
('abcde fghij klmno pqrst uvwxyz', [0, 18, 30]),
|
||||
|
||||
))
|
||||
def test_wrap(draw, input, expected):
|
||||
assert draw.wrap(input, 240) == expected
|
||||
|
|
|
@ -366,12 +366,15 @@ class Draw565(object):
|
|||
l = 0
|
||||
|
||||
for i in range(start, max+1):
|
||||
if i >= len(s):
|
||||
if i >= max:
|
||||
end = i
|
||||
break
|
||||
ch = s[i]
|
||||
(_, h, w) = font.get_ch(ch)
|
||||
l += w + 1
|
||||
if l > width:
|
||||
if end <= start:
|
||||
end = i
|
||||
break
|
||||
|
||||
# Break the line immediately if requested
|
||||
|
@ -382,8 +385,6 @@ class Draw565(object):
|
|||
# Remember the right-most place we can cleanly break the line
|
||||
if ch == ' ':
|
||||
end = i+1
|
||||
if end <= start:
|
||||
end = i
|
||||
chunks.append(end)
|
||||
|
||||
return chunks
|
||||
|
|
Loading…
Reference in a new issue