From eca6b1a7ff4b3d89c14d225adfb006558bb996e6 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 29 Jul 2023 19:30:52 +0200 Subject: [PATCH] Added a reference word-wrap implementation to be applied to current `GuiDrawText()` --- src/raygui.h | 129 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 3 deletions(-) diff --git a/src/raygui.h b/src/raygui.h index d98d1cb..623030e 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -2867,7 +2867,7 @@ int GuiProgressBar(Rectangle bounds, const char *textLeft, const char *textRight GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x + (int)progress.width + 1, bounds.y + bounds.height - 1, bounds.width - (int)progress.width - 1, (float)GuiGetStyle(PROGRESSBAR, BORDER_WIDTH) }, 0, BLANK, GetColor(GuiGetStyle(PROGRESSBAR, BORDER_COLOR_NORMAL))); GuiDrawRectangle(RAYGUI_CLITERAL(Rectangle){ bounds.x + bounds.width - 1, bounds.y + 1, (float)GuiGetStyle(PROGRESSBAR, BORDER_WIDTH), bounds.height - 2 }, 0, BLANK, GetColor(GuiGetStyle(PROGRESSBAR, BORDER_COLOR_NORMAL))); } - + // Draw slider internal progress bar (depends on state) GuiDrawRectangle(progress, 0, BLANK, GetColor(GuiGetStyle(PROGRESSBAR, BASE_COLOR_PRESSED))); } @@ -3401,7 +3401,7 @@ int GuiColorPicker(Rectangle bounds, const char *text, Color *color) int GuiColorPickerHSV(Rectangle bounds, const char *text, Vector3 *colorHsv) { int result = 0; - + Vector3 tempHsv = { 0 }; if (colorHsv == NULL) @@ -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++){ } 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 textOffsetY = 0; float textOffsetX = 0.0f; @@ -4567,7 +4690,7 @@ static void GuiDrawText(const char *text, Rectangle bounds, int alignment, Color } else if (wrapMode == 2) // 2-WORD_WRAP { - // TODO: Word wrap mode requires previously measured text to last space! + // TODO: Word wrap mode requires previously measured text to last space! } } else lastSpacePos = c;