From c475d72110db0010fa07f169654988c0856ee9f4 Mon Sep 17 00:00:00 2001 From: gymnast86 Date: Fri, 21 Aug 2026 18:30:48 -0700 Subject: [PATCH] fix line width calculation in breakLines --- mods/randomizer/generator/utility/text.cpp | 24 ++++++++++------------ mods/randomizer/generator/utility/text.hpp | 8 ++++---- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/mods/randomizer/generator/utility/text.cpp b/mods/randomizer/generator/utility/text.cpp index d42c5f42a0..f3bc5240f3 100644 --- a/mods/randomizer/generator/utility/text.cpp +++ b/mods/randomizer/generator/utility/text.cpp @@ -60,7 +60,7 @@ namespace randomizer { } } - void Text::BreakLines(int maxLineWidth /*= MAX_LINE_WIDTH_ITEM_TEXTBOX*/) { + void Text::BreakLines(float maxLineWidth /*= MAX_LINE_WIDTH_ITEM_TEXTBOX*/) { for (auto& text : mText) { breakLines(text, maxLineWidth); } @@ -441,21 +441,21 @@ namespace randomizer { {"", "\x1A\x06\xFF\x00\x00\x0B"sv}, }; - void breakLines(std::string& str, int maxLineWidth) { + void breakLines(std::string& str, float maxStrLength) { // Randomizer Only shouldn't rely on needing access to the iso #ifndef RANDOMIZER_ONLY // Get game's font auto gameFont = mDoExt_getMesgFont(); #endif - int curLineWidth = 0; + float curLineWidth = 0.f; size_t i = 0; size_t previousSpace = 0; while (i < str.length()) { // Skip over control codes since they don't get displayed std::string code{}; - for (const auto& [messageCode, replacement] : messageCodes) { + for (const auto& messageCode : messageCodes | std::views::keys) { if (str.substr(i, messageCode.length()) == messageCode) { code = messageCode; break; @@ -464,9 +464,8 @@ namespace randomizer { if (!code.empty()) { // Assume worst case for player name width. - // 8 chars max * max char width if (code == "") { - curLineWidth += 8 * 21; + curLineWidth += 8.f; } i += code.length(); continue; @@ -479,27 +478,26 @@ namespace randomizer { } // If we encounter an already inserted newline, reset the counter else if (str[i] == '\n') { - curLineWidth = 0; + curLineWidth = 0.f; ++i; continue; } #ifndef RANDOMIZER_ONLY - JUTFont::TWidth width{}; - gameFont->getWidthEntry(str[i], &width); - curLineWidth += /*width.field_0x0 + */width.field_0x1; + auto width = gameFont->getWidth(str[i]); + curLineWidth += width / static_cast(gameFont->getCellWidth()); #else // Assume worst case with no iso access - curLineWidth += 21; + curLineWidth += 1; #endif // If we exceed the maximum line width, replace the // previous space with a newline and start counting // from the newline again - if (curLineWidth > maxLineWidth) { + if (curLineWidth > maxStrLength) { str[previousSpace] = '\n'; i = previousSpace; - curLineWidth = 0; + curLineWidth = 0.f; } ++i; diff --git a/mods/randomizer/generator/utility/text.hpp b/mods/randomizer/generator/utility/text.hpp index 400991f901..aec16f3ebb 100644 --- a/mods/randomizer/generator/utility/text.hpp +++ b/mods/randomizer/generator/utility/text.hpp @@ -60,8 +60,8 @@ namespace randomizer { PLURALITY_MAX, }; - static constexpr size_t MAX_LINE_WIDTH_ITEM_TEXTBOX = 441; - static constexpr size_t MAX_LINE_WIDTH_NORMAL_TEXTBOX = 750; + static constexpr float MAX_LINE_WIDTH_ITEM_TEXTBOX = 14.0f; + static constexpr float MAX_LINE_WIDTH_NORMAL_TEXTBOX = 17.0f; static constexpr size_t MAX_NEWLINES_PER_MESSAGE = 40; static constexpr size_t LINES_PER_BOX = 4; @@ -80,7 +80,7 @@ namespace randomizer { */ void Replace(const std::string& oldText, const Text& replacementText, uint32_t count = std::numeric_limits::max()); void Replace(const std::string& oldText, const std::string& replacementText, uint32_t count = std::numeric_limits::max()); - void BreakLines(int maxLineWidth = MAX_LINE_WIDTH_NORMAL_TEXTBOX); + void BreakLines(float maxLineWidth = MAX_LINE_WIDTH_NORMAL_TEXTBOX); // Inserts newlines to pad the text to the next box void PadToNextBox(); @@ -126,7 +126,7 @@ namespace randomizer { Text addColor(const Text& t, Text::Color color, int count = 1); // Adds newlines in appropriate places to properly break the text string for textboxes - void breakLines(std::string& str, int maxLineWidth); + void breakLines(std::string& str, float maxStrLength); // Replaces the message codes in the string with the ingame hex equivalents void applyMessageCodes(std::string&);