From 0de1f0791512890e2a24bb8bc997562fc07fdc77 Mon Sep 17 00:00:00 2001
From: Fabrice Weinberg <Fabrice@weinberg.me>
Date: Sat, 9 Jul 2016 16:56:09 +0200
Subject: [PATCH] Fixes #61  by calculating strWidth in drawStringMaxWidth
 correctly

---
 OLEDDisplay.cpp | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/OLEDDisplay.cpp b/OLEDDisplay.cpp
index 5ebb433..5e699ee 100644
--- a/OLEDDisplay.cpp
+++ b/OLEDDisplay.cpp
@@ -427,11 +427,17 @@ void OLEDDisplay::drawStringMaxWidth(int16_t xMove, int16_t yMove, uint16_t maxL
     }
 
     if (strWidth >= maxLineWidth) {
-      preferredBreakpoint = preferredBreakpoint ? preferredBreakpoint : i;
-      widthAtBreakpoint = preferredBreakpoint ? widthAtBreakpoint : strWidth;
-
+      if (preferredBreakpoint == 0) {
+        preferredBreakpoint = i;
+        widthAtBreakpoint = strWidth;
+      }
       drawStringInternal(xMove, yMove + (lineNumber++) * lineHeight , &text[lastDrawnPos], preferredBreakpoint - lastDrawnPos, widthAtBreakpoint);
-      lastDrawnPos = preferredBreakpoint + 1; strWidth = 0; preferredBreakpoint = 0;
+      lastDrawnPos = preferredBreakpoint + 1;
+      // It is possible that we did not draw all letters to i so we need
+      // to account for the width of the chars from `i - preferredBreakpoint`
+      // by calculating the width we did not draw yet.
+      strWidth = strWidth - widthAtBreakpoint;
+      preferredBreakpoint = 0;
     }
   }
 
-- 
GitLab