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

drawStringMaxWidth doesn't return 0 even though text fits #375

Open drount opened 2 years ago

drount commented 2 years ago

https://github.com/ThingPulse/esp8266-oled-ssd1306/blob/057629d467d6d57a518dcbb0e09726570f1d1940/src/OLEDDisplay.cpp#L716

Hello,

in every drawStringInternal there in an increase in line height so the last check: (yMove + lineNumber * lineHeight) >= this->height()

will always be true if the texts fill all the lines of the screen.

In order to solve, you can break the loop just after drawing the string:

drawStringResult = drawStringInternal(xMove, yMove + (lineNumber++) * lineHeight , &text[lastDrawnPos], preferredBreakpoint - lastDrawnPos, widthAtBreakpoint, true);
if (drawStringResult == 0) // we are past the display already?
        break;
if (firstLineChars == 0)
       firstLineChars = preferredBreakpoint;

and return:

if ((drawStringResult + lastDrawnPos) < length) {
    return firstLineChars ;
}
return 0;