adafruit/Adafruit_CircuitPython_Display_Text

Spaces are deleted when word-wrapping

kmatch98 opened this issue · 1 comments

I noticed that some spaces are being deleted when word-wrapping text using the wrap_text_to_pixels function in the adafruit_display_text library. Library is pulled from bundle adafruit-circuitpython-bundle-7.x-mpy-20220131.

I use a long string with long words and a narrow pixel width, and it causes some spaces to be deleted , such as in:

  • simplifyexp - "simplify experimenting"
  • low-costmic - "low-cost microcontroller boards"

Here's the output I'm getting:

code.py output:
width: 75
text_lines: ['CircuitPyth-', 'on is a', 'programming', 'language', 'designed to', 'simplifyexp-', 'erimenting', 
'and learning', 'to code on', 'low-costmic-', 'rocontrolle-', 'r boards.', '', 'WithCircuit-', 'Python,', 'there are no', 
'upfront', 'desktop', 'downloads', 'needed. Once', 'you get your', 'board set', 'up, open any', 'text editor,', 
'and start', 'editing', "code. It's", 'that simple.']

Here's my code:

import terminalio
from adafruit_display_text import bitmap_label, wrap_text_to_pixels

text_string = "CircuitPython is a programming language designed to simplify experimenting and learning to code on low-cost microcontroller boards."
text_string=text_string+("\n\nWith CircuitPython, there are no upfront desktop downloads needed. Once you get your board set up, open any text editor, and start editing code. It's that simple.")

width=75
text_lines = wrap_text_to_pixels(text_string, width, terminalio.FONT)
print("width: {}".format(width))
print('text_lines: {}'.format(text_lines))

while True:
    pass

At first glance, I think the issue is here

if wwidth > max_width:
for char in word:
if (
measure("".join(partial))
+ measure(cur_part)
+ measure(char)
+ measure("-")
> max_width
):
word_parts.append("".join(partial) + cur_part + "-")
cur_part = char
partial = [indent1]
else:
cur_part += char
if cur_part:
word_parts.append(cur_part)
for line in word_parts[:-1]:
lines.append(line)
partial.append(word_parts[-1])
width = measure(word_parts[-1])
if firstword:
firstword = False

This code should add a space before placing a word if it’s not the initial word in a line. This can be detected by seeing if partial == indent0 when firstword == True or partial == indent1 when firstword==False.