Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spaces are deleted when word-wrapping #162

Closed
kmatch98 opened this issue Feb 5, 2022 · 1 comment · Fixed by #163
Closed

Spaces are deleted when word-wrapping #162

kmatch98 opened this issue Feb 5, 2022 · 1 comment · Fixed by #163
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@kmatch98
Copy link
Contributor

kmatch98 commented Feb 5, 2022

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
@kmatch98 kmatch98 added bug Something isn't working good first issue Good for newcomers labels Feb 5, 2022
@kmatch98
Copy link
Contributor Author

kmatch98 commented Feb 7, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
1 participant