mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-07-29 16:28:11 +00:00
feat: runtime font switcher — Default vs Lemon in Settings > Display
- Add Lemon font (LemonFont.h, LemonIcons.h) to the standard build alongside the Adafruit built-in font; ~101 KB added to flash - SH1106Display: add _use_lemon flag + setLemonFont(bool) to switch at runtime; print() and getTextWidth() use Lemon path when active; translateUTF8ToBlocks() passes UTF-8 through unchanged (Lemon supports full Unicode U+0020-U+04FF) vs transliterating for the default font - DisplayDriver: add getCharWidth()/getLineHeight() virtuals (defaults 6/8); SH1106Display returns 5/9 for Lemon, 6/8 for default; add setLemonFont() no-op virtual; fix orphaned UTF-8 leading byte in drawTextEllipsized - FullscreenMsgView: replace fixed-char-count word-wrap with pixel-accurate wrap using display.getTextWidth(); use getLineHeight() for spacing so Lemon (9 px) and default (8 px) both render correctly - NodePrefs: add use_lemon_font field (0=default, 1=Lemon) - SettingsScreen: add "Font" item in Display section (Default / Lemon), calls UITask::applyFont() on change - UITask: add applyFont() that calls display->setLemonFont(use_lemon_font); called at startup (begin()) and when the setting changes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "SH1106Display.h"
|
||||
#include <Adafruit_GrayOLED.h>
|
||||
#include "Adafruit_SH110X.h"
|
||||
#include "LemonFont.h"
|
||||
#include "LemonIcons.h"
|
||||
|
||||
bool SH1106Display::i2c_probe(TwoWire &wire, uint8_t addr)
|
||||
{
|
||||
@@ -60,9 +62,98 @@ void SH1106Display::setCursor(int x, int y)
|
||||
display.setCursor(x, y);
|
||||
}
|
||||
|
||||
uint32_t SH1106Display::decodeUtf8(const uint8_t*& p) {
|
||||
uint8_t c = *p++;
|
||||
if (c < 0x80) return c;
|
||||
if ((c & 0xE0) == 0xC0) {
|
||||
uint32_t cp = c & 0x1F;
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
return cp;
|
||||
}
|
||||
if ((c & 0xF0) == 0xE0) {
|
||||
uint32_t cp = c & 0x0F;
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
return cp;
|
||||
}
|
||||
if ((c & 0xF8) == 0xF0) {
|
||||
uint32_t cp = c & 0x07;
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
if (*p) cp = (cp << 6) | (*p++ & 0x3F);
|
||||
return cp;
|
||||
}
|
||||
while (*p && (*p & 0xC0) == 0x80) p++;
|
||||
return 0xFFFD;
|
||||
}
|
||||
|
||||
int16_t SH1106Display::drawLemonChar(int16_t x, int16_t y, uint32_t cp) {
|
||||
for (uint8_t i = 0; i < lemonIconCount; i++) {
|
||||
if (pgm_read_dword(&lemonIconCPs[i]) == cp) {
|
||||
const GFXglyph* g = &lemonIconGlyphs[i];
|
||||
uint8_t w = pgm_read_byte(&g->width), h = pgm_read_byte(&g->height);
|
||||
int8_t xo = (int8_t)pgm_read_byte(&g->xOffset), yo = (int8_t)pgm_read_byte(&g->yOffset);
|
||||
uint8_t xa = pgm_read_byte(&g->xAdvance);
|
||||
uint16_t bo = pgm_read_word(&g->bitmapOffset);
|
||||
uint8_t bits = 0, bit = 0;
|
||||
for (uint8_t row = 0; row < h; row++)
|
||||
for (uint8_t col = 0; col < w; col++) {
|
||||
if (!bit) { bits = pgm_read_byte(&lemonIconBitmaps[bo++]); bit = 0x80; }
|
||||
if (bits & bit) display.drawPixel(x + xo + col, y + 6 + yo + row, _color);
|
||||
bit >>= 1;
|
||||
}
|
||||
return x + xa;
|
||||
}
|
||||
}
|
||||
|
||||
if (cp < Lemon.first || cp > Lemon.last) {
|
||||
if (cp >= 0x20) display.fillRect(x + 1, y - 1, 4, 6, _color);
|
||||
return x + 6;
|
||||
}
|
||||
const GFXglyph* g = &lemonGlyphs[cp - Lemon.first];
|
||||
uint8_t w = pgm_read_byte(&g->width), h = pgm_read_byte(&g->height);
|
||||
int8_t xo = (int8_t)pgm_read_byte(&g->xOffset), yo = (int8_t)pgm_read_byte(&g->yOffset);
|
||||
uint8_t xa = pgm_read_byte(&g->xAdvance);
|
||||
uint16_t bo = pgm_read_word(&g->bitmapOffset);
|
||||
uint8_t bits = 0, bit = 0;
|
||||
for (uint8_t row = 0; row < h; row++)
|
||||
for (uint8_t col = 0; col < w; col++) {
|
||||
if (!bit) { bits = pgm_read_byte(&lemonBitmaps[bo++]); bit = 0x80; }
|
||||
if (bits & bit) display.drawPixel(x + xo + col, y + 6 + yo + row, _color);
|
||||
bit >>= 1;
|
||||
}
|
||||
return x + xa;
|
||||
}
|
||||
|
||||
uint8_t SH1106Display::lemonXAdvance(uint32_t cp) {
|
||||
if (cp < Lemon.first || cp > Lemon.last) return 6;
|
||||
return pgm_read_byte(&lemonGlyphs[cp - Lemon.first].xAdvance);
|
||||
}
|
||||
|
||||
void SH1106Display::translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) {
|
||||
if (_use_lemon) {
|
||||
size_t n = strlen(src);
|
||||
if (n >= dest_size) n = dest_size - 1;
|
||||
memcpy(dest, src, n);
|
||||
dest[n] = '\0';
|
||||
} else {
|
||||
DisplayDriver::translateUTF8ToBlocks(dest, src, dest_size);
|
||||
}
|
||||
}
|
||||
|
||||
void SH1106Display::print(const char *str)
|
||||
{
|
||||
display.print(str);
|
||||
if (!_use_lemon) { display.print(str); return; }
|
||||
|
||||
int16_t cx = display.getCursorX();
|
||||
int16_t cy = display.getCursorY();
|
||||
const uint8_t* p = (const uint8_t*)str;
|
||||
while (*p) {
|
||||
uint32_t cp = decodeUtf8(p);
|
||||
if (cp == '\n') { cy += Lemon.yAdvance; cx = 0; }
|
||||
else { cx = drawLemonChar(cx, cy, cp); }
|
||||
}
|
||||
display.setCursor(cx, cy);
|
||||
}
|
||||
|
||||
void SH1106Display::fillRect(int x, int y, int w, int h)
|
||||
@@ -82,6 +173,12 @@ void SH1106Display::drawXbm(int x, int y, const uint8_t *bits, int w, int h)
|
||||
|
||||
uint16_t SH1106Display::getTextWidth(const char *str)
|
||||
{
|
||||
if (_use_lemon) {
|
||||
uint16_t width = 0;
|
||||
const uint8_t* p = (const uint8_t*)str;
|
||||
while (*p) width += lemonXAdvance(decodeUtf8(p));
|
||||
return width;
|
||||
}
|
||||
int16_t x1, y1;
|
||||
uint16_t w, h;
|
||||
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
|
||||
|
||||
Reference in New Issue
Block a user