mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-05 15:17:37 +03:00
fix: optimize UTF-8 safe text truncation logic in GfxRenderer
This commit is contained in:
parent
8bb746aed4
commit
8f8eb869d3
@ -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,
|
std::string GfxRenderer::truncatedText(const int fontId, const char* text, const int maxWidth,
|
||||||
const EpdFontFamily::Style style) const {
|
const EpdFontFamily::Style style) const {
|
||||||
std::string item = text ? text : "";
|
if (!text || maxWidth <= 0) return "";
|
||||||
if (maxWidth <= 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
int itemWidth = getTextWidth(fontId, item.c_str(), style);
|
|
||||||
if (itemWidth <= maxWidth) {
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
std::string item = text;
|
||||||
const char* ellipsis = "...";
|
const char* ellipsis = "...";
|
||||||
const int ellipsisWidth = getTextWidth(fontId, ellipsis, style);
|
int ellipsisWidth = getTextWidth(fontId, ellipsis, style);
|
||||||
if (ellipsisWidth > maxWidth) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!item.empty() && itemWidth + ellipsisWidth > maxWidth) {
|
while (!item.empty() && getTextWidth(fontId, (item + ellipsis).c_str(), style) > maxWidth) {
|
||||||
utf8RemoveLastChar(item);
|
utf8RemoveLastChar(item);
|
||||||
itemWidth = getTextWidth(fontId, item.c_str(), style);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.empty()) {
|
return item.empty() ? ellipsis : item + ellipsis;
|
||||||
return ellipsis;
|
|
||||||
}
|
|
||||||
|
|
||||||
return item + ellipsis;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Internal driver treats screen in command orientation; this library exposes a logical orientation
|
// Note: Internal driver treats screen in command orientation; this library exposes a logical orientation
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user