From d9390233a6cf00f5989af05efbd4365534ffe3f9 Mon Sep 17 00:00:00 2001 From: Daniel Eichhorn Date: Thu, 31 Jan 2019 09:52:16 +0100 Subject: [PATCH] Have drawString return the max width for all drawn lines. Fixes issue #37 --- src/MiniGrafx.cpp | 9 +++++++-- src/MiniGrafx.h | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/MiniGrafx.cpp b/src/MiniGrafx.cpp index a13011c..dc176ee 100644 --- a/src/MiniGrafx.cpp +++ b/src/MiniGrafx.cpp @@ -255,7 +255,7 @@ void MiniGrafx::drawVerticalLine(int16_t x, int16_t y, int16_t length) { } -void MiniGrafx::drawString(int16_t xMove, int16_t yMove, String strUser) { +uint16_t MiniGrafx::drawString(int16_t xMove, int16_t yMove, String strUser) { uint16_t lineHeight = readFontData(fontData, HEIGHT_POS); // char* text must be freed! @@ -276,12 +276,17 @@ void MiniGrafx::drawString(int16_t xMove, int16_t yMove, String strUser) { uint16_t line = 0; char* textPart = strtok(text,"\n"); + uint16_t maxStringWidth = 0; + uint16_t stringWidth = 0; while (textPart != NULL) { uint16_t length = strlen(textPart); - drawStringInternal(xMove, yMove - yOffset + (line++) * lineHeight, textPart, length, getStringWidth(textPart, length)); + stringWidth = getStringWidth(textPart, length); + maxStringWidth = max(maxStringWidth, stringWidth); + drawStringInternal(xMove, yMove - yOffset + (line++) * lineHeight, textPart, length, stringWidth); textPart = strtok(NULL, "\n"); } free(text); + return stringWidth; } void MiniGrafx::drawStringMaxWidth(int16_t xMove, int16_t yMove, uint16_t maxLineWidth, String strUser) { diff --git a/src/MiniGrafx.h b/src/MiniGrafx.h index 2e6544b..4fa437e 100644 --- a/src/MiniGrafx.h +++ b/src/MiniGrafx.h @@ -106,7 +106,7 @@ class MiniGrafx { void fillCircle(int16_t x0, int16_t y0, int16_t radius); void drawHorizontalLine(int16_t x, int16_t y, int16_t length); void drawVerticalLine(int16_t x, int16_t y, int16_t length); - void drawString(int16_t xMove, int16_t yMove, String strUser); + uint16_t drawString(int16_t xMove, int16_t yMove, String strUser); // Draws a String with a maximum width at the given location. // If the given String is wider than the specified width