Xteink-X4-crosspoint-reader/lib/ExternalFont/FontManager.h
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

138 lines
3.1 KiB
C++

#pragma once
#include <cstdint>
#include "ExternalFont.h"
/**
* Font information structure
*/
struct FontInfo {
char filename[64]; // Full filename
char name[32]; // Font name
uint8_t size; // Font size (pt)
uint8_t width; // Character width
uint8_t height; // Character height
};
/**
* Font Manager - Singleton pattern
* Manages font scanning, selection, and settings persistence
* Supports two font slots: Reader font (for book content) and UI font (for
* menus/titles)
*/
class FontManager {
public:
static FontManager &getInstance();
// Disable copy
FontManager(const FontManager &) = delete;
FontManager &operator=(const FontManager &) = delete;
/**
* Scan /fonts/ directory to get available font list
*/
void scanFonts();
/**
* Get font count
*/
int getFontCount() const { return _fontCount; }
/**
* Get font info
* @param index Font index (0 to getFontCount()-1)
*/
const FontInfo *getFontInfo(int index) const;
/**
* Select reader font (for book content)
* @param index Font index, -1 means disable external font (use built-in)
*/
void selectFont(int index);
/**
* Select UI font (for menus, titles, etc.)
* @param index Font index, -1 means disable (fallback to reader font or
* built-in)
*/
void selectUiFont(int index);
/**
* Get currently selected reader font index
* @return -1 means using built-in font
*/
int getSelectedIndex() const { return _selectedIndex; }
/**
* Get currently selected UI font index
* @return -1 means using reader font fallback
*/
int getUiSelectedIndex() const { return _selectedUiIndex; }
/**
* Get currently active reader font
* @return Font pointer, nullptr if not enabled
*/
ExternalFont *getActiveFont();
/**
* Get currently active UI font
* @return Font pointer, nullptr if not enabled (will fallback to reader font)
*/
ExternalFont *getActiveUiFont();
/**
* Check if external reader font is enabled
*/
bool isExternalFontEnabled() const {
return _selectedIndex >= 0 && _activeFont.isLoaded();
}
/**
* Check if external UI font is enabled
*/
bool isUiFontEnabled() const {
return _selectedUiIndex >= 0 && _activeUiFont.isLoaded();
}
/**
* Save settings to SD card
*/
void saveSettings();
/**
* Load settings from SD card
*/
void loadSettings();
private:
FontManager() = default;
static constexpr int MAX_FONTS = 16;
static constexpr const char *FONTS_DIR = "/fonts";
static constexpr const char *SETTINGS_FILE = "/.crosspoint/font_settings.bin";
static constexpr uint8_t SETTINGS_VERSION = 2; // Bumped for UI font support
FontInfo _fonts[MAX_FONTS];
int _fontCount = 0;
int _selectedIndex = -1; // -1 = built-in font (reader)
int _selectedUiIndex = -1; // -1 = fallback to reader font
ExternalFont _activeFont; // Reader font
ExternalFont _activeUiFont; // UI font
/**
* Load selected reader font file
*/
bool loadSelectedFont();
/**
* Load selected UI font file
*/
bool loadSelectedUiFont();
};
// Convenience macro
#define FontMgr FontManager::getInstance()