From 5a633ed7910ca034a5715e3ed109278ee383c26f Mon Sep 17 00:00:00 2001 From: Christian Seiler Date: Fri, 29 May 2026 21:55:04 +0200 Subject: [PATCH] C++/font rendering: properly handle spaces at the end of strings --- cpp/src/monoformat_fontreader.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cpp/src/monoformat_fontreader.hpp b/cpp/src/monoformat_fontreader.hpp index dcdf69a..5418e0e 100644 --- a/cpp/src/monoformat_fontreader.hpp +++ b/cpp/src/monoformat_fontreader.hpp @@ -558,6 +558,7 @@ public: } // FIXME: newline support + // also FIXME: call ensureAdvance when handling newline if (cp == '\n') { cp = ' '; } @@ -570,6 +571,8 @@ public: boundingBox = combineBoundingBoxes(boundingBox, dims->boundingBox); } + boundingBox = ensureAdvance(boundingBox, advance); + return RenderedDimensions{advance, boundingBox}; } @@ -619,6 +622,22 @@ private: } } + static std::optional ensureAdvance(std::optional a, Point advance) { + if (!a) { + return std::nullopt; + } + + // FIXME: once we support multi-line also handle y component + + auto bottomRight = a->getBottomRight(); + if ((bottomRight.x + 1) < advance.x) { + bottomRight.x = advance.x - 1; + return Rectangle::fromCorners(a->topLeft, bottomRight); + } else { + return a; + } + } + template static std::expected renderGlyph(std::uint32_t cp, Point position, FontReader const& font, Display& display, typename Display::Color fontColor, std::optional bgColor) { auto glyph_ = font.tryRetrieveGlyphData(cp);