fix: optimize UTF-8 safe text truncation logic in GfxRenderer

This commit is contained in:
Arthur Tazhitdinov 2026-01-29 02:41:06 +05:00
parent 8bb746aed4
commit 8f8eb869d3

View File

@ -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