From 8f8eb869d345da9743a735fa100cab74a4676e8b Mon Sep 17 00:00:00 2001 From: Arthur Tazhitdinov Date: Thu, 29 Jan 2026 02:41:06 +0500 Subject: [PATCH] fix: optimize UTF-8 safe text truncation logic in GfxRenderer --- lib/GfxRenderer/GfxRenderer.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/lib/GfxRenderer/GfxRenderer.cpp b/lib/GfxRenderer/GfxRenderer.cpp index f9f35efc..2eab1a44 100644 --- a/lib/GfxRenderer/GfxRenderer.cpp +++ b/lib/GfxRenderer/GfxRenderer.cpp @@ -415,32 +415,17 @@ void GfxRenderer::displayBuffer(const HalDisplay::RefreshMode refreshMode) const std::string GfxRenderer::truncatedText(const int fontId, const char* text, const int maxWidth, const EpdFontFamily::Style style) const { - std::string item = text ? text : ""; - if (maxWidth <= 0) { - return ""; - } - - int itemWidth = getTextWidth(fontId, item.c_str(), style); - if (itemWidth <= maxWidth) { - return item; - } + if (!text || maxWidth <= 0) return ""; + std::string item = text; const char* ellipsis = "..."; - const int ellipsisWidth = getTextWidth(fontId, ellipsis, style); - if (ellipsisWidth > maxWidth) { - return ""; - } + int ellipsisWidth = getTextWidth(fontId, ellipsis, style); - while (!item.empty() && itemWidth + ellipsisWidth > maxWidth) { + while (!item.empty() && getTextWidth(fontId, (item + ellipsis).c_str(), style) > maxWidth) { utf8RemoveLastChar(item); - itemWidth = getTextWidth(fontId, item.c_str(), style); } - if (item.empty()) { - return ellipsis; - } - - return item + ellipsis; + return item.empty() ? ellipsis : item + ellipsis; } // Note: Internal driver treats screen in command orientation; this library exposes a logical orientation