From 25e1a64eb84f05955795c3f15b89bb5dc050c6fc Mon Sep 17 00:00:00 2001 From: jpirnay Date: Fri, 15 May 2026 00:02:16 +0200 Subject: [PATCH] More fixes --- src/SdCardFontGlobals.h | 5 ++++ src/SdCardFontSystem.cpp | 30 +++++++++++++++++++ src/SdCardFontSystem.h | 4 +++ src/activities/ActivityManager.cpp | 4 +-- src/activities/settings/SettingsActivity.cpp | 3 +- .../settings/SettingsSubmenuActivity.cpp | 21 +++++++++++++ src/main.cpp | 18 +++++++++++ 7 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/SdCardFontGlobals.h b/src/SdCardFontGlobals.h index ad084d12..4282bf71 100644 --- a/src/SdCardFontGlobals.h +++ b/src/SdCardFontGlobals.h @@ -11,6 +11,11 @@ extern SdCardFontSystem sdFontSystem; // Defined in main.cpp; call before entering the reader or after settings change. extern void ensureSdFontLoaded(); +// Ensure the correct SD card font family is loaded for the given book path. +// Selects EPUB or TXT/MD settings depending on the file extension. +// Defined in main.cpp. +extern void ensureSdFontLoadedForPath(const char* path); + // Resolve the SD card font ID for the given family name and font size enum. // Returns 0 if no SD font with that family name and size is currently loaded. // Free function (not stored as a callback in CrossPointSettings) so the linker diff --git a/src/SdCardFontSystem.cpp b/src/SdCardFontSystem.cpp index 965ee8d0..d2e880eb 100644 --- a/src/SdCardFontSystem.cpp +++ b/src/SdCardFontSystem.cpp @@ -179,6 +179,36 @@ static uint8_t targetPtSizeFromEnum(uint8_t fontSizeEnum) { return FONT_SIZE_TO_PT[fontSizeEnum]; } +void SdCardFontSystem::ensureLoaded(GfxRenderer& renderer, const char* wantedFamily, uint8_t fontSizeEnum) { + const std::string& currentFamily = manager_.currentFamilyName(); + const uint8_t targetPt = targetPtSizeFromEnum(fontSizeEnum); + + if (!wantedFamily || wantedFamily[0] == '\0') { + if (!currentFamily.empty()) manager_.unloadAll(renderer); + return; + } + + bool familyMatches = (currentFamily == wantedFamily); + if (familyMatches) { + const auto* family = registry_.findFamily(wantedFamily); + if (!family) { + manager_.unloadAll(renderer); + return; + } + const auto* best = family->pickClosestSize(targetPt); + if (best && best->pointSize == manager_.currentPointSize()) return; + } + + if (!currentFamily.empty()) manager_.unloadAll(renderer); + + const auto* family = registry_.findFamily(wantedFamily); + if (family) { + if (!manager_.loadFamily(*family, renderer, targetPt)) { + LOG_ERR("SDFS", "Failed to load SD font family: %s", wantedFamily); + } + } +} + int SdCardFontSystem::resolveFontId(const char* familyName, uint8_t fontSizeEnum) const { // The manager loads exactly one size for the active SD family. Resolve only // if the requested family matches the loaded family and the requested size diff --git a/src/SdCardFontSystem.h b/src/SdCardFontSystem.h index 11457539..ac3aee41 100644 --- a/src/SdCardFontSystem.h +++ b/src/SdCardFontSystem.h @@ -19,6 +19,10 @@ class SdCardFontSystem { /// Call before entering the reader or after settings change. void ensureLoaded(GfxRenderer& renderer); + /// Ensure the correct SD font family is loaded for an explicit family + size. + /// Used when the reader type determines which settings field to consult. + void ensureLoaded(GfxRenderer& renderer, const char* familyName, uint8_t fontSizeEnum); + /// Resolve an SD card font ID from family name + fontSize enum. /// Returns 0 if not found. Used by CrossPointSettings::getReaderFontId(). int resolveFontId(const char* familyName, uint8_t fontSizeEnum) const; diff --git a/src/activities/ActivityManager.cpp b/src/activities/ActivityManager.cpp index 593e9d45..a40826f8 100644 --- a/src/activities/ActivityManager.cpp +++ b/src/activities/ActivityManager.cpp @@ -286,7 +286,7 @@ void ActivityManager::goToBrowser() { void ActivityManager::goToReader(std::string path) { RenderLock lock; - ensureSdFontLoaded(); + ensureSdFontLoadedForPath(path.c_str()); replaceActivity(std::make_unique(renderer, mappedInput, std::move(path))); } @@ -307,7 +307,7 @@ void ActivityManager::replaceWithReader(std::string path, ReturnHint hint) { returnHint = std::move(hint); hasReturnHint = true; RenderLock lock; - ensureSdFontLoaded(); + ensureSdFontLoadedForPath(path.c_str()); replaceActivity(std::make_unique(renderer, mappedInput, std::move(path))); } diff --git a/src/activities/settings/SettingsActivity.cpp b/src/activities/settings/SettingsActivity.cpp index 85e6b8ae..13e6850f 100644 --- a/src/activities/settings/SettingsActivity.cpp +++ b/src/activities/settings/SettingsActivity.cpp @@ -97,7 +97,8 @@ void SettingsActivity::onEnter() { continue; } const bool isReaderFontEntry = - enriched.category == StrId::STR_CAT_READER && enriched.submenu == StrId::STR_MENU_READER_FONT; + enriched.category == StrId::STR_CAT_READER && + (enriched.submenu == StrId::STR_MENU_READER_FONT || enriched.submenu == StrId::STR_MENU_TXT_FONT); if (!insertedFontDownload && sawReaderFontSection && !isReaderFontEntry) { insertFontDownloadBelowFontSection(); diff --git a/src/activities/settings/SettingsSubmenuActivity.cpp b/src/activities/settings/SettingsSubmenuActivity.cpp index 1dc4c5e8..754bac00 100644 --- a/src/activities/settings/SettingsSubmenuActivity.cpp +++ b/src/activities/settings/SettingsSubmenuActivity.cpp @@ -6,6 +6,7 @@ #include #include "CrossPointSettings.h" +#include "FontSelectionActivity.h" #include "MappedInputManager.h" #include "SettingActionDispatch.h" #include "components/UITheme.h" @@ -22,6 +23,26 @@ void SettingsSubmenuActivity::onActionSelected(int index) { const auto& setting = menuItems[index]; if (setting.isSeparator) return; + if (setting.type == SettingType::ENUM && setting.nameId == StrId::STR_FONT_FAMILY) { + startActivityForResult( + std::make_unique(renderer, mappedInput, FontSelectionActivity::Target::EPUB), + [this](const ActivityResult&) { + SETTINGS.saveToFile(); + needsHalfRefresh = true; + }); + return; + } + + if (setting.type == SettingType::ENUM && setting.nameId == StrId::STR_TXT_FONT_FAMILY) { + startActivityForResult( + std::make_unique(renderer, mappedInput, FontSelectionActivity::Target::TXT), + [this](const ActivityResult&) { + SETTINGS.saveToFile(); + needsHalfRefresh = true; + }); + return; + } + if (setting.type == SettingType::ACTION) { MenuResult menuResult; if (setting.action != SettingAction::None) { diff --git a/src/main.cpp b/src/main.cpp index f46564eb..6d92e140 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -191,6 +191,24 @@ void setupDisplayAndFonts() { // activity-side callers out of SdCardFontSystem internals. void ensureSdFontLoaded() { sdFontSystem.ensureLoaded(renderer); } +void ensureSdFontLoadedForPath(const char* path) { + if (!path) { + ensureSdFontLoaded(); + return; + } + const size_t len = strlen(path); + auto endsWith = [&](const char* suffix) { + const size_t sl = strlen(suffix); + return len >= sl && strcasecmp(path + len - sl, suffix) == 0; + }; + const bool isTxtMd = endsWith(".txt") || endsWith(".md"); + if (isTxtMd) { + sdFontSystem.ensureLoaded(renderer, SETTINGS.txtSdFontFamilyName, SETTINGS.txtFontSize); + } else { + sdFontSystem.ensureLoaded(renderer, SETTINGS.sdFontFamilyName, SETTINGS.fontSize); + } +} + void setup() { { esp_ota_img_states_t otaState;