refactor / cleanup font selection logic

* centralize font selection logic into  getFontInfo()
* removed unneeded parameter "numberOfChars" from drawText()
* new file: wled_fonts.hpp to centralize font management logic (inline)
* only compile wchar16ToCodepage437 when WLED_ENABLE_FULL_FONTS
* minor cleanup
This commit is contained in:
Frank
2025-11-21 17:04:08 +01:00
parent 213cd185b5
commit e6676372d8
6 changed files with 142 additions and 47 deletions

87
wled00/wled_fonts.hpp Normal file
View File

@@ -0,0 +1,87 @@
#pragma once
#ifndef WLED_FONTS_H
#define WLED_FONTS_H
#include <stdlib.h> // needed to get uint16_t definition
#include <stdint.h> // helps for code analysis with clang
// always disable unicode for 8266 builds - not enough program space
#if !defined(ARDUINO_ARCH_ESP32) && defined(WLED_ENABLE_FULL_FONTS)
#undef WLED_ENABLE_FULL_FONTS
#endif
// pull in all fonts
#include "src/font/console_font_4x6.h"
#include "src/font/console_font_5x8.h"
#include "src/font/console_font_5x12.h"
#include "src/font/console_font_6x8.h"
#include "src/font/console_font_7x9.h"
// fontInfo struct returned by getFontInfo
typedef struct {
unsigned firstChar; // first supported glyph (32 for standard "reduced" fonts)
unsigned lastChar; // last supported glyph (126 for standard "reduced" fonts)
unsigned width_bytes; // single letter width in bytes (default:1)
// unsigned height_bytes; // future support
bool isProgMem; // raw data points into ProgMem => 8266 needs pgm_read_byte_near()
const unsigned char* raw; // array of bytes with raw pixeldata (typicially lives in PROGMEM)
// note: we only support monospaced fonts
} FontInfo_t;
// logic for font selection based on width and height
inline FontInfo_t getFontInfo(unsigned width, unsigned height) {
static FontInfo_t font = {0}; // not sure if this needs to be static - just wanted to be sure that the function return value is not in the stack area (use-after-free).
unsigned pixels = width * height;
switch (pixels) {
// WLED standard fonts (PROGMEM)
case 24: // 4x6 font
font.raw = console_font_4x6;
font.isProgMem = true;
font.firstChar = console_font_4x6_first;
font.lastChar = console_font_4x6_last;
font.width_bytes= 1;
break;
case 40: // 5x8 font
font.raw = console_font_5x8;
font.isProgMem = true;
font.firstChar = console_font_5x8_first;
font.lastChar = console_font_5x8_last;
font.width_bytes= 1;
break;
case 48: // 6x8 font
font.raw = console_font_6x8;
font.isProgMem = true;
font.firstChar = console_font_6x8_first;
font.lastChar = console_font_6x8_last;
font.width_bytes= 1;
break;
case 63: // 7x9 font
font.raw = console_font_7x9;
font.isProgMem = true;
font.firstChar = console_font_7x9_first;
font.lastChar = console_font_7x9_last;
font.width_bytes= 1;
break;
case 60: // 5x12 font
font.raw = console_font_5x12;
font.isProgMem = true;
font.firstChar = console_font_5x12_first;
font.lastChar = console_font_5x12_last;
font.width_bytes= 1;
break;
// you can add any custom fonts here
default: // no font
font.raw = nullptr;
font.isProgMem = false;
font.firstChar = 1;
font.lastChar = 1;
font.width_bytes= 1;
}
return font;
}
#endif