Xteink-X4-crosspoint-reader/lib/ExternalFont/FontManager.cpp
aber0724 895fe470c6 feat: Add CJK UI font support and complete I18N implementation
## Major Features

### 1. CJK UI Font System
- Implemented external font loading system for CJK characters
- Added Source Han Sans (思源黑体) as base font for UI rendering
- Support for multiple font sizes (20pt, 22pt, 24pt)
- Font selection UI for both reader and UI fonts
- Automatic fallback to built-in fonts when external fonts unavailable
- External UI font now renders ALL characters (including ASCII) for consistent style
- Proportional spacing for external fonts (variable width per character)

### 2. Complete I18N Implementation
- Added comprehensive internationalization system
- Support for English, Chinese Simplified, and Japanese
- Translated all UI strings across the entire application
- Language selection UI in settings with native language names
  - English displayed as "English"
  - Chinese displayed as "简体中文"
  - Japanese displayed as "日本語"
- Dynamic language switching without restart

### 3. Bug Fixes

#### Rendering Race Conditions
- Fixed race condition where parent and child Activity rendering tasks run simultaneously
- Added 500ms delay in child Activity displayTaskLoop() to wait for parent rendering completion
- Unified displayTaskLoop() logic: `if (updateRequired && !subActivity)`
- Prevents duplicate RED RAM writes and incomplete screen refreshes

**Affected Activities:**
- CategorySettingsActivity: Unified displayTaskLoop check logic
- KOReaderSettingsActivity: Added 500ms delay before first render
- CalibreSettingsActivity: Added 500ms delay before first render
- FontSelectActivity: Added 500ms delay before first render
- ClearCacheActivity: Added 500ms delay and subActivity check
- LanguageSelectActivity: Added 500ms delay in displayTaskLoop (not onEnter)

#### Button Response Issues
- Fixed CrossPointWebServer exit button requiring long press
- Added MappedInputManager::update() method
- Call update() before wasPressed() in tight HTTP processing loop
- Button presses during loop are now properly detected

#### ClearCache Crash
- Fixed FreeRTOS mutex deadlock when exiting ClearCache activity
- Added isExiting flag to prevent operations during exit
- Added clearCacheTaskHandle tracking
- Wait for clearCache task completion before deleting mutex

#### External UI Font Rendering
- Fixed ASCII characters not using external UI font (was using built-in EPD font)
- Fixed character spacing too wide (now uses proportional spacing via getGlyphMetrics)

## Technical Details

**Files Added:**
- lib/ExternalFont/: External font loading system
- lib/I18n/: Internationalization system
- lib/GfxRenderer/cjk_ui_font*.h: Pre-rendered CJK font data
- scripts/generate_cjk_ui_font.py: Font generation script
- src/activities/settings/FontSelectActivity.*: Font selection UI
- src/activities/settings/LanguageSelectActivity.*: Language selection UI
- docs/cjk-fonts.md: CJK font documentation
- docs/i18n.md: I18N documentation

**Files Modified:**
- lib/GfxRenderer/: Added CJK font rendering support with proportional spacing
- src/activities/: I18N integration across all activities
- src/MappedInputManager.*: Added update() method
- src/CrossPointSettings.cpp: Added language and font settings

**Memory Usage:**
- Flash: 94.7% (6204434 bytes / 6553600 bytes)
- RAM: 66.4% (217556 bytes / 327680 bytes)

## Testing Notes

All rendering race conditions and button response issues have been fixed and tested.
ClearCache no longer crashes when exiting.
File transfer page now responds to short press on exit button.
External UI font now renders all characters with proper proportional spacing.
Language selection page displays language names in their native scripts.

Co-authored-by: Claude (Anthropic AI Assistant)
2026-01-23 16:54:56 +09:00

285 lines
6.9 KiB
C++

