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

129 lines
3.8 KiB
C++

#pragma once
#include <SDCardManager.h>
#include <cstdint>
/**
* External font loader - supports Xteink .bin format
* Filename format: FontName_size_WxH.bin (e.g. KingHwaOldSong_38_33x39.bin)
*
* Font format:
* - Direct Unicode codepoint indexing
* - Offset = codepoint * bytesPerChar
* - Each char = bytesPerRow * charHeight bytes
* - 1-bit black/white bitmap, MSB first
*/
class ExternalFont {
public:
ExternalFont() = default;
~ExternalFont();
// Disable copy
ExternalFont(const ExternalFont &) = delete;
ExternalFont &operator=(const ExternalFont &) = delete;
/**
* Load font from .bin file
* @param filepath Full path on SD card (e.g.
* "/fonts/KingHwaOldSong_38_33x39.bin")
* @return true on success
*/
bool load(const char *filepath);
/**
* Get glyph bitmap data (with LRU cache)
* @param codepoint Unicode codepoint
* @return Bitmap data pointer, nullptr if char not found
*/
const uint8_t *getGlyph(uint32_t codepoint);
/**
* Preload multiple glyphs at once (optimized for batch SD reads)
* Call this before rendering a chapter to warm up the cache
* @param codepoints Array of unicode codepoints to preload
* @param count Number of codepoints in the array
*/
void preloadGlyphs(const uint32_t *codepoints, size_t count);
// Font properties
uint8_t getCharWidth() const { return _charWidth; }
uint8_t getCharHeight() const { return _charHeight; }
uint8_t getBytesPerRow() const { return _bytesPerRow; }
uint16_t getBytesPerChar() const { return _bytesPerChar; }
const char *getFontName() const { return _fontName; }
uint8_t getFontSize() const { return _fontSize; }
bool isLoaded() const { return _isLoaded; }
void unload();
/**
* Get cached metrics for a glyph.
* Must call getGlyph() first to ensure it's loaded!
* @param cp Unicode codepoint
* @param outMinX Minimum X offset (left bearing)
* @param outAdvanceX Advance width for cursor positioning
* @return true if metrics found in cache, false otherwise
*/
bool getGlyphMetrics(uint32_t cp, uint8_t *outMinX, uint8_t *outAdvanceX);
private:
// Font file handle (keep open to avoid repeated open/close)
FsFile _fontFile;
bool _isLoaded = false;
// Properties parsed from filename
char _fontName[32] = {0};
uint8_t _fontSize = 0;
uint8_t _charWidth = 0;
uint8_t _charHeight = 0;
uint8_t _bytesPerRow = 0;
uint16_t _bytesPerChar = 0;
// LRU cache - 256 glyphs for better Chinese text performance
// Memory: ~52KB (256 * 204 bytes per entry)
static constexpr int CACHE_SIZE = 256; // 256 glyphs
static constexpr int MAX_GLYPH_BYTES =
200; // Max 200 bytes per glyph (enough for 33x39)
// Flag to mark cached "non-existent" glyphs (avoid repeated SD reads)
static constexpr uint8_t GLYPH_NOT_FOUND_MARKER = 0xFE;
struct CacheEntry {
uint32_t codepoint = 0xFFFFFFFF; // Invalid marker
uint8_t bitmap[MAX_GLYPH_BYTES];
uint32_t lastUsed = 0;
bool notFound = false; // True if glyph doesn't exist in font
uint8_t minX = 0; // Cached rendering metrics
uint8_t advanceX = 0; // Cached advance width
};
CacheEntry _cache[CACHE_SIZE];
uint32_t _accessCounter = 0;
// Simple hash table for O(1) cache lookup (codepoint -> cache index, -1 if
// not cached)
int16_t _hashTable[CACHE_SIZE];
static int hashCodepoint(uint32_t cp) { return cp % CACHE_SIZE; }
/**
* Read glyph data from SD card
*/
bool readGlyphFromSD(uint32_t codepoint, uint8_t *buffer);
/**
* Parse filename to get font parameters
* Format: FontName_size_WxH.bin
*/
bool parseFilename(const char *filename);
/**
* Find glyph in cache
* @return Cache index, -1 if not found
*/
int findInCache(uint32_t codepoint);
/**
* Get LRU cache slot (least recently used)
*/
int getLruSlot();
};