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

View File

@@ -1,5 +1,3 @@
#if defined(WLED_ENABLE_FULL_FONTS)
/*
@title WLED(-MM) - unicode to CP437 conversion
@repo https://github.com/MoonModules/WLED-MM, https://github.com/wled/WLED
@@ -7,6 +5,8 @@
@license Licensed under the EUPL-1.2 or later
*/
#if defined(WLED_ENABLE_FULL_FONTS)
#include "codepages.h"
#include <string.h>

View File

@@ -8,7 +8,7 @@
#undef WLED_ENABLE_FULL_FONTS
#endif
// visual replacements when decoding fails
// UTF-16 visual replacements when decoding fails
//constexpr uint16_t UNKNOWN_CODE = 0x2219; // ∙ multiplication dot (try this if you don't like the middle dot)
constexpr uint16_t UNKNOWN_CODE = 0x00B7; // · middle dot = unknown code (generic error)
constexpr uint16_t BAD_CODE = 0x2022; // • bigger dot = cannot decode (unicode malformed)
@@ -19,6 +19,11 @@ constexpr uint16_t EXT_CODE = 0x263B; // ☻ smiling face = extended cod
// return "•" in case of input errors, and for unsupported/invalid UTF-8
uint16_t unicodeToWchar16(const unsigned char* utf8, size_t maxLen); // unicodetool.cpp
#if defined(WLED_ENABLE_FULL_FONTS)
// translates unicode 2-byte (UTF-16) "code point" into corresponding character in codepage 437 (IBM PC aka PC-8)
uint16_t wchar16ToCodepage437(uint16_t wideChar); // codepage437.cpp
#endif
// returns a pointer to the next unicode item - can be used to "advance" conversion after unicodeToWchar16()
// return nullptr at end of input
const unsigned char* nextUnicode(const unsigned char* utf8, size_t maxLen); // unicodetool.cpp
@@ -30,7 +35,16 @@ size_t strlenUC(const unsigned char* utf8);
// Important: calling code is responsible to provide a string with at least _where_ chars
size_t cutUnicodeAt(const unsigned char* utf8, size_t where);
// translates unicode 2-byte (UTF-16) "code point" into corresponding character in codepage 437 (IBM PC aka PC-8)
uint16_t wchar16ToCodepage437(uint16_t wideChar); // codepage437.cpp
// special utility function for @troyhacks ;-)
// removes all unicode letter from a C style char[] - conversion is in-place, to avoid heap fragging
// doesn't work on PROGMEM strings, unless you strdup() them into RAM before calling this function
inline void killUnicode(unsigned char* utf8) {
if (utf8 == nullptr) return;
size_t clean_index = 0;
for (size_t i=0; utf8[i] != '\0'; i++)
if ((utf8[i] > 0) && (utf8[i] < 128)) utf8[clean_index++] = utf8[i]; // only keep pure ASCII; unicode extended chars start at 128
utf8[clean_index] = '\0'; // ensure proper string termination
}
#endif