From c0c0d58219b936d6867a534d3779176acca6fda7 Mon Sep 17 00:00:00 2001 From: phantom10111 Date: Mon, 11 Mar 2024 07:27:01 +0100 Subject: [PATCH] Fix coloring of multiline BitmapText BitmapText internally stores characters with newlines removed, since they don't need to be rendered. Due to this, BitmapText::AddAttributes() needs to recalculate attribute positions and lengths in order to apply them in the correct place. Unfortuantely the code would calculate the position incorrectly and completely ignore length. Fix it so that everything is correct even in multiline text. --- src/BitmapText.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/BitmapText.cpp b/src/BitmapText.cpp index d3ecc8376b..b10ab28af9 100644 --- a/src/BitmapText.cpp +++ b/src/BitmapText.cpp @@ -879,18 +879,39 @@ BitmapText::Attribute BitmapText::GetDefaultAttribute() const void BitmapText::AddAttribute( std::size_t iPos, const Attribute &attr ) { // Fixup position for new lines. + Attribute newAttr = attr; + std::vector::const_iterator lineIter = m_wTextLines.cbegin(); + int iLines = 0; std::size_t iAdjustedPos = iPos; - for (std::wstring const & line : m_wTextLines) + for( ; lineIter != m_wTextLines.cend(); ++lineIter ) { - std::size_t length = line.length(); - if( length >= iAdjustedPos ) + std::size_t length = lineIter->length() + 1; // +1 to account for implicit newline at the end + if( length > iAdjustedPos ) break; iAdjustedPos -= length; ++iLines; } - m_mAttributes[iPos-iLines] = attr; + + if( newAttr.length > 0 ) + { + // Fixup length for new lines. + std::size_t iAdjustedEndPos = iAdjustedPos + newAttr.length; + for( ; lineIter != m_wTextLines.cend(); ++lineIter ) + { + std::size_t length = lineIter->length() + 1; // +1 to account for implicit newline at the end + if( length > iAdjustedEndPos || newAttr.length == 0 ) + break; + iAdjustedEndPos -= length; + newAttr.length -= 1; + } + } + + if( newAttr.length == 0 ) // Attribute doesn't cover any printable characters + return; + + m_mAttributes[iPos-iLines] = newAttr; m_bHasGlowAttribute = m_bHasGlowAttribute || attr.glow.a > 0.0001f; }