mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
feat: add DisplayDriver methods for UTF-8 filtering and text ellipsis
- Add translateUTF8ToBlocks() method to convert UTF-8 characters to displayable blocks - Add drawTextEllipsized() method for text truncation with ellipsis - Apply UTF-8 filtering to node names, recent contacts, and message content - Use ellipsized text rendering for recent contacts to prevent overflow - Addresses PR feedback by moving text processing to DisplayDriver level
This commit is contained in:
parent
d86851b881
commit
bd6aa983a3
2 changed files with 75 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
class DisplayDriver {
|
||||
int _w, _h;
|
||||
|
|
@ -31,5 +32,60 @@ public:
|
|||
setCursor(mid_x - w/2, y);
|
||||
print(str);
|
||||
}
|
||||
|
||||
// convert UTF-8 characters to displayable block characters for compatibility
|
||||
virtual void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) {
|
||||
size_t j = 0;
|
||||
for (size_t i = 0; src[i] != 0 && j < dest_size - 1; i++) {
|
||||
unsigned char c = (unsigned char)src[i];
|
||||
if (c >= 32 && c <= 126) {
|
||||
dest[j++] = c; // ASCII printable
|
||||
} else if (c >= 0x80) {
|
||||
dest[j++] = '\xDB'; // CP437 full block █
|
||||
while (src[i+1] && (src[i+1] & 0xC0) == 0x80)
|
||||
i++; // skip UTF-8 continuation bytes
|
||||
}
|
||||
}
|
||||
dest[j] = 0;
|
||||
}
|
||||
|
||||
// draw text with ellipsis if it exceeds max_width
|
||||
virtual void drawTextEllipsized(int x, int y, int max_width, const char* str) {
|
||||
char temp_str[256]; // reasonable buffer size
|
||||
size_t len = strlen(str);
|
||||
if (len >= sizeof(temp_str)) len = sizeof(temp_str) - 1;
|
||||
memcpy(temp_str, str, len);
|
||||
temp_str[len] = 0;
|
||||
|
||||
if (getTextWidth(temp_str) <= max_width) {
|
||||
setCursor(x, y);
|
||||
print(temp_str);
|
||||
return;
|
||||
}
|
||||
|
||||
// for variable-width fonts (GxEPD), add space after ellipsis
|
||||
// for fixed-width fonts (OLED), keep tight spacing to save precious characters
|
||||
const char* ellipsis;
|
||||
// use a simple heuristic: if 'i' and 'l' have different widths, it's variable-width
|
||||
int i_width = getTextWidth("i");
|
||||
int l_width = getTextWidth("l");
|
||||
if (i_width != l_width) {
|
||||
ellipsis = "... "; // variable-width fonts: add space
|
||||
} else {
|
||||
ellipsis = "..."; // fixed-width fonts: no space
|
||||
}
|
||||
|
||||
int ellipsis_width = getTextWidth(ellipsis);
|
||||
int str_len = strlen(temp_str);
|
||||
|
||||
while (str_len > 0 && getTextWidth(temp_str) > max_width - ellipsis_width) {
|
||||
temp_str[--str_len] = 0;
|
||||
}
|
||||
strcat(temp_str, ellipsis);
|
||||
|
||||
setCursor(x, y);
|
||||
print(temp_str);
|
||||
}
|
||||
|
||||
virtual void endFrame() = 0;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue