From c4893baf79d407bfca5b36c5a7e7b010fa1cf16e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zaremba?= Date: Wed, 12 Nov 2025 01:11:08 +0100 Subject: [PATCH] Fix word wrap --- client/widgets/CComponent.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/client/widgets/CComponent.cpp b/client/widgets/CComponent.cpp index ccd19ce91..ec94777b3 100644 --- a/client/widgets/CComponent.cpp +++ b/client/widgets/CComponent.cpp @@ -96,14 +96,29 @@ void CComponent::init(ComponentType Type, ComponentSubType Subtype, std::optiona if(Type == ComponentType::RESOURCE && !ValText.empty()) max = 80; - std::vector textLines = CMessage::breakText(getSubtitle(), std::max(max, pos.w), font); const auto & fontPtr = ENGINE->renderHandler().loadFont(font); + { + std::string s = getSubtitle(); + + // remove color markup: "{color|" + s = std::regex_replace(s, std::regex("\\{[^|}]*\\|"), ""); + // remove closing braces "}" + s.erase(std::remove(s.begin(), s.end(), '}'), s.end()); + + size_t longestWordLen = 0; + for(std::istringstream iss(s); iss >> s; ) + longestWordLen = std::max(longestWordLen, fontPtr->getStringWidth(s)); + + max = std::max(max, longestWordLen + 8); + } + + std::vector textLines = CMessage::breakText(getSubtitle(), std::max(max, pos.w), font); + const int height = static_cast(fontPtr->getLineHeight()); for(auto & line : textLines) { auto label = std::make_shared(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line); - pos.h += height; if(label->pos.w > pos.w) {