1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

Merge pull request #6311 from MichalZr6/fix_word_wrap

Prevent mid-word wrapping in component captions (artifact names)
This commit is contained in:
Ivan Savenko
2025-11-17 08:51:29 +02:00
committed by GitHub

View File

@@ -96,14 +96,29 @@ void CComponent::init(ComponentType Type, ComponentSubType Subtype, std::optiona
if(Type == ComponentType::RESOURCE && !ValText.empty()) if(Type == ComponentType::RESOURCE && !ValText.empty())
max = 80; max = 80;
std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(max, pos.w), font);
const auto & fontPtr = ENGINE->renderHandler().loadFont(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<int>(max, longestWordLen + 8);
}
std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(max, pos.w), font);
const int height = static_cast<int>(fontPtr->getLineHeight()); const int height = static_cast<int>(fontPtr->getLineHeight());
for(auto & line : textLines) for(auto & line : textLines)
{ {
auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line); auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line);
pos.h += height; pos.h += height;
if(label->pos.w > pos.w) if(label->pos.w > pos.w)
{ {