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:
Jakub
2026-05-20 09:29:38 +02:00
parent 72c07df8b3
commit a115916d8f
10 changed files with 1732 additions and 29 deletions

View File

@@ -27,6 +27,9 @@ public:
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; } // typical character advance width (px)
virtual int getLineHeight() const { return 8; } // pixel rows per text line
virtual void setLemonFont(bool) { } // no-op; overridden by displays that support Lemon
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);
@@ -182,6 +185,10 @@ public:
while (str_len > 0 && getTextWidth(temp_str) > max_width - ellipsis_width) {
temp_str[--str_len] = 0;
}
// Strip orphaned UTF-8 leading byte left by byte-at-a-time trimming above.
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);

1523
src/helpers/ui/LemonFont.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,19 @@
// Lemon font icon glyphs (Private Use Area), converted manually from lemon.bdf
// Add new icons by appending to the bitmap array, lemonIconCPs, and lemonIconGlyphs.
#pragma once
#include <Adafruit_GFX.h>
static const uint8_t lemonIconBitmaps[] PROGMEM = {
0xEE, 0xBA, 0x2B, 0xA3, 0xB0, // U+E03B mute: 6×6, yoff=0
};
static const uint32_t lemonIconCPs[] PROGMEM = {
0xE03B, // mute
};
static const GFXglyph lemonIconGlyphs[] PROGMEM = {
// off w h adv xo yo
{ 0, 6, 6, 5, 0, -6 }, // U+E03B mute
};
static const uint8_t lemonIconCount = sizeof(lemonIconCPs) / sizeof(lemonIconCPs[0]);

View File

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

View File

@@ -21,11 +21,17 @@ class SH1106Display : public DisplayDriver
uint8_t _color;
uint8_t _contrast;
uint8_t _precharge;
bool _use_lemon;
bool i2c_probe(TwoWire &wire, uint8_t addr);
static uint32_t decodeUtf8(const uint8_t*& p);
int16_t drawLemonChar(int16_t x, int16_t y, uint32_t cp);
uint8_t lemonXAdvance(uint32_t cp);
public:
SH1106Display() : DisplayDriver(128, 64), display(128, 64, &Wire, PIN_OLED_RESET) { _isOn = false; _contrast = 255; _precharge = 0x1F; }
SH1106Display() : DisplayDriver(128, 64), display(128, 64, &Wire, PIN_OLED_RESET) {
_isOn = false; _contrast = 255; _precharge = 0x1F; _use_lemon = false;
}
bool begin();
bool isOn() override { return _isOn; }
@@ -41,6 +47,10 @@ public:
void drawRect(int x, int y, int w, int h) override;
void drawXbm(int x, int y, const uint8_t *bits, int w, int h) override;
uint16_t getTextWidth(const char *str) override;
int getCharWidth() const override { return _use_lemon ? 5 : 6; }
int getLineHeight() const override { return _use_lemon ? 9 : 8; }
void setLemonFont(bool enabled) override { _use_lemon = enabled; }
void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) override;
void setBrightness(uint8_t level) override;
void endFrame() override;
};