Skip to content

Commit 5283f39

Browse files
authored
Merge pull request #163 from kmatch98/text_wrap
Resolve missing spaces when long words wrap across a full line.
2 parents 1a9edbd + 8f8a8a7 commit 5283f39

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

adafruit_display_text/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def wrap_text_to_pixels(
2424
indent0: str = "",
2525
indent1: str = "",
2626
) -> List[str]:
27-
# pylint: disable=too-many-branches, too-many-locals
27+
# pylint: disable=too-many-branches, too-many-locals, too-many-nested-blocks, too-many-statements
2828

2929
"""wrap_text_to_pixels function
3030
A helper that will return a list of lines with word-break wrapping.
@@ -62,23 +62,38 @@ def measure(text):
6262
swidth = measure(" ")
6363
firstword = True
6464
for line_in_input in string.split("\n"):
65+
newline = True
6566
for index, word in enumerate(line_in_input.split(" ")):
6667
wwidth = measure(word)
6768
word_parts = []
6869
cur_part = ""
6970

7071
if wwidth > max_width:
7172
for char in word:
73+
if newline:
74+
extraspace = 0
75+
leadchar = ""
76+
else:
77+
extraspace = swidth
78+
leadchar = " "
7279
if (
7380
measure("".join(partial))
7481
+ measure(cur_part)
7582
+ measure(char)
7683
+ measure("-")
84+
+ extraspace
7785
> max_width
7886
):
79-
word_parts.append("".join(partial) + cur_part + "-")
87+
if cur_part:
88+
word_parts.append(
89+
"".join(partial) + leadchar + cur_part + "-"
90+
)
91+
92+
else:
93+
word_parts.append("".join(partial))
8094
cur_part = char
8195
partial = [indent1]
96+
newline = True
8297
else:
8398
cur_part += char
8499
if cur_part:
@@ -103,6 +118,8 @@ def measure(text):
103118
lines.append("".join(partial))
104119
partial = [indent1, word]
105120
width = measure(indent1) + wwidth
121+
if newline:
122+
newline = False
106123

107124
lines.append("".join(partial))
108125
partial = [indent1]

0 commit comments

Comments
 (0)