diff options
| author | Diesirae <[email protected]> | 2021-06-22 18:16:04 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-22 18:16:04 +0200 |
| commit | 30a0f6f2925fa287719e9825437230361bb26125 (patch) | |
| tree | 245ba9f2bfd118bc6e6d18cc15022515bf4f5e40 | |
| parent | 52f1c7df6d6743a9078b9d56f68770b40c02b2db (diff) | |
| download | raylib-30a0f6f2925fa287719e9825437230361bb26125.tar.gz raylib-30a0f6f2925fa287719e9825437230361bb26125.zip | |
Fix DrawTextRec (#1843)
* fix text wrapping
* fix indent
* fix indent
* fix indent
* fix DrawTextRec
| -rw-r--r-- | src/text.c | 28 |
1 files changed, 14 insertions, 14 deletions
@@ -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; |
