**What is the goal of this PR?** This PR introduces Internationalization (i18n) support, enabling users to switch the UI language dynamically. **What changes are included?** - Core Logic: Added I18n class (`lib/I18n/I18n.h/cpp`) to manage language state and string retrieval. - Data Structures: - `lib/I18n/I18nStrings.h/cpp`: Static string arrays for each supported language. - `lib/I18n/I18nKeys.h`: Enum definitions for type-safe string access. - `lib/I18n/translations.csv`: single source of truth. - Documentation: Added `docs/i18n.md` detailing the workflow for developers and translators. - New Settings activity: `src/activities/settings/LanguageSelectActivity.h/cpp` This implementation (building on concepts from #505) prioritizes performance and memory efficiency. The core approach is to store all localized strings for each language in dedicated arrays and access them via enums. This provides O(1) access with zero runtime overhead, and avoids the heap allocations, hashing, and collision handling required by `std::map` or `std::unordered_map`. The main trade-off is that enums and string arrays must remain perfectly synchronized—any mismatch would result in incorrect strings being displayed in the UI. To eliminate this risk, I added a Python script that automatically generates `I18nStrings.h/.cpp` and `I18nKeys.h` from a CSV file, which will serve as the single source of truth for all translations. The full design and workflow are documented in `docs/i18n.md`. - [x] Python script `generate_i18n.py` to auto-generate C++ files from CSV - [x] Populate translations.csv with initial translations. Currently available translations: English, Español, Français, Deutsch, Čeština, Português (Brasil), Русский, Svenska. Thanks, community! **Status:** EDIT: ready to be merged. As a proof of concept, the SPANISH strings currently mirror the English ones, but are fully uppercased. --- Did you use AI tools to help write this code? _**< PARTIALLY >**_ I used AI for the black work of replacing strings with I18n references across the project, and for generating the documentation. EDIT: also some help with merging changes from master. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: yeyeto2788 <juanernestobiondi@gmail.com>
409 lines
14 KiB
C++
409 lines
14 KiB
C++
/**
|
|
* XtcReaderActivity.cpp
|
|
*
|
|
* XTC ebook reader activity implementation
|
|
* Displays pre-rendered XTC pages on e-ink display
|
|
*/
|
|
|
|
#include "XtcReaderActivity.h"
|
|
|
|
#include <FsHelpers.h>
|
|
#include <GfxRenderer.h>
|
|
#include <HalStorage.h>
|
|
#include <I18n.h>
|
|
|
|
#include <PlaceholderCoverGenerator.h>
|
|
|
|
#include "CrossPointSettings.h"
|
|
#include "CrossPointState.h"
|
|
#include "MappedInputManager.h"
|
|
#include "RecentBooksStore.h"
|
|
#include "XtcReaderChapterSelectionActivity.h"
|
|
#include "components/UITheme.h"
|
|
#include "fontIds.h"
|
|
|
|
namespace {
|
|
constexpr unsigned long skipPageMs = 700;
|
|
constexpr unsigned long goHomeMs = 1000;
|
|
} // namespace
|
|
|
|
void XtcReaderActivity::onEnter() {
|
|
ActivityWithSubactivity::onEnter();
|
|
|
|
if (!xtc) {
|
|
return;
|
|
}
|
|
|
|
xtc->setupCacheDir();
|
|
|
|
// Load saved progress
|
|
loadProgress();
|
|
|
|
// Prerender covers and thumbnails on first open so Home and Sleep screens are instant.
|
|
// Each generate* call is a no-op if the file already exists, so this only does work once.
|
|
{
|
|
int totalSteps = 0;
|
|
if (!Storage.exists(xtc->getCoverBmpPath().c_str())) totalSteps++;
|
|
for (int i = 0; i < PRERENDER_THUMB_HEIGHTS_COUNT; i++) {
|
|
if (!Storage.exists(xtc->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) totalSteps++;
|
|
}
|
|
|
|
if (totalSteps > 0) {
|
|
Rect popupRect = GUI.drawPopup(renderer, "Preparing book...");
|
|
int completedSteps = 0;
|
|
|
|
auto updateProgress = [&]() {
|
|
completedSteps++;
|
|
GUI.fillPopupProgress(renderer, popupRect, completedSteps * 100 / totalSteps);
|
|
};
|
|
|
|
if (!Storage.exists(xtc->getCoverBmpPath().c_str())) {
|
|
xtc->generateCoverBmp();
|
|
// Fallback: generate placeholder if first-page cover extraction failed
|
|
if (!Storage.exists(xtc->getCoverBmpPath().c_str())) {
|
|
PlaceholderCoverGenerator::generate(xtc->getCoverBmpPath(), xtc->getTitle(), xtc->getAuthor(), 480, 800);
|
|
}
|
|
updateProgress();
|
|
}
|
|
for (int i = 0; i < PRERENDER_THUMB_HEIGHTS_COUNT; i++) {
|
|
if (!Storage.exists(xtc->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) {
|
|
xtc->generateThumbBmp(PRERENDER_THUMB_HEIGHTS[i]);
|
|
// Fallback: generate placeholder thumbnail
|
|
if (!Storage.exists(xtc->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) {
|
|
const int thumbHeight = PRERENDER_THUMB_HEIGHTS[i];
|
|
const int thumbWidth = static_cast<int>(thumbHeight * 0.6);
|
|
PlaceholderCoverGenerator::generate(xtc->getThumbBmpPath(thumbHeight), xtc->getTitle(), xtc->getAuthor(),
|
|
thumbWidth, thumbHeight);
|
|
}
|
|
updateProgress();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Save current XTC as last opened book and add to recent books
|
|
APP_STATE.openEpubPath = xtc->getPath();
|
|
APP_STATE.saveToFile();
|
|
RECENT_BOOKS.addBook(xtc->getPath(), xtc->getTitle(), xtc->getAuthor(), xtc->getThumbBmpPath());
|
|
|
|
// Trigger first update
|
|
requestUpdate();
|
|
}
|
|
|
|
void XtcReaderActivity::onExit() {
|
|
ActivityWithSubactivity::onExit();
|
|
|
|
APP_STATE.readerActivityLoadCount = 0;
|
|
APP_STATE.saveToFile();
|
|
xtc.reset();
|
|
}
|
|
|
|
void XtcReaderActivity::loop() {
|
|
// Pass input responsibility to sub activity if exists
|
|
if (subActivity) {
|
|
subActivity->loop();
|
|
return;
|
|
}
|
|
|
|
// Enter chapter selection activity
|
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
|
if (xtc && xtc->hasChapters() && !xtc->getChapters().empty()) {
|
|
exitActivity();
|
|
enterNewActivity(new XtcReaderChapterSelectionActivity(
|
|
this->renderer, this->mappedInput, xtc, currentPage,
|
|
[this] {
|
|
exitActivity();
|
|
requestUpdate();
|
|
},
|
|
[this](const uint32_t newPage) {
|
|
currentPage = newPage;
|
|
exitActivity();
|
|
requestUpdate();
|
|
}));
|
|
}
|
|
}
|
|
|
|
// Long press BACK (1s+) goes to file selection
|
|
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
|
onGoBack();
|
|
return;
|
|
}
|
|
|
|
// Short press BACK goes directly to home
|
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back) && mappedInput.getHeldTime() < goHomeMs) {
|
|
onGoHome();
|
|
return;
|
|
}
|
|
|
|
// When long-press chapter skip is disabled, turn pages on press instead of release.
|
|
const bool usePressForPageTurn = !SETTINGS.longPressChapterSkip;
|
|
const bool prevTriggered = usePressForPageTurn ? (mappedInput.wasPressed(MappedInputManager::Button::PageBack) ||
|
|
mappedInput.wasPressed(MappedInputManager::Button::Left))
|
|
: (mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
|
|
mappedInput.wasReleased(MappedInputManager::Button::Left));
|
|
const bool powerPageTurn = SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
|
|
mappedInput.wasReleased(MappedInputManager::Button::Power);
|
|
const bool nextTriggered = usePressForPageTurn
|
|
? (mappedInput.wasPressed(MappedInputManager::Button::PageForward) || powerPageTurn ||
|
|
mappedInput.wasPressed(MappedInputManager::Button::Right))
|
|
: (mappedInput.wasReleased(MappedInputManager::Button::PageForward) || powerPageTurn ||
|
|
mappedInput.wasReleased(MappedInputManager::Button::Right));
|
|
|
|
if (!prevTriggered && !nextTriggered) {
|
|
return;
|
|
}
|
|
|
|
// Handle end of book
|
|
if (currentPage >= xtc->getPageCount()) {
|
|
currentPage = xtc->getPageCount() - 1;
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
|
|
const bool skipPages = SETTINGS.longPressChapterSkip && mappedInput.getHeldTime() > skipPageMs;
|
|
const int skipAmount = skipPages ? 10 : 1;
|
|
|
|
if (prevTriggered) {
|
|
if (currentPage >= static_cast<uint32_t>(skipAmount)) {
|
|
currentPage -= skipAmount;
|
|
} else {
|
|
currentPage = 0;
|
|
}
|
|
requestUpdate();
|
|
} else if (nextTriggered) {
|
|
currentPage += skipAmount;
|
|
if (currentPage >= xtc->getPageCount()) {
|
|
currentPage = xtc->getPageCount(); // Allow showing "End of book"
|
|
}
|
|
requestUpdate();
|
|
}
|
|
}
|
|
|
|
void XtcReaderActivity::render(Activity::RenderLock&&) {
|
|
if (!xtc) {
|
|
return;
|
|
}
|
|
|
|
// Bounds check
|
|
if (currentPage >= xtc->getPageCount()) {
|
|
// Show end of book screen
|
|
renderer.clearScreen();
|
|
renderer.drawCenteredText(UI_12_FONT_ID, 300, tr(STR_END_OF_BOOK), true, EpdFontFamily::BOLD);
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
renderPage();
|
|
saveProgress();
|
|
}
|
|
|
|
void XtcReaderActivity::renderPage() {
|
|
const uint16_t pageWidth = xtc->getPageWidth();
|
|
const uint16_t pageHeight = xtc->getPageHeight();
|
|
const uint8_t bitDepth = xtc->getBitDepth();
|
|
|
|
// Calculate buffer size for one page
|
|
// XTG (1-bit): Row-major, ((width+7)/8) * height bytes
|
|
// XTH (2-bit): Two bit planes, column-major, ((width * height + 7) / 8) * 2 bytes
|
|
size_t pageBufferSize;
|
|
if (bitDepth == 2) {
|
|
pageBufferSize = ((static_cast<size_t>(pageWidth) * pageHeight + 7) / 8) * 2;
|
|
} else {
|
|
pageBufferSize = ((pageWidth + 7) / 8) * pageHeight;
|
|
}
|
|
|
|
// Allocate page buffer
|
|
uint8_t* pageBuffer = static_cast<uint8_t*>(malloc(pageBufferSize));
|
|
if (!pageBuffer) {
|
|
LOG_ERR("XTR", "Failed to allocate page buffer (%lu bytes)", pageBufferSize);
|
|
renderer.clearScreen();
|
|
renderer.drawCenteredText(UI_12_FONT_ID, 300, tr(STR_MEMORY_ERROR), true, EpdFontFamily::BOLD);
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
// Load page data
|
|
size_t bytesRead = xtc->loadPage(currentPage, pageBuffer, pageBufferSize);
|
|
if (bytesRead == 0) {
|
|
LOG_ERR("XTR", "Failed to load page %lu", currentPage);
|
|
free(pageBuffer);
|
|
renderer.clearScreen();
|
|
renderer.drawCenteredText(UI_12_FONT_ID, 300, tr(STR_PAGE_LOAD_ERROR), true, EpdFontFamily::BOLD);
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
// Clear screen first
|
|
renderer.clearScreen();
|
|
|
|
// Copy page bitmap using GfxRenderer's drawPixel
|
|
// XTC/XTCH pages are pre-rendered with status bar included, so render full page
|
|
const uint16_t maxSrcY = pageHeight;
|
|
|
|
if (bitDepth == 2) {
|
|
// XTH 2-bit mode: Two bit planes, column-major order
|
|
// - Columns scanned right to left (x = width-1 down to 0)
|
|
// - 8 vertical pixels per byte (MSB = topmost pixel in group)
|
|
// - First plane: Bit1, Second plane: Bit2
|
|
// - Pixel value = (bit1 << 1) | bit2
|
|
// - Grayscale: 0=White, 1=Dark Grey, 2=Light Grey, 3=Black
|
|
|
|
const size_t planeSize = (static_cast<size_t>(pageWidth) * pageHeight + 7) / 8;
|
|
const uint8_t* plane1 = pageBuffer; // Bit1 plane
|
|
const uint8_t* plane2 = pageBuffer + planeSize; // Bit2 plane
|
|
const size_t colBytes = (pageHeight + 7) / 8; // Bytes per column (100 for 800 height)
|
|
|
|
// Lambda to get pixel value at (x, y)
|
|
auto getPixelValue = [&](uint16_t x, uint16_t y) -> uint8_t {
|
|
const size_t colIndex = pageWidth - 1 - x;
|
|
const size_t byteInCol = y / 8;
|
|
const size_t bitInByte = 7 - (y % 8);
|
|
const size_t byteOffset = colIndex * colBytes + byteInCol;
|
|
const uint8_t bit1 = (plane1[byteOffset] >> bitInByte) & 1;
|
|
const uint8_t bit2 = (plane2[byteOffset] >> bitInByte) & 1;
|
|
return (bit1 << 1) | bit2;
|
|
};
|
|
|
|
// Optimized grayscale rendering without storeBwBuffer (saves 48KB peak memory)
|
|
// Flow: BW display → LSB/MSB passes → grayscale display → re-render BW for next frame
|
|
|
|
// Count pixel distribution for debugging
|
|
uint32_t pixelCounts[4] = {0, 0, 0, 0};
|
|
for (uint16_t y = 0; y < pageHeight; y++) {
|
|
for (uint16_t x = 0; x < pageWidth; x++) {
|
|
pixelCounts[getPixelValue(x, y)]++;
|
|
}
|
|
}
|
|
LOG_DBG("XTR", "Pixel distribution: White=%lu, DarkGrey=%lu, LightGrey=%lu, Black=%lu", pixelCounts[0],
|
|
pixelCounts[1], pixelCounts[2], pixelCounts[3]);
|
|
|
|
// Pass 1: BW buffer - draw all non-white pixels as black
|
|
for (uint16_t y = 0; y < pageHeight; y++) {
|
|
for (uint16_t x = 0; x < pageWidth; x++) {
|
|
if (getPixelValue(x, y) >= 1) {
|
|
renderer.drawPixel(x, y, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Display BW with conditional refresh based on pagesUntilFullRefresh
|
|
if (pagesUntilFullRefresh <= 1) {
|
|
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
|
|
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
|
|
} else {
|
|
renderer.displayBuffer();
|
|
pagesUntilFullRefresh--;
|
|
}
|
|
|
|
// Pass 2: LSB buffer - mark DARK gray only (XTH value 1)
|
|
// In LUT: 0 bit = apply gray effect, 1 bit = untouched
|
|
renderer.clearScreen(0x00);
|
|
for (uint16_t y = 0; y < pageHeight; y++) {
|
|
for (uint16_t x = 0; x < pageWidth; x++) {
|
|
if (getPixelValue(x, y) == 1) { // Dark grey only
|
|
renderer.drawPixel(x, y, false);
|
|
}
|
|
}
|
|
}
|
|
renderer.copyGrayscaleLsbBuffers();
|
|
|
|
// Pass 3: MSB buffer - mark LIGHT AND DARK gray (XTH value 1 or 2)
|
|
// In LUT: 0 bit = apply gray effect, 1 bit = untouched
|
|
renderer.clearScreen(0x00);
|
|
for (uint16_t y = 0; y < pageHeight; y++) {
|
|
for (uint16_t x = 0; x < pageWidth; x++) {
|
|
const uint8_t pv = getPixelValue(x, y);
|
|
if (pv == 1 || pv == 2) { // Dark grey or Light grey
|
|
renderer.drawPixel(x, y, false);
|
|
}
|
|
}
|
|
}
|
|
renderer.copyGrayscaleMsbBuffers();
|
|
|
|
// Display grayscale overlay
|
|
renderer.displayGrayBuffer();
|
|
|
|
// Pass 4: Re-render BW to framebuffer (restore for next frame, instead of restoreBwBuffer)
|
|
renderer.clearScreen();
|
|
for (uint16_t y = 0; y < pageHeight; y++) {
|
|
for (uint16_t x = 0; x < pageWidth; x++) {
|
|
if (getPixelValue(x, y) >= 1) {
|
|
renderer.drawPixel(x, y, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Cleanup grayscale buffers with current frame buffer
|
|
renderer.cleanupGrayscaleWithFrameBuffer();
|
|
|
|
free(pageBuffer);
|
|
|
|
LOG_DBG("XTR", "Rendered page %lu/%lu (2-bit grayscale)", currentPage + 1, xtc->getPageCount());
|
|
return;
|
|
} else {
|
|
// 1-bit mode: 8 pixels per byte, MSB first
|
|
const size_t srcRowBytes = (pageWidth + 7) / 8; // 60 bytes for 480 width
|
|
|
|
for (uint16_t srcY = 0; srcY < maxSrcY; srcY++) {
|
|
const size_t srcRowStart = srcY * srcRowBytes;
|
|
|
|
for (uint16_t srcX = 0; srcX < pageWidth; srcX++) {
|
|
// Read source pixel (MSB first, bit 7 = leftmost pixel)
|
|
const size_t srcByte = srcRowStart + srcX / 8;
|
|
const size_t srcBit = 7 - (srcX % 8);
|
|
const bool isBlack = !((pageBuffer[srcByte] >> srcBit) & 1); // XTC: 0 = black, 1 = white
|
|
|
|
if (isBlack) {
|
|
renderer.drawPixel(srcX, srcY, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// White pixels are already cleared by clearScreen()
|
|
|
|
free(pageBuffer);
|
|
|
|
// XTC pages already have status bar pre-rendered, no need to add our own
|
|
|
|
// Display with appropriate refresh
|
|
if (pagesUntilFullRefresh <= 1) {
|
|
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
|
|
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
|
|
} else {
|
|
renderer.displayBuffer();
|
|
pagesUntilFullRefresh--;
|
|
}
|
|
|
|
LOG_DBG("XTR", "Rendered page %lu/%lu (%u-bit)", currentPage + 1, xtc->getPageCount(), bitDepth);
|
|
}
|
|
|
|
void XtcReaderActivity::saveProgress() const {
|
|
FsFile f;
|
|
if (Storage.openFileForWrite("XTR", xtc->getCachePath() + "/progress.bin", f)) {
|
|
uint8_t data[4];
|
|
data[0] = currentPage & 0xFF;
|
|
data[1] = (currentPage >> 8) & 0xFF;
|
|
data[2] = (currentPage >> 16) & 0xFF;
|
|
data[3] = (currentPage >> 24) & 0xFF;
|
|
f.write(data, 4);
|
|
f.close();
|
|
}
|
|
}
|
|
|
|
void XtcReaderActivity::loadProgress() {
|
|
FsFile f;
|
|
if (Storage.openFileForRead("XTR", xtc->getCachePath() + "/progress.bin", f)) {
|
|
uint8_t data[4];
|
|
if (f.read(data, 4) == 4) {
|
|
currentPage = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
|
LOG_DBG("XTR", "Loaded progress: page %lu", currentPage);
|
|
|
|
// Validate page number
|
|
if (currentPage >= xtc->getPageCount()) {
|
|
currentPage = 0;
|
|
}
|
|
}
|
|
f.close();
|
|
}
|
|
}
|