From 30a0f6f2925fa287719e9825437230361bb26125 Mon Sep 17 00:00:00 2001 From: Diesirae <15613425+n67094@users.noreply.github.com> Date: Tue, 22 Jun 2021 18:16:04 +0200 Subject: [PATCH] Fix DrawTextRec (#1843) * fix text wrapping * fix indent * fix indent * fix indent * fix DrawTextRec --- src/text.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/text.c b/src/text.c index e18afe41..a5e87121 100644 --- a/src/text.c +++ b/src/text.c @@ -901,10 +901,10 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f { int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop - int textOffsetY = 0; // Offset between lines (on line break '\n') + float textOffsetY = 0; // Offset between lines (on line break '\n') float textOffsetX = 0.0f; // Offset X to next character to draw - float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor + float scaleFactor = fontSize/(float)font.baseSize; // Character quad scaling factor // Word/character wrapping mechanism variables enum { MEASURE_STATE = 0, DRAW_STATE = 1 }; @@ -926,12 +926,13 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f if (codepoint == 0x3f) codepointByteCount = 1; i += (codepointByteCount - 1); - int glyphWidth = 0; + float glyphWidth = 0; if (codepoint != '\n') { - glyphWidth = (font.chars[index].advanceX == 0)? - (int)(font.recs[index].width*scaleFactor + spacing): - (int)(font.chars[index].advanceX*scaleFactor + spacing); + glyphWidth = (font.chars[index].advanceX == 0) ? font.recs[index].width * scaleFactor : font.chars[index].advanceX * scaleFactor; + + if (i + 1 < length) + glyphWidth = glyphWidth + spacing; } // NOTE: When wordWrap is ON we first measure how much of the text we can draw before going outside of the rec container @@ -945,7 +946,7 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f // Ref: http://jkorpela.fi/chars/spaces.html if ((codepoint == ' ') || (codepoint == '\t') || (codepoint == '\n')) endLine = i; - if ((textOffsetX + glyphWidth + 1) >= rec.width) + if ((textOffsetX + glyphWidth) > rec.width) { endLine = (endLine < 1)? i : endLine; if (i == endLine) endLine -= codepointByteCount; @@ -956,7 +957,6 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f else if ((i + 1) == length) { endLine = i; - state = !state; } else if (codepoint == '\n') state = !state; @@ -979,26 +979,26 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f { if (!wordWrap) { - textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); + textOffsetY += (font.baseSize + font.baseSize / 2) * scaleFactor; textOffsetX = 0; } } else { - if (!wordWrap && ((textOffsetX + glyphWidth + 1) >= rec.width)) + if (!wordWrap && ((textOffsetX + glyphWidth) > rec.width)) { - textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); + textOffsetY += (font.baseSize + font.baseSize / 2) * scaleFactor; textOffsetX = 0; } // When text overflows rectangle height limit, just stop drawing - if ((textOffsetY + (int)(font.baseSize*scaleFactor)) > rec.height) break; + if ((textOffsetY + font.baseSize*scaleFactor) > rec.height) break; // Draw selection background bool isGlyphSelected = false; if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength))) { - DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, (float)glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint); + DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize * scaleFactor }, selectBackTint); isGlyphSelected = true; } @@ -1011,7 +1011,7 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f if (wordWrap && (i == endLine)) { - textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); + textOffsetY += (font.baseSize + font.baseSize / 2) * scaleFactor; textOffsetX = 0; startLine = endLine; endLine = -1;