Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Wrapping Text on 1.3" Color TFT Bonnet?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Wrapping Text on 1.3" Color TFT Bonnet?

by eat_sleep_code on Fri May 14, 2021 8:41 pm

I just purchased a Adafruit 1.3" Color TFT Bonnet for Raspberry Pi - 240x240 TFT + Joystick Add-on [url](https://www.adafruit.com/product/4506)[/url].

I pretty much have it all working with my Python script. However, I am trying to figure out if there is a way to wrap text lines at word breaks that are longer than the width of the screen?

This is my display write method:

Code: Select all | TOGGLE FULL SIZE
def write(inputLines = [], x = 0, y = 0, textColor='#FFFFFF'):
      global rgbDisplay
      global rgbImage
      global draw
      global font
      global rotation

      Backlight.on()
      Text.clear()
      
      for line in inputLines:
         draw.text((x, y), str(line), font=font, fill=textColor)
         y += font.getsize(str(line))[1]

      rgbDisplay.image(rgbImage, rotation)

eat_sleep_code
 
Posts: 42
Joined: Sat Dec 30, 2017 1:30 am

Re: Wrapping Text on 1.3" Color TFT Bonnet?

by mikeysklar on Tue May 18, 2021 1:01 pm

@eat_sleep_code,

Can you post your full code so I can see which libraries you are using for drawing out the TFT. The guide for this product supports quite a few displays.

We do have word wrap in a number of libraries such as adafruit_display_text, but I don't think you are using that one.

https://circuitpython.readthedocs.io/pr ... st-example

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: Wrapping Text on 1.3" Color TFT Bonnet?

by eat_sleep_code on Tue May 18, 2021 3:10 pm

Ended up finding my own solution -- after searching around found some posts that got me started in the right direction.

This appears to work -- at least for my use case:

Code: Select all | TOGGLE FULL SIZE
   def write(inputLines = [], x = 0, y = 0, textColor = '#FFFFFF'):
      global rgbDisplay
      global rgbImage
      global draw
      global font
      global width
      global rotation

      Text.clear()
      
      for unprocessedLine in inputLines:
         wrappedLines = Text.wrap(str(unprocessedLine), font, width)
         for line in wrappedLines:
            draw.text((x, y), line, font=font, fill=textColor)
            y += Text.width(line, font, 1)

      rgbDisplay.image(rgbImage, rotation)


   def width(text, font, index = 0):
      textWidth = font.getsize(text)[index]
      return textWidth


   def wrap(text, font, maxWidth, maxLines = 8):
      words = text.split()
      lines = []
      while(words):
         word = words.pop(0)
         if len(lines) > 0 and (Text.width(" ".join(lines[-1]), font) + 1 + Text.width(word, font)) < maxWidth:
            lines[-1].append(word)
         else:
            chunk = len(word)
            while chunk > 0:
               while (Text.width(word[:chunk],font) > maxWidth and chunk > 1):
                     chunk -= 1
               lines.append( [word[:chunk]] )
               word = word[chunk:]
               chunk = len(word)
      lines = [" ".join(words) for words in lines]
      if maxLines and len(lines) > maxLines:
         lines[maxLines-1] = lines[maxLines-1][:-1] + "..."
      return lines[:maxLines]

eat_sleep_code
 
Posts: 42
Joined: Sat Dec 30, 2017 1:30 am

Re: Wrapping Text on 1.3" Color TFT Bonnet?

by mikeysklar on Wed May 19, 2021 3:55 pm

Thank you for sharing your solution. It looks like a good universal algorithm for chunking text into clean line wraps.

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.