Skip to content

Commit

Permalink
Have drawString return the max width for all drawn lines. Fixes issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
squix78 committed Jan 31, 2019
1 parent f049f94 commit d939023
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/MiniGrafx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/MiniGrafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d939023

Please sign in to comment.