Text wrapping / Font Metrics in Display Rendering Engine?

is there some way to calculate the length that a string would take up on the screen with a specific (non-fixed width) font?
Or a way to automatically split a string that is too long to be displayed in one row on the given display into suitable sub-strings?
(For example, the Qt library has QFontMetrics and the wordwrap property for QLabel to achieve this)
Any best practices for ESPhome?

1 Like

If there is a C++ library for that, then some sort of custom component may do it.

Digging deeper into the API, it looks like DisplayBuffer already has something like this:

That should allow to detect if a string is too large to be fully visible on a display.
Still need to act on that information though - either trying to split the string into smaller substrings and print them as rows, or maybe create some text scrolling animation. Not sure if that can be done in a lambda though. Would be nice if the Display rendering API would allow to call something like “Pan this (prerendered text) image in this rect on x-axis with this speed”.

Hi @code_talker, a bit too late to the party but I have answered this question here

1 Like

Thanks. Looks elegant.
For proper wrapping, the Position in the String would still have to be calculated somehow, maybe simply by iteratively checking the width, or some binary search strategy.
when I get around to revisit this project, I’ll Post an update.

the way I see it, the position is irrelevant, the width of a string does not change with the position, no matter where you place it :slight_smile:

What you need to care about is layouting. I have my own layout algorithm which requires knowing the width of the different objects I am displaying.