mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-07-27 15:28:11 +00:00
Fix drawTextEllipsized to strip orphaned UTF-8 leading bytes after byte-at-a-time trimming, preventing an invisible 6px gap before "..." when truncating non-ASCII names. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
98 lines
3.5 KiB
C++
98 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
class DisplayDriver {
|
|
int _w, _h;
|
|
protected:
|
|
DisplayDriver(int w, int h) { _w = w; _h = h; }
|
|
public:
|
|
enum Color { DARK=0, LIGHT, RED, GREEN, BLUE, YELLOW, ORANGE }; // on b/w screen, colors will be !=0 synonym of light
|
|
|
|
int width() const { return _w; }
|
|
int height() const { return _h; }
|
|
|
|
virtual bool isOn() = 0;
|
|
virtual void turnOn() = 0;
|
|
virtual void turnOff() = 0;
|
|
virtual void clear() = 0;
|
|
virtual void startFrame(Color bkg = DARK) = 0;
|
|
virtual void setTextSize(int sz) = 0;
|
|
virtual void setColor(Color c) = 0;
|
|
virtual void setCursor(int x, int y) = 0;
|
|
virtual void print(const char* str) = 0;
|
|
virtual void printWordWrap(const char* str, int max_width) { print(str); } // fallback to basic print() if no override
|
|
virtual void fillRect(int x, int y, int w, int h) = 0;
|
|
virtual void drawRect(int x, int y, int w, int h) = 0;
|
|
virtual void drawXbm(int x, int y, const uint8_t* bits, int w, int h) = 0;
|
|
virtual uint16_t getTextWidth(const char* str) = 0;
|
|
virtual int getCharWidth() const { return 6; } // advance width of a typical character (xAdvance for monospace)
|
|
virtual int getLineHeight() const { return 8; } // pixel rows per line (cell height, not just glyph height)
|
|
virtual void drawTextCentered(int mid_x, int y, const char* str) { // helper method (override to optimise)
|
|
int w = getTextWidth(str);
|
|
setCursor(mid_x - w/2, y);
|
|
print(str);
|
|
}
|
|
virtual void drawTextRightAlign(int x_anch, int y, const char* str) {
|
|
int w = getTextWidth(str);
|
|
setCursor(x_anch - w, y);
|
|
print(str);
|
|
}
|
|
virtual void drawTextLeftAlign(int x_anch, int y, const char* str) {
|
|
setCursor(x_anch, y);
|
|
print(str);
|
|
}
|
|
|
|
virtual void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) {
|
|
size_t n = strlen(src);
|
|
if (n >= dest_size) n = dest_size - 1;
|
|
memcpy(dest, src, n);
|
|
dest[n] = 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
|
|
translateUTF8ToBlocks(temp_str, str, sizeof(temp_str));
|
|
|
|
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;
|
|
}
|
|
// Strip any orphaned UTF-8 leading byte left by the byte-at-a-time trimming above.
|
|
// A leading byte (0xC0-0xFF, high two bits = 11) with no following continuation
|
|
// byte renders as an invisible 6px advance, leaving a gap before the ellipsis.
|
|
while (str_len > 0 && ((uint8_t)temp_str[str_len - 1] & 0xC0) == 0xC0) {
|
|
temp_str[--str_len] = 0;
|
|
}
|
|
strcat(temp_str, ellipsis);
|
|
|
|
setCursor(x, y);
|
|
print(temp_str);
|
|
}
|
|
|
|
virtual void setBrightness(uint8_t level) { } // level 0-4 (min to max), no-op default
|
|
virtual void endFrame() = 0;
|
|
};
|