Files
WLED_MM_Infinity/wled00/wled_fonts.hpp
Frank e38c2b32c8 finishing touches
* replace strlen with strnlen
* avoid static variable in getFontInfo
* remove legacy code in drawCharacter (new code works well)

it works for me, let's ship it ;-)
2025-11-21 17:26:04 +01:00

88 lines
2.8 KiB
C++

#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) {
FontInfo_t font = {0}; // will be returned by value
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