Tufty fonts problems

I got my Tufty yesterday and have been playing. I’m delighted with the supplied examples and the device as a whole.

I decided to explore the fonts in more detail and hit a couple of problems.

# Tufty_2040 Fonts demo
# Tony Goodhew 25th June, 2022
import utime
import random
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_TUFTY_2040

display = PicoGraphics(display=DISPLAY_TUFTY_2040)
WIDTH, HEIGHT = display.get_bounds()

#Set some colours
white = display.create_pen(200, 200, 255)
black = display.create_pen(0, 0, 0)
red = display.create_pen(255, 0, 0)
blue = display.create_pen(0 ,0, 255)
yellow = display.create_pen(255, 255 ,0)
green = display.create_pen(0, 255,0)

display.set_pen(black)
display.clear()
display.update()

display.set_pen(white)
display.text("Pimoroni", 20, 20, 240, 4)
display.text("Tufty_2040", 20, 45, 240, 4)
display.text("Font", 20, 70, 240, 4)
display.set_pen(yellow)
display.text("Demonstration", 20, 100, 240, 4)

display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("bitmap8")
display.text("Hello World", 0, 0, scale=2)

display.set_font("bitmap6")
display.text("Hello World", 0, 15, scale=2)

display.set_font("bitmap14_outline")
display.text("Pimoroni", 0, 25, scale=2)

display.set_font("sans")
display.text("Pimoroni", 0, 65, scale=1)

display.set_font("gothic")
display.text("Pimoroni", 0, 90, scale=1)

width = display.measure_text("Centred", scale=1)
x = int((320 - width)/2)
display.set_pen(blue)
display.text("Centred",x, 115, scale=1)

display.set_pen(green)
display.set_font("serif")
display.text("Pimoroni", 0, 140, scale=1)

display.set_pen(red)
display.set_font("serif_italic")
display.text("Pimoroni", 0, 170, scale=1)

display.set_pen(white)
display.set_font("cursive")
display.text("Pimoroni", 0, 200, scale=1.4)

display.set_font("serif")
display.set_pen(yellow)
display.text("Angle 300 deg", 190, 230, 200, scale=0.9, angle = 300)

display.set_font("sans")
display.set_pen(green)
display.text("Angle 30 deg", 130, 20, 30, scale=0.6, angle = 30)
display.update()
utime.sleep(3)

s=""
for i in range(32,128):
    if (i % 28) == 0:
        s = s+chr(32)
    s = s + chr(i)
print(s)

display.set_pen(black)
display.clear()

display.set_pen(white)
display.set_font("bitmap6")
#display.text(text, x, y, wordwrap, scale, angle, spacing)
display.set_pen(yellow)
display.text("bitmap6", 0, 10, 320,scale=3)
display.set_pen(white)
display.text(s, 0, 40, 320,scale=2)
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("bitmap8")
#display.text(text, x, y, wordwrap, scale, angle, spacing)
display.set_pen(yellow)
display.text("bitmap8", 0, 10, 320,scale=3)
display.set_pen(white)
display.text(s, 0, 40, 320,scale=2)
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("bitmap14_outline")
display.set_pen(yellow)
display.text("bitmap14_outline", 0, 10, 320,scale=2.2)
display.set_pen(white)
display.text(s, 0, 45, 300,scale=1.9)
display.update()
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("sans")
display.set_pen(yellow)
display.text("sans", 0, 10, 320,scale=1.7)
display.set_pen(white)
s = ""
for i in range(32,64):
    s = s+chr(i)
display.text(s, 0, 45, 300,scale=0.5)
s = ""
for i in range(64,96):
    s = s+chr(i)
display.text(s, 0, 65, 300,scale=0.5)
s = ""
for i in range(96,128):
    s = s+chr(i)
display.text(s, 0, 85, 300,scale=0.5)
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("serif")
display.set_pen(yellow)
display.text("serif", 0, 19, 320,scale=1.5)
display.set_pen(white)
s = ""
for i in range(32,64):
    s = s+chr(i)
