Fix DrawTextRec (#1843)

* fix text wrapping

* fix indent

* fix indent

* fix indent

* fix DrawTextRec
This commit is contained in:
Diesirae 2021-06-22 18:16:04 +02:00 committed by GitHub
parent 52f1c7df6d
commit 30a0f6f292
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 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 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 // Word/character wrapping mechanism variables
enum { MEASURE_STATE = 0, DRAW_STATE = 1 }; 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; if (codepoint == 0x3f) codepointByteCount = 1;
i += (codepointByteCount - 1); i += (codepointByteCount - 1);
int glyphWidth = 0; float glyphWidth = 0;
if (codepoint != '\n') if (codepoint != '\n')
{ {
glyphWidth = (font.chars[index].advanceX == 0)? glyphWidth = (font.chars[index].advanceX == 0) ? font.recs[index].width * scaleFactor : font.chars[index].advanceX * scaleFactor;
(int)(font.recs[index].width*scaleFactor + spacing):
(int)(font.chars[index].advanceX*scaleFactor + spacing); 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 // 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 // Ref: http://jkorpela.fi/chars/spaces.html
if ((codepoint == ' ') || (codepoint == '\t') || (codepoint == '\n')) endLine = i; if ((codepoint == ' ') || (codepoint == '\t') || (codepoint == '\n')) endLine = i;
if ((textOffsetX + glyphWidth + 1) >= rec.width) if ((textOffsetX + glyphWidth) > rec.width)
{ {
endLine = (endLine < 1)? i : endLine; endLine = (endLine < 1)? i : endLine;
if (i == endLine) endLine -= codepointByteCount; 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) else if ((i + 1) == length)
{ {
endLine = i; endLine = i;
state = !state; state = !state;
} }
else if (codepoint == '\n') 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) if (!wordWrap)
{ {
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); textOffsetY += (font.baseSize + font.baseSize / 2) * scaleFactor;
textOffsetX = 0; textOffsetX = 0;
} }
} }
else 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; textOffsetX = 0;
} }
// When text overflows rectangle height limit, just stop drawing // 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 // Draw selection background
bool isGlyphSelected = false; bool isGlyphSelected = false;
if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength))) 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; isGlyphSelected = true;
} }
@ -1011,7 +1011,7 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f
if (wordWrap && (i == endLine)) if (wordWrap && (i == endLine))
{ {
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); textOffsetY += (font.baseSize + font.baseSize / 2) * scaleFactor;
textOffsetX = 0; textOffsetX = 0;
startLine = endLine; startLine = endLine;
endLine = -1; endLine = -1;