Skip to content

Commit 59542b4

Browse files
authored
Merge pull request #62 from squix78/fix-drawStringMaxWidth-bug
Calculating `strWidth` in drawStringMaxWidth correctly
2 parents 6b303e5 + 0de1f07 commit 59542b4

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

OLEDDisplay.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,17 @@ void OLEDDisplay::drawStringMaxWidth(int16_t xMove, int16_t yMove, uint16_t maxL
431431
}
432432

433433
if (strWidth >= maxLineWidth) {
434-
preferredBreakpoint = preferredBreakpoint ? preferredBreakpoint : i;
435-
widthAtBreakpoint = preferredBreakpoint ? widthAtBreakpoint : strWidth;
436-
434+
if (preferredBreakpoint == 0) {
435+
preferredBreakpoint = i;
436+
widthAtBreakpoint = strWidth;
437+
}
437438
drawStringInternal(xMove, yMove + (lineNumber++) * lineHeight , &text[lastDrawnPos], preferredBreakpoint - lastDrawnPos, widthAtBreakpoint);
438-
lastDrawnPos = preferredBreakpoint + 1; strWidth = 0; preferredBreakpoint = 0;
439+
lastDrawnPos = preferredBreakpoint + 1;
440+
// It is possible that we did not draw all letters to i so we need
441+
// to account for the width of the chars from `i - preferredBreakpoint`
442+
// by calculating the width we did not draw yet.
443+
strWidth = strWidth - widthAtBreakpoint;
444+
preferredBreakpoint = 0;
439445
}
440446
}
441447

0 commit comments

Comments
 (0)