#include "FontManager.h"
#include <HardwareSerial.h>
#include <SDCardManager.h>
#include <Serialization.h>
#include <cstring>
// Out-of-class definitions for static constexpr members (required for ODR-use
// in C++14)
constexpr int FontManager::MAX_FONTS;
constexpr const char *FontManager::FONTS_DIR;
constexpr const char *FontManager::SETTINGS_FILE;
constexpr uint8_t FontManager::SETTINGS_VERSION;
FontManager &FontManager::getInstance() {
static FontManager instance;
return instance;
}
void FontManager::scanFonts() {
_fontCount = 0;
FsFile dir = SdMan.open(FONTS_DIR, O_RDONLY);
if (!dir) {
Serial.printf("[FONT_MGR] Cannot open fonts directory: %s\n", FONTS_DIR);
return;
}
if (!dir.isDir()) {
Serial.printf("[FONT_MGR] %s is not a directory\n", FONTS_DIR);
dir.close();
return;
}
FsFile entry;
while (_fontCount < MAX_FONTS && entry.openNext(&dir, O_RDONLY)) {
if (entry.isDir()) {
entry.close();
continue;
}
char filename[64];
entry.getName(filename, sizeof(filename));
entry.close();
// Check .bin extension
if (!strstr(filename, ".bin")) {
continue;
}
// Try to parse filename
FontInfo &info = _fonts[_fontCount];
strncpy(info.filename, filename, sizeof(info.filename) - 1);
info.filename[sizeof(info.filename) - 1] = '\0';
// Parse filename to get font info
char nameCopy[64];
strncpy(nameCopy, filename, sizeof(nameCopy) - 1);
nameCopy[sizeof(nameCopy) - 1] = '\0';
// Remove .bin
char *ext = strstr(nameCopy, ".bin");
if (ext)
*ext = '\0';
// Parse _WxH
char *lastUnderscore = strrchr(nameCopy, '_');
if (!lastUnderscore)
continue;
int w, h;
if (sscanf(lastUnderscore + 1, "%dx%d", &w, &h) != 2)
continue;
info.width = (uint8_t)w;
info.height = (uint8_t)h;
*lastUnderscore = '\0';
// Parse _size
lastUnderscore = strrchr(nameCopy, '_');
if (!lastUnderscore)
continue;
int size;
if (sscanf(lastUnderscore + 1, "%d", &size) != 1)
continue;
info.size = (uint8_t)size;
*lastUnderscore = '\0';
// Font name
strncpy(info.name, nameCopy, sizeof(info.name) - 1);
info.name[sizeof(info.name) - 1] = '\0';
Serial.printf("[FONT_MGR] Found font: %s (%dpt, %dx%d)\n", info.name,
info.size, info.width, info.height);
_fontCount++;
}
dir.close();
Serial.printf("[FONT_MGR] Scan complete: %d fonts found\n", _fontCount);
}
const FontInfo *FontManager::getFontInfo(int index) const {
if (index < 0 || index >= _fontCount) {
return nullptr;
}
return &_fonts[index];
}
bool FontManager::loadSelectedFont() {
_activeFont.unload();
if (_selectedIndex < 0 || _selectedIndex >= _fontCount) {
return false;
}
char filepath[80];
snprintf(filepath, sizeof(filepath), "%s/%s", FONTS_DIR,
_fonts[_selectedIndex].filename);
return _activeFont.load(filepath);
}
bool FontManager::loadSelectedUiFont() {
_activeUiFont.unload();
if (_selectedUiIndex < 0 || _selectedUiIndex >= _fontCount) {
return false;
}
char filepath[80];
snprintf(filepath, sizeof(filepath), "%s/%s", FONTS_DIR,
_fonts[_selectedUiIndex].filename);
return _activeUiFont.load(filepath);
}
void FontManager::selectFont(int index) {
if (index == _selectedIndex) {
return;
}
_selectedIndex = index;
if (index >= 0) {
loadSelectedFont();
} else {
_activeFont.unload();
}
saveSettings();
}
void FontManager::selectUiFont(int index) {
if (index == _selectedUiIndex) {
return;
}
_selectedUiIndex = index;
if (index >= 0) {
loadSelectedUiFont();
} else {
_activeUiFont.unload();
}
saveSettings();
}
ExternalFont *FontManager::getActiveFont() {
if (_selectedIndex >= 0 && _activeFont.isLoaded()) {
return &_activeFont;
}
return nullptr;
}
ExternalFont *FontManager::getActiveUiFont() {
if (_selectedUiIndex >= 0 && _activeUiFont.isLoaded()) {
return &_activeUiFont;
}
return nullptr;
}
void FontManager::saveSettings() {
SdMan.mkdir("/.crosspoint");
FsFile file;
if (!SdMan.openFileForWrite("FONT_MGR", SETTINGS_FILE, file)) {
Serial.printf("[FONT_MGR] Failed to save settings\n");
return;
}
serialization::writePod(file, SETTINGS_VERSION);
serialization::writePod(file, _selectedIndex);
// Save selected reader font filename (for matching when restoring)
if (_selectedIndex >= 0 && _selectedIndex < _fontCount) {
serialization::writeString(file,
std::string(_fonts[_selectedIndex].filename));
} else {
serialization::writeString(file, std::string(""));
}
// Save UI font settings (version 2+)
serialization::writePod(file, _selectedUiIndex);
if (_selectedUiIndex >= 0 && _selectedUiIndex < _fontCount) {
serialization::writeString(file,
std::string(_fonts[_selectedUiIndex].filename));
} else {
serialization::writeString(file, std::string(""));
}
file.close();
Serial.printf("[FONT_MGR] Settings saved\n");
}
void FontManager::loadSettings() {
FsFile file;
if (!SdMan.openFileForRead("FONT_MGR", SETTINGS_FILE, file)) {
Serial.printf("[FONT_MGR] No settings file, using defaults\n");
return;
}
uint8_t version;
serialization::readPod(file, version);
if (version < 1 || version > SETTINGS_VERSION) {
Serial.printf("[FONT_MGR] Settings version mismatch (%d vs %d)\n", version,
SETTINGS_VERSION);
file.close();
return;
}
// Load reader font settings
int savedIndex;
serialization::readPod(file, savedIndex);
std::string savedFilename;
serialization::readString(file, savedFilename);
// Find matching reader font by filename
if (savedIndex >= 0 && !savedFilename.empty()) {
for (int i = 0; i < _fontCount; i++) {
if (savedFilename == _fonts[i].filename) {
_selectedIndex = i;
loadSelectedFont();
Serial.printf("[FONT_MGR] Restored reader font: %s\n",
savedFilename.c_str());
break;
}
}
if (_selectedIndex < 0) {
Serial.printf("[FONT_MGR] Saved reader font not found: %s\n",
savedFilename.c_str());
}
}
// Load UI font settings (version 2+)
if (version >= 2) {
int savedUiIndex;
serialization::readPod(file, savedUiIndex);
std::string savedUiFilename;
serialization::readString(file, savedUiFilename);
if (savedUiIndex >= 0 && !savedUiFilename.empty()) {
for (int i = 0; i < _fontCount; i++) {
if (savedUiFilename == _fonts[i].filename) {
_selectedUiIndex = i;
loadSelectedUiFont();
Serial.printf("[FONT_MGR] Restored UI font: %s\n",
savedUiFilename.c_str());
break;
}
}
if (_selectedUiIndex < 0) {
Serial.printf("[FONT_MGR] Saved UI font not found: %s\n",
savedUiFilename.c_str());
}
}
}
file.close();
}