Added a reference word-wrap implementation to be applied to current GuiDrawText()

This commit is contained in:
Ray 2023-07-29 19:30:52 +02:00
parent c8260e22de
commit eca6b1a7ff

View File

@ -4524,6 +4524,129 @@ static void GuiDrawText(const char *text, Rectangle bounds, int alignment, Color
for (int c = 0; (lines[i][c] != '\0') && (lines[i][c] != '\n'); c++, lineSize++){ } for (int c = 0; (lines[i][c] != '\0') && (lines[i][c] != '\n'); c++, lineSize++){ }
float scaleFactor = (float)GuiGetStyle(DEFAULT, TEXT_SIZE)/guiFont.baseSize; float scaleFactor = (float)GuiGetStyle(DEFAULT, TEXT_SIZE)/guiFont.baseSize;
/*
// TODO: WARNING: For wordwrap, text must be measured from space to space before being drawn!
int wrapModeState = 0; // 0-TEXT_MEASURING, 1-TEXT_DRAWING
float textOffsetY = 0.0f; // Offset between wordwrap lines
float textOffsetX = 0.0f; // Offset X to next character to draw
int startLine = -1; // Index where to begin drawing (where a line begins)
int endLine = -1; // Index where to stop drawing (where a line ends)
int lastk = -1; // Holds last value of the character position
for (int i = 0, k = 0; i < lineSize; i++, k++)
{
// Get next codepoint from byte string and glyph index in font
int codepointByteCount = 0;
int codepoint = GetCodepoint(&text[i], &codepointByteCount);
int index = GetGlyphIndex(guiFont, codepoint);
// NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
// but we need to draw all of the bad bytes using the '?' symbol moving one byte
if (codepoint == 0x3f) codepointByteCount = 1;
i += (codepointByteCount - 1);
float glyphWidth = 0;
if (codepoint != '\n')
{
glyphWidth = (guiFont.glyphs[index].advanceX == 0)? guiFont.recs[index].width*scaleFactor : guiFont.glyphs[index].advanceX*scaleFactor;
if (i + 1 < lineSize) glyphWidth = glyphWidth + (float)GuiGetStyle(DEFAULT, TEXT_SPACING);
}
// NOTE: When wordWrap is ON we first measure how much of the text we can draw before going outside of the rec container
// We store this info in startLine and endLine, then we change states, draw the text between those two variables
// and change states again and again recursively until the end of the text (or until we get outside of the container).
// When wordWrap is OFF we don't need the measure state so we go to the drawing state immediately
// and begin drawing on the next line before we can get outside the container.
if (wrapModeState == 0) // TEXT_MEASURING
{
// TODO: There are multiple types of spaces in UNICODE, maybe it's a good idea to add support for more
// Ref: http://jkorpela.fi/chars/spaces.html
if ((codepoint == ' ') || (codepoint == '\t') || (codepoint == '\n')) endLine = i;
if ((textOffsetX + glyphWidth) > bounds.width)
{
endLine = (endLine < 1)? i : endLine;
if (i == endLine) endLine -= codepointByteCount;
if ((startLine + codepointByteCount) == endLine) endLine = (i - codepointByteCount);
wrapModeState = !wrapModeState;
}
else if ((i + 1) == lineSize)
{
endLine = i;
wrapModeState = !wrapModeState;
}
//else if (codepoint == '\n') state = !state;
if (wrapModeState == 1) // TEXT_DRAWING
{
textOffsetX = 0;
i = startLine;
glyphWidth = 0;
// Save character position when we switch states
int tmp = lastk;
lastk = k - 1;
k = tmp;
}
}
else
{
if (codepoint == '\n')
{
if (wrapMode != 2) // WORD_WRAP
{
textOffsetY += (guiFont.baseSize + guiFont.baseSize/2)*scaleFactor;
textOffsetX = 0;
}
}
else
{
if ((wrapMode == 1) && ((textOffsetX + glyphWidth) > bounds.width)) // CHAR_WRAP
{
textOffsetY += (guiFont.baseSize + guiFont.baseSize/2)*scaleFactor;
textOffsetX = 0;
}
// When text overflows rectangle height limit, just stop drawing
if ((textOffsetY + guiFont.baseSize*scaleFactor) > bounds.height) break;
// Draw text selected background
//bool isGlyphSelected = false;
//if ((selectStart >= 0) && (k >= selectStart) && (k < (selectStart + selectLength)))
//{
// DrawRectangleRec((Rectangle){ rec.x + textOffsetX - 1, rec.y + textOffsetY, glyphWidth, (float)font.baseSize*scaleFactor }, selectBackTint);
// isGlyphSelected = true;
//}
// Draw current character glyph
if ((codepoint != ' ') && (codepoint != '\t'))
{
DrawTextCodepoint(guiFont, codepoint, RAYGUI_CLITERAL(Vector2){ boundsPos.x + textOffsetX, boundsPos.y + textOffsetY }, (float)GuiGetStyle(DEFAULT, TEXT_SIZE), tint); // isGlyphSelected? selectTint : tint);
}
}
if ((wrapMode == 2) && (i == endLine)) // WORD_WRAP
{
textOffsetY += (guiFont.baseSize + guiFont.baseSize/2)*scaleFactor;
textOffsetX = 0;
startLine = endLine;
endLine = -1;
glyphWidth = 0;
//selectStart += lastk - k;
k = lastk;
wrapModeState = !wrapModeState;
}
}
if ((textOffsetX != 0) || (codepoint != ' ')) textOffsetX += glyphWidth; // Avoid leading spaces
}
*/
int lastSpacePos = 0; int lastSpacePos = 0;
int textOffsetY = 0; int textOffsetY = 0;
float textOffsetX = 0.0f; float textOffsetX = 0.0f;