mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-05 15:17:37 +03:00
Fix: Add fallback for missing custom fonts to prevent render errors
This commit is contained in:
parent
98d5d64cfc
commit
46c3cb0e55
@ -2,13 +2,19 @@
|
|||||||
|
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "../../src/CrossPointSettings.h"
|
#include "../../src/CrossPointSettings.h"
|
||||||
#include "../../src/managers/FontManager.h"
|
#include "../../src/managers/FontManager.h"
|
||||||
|
|
||||||
|
std::vector<int> EpdFontLoader::loadedCustomIds;
|
||||||
|
|
||||||
void EpdFontLoader::loadFontsFromSd(GfxRenderer& renderer) {
|
void EpdFontLoader::loadFontsFromSd(GfxRenderer& renderer) {
|
||||||
|
loadedCustomIds.clear();
|
||||||
|
|
||||||
// Check settings for custom font
|
// Check settings for custom font
|
||||||
if (SETTINGS.fontFamily == CrossPointSettings::FONT_CUSTOM) {
|
if (SETTINGS.fontFamily == CrossPointSettings::FONT_CUSTOM) {
|
||||||
if (strlen(SETTINGS.customFontFamily) > 0) {
|
if (strlen(SETTINGS.customFontFamily) > 0) {
|
||||||
@ -50,6 +56,7 @@ void EpdFontLoader::loadFontsFromSd(GfxRenderer& renderer) {
|
|||||||
Serial.printf("[FontLoader] Inserting custom font '%s' with ID %d (key: %s)\n", SETTINGS.customFontFamily, id,
|
Serial.printf("[FontLoader] Inserting custom font '%s' with ID %d (key: %s)\n", SETTINGS.customFontFamily, id,
|
||||||
key.c_str());
|
key.c_str());
|
||||||
renderer.insertFont(id, *family);
|
renderer.insertFont(id, *family);
|
||||||
|
loadedCustomIds.push_back(id);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Failed to load custom font family");
|
Serial.println("Failed to load custom font family");
|
||||||
}
|
}
|
||||||
@ -60,15 +67,24 @@ void EpdFontLoader::loadFontsFromSd(GfxRenderer& renderer) {
|
|||||||
int EpdFontLoader::getBestFontId(const char* familyName, int size) {
|
int EpdFontLoader::getBestFontId(const char* familyName, int size) {
|
||||||
if (!familyName || strlen(familyName) == 0) return -1;
|
if (!familyName || strlen(familyName) == 0) return -1;
|
||||||
|
|
||||||
// We assume the font is loaded if we are asking for its ID,
|
|
||||||
// or at least that the ID generation is deterministic.
|
|
||||||
// The renderer uses the ID to look up the font.
|
|
||||||
// If we return an ID that isn't inserted, renderer might crash or show nothing.
|
|
||||||
// So we should ideally check if it's available.
|
|
||||||
|
|
||||||
// For now, just return the deterministic hash.
|
// For now, just return the deterministic hash.
|
||||||
std::string key = std::string(familyName) + "-" + std::to_string(size);
|
std::string key = std::string(familyName) + "-" + std::to_string(size);
|
||||||
uint32_t hash = 5381;
|
uint32_t hash = 5381;
|
||||||
for (char c : key) hash = ((hash << 5) + hash) + c;
|
for (char c : key) hash = ((hash << 5) + hash) + c;
|
||||||
return (int)hash;
|
int id = (int)hash;
|
||||||
|
|
||||||
|
// Verify if the font was actually loaded
|
||||||
|
bool found = false;
|
||||||
|
for (int loadedId : loadedCustomIds) {
|
||||||
|
if (loadedId == id) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (found) {
|
||||||
|
return id;
|
||||||
|
} else {
|
||||||
|
return -1; // Fallback to builtin font
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,8 +2,13 @@
|
|||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class EpdFontLoader {
|
class EpdFontLoader {
|
||||||
public:
|
public:
|
||||||
static void loadFontsFromSd(GfxRenderer& renderer);
|
static void loadFontsFromSd(GfxRenderer& renderer);
|
||||||
static int getBestFontId(const char* familyName, int size);
|
static int getBestFontId(const char* familyName, int size);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::vector<int> loadedCustomIds;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user