MagTag: what to do when you have too much text?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
iAmBen
 
Posts: 4
Joined: Sat May 21, 2022 10:09 am

MagTag: what to do when you have too much text?

Post by iAmBen »

Hi all,

I'm wanting to make what I assume is a fairly typical MagTag project which displays data from an API. The API returns an array of strings which will usually each map quite nicely to a line on the display. I say usually because the strings aren't of a fixed length so will sometimes be longer than 1 line and the number of strings returned will sometimes be greater than the number of lines I can fit on the display.

My questions:

Is there a programmatic way of telling how many characters I can fit on 1 line for a given font?
Is there a programmatic way of telling how many lines I can fit on the display for a given font?
Is there any best practice that I can read up on around how to handle situations where I'm receiving an amount of text that might be a bit too big for the display? (The aim is to try and handle these situations relatively gracefully, although I'm still working on what graceful looks like).

Many thanks in advance for any pointers!

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: MagTag: what to do when you have too much text?

Post by neradoc »

Hi, the adafruit_display_text library has a function to split the text into lines based on the actual length of the characters. (It returns a list of lines).
https://docs.circuitpython.org/projects ... _to_pixels

You can look at the source to learn how it does it and adapt that to your own needs. It shows using get_glyph and the Glyph class properties to measure the width of every characters to add up to the line's width. See the fontio module for those functions:
https://docs.circuitpython.org/en/lates ... index.html

Fixed-width fonts like the builtin terminalio.FONT can use `font.get_bounding_box()` to know the size of a character and do math with it.

User avatar
iAmBen
 
Posts: 4
Joined: Sat May 21, 2022 10:09 am

Re: MagTag: what to do when you have too much text?

Post by iAmBen »

That's incredibly useful. Looks like I can get the info I need from get_glyph as you say. Thank you!

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”