From e8aa2f922b41864f45749950ab75a86a5d91dc6e Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 14 Apr 2023 15:00:50 -0500 Subject: [PATCH] Allow word wrapping to wrap on capital letters and underscores. --- Source/Engine/Render2D/Font.cpp | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Source/Engine/Render2D/Font.cpp b/Source/Engine/Render2D/Font.cpp index 0cbc30701..d80f7d19c 100644 --- a/Source/Engine/Render2D/Font.cpp +++ b/Source/Engine/Render2D/Font.cpp @@ -122,6 +122,12 @@ void Font::ProcessText(const StringView& text, Array& outputLines float lastWhitespaceX = 0; bool lastMoveLine = false; + int32 lastUpperIndex = INVALID_INDEX; + float lastUpperX = 0; + + int32 lastUnderscoreIndex = INVALID_INDEX; + float lastUnderscoreX = 0; + // Process each character to split text into single lines for (int32 currentIndex = 0; currentIndex < textLength;) { @@ -141,6 +147,21 @@ void Font::ProcessText(const StringView& text, Array& outputLines lastWhitespaceX = cursorX; } + // Check if character is an upper case letter + const bool isUpper = StringUtils::IsUpper(currentChar); + if (isUpper && currentIndex != 0) + { + lastUpperIndex = currentIndex; + lastUpperX = cursorX; + } + + const bool isUnderscore = currentChar == '_'; + if (isUnderscore) + { + lastUnderscoreIndex = currentIndex; + lastUnderscoreX = cursorX; + } + // Check if it's a newline character if (currentChar == '\n') { @@ -185,6 +206,20 @@ void Font::ProcessText(const StringView& text, Array& outputLines currentIndex = lastWhitespaceIndex + 1; nextCharIndex = currentIndex; } + else if (lastUpperIndex != INVALID_INDEX) + { + cursorX = lastUpperX; + tmpLine.LastCharIndex = lastUpperIndex - 1; + currentIndex = lastUpperIndex; + nextCharIndex = currentIndex; + } + else if (lastUnderscoreIndex != INVALID_INDEX) + { + cursorX = lastUnderscoreX; + tmpLine.LastCharIndex = lastUnderscoreIndex; + currentIndex = lastUnderscoreIndex + 1; + nextCharIndex = currentIndex; + } else { nextCharIndex = currentIndex; @@ -224,6 +259,12 @@ void Font::ProcessText(const StringView& text, Array& outputLines lastWhitespaceIndex = INVALID_INDEX; lastWhitespaceX = 0; + lastUpperIndex = INVALID_INDEX; + lastUpperX = 0; + + lastUnderscoreIndex = INVALID_INDEX; + lastUnderscoreX = 0; + previous.IsValid = false; }