pimoroni-pico icon indicating copy to clipboard operation
pimoroni-pico copied to clipboard

Text does not wrap when using vector (hershey) fonts

Open Trantion897 opened this issue 1 year ago • 1 comments

When I call display.text("Lorem ipsum dolor sit amet", x, y, 20, 1), the docs say it should wrap after 20 pixels, but it doesn't wrap at all. This function isn't implemented for vector fonts:

https://github.com/pimoroni/pimoroni-pico/blob/f18f1ba259e80e6b6fce534ba16d9d26701154aa/libraries/bitmap_fonts/bitmap_fonts.cpp#L150 https://github.com/pimoroni/pimoroni-pico/blob/f18f1ba259e80e6b6fce534ba16d9d26701154aa/libraries/hershey_fonts/hershey_fonts.cpp#L118

I don't do C++ so I haven't tried modifying that, but if there can't be support for wrapping vector fonts it should be in the readme.

Tested on an Inky Frame 7.3 with Pico W

Trantion897 avatar Jun 28 '24 20:06 Trantion897

Yeah, I ran in to this too. Here's a workaround you can use in your python code:

def wordwrap(text, scale, width, display):

    words = text.split()  # Splits by any whitespace and handles multiple spaces

    lines = []
    
    if words:

        current = words[0] # start with the first word...

        for word in words[1:]: # and then test the rest
            potential = current + " " + word
            if display.measure_text(potential,scale) <= width:
                # it fits, so keep it
                current = potential
            else:
                # too long, so start a new line
                lines.append(current)
                current = word

        lines.append(current) # don't forget the last line!

    return lines

which you then use something like this:

y=280
for line in wordwrap(message,1,500,display):
    display.text(line,25,y,scale=1)
    y+=25

mattdm avatar May 18 '25 22:05 mattdm