display.text(s, 0, 45, 300,scale=0.45)
s = ""
for i in range(64,96):
    s = s+chr(i)
display.text(s, 0, 65, 300,scale=0.45)
s = ""
for i in range(96,128):
    s = s+chr(i)
display.text(s, 0, 85, 300,scale=0.45)
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("serif_italic")
display.set_pen(yellow)
display.text("serif_italic", 0, 19, 320,scale=1.5)
display.set_pen(white)
s = ""
for i in range(32,64):
    s = s+chr(i)
display.text(s, 0, 65, 300,scale=0.45)
s = ""
for i in range(64,96):
    s = s+chr(i)
display.text(s, 0, 85, 300,scale=0.45)
s = ""
for i in range(96,128):
    s = s+chr(i)
display.text(s, 0, 105, 300,scale=0.45)
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("gothic")
display.set_pen(yellow)
display.text("gothic", 0, 19, 320,scale=1.5)
display.set_pen(white)
s = ""
for i in range(32,64):
    s = s+chr(i)
display.text(s, 0, 65, 300,scale=0.45)
s = ""
for i in range(64,96):
    s = s+chr(i)
display.text(s, 0, 85, 300,scale=0.45)
s = ""
for i in range(96,128):
    s = s+chr(i)
display.text(s, 0, 105, 300,scale=0.45)
display.update()
utime.sleep(2)

display.set_pen(black)
display.clear()
display.set_pen(white)
display.set_font("cursive")
display.set_pen(yellow)
display.text("cursive", 0, 19, 320,scale=1.5)
display.set_pen(white)
s = ""
for i in range(32,64):
    s = s+chr(i)
display.text(s, 0, 65, 300,scale=0.3)
s = ""
for i in range(64,96):
    s = s+chr(i)
display.text(s, 0, 85, 300,scale=0.3)
s = ""
for i in range(96,128):
    s = s+chr(i)
display.text(s, 0, 105, 300,scale=0.3)
display.update()
utime.sleep(2)

The first was that the word wrap did not work as expected so I had to modify my code as inserted spaces did not always get picked up to break a line.

The second was that the final section, CURSIVE, just crashed without an error. Lost connection with Thonny.

Not sure what is going on. I’d be glad of a bit of help.

You know so much more about displaying graphics than I do, so please forgive me if I just confuse things. ;)

I don’t think this is your issue but I noticed
green = display.create_pen(0, 255,0)
which “I think” should be (0, 255, 0)

I see in some places you use
display.text("Demonstration", 20, 100, 240, 4)
and in others
display.text("Hello World", 0, 0, scale=2)

Isn’t the 240 in the 20, 100, 240, 4, the word wrap point? Or it used to be before the Pico Graphics update. It looks like you have old code and new code in the same file?

Having said that, I’m doing the same thing without issues, Pi Pico wise anyway. I don’t have my Tufty just yet.

I believe that spaces - white space - in statements are ignored unless between quotes for strings or for indents.

The ‘new’ text statement still has the wordwrap as 4th parameter but I think you can leave out unneeded parameters at the end.
display.text(text, x, y, wordwrap, scale, angle, spacing)

The code works perfectly until the font is changed to cursive. I had just copied the working code for gothic and changed the font.

A real puzzle?

I don’t know why the wordwrap isn’t working properly, but one puzzle is solved:

ASCII character 127 is a DEL character which isn’t usually printable.
I haven’t looked in the fonts to see what is defined for a DEL character.
It looks like the cursive font doesn’t have a 127 DEL character at all.
If you change your last range just to go to 126, it’ll show all the cursive letters you want, including the tilde ~.

for i in range(96,127):
    s = s+chr(i)

Thank you. It’s usually the end conditions that cause the problems.

I think wordwrap only works with the bitmap fonts. The vector (Hershey) font may have different character lengths, compare j and w. This make it harder to calculate the ‘space used so far’.
I’ll only use wordwrap with bitmapped fonts.