ThingPulse / esp8266-oled-ssd1306

Driver for the SSD1306 and SH1106 based 128x64, 128x32, 64x48 pixel OLED display running on ESP8266/ESP32
https://thingpulse.com
Other
2.01k stars 638 forks source link

add return values to drawString & drawStringMaxWidth #365

Closed seife closed 2 years ago

seife commented 2 years ago

The idea is, to give the calling code a bit more information about what was drawn.

drawString() will return the number of characters that have been drawn before the display ended, so that the calling code can use this to draw the rest of the string into a new line, or to implement some kind of horizontal scrolling.

drawStringMaxWidth() will return the number of characters drawn in the first line, so that the calling code can start drawing with the second line in the next iteration, to implement a kind of vertical scrolling.

To get the counting of UTF-8 characters right, I removed utf8ascii() and instead convert the strings on the fly while processing. This might save some runtime memory (the copy that holds the decoded string is not needed anymore) and slightly reduce code size, but I have not measured this extensively.

When breaking up lines in drawStringMaxWidth(), trailing and leading spaces in each line are now removed, leading to smoother display, e.g. in TEXT_ALIGN_CENTER mode. This is the only change that might be visible when the other new features (return codes) are not used. Other than that, the code should display identically with unchanged usage.

This is intended as a draft / suggestion for discussion, not necessarily as a finished solution (even though it works well with my test cases ;-))

marcelstoer commented 2 years ago

Thanks! I like the overall idea as it opens up new possibilities. I only just scanned the code briefly and haven't tested it yet. We could keep utf8ascii even though it's not used anymore and thus avoid breaking the API, correct?

seife commented 2 years ago

OK. I did not think of classes derived from OLEDDisplay, when I was removing utf8ascii because it's "only" protected.

I rebased the branch on current master and edited the first commit in the series to not remove utf8ascii.