Files
MeshCore-Solo/src/helpers/ui/MiscFixedRenderer.h
T
JakubandClaude Opus 5 f8b9e1acb8 feat(oled): misc-fixed 6x9 font on SSD1306 too, for GAT562 30S solo
The misc-fixed font (full Latin/Greek/Cyrillic) had only ever reached
SH1106Display and the e-ink driver. GAT562 30S -- like 24 other variants
-- uses SSD1306Display, which stayed on the built-in 5x7 font, so every
keyboard alphabet beyond ASCII and every accented contact name was
transliterated (Łódź -> Lodz) or drawn as a filled block.

Extract the renderer into MiscFixedRenderer.h rather than copying it:
Adafruit_SH110X and Adafruit_SSD1306 both derive from Adafruit_GFX and
glyph drawing is pure pixel plotting, so one implementation serves both
drivers (-79 lines from SH1106Display.cpp, same flash usage as before).
The header is .cpp-only by contract -- the font tables are static const,
so including it from a driver header would land a copy in every
translation unit.

On SSD1306 the path is opt-in behind OLED_MISC_FIXED_FONT, set only in
the two GAT562 30S solo envs: the font costs ~14 KB of flash and the
other 24 variants' repeater/companion builds have no keyboard to type
those alphabets on. Without the flag that driver is unchanged, verified
by building GAT562_30S_Mesh_Kit_repeater.

Drive-by consistency fix from the extraction: a newline in print() now
advances the cursor by yAdvance * text_size instead of a single yAdvance,
which was half a row short at setTextSize(2). The e-ink driver always
scaled it. No effect at size 1, which is everywhere the UI prints '\n'.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 22:48:06 +02:00

104 lines
4.4 KiB
C

#pragma once
#include <Adafruit_GFX.h>
#include "DisplayDriver.h"
#include "MiscFixedFont.h"
#include "LemonIcons.h"
// Shared misc-fixed 6x9 text renderer for the monochrome OLED drivers.
//
// Both Adafruit_SH110X and Adafruit_SSD1306 derive from Adafruit_GFX and the
// font path is pure pixel plotting, so SH1106Display and SSD1306Display render
// off this one implementation instead of two copies of it. Everything here
// bypasses GFX's own print(): GFX walks bytes and would treat each byte of a
// multi-byte UTF-8 sequence as its own CP437 glyph, and it can't reach the
// custom UI icons either.
//
// Include only from a .cpp — the font tables are static const arrays, so a
// header pulling this in would land a copy of them in every translation unit.
//
// `y` is the TOP of the text row in every function below, matching the UI's
// coordinate convention (GFX fonts would use the baseline).
// Pixel advance of one codepoint at text size sz. Unmapped codepoints get the
// font's own 6px cell, same as the substitution box drawn for them.
static inline uint8_t miscFixedXAdvance(uint32_t cp, int sz) {
uint8_t xa;
if (cp < MiscFixed.first || cp > MiscFixed.last) xa = 6;
else xa = pgm_read_byte(&MiscFixedGlyphs[cp - MiscFixed.first].xAdvance);
return xa * sz;
}
// Draw one codepoint at (x, y); returns the x to continue from.
static inline int16_t miscFixedDrawGlyph(Adafruit_GFX& gfx, int16_t x, int16_t y,
uint32_t cp, int sz, uint16_t color) {
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) {
// The UI icons keep a +6 baseline; the font glyphs below use +7
// (misc-fixed's ascent), so the icons sit 1px higher in their cell.
if (sz == 1) gfx.drawPixel(x + xo + col, y + 6 + yo + row, color);
else gfx.fillRect(x + xo*sz + col*sz, y + 6*sz + yo*sz + row*sz, sz, sz, color);
}
bit >>= 1;
}
return x + xa * sz;
}
}
if (cp < MiscFixed.first || cp > MiscFixed.last) {
// Substitution box for anything the font doesn't cover. Drawn at plain `y`
// (not y - 7*sz): `y` is already the ascent top, so the box stays inside
// its own row instead of bleeding into the one above.
if (cp >= 0x20) gfx.fillRect(x + sz, y, 4*sz, 6*sz, color);
return x + 6 * sz;
}
const GFXglyph* g = &MiscFixedGlyphs[cp - MiscFixed.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(&MiscFixedBitmaps[bo++]); bit = 0x80; }
if (bits & bit) {
if (sz == 1) gfx.drawPixel(x + xo + col, y + 7 + yo + row, color);
else gfx.fillRect(x + xo*sz + col*sz, y + 7*sz + yo*sz + row*sz, sz, sz, color);
}
bit >>= 1;
}
return x + xa * sz;
}
// Draw a UTF-8 string from the driver's current cursor, honouring '\n', and
// leave the cursor where the text ended (same contract as GFX's print()).
static inline void miscFixedPrint(Adafruit_GFX& gfx, const char* str, int sz, uint16_t color) {
int16_t cx = gfx.getCursorX();
int16_t cy = gfx.getCursorY();
const uint8_t* p = (const uint8_t*)str;
while (*p) {
uint32_t cp = DisplayDriver::decodeCodepoint(p);
if (cp == '\n') { cy += MiscFixed.yAdvance * sz; cx = 0; }
else { cx = miscFixedDrawGlyph(gfx, cx, cy, cp, sz, color); }
}
gfx.setCursor(cx, cy);
}
static inline uint16_t miscFixedTextWidth(const char* str, int sz) {
uint16_t width = 0;
const uint8_t* p = (const uint8_t*)str;
while (*p) width += miscFixedXAdvance(DisplayDriver::decodeCodepoint(p), sz);
return width;
}