From 8e820a988eff3fb962518037ed5bd952e32c7761 Mon Sep 17 00:00:00 2001 From: trompetin17 Date: Mon, 3 Nov 2025 13:12:21 -0500 Subject: [PATCH] Fix exceeding input boundaries in console After 734386ce9f the new font system calculate height and cap height, previously was having some magic number. Now we are using cap height divide 2 to give a more nice space. Remove an extra pixel in the buffer and improves background color for better reading and eye care. This also support the change console font option added in d549cbeeaa Fixes: #8351 --- source/graphics/FontMetrics.cpp | 2 +- source/graphics/FontMetrics.h | 2 +- source/ps/CConsole.cpp | 23 ++++++++++++----------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/source/graphics/FontMetrics.cpp b/source/graphics/FontMetrics.cpp index 91ac545d28..2931296a9c 100644 --- a/source/graphics/FontMetrics.cpp +++ b/source/graphics/FontMetrics.cpp @@ -56,7 +56,7 @@ void CFontMetrics::CalculateStringSize(const wchar_t* string, float& w, float& h m_Font->CalculateStringSize(string, w, h); } -float CFontMetrics::GetCapHeight() +float CFontMetrics::GetCapHeight() const { if (!m_Font) return 0.0f; diff --git a/source/graphics/FontMetrics.h b/source/graphics/FontMetrics.h index fb4586502e..6c46824ef1 100644 --- a/source/graphics/FontMetrics.h +++ b/source/graphics/FontMetrics.h @@ -33,7 +33,7 @@ public: CFontMetrics(CStrIntern font, CStrIntern locale); float GetHeight() const; - float GetCapHeight(); + float GetCapHeight() const; float GetCharacterWidth(wchar_t c) const; void CalculateStringSize(const wchar_t* string, float& w, float& h) const; diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index c04b80a9aa..80e299e5f9 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -118,13 +118,14 @@ void CConsole::Init() UpdateScreenSize(g_xres, g_yres); - // Calculate and store the line spacing + // Calculate and store the line spacing. const CFontMetrics font{CStrIntern(m_consoleFont)}; m_FontHeight = font.GetHeight(); m_FontWidth = font.GetCharacterWidth(L'C'); m_CharsPerPage = static_cast(g_xres / m_FontWidth); - // Offset by an arbitrary amount, to make it fit more nicely - m_FontOffset = 7.0f; + // Fonts constains two dimensions: the full height, and the cap height. + // We are adding some offset to move the text up a bit, so it looks better in the console. + m_FontOffset = font.GetCapHeight() / 2.f; m_CursorBlinkRate = g_ConfigDB.Get("gui.cursorblinkrate", 0.5); } @@ -249,18 +250,18 @@ void CConsole::DrawWindow(CCanvas2D& canvas) for (CVector2D& point : points) point += CVector2D{m_X, m_Y - (1.0f - m_VisibleFrac) * m_Height}; - canvas.DrawRect(CRect(points[1], points[3]), CColor(0.0f, 0.0f, 0.5f, 0.6f)); - canvas.DrawLine(points, 1.0f, CColor(0.5f, 0.5f, 0.0f, 0.6f)); + canvas.DrawRect(CRect(points[1], points[3]), CColor(0.05f, 0.05f, 0.2f, 0.85f)); + canvas.DrawLine(points, 1.0f, CColor(0.5f, 0.5f, 0.0f, 0.85f)); - if (m_Height > m_FontHeight + 4) + if (m_Height > m_FontHeight + 2.0f * m_FontOffset) { points = { - CVector2D{0.0f, m_Height - m_FontHeight - 4.0f}, - CVector2D{m_Width, m_Height - m_FontHeight - 4.0f} + CVector2D{0.0f, m_Height - m_FontHeight - 2.0f * m_FontOffset}, + CVector2D{m_Width, m_Height - m_FontHeight - 2.0f * m_FontOffset} }; for (CVector2D& point : points) point += CVector2D{m_X, m_Y - (1.0f - m_VisibleFrac) * m_Height}; - canvas.DrawLine(points, 1.0f, CColor(0.5f, 0.5f, 0.0f, 0.6f)); + canvas.DrawLine(points, 1.0f, CColor(0.5f, 0.5f, 0.0f, 0.85f)); } } @@ -283,7 +284,7 @@ void CConsole::DrawHistory(CTextRenderer& textRenderer) { textRenderer.Put( 9.0f, - m_Height - m_FontOffset - m_FontHeight * (i - m_MsgHistPos + 1), + m_Height - 3.0f * m_FontOffset - m_FontHeight * (i - m_MsgHistPos + 1), it->c_str()); } @@ -299,7 +300,7 @@ void CConsole::DrawBuffer(CTextRenderer& textRenderer) const CVector2D savedTranslate = textRenderer.GetTranslate(); - textRenderer.Translate(2.0f, m_Height - m_FontOffset + 1.0f); + textRenderer.Translate(2.0f, m_Height - m_FontOffset); textRenderer.SetCurrentColor(CColor(1.0f, 1.0f, 0.0f, 1.0f)); textRenderer.PutAdvance(L"]");