Files
tactility/Modules/lvgl-module/source/lvgl_fonts.c
T
Adolfo Reyna e29e9b4a66 feat(rlcd): runtime font size selector in Settings->Display
- device.properties bump default to 20
- lvgl_fonts add runtime override API (g_runtime_font_size + externs for all Montserrat sizes)
- DisplaySettings add fontSize field persisted in properties
- Display app adds Font size dropdown 14-28 applying immediately
- Lvgl.cpp applies saved runtime font on boot
- device.py new bucket <=20 includes all fonts for runtime switching (~150KB)

Co-authored-by: internal-model
2026-07-18 15:10:42 -04:00

127 lines
4.8 KiB
C

// SPDX-License-Identifier: Apache-2.0
#include <lvgl.h>
#include <tactility/lvgl_fonts.h>
#include <tactility/check.h>
// The preprocessor definitions that are used below are defined in the CMakeLists.txt from this module.
extern const lv_font_t TT_LVGL_TEXT_FONT_SMALL_SYMBOL;
extern const lv_font_t TT_LVGL_TEXT_FONT_DEFAULT_SYMBOL;
extern const lv_font_t TT_LVGL_TEXT_FONT_LARGE_SYMBOL;
extern const lv_font_t TT_LVGL_LAUNCHER_FONT_ICON_SYMBOL;
extern const lv_font_t TT_LVGL_STATUSBAR_FONT_ICON_SYMBOL;
extern const lv_font_t TT_LVGL_SHARED_FONT_ICON_SYMBOL;
// All compiled-in Montserrat fonts for runtime switching (extern from lvgl lib)
#ifdef CONFIG_LV_FONT_MONTSERRAT_14
extern const lv_font_t lv_font_montserrat_14;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_16
extern const lv_font_t lv_font_montserrat_16;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_18
extern const lv_font_t lv_font_montserrat_18;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_20
extern const lv_font_t lv_font_montserrat_20;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_24
extern const lv_font_t lv_font_montserrat_24;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_26
extern const lv_font_t lv_font_montserrat_26;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_28
extern const lv_font_t lv_font_montserrat_28;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_30
extern const lv_font_t lv_font_montserrat_30;
#endif
// Runtime font size override - 0 = use compile-time default, otherwise explicit point size
static uint8_t g_runtime_font_size = 0;
void lvgl_set_runtime_font_size(uint8_t font_size) {
g_runtime_font_size = font_size;
}
uint8_t lvgl_get_runtime_font_size() {
return g_runtime_font_size;
}
static const lv_font_t* get_font_for_size(uint8_t size) {
// Map requested size to closest available compiled font
if (size == 0) return NULL; // use compile-time
#ifdef CONFIG_LV_FONT_MONTSERRAT_14
if (size <= 14) return &lv_font_montserrat_14;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_16
if (size <= 16) return &lv_font_montserrat_16;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_18
if (size <= 18) return &lv_font_montserrat_18;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_20
if (size <= 20) return &lv_font_montserrat_20;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_24
if (size <= 24) return &lv_font_montserrat_24;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_26
if (size <= 26) return &lv_font_montserrat_26;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_28
if (size <= 28) return &lv_font_montserrat_28;
#endif
#ifdef CONFIG_LV_FONT_MONTSERRAT_30
return &lv_font_montserrat_30;
#else
return NULL;
#endif
}
uint32_t lvgl_get_text_font_height(enum LvglFontSize font_size) {
if (g_runtime_font_size != 0) {
// Scale heights: small = runtime-4, default = runtime, large = runtime+6
switch (font_size) {
case FONT_SIZE_SMALL: return g_runtime_font_size > 4 ? g_runtime_font_size - 4 : g_runtime_font_size;
case FONT_SIZE_DEFAULT: return g_runtime_font_size;
case FONT_SIZE_LARGE: return g_runtime_font_size + 6;
default: check(false);
}
}
switch (font_size) {
case FONT_SIZE_SMALL: return TT_LVGL_TEXT_FONT_SMALL_SIZE;
case FONT_SIZE_DEFAULT: return TT_LVGL_TEXT_FONT_DEFAULT_SIZE;
case FONT_SIZE_LARGE: return TT_LVGL_TEXT_FONT_LARGE_SIZE;
default: check(false);
}
}
const lv_font_t* lvgl_get_text_font(enum LvglFontSize font_size) {
if (g_runtime_font_size != 0) {
uint8_t target_size;
switch (font_size) {
case FONT_SIZE_SMALL: target_size = g_runtime_font_size > 4 ? g_runtime_font_size - 4 : g_runtime_font_size; break;
case FONT_SIZE_DEFAULT: target_size = g_runtime_font_size; break;
case FONT_SIZE_LARGE: target_size = g_runtime_font_size + 6; break;
default: check(false); return &TT_LVGL_TEXT_FONT_DEFAULT_SYMBOL;
}
const lv_font_t* f = get_font_for_size(target_size);
if (f != NULL) return f;
}
switch (font_size) {
case FONT_SIZE_SMALL: return &TT_LVGL_TEXT_FONT_SMALL_SYMBOL;
case FONT_SIZE_DEFAULT: return &TT_LVGL_TEXT_FONT_DEFAULT_SYMBOL;
case FONT_SIZE_LARGE: return &TT_LVGL_TEXT_FONT_LARGE_SYMBOL;
default: check(false); return &TT_LVGL_TEXT_FONT_DEFAULT_SYMBOL;
}
}
uint32_t lvgl_get_shared_icon_font_height() { return TT_LVGL_SHARED_FONT_ICON_SIZE; }
const lv_font_t* lvgl_get_shared_icon_font() { return &TT_LVGL_SHARED_FONT_ICON_SYMBOL; }
uint32_t lvgl_get_launcher_icon_font_height() { return TT_LVGL_LAUNCHER_FONT_ICON_SIZE; }
const lv_font_t* lvgl_get_launcher_icon_font() { return &TT_LVGL_LAUNCHER_FONT_ICON_SYMBOL; }
uint32_t lvgl_get_statusbar_icon_font_height() { return TT_LVGL_STATUSBAR_FONT_ICON_SIZE; }
const lv_font_t* lvgl_get_statusbar_icon_font() { return &TT_LVGL_STATUSBAR_FONT_ICON_SYMBOL; }