Refactor sleep cover overlay: BookOverlayInfo struct, multi-line layout, 4 color modes (White/Gray/Black/Off)

This commit is contained in:
pablohc
2026-03-29 20:51:56 +02:00
parent b96750f30e
commit 389ed7266b
8 changed files with 41512 additions and 96 deletions
+92 -92
View File
@@ -137,20 +137,15 @@ void SleepActivity::renderDefaultSleepScreen() const {
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
}
std::string SleepActivity::getBookOverlayText(const std::string& bookPath) const {
std::string title;
std::string author;
std::string progressLine = "Reading..."; // Default progress text
BookOverlayInfo SleepActivity::getBookOverlayInfo(const std::string& bookPath) const {
BookOverlayInfo info;
// Extract metadata based on file type
if (StringUtils::checkFileExtension(bookPath, ".xtc") || StringUtils::checkFileExtension(bookPath, ".xtch")) {
// Handle XTC file
if (FsHelpers::checkFileExtension(bookPath, ".xtc") || FsHelpers::checkFileExtension(bookPath, ".xtch")) {
Xtc xtc(bookPath, "/.crosspoint");
if (xtc.load()) {
title = xtc.getTitle();
author = xtc.getAuthor();
info.title = xtc.getTitle();
info.author = xtc.getAuthor();
// Load progress - XTC stores current page
FsFile f;
if (Storage.openFileForRead("SLP", xtc.getCachePath() + "/progress.bin", f)) {
uint8_t data[4];
@@ -159,65 +154,55 @@ std::string SleepActivity::getBookOverlayText(const std::string& bookPath) const
(static_cast<uint32_t>(data[2]) << 16) | (static_cast<uint32_t>(data[3]) << 24);
uint32_t totalPages = xtc.getPageCount();
float progress = xtc.calculateProgress(currentPage) * 100.0f;
char progressStr[32];
snprintf(progressStr, sizeof(progressStr), "%lu/%u %.0f%%", (unsigned long)currentPage + 1, totalPages,
progress);
progressLine = progressStr;
char buf[64];
snprintf(buf, sizeof(buf), "Reading progress: Page %lu/%u - %.0f%%", (unsigned long)currentPage + 1,
totalPages, progress);
info.progressText = buf;
}
f.close();
}
}
} else if (StringUtils::checkFileExtension(bookPath, ".txt")) {
// Handle TXT file
} else if (FsHelpers::checkFileExtension(bookPath, ".txt")) {
Txt txt(bookPath, "/.crosspoint");
if (txt.load()) {
title = txt.getTitle();
// TXT files don't have author metadata
info.title = txt.getTitle();
// Load progress - TXT stores current page
FsFile f;
if (Storage.openFileForRead("SLP", txt.getCachePath() + "/progress.bin", f)) {
uint8_t data[4];
if (f.read(data, 4) == 4) {
uint32_t currentPage = data[0] + (data[1] << 8);
// Load total pages from page index cache
uint32_t totalPages = 0;
FsFile indexFile;
if (Storage.openFileForRead("SLP", txt.getCachePath() + "/index.bin", indexFile)) {
// Read numPages directly (it's at a fixed offset after headers)
// Skip: magic(4) + version(4) + fileSize(4) + cachedWidth(4) + cachedLines(4) + fontId(4) + margin(4) +
// alignment(4) = 32 bytes
indexFile.seek(32);
if (indexFile.read(reinterpret_cast<uint8_t*>(&totalPages), sizeof(totalPages)) == sizeof(totalPages)) {
// Successfully read totalPages
}
indexFile.close();
}
if (totalPages > 0) {
float progress = (currentPage + 1) * 100.0f / totalPages;
char progressStr[32];
snprintf(progressStr, sizeof(progressStr), "%lu/%u %.0f%%", (unsigned long)currentPage + 1, totalPages,
progress);
progressLine = progressStr;
char buf[64];
snprintf(buf, sizeof(buf), "Reading progress: Page %lu/%u - %.0f%%", (unsigned long)currentPage + 1,
totalPages, progress);
info.progressText = buf;
} else {
char progressStr[16];
snprintf(progressStr, sizeof(progressStr), "Page %lu", (unsigned long)currentPage + 1);
progressLine = progressStr;
char buf[64];
snprintf(buf, sizeof(buf), "Reading progress: Page %lu", (unsigned long)currentPage + 1);
info.progressText = buf;
}
}
f.close();
}
}
} else if (StringUtils::checkFileExtension(bookPath, ".epub")) {
// Handle EPUB file
} else if (FsHelpers::checkFileExtension(bookPath, ".epub")) {
Epub epub(bookPath, "/.crosspoint");
if (epub.load(true, true)) { // Skip CSS loading for metadata only
title = epub.getTitle();
author = epub.getAuthor();
if (epub.load(true, true)) {
info.title = epub.getTitle();
info.author = epub.getAuthor();
// Load progress and get chapter information
FsFile f;
if (Storage.openFileForRead("SLP", epub.getCachePath() + "/progress.bin", f)) {
uint8_t data[6];
@@ -229,21 +214,20 @@ std::string SleepActivity::getBookOverlayText(const std::string& bookPath) const
float chapterProgress = static_cast<float>(currentPage) / static_cast<float>(pageCount);
float bookProgress = epub.calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
// Get chapter name from TOC
const int tocIndex = epub.getTocIndexForSpineIndex(currentSpineIndex);
std::string chapterName;
if (tocIndex == -1) {
chapterName = "Unnamed Chapter";
} else {
if (tocIndex != -1) {
const auto tocItem = epub.getTocItem(tocIndex);
chapterName = tocItem.title;
info.chapterName = tocItem.title;
char suffix[64];
snprintf(suffix, sizeof(suffix), " - Page %d/%d - %.0f%%", currentPage + 1, pageCount, bookProgress);
info.progressSuffix = suffix;
info.progressText = "Chapter: " + info.chapterName + info.progressSuffix;
} else {
char buf[80];
snprintf(buf, sizeof(buf), "Reading progress: Page %d/%d - %.0f%%", currentPage + 1, pageCount,
bookProgress);
info.progressText = buf;
}
// Create chapter info line with chapter name and progress
char chapterProgressStr[128];
snprintf(chapterProgressStr, sizeof(chapterProgressStr), "%s: %d/%d %.0f%%", chapterName.c_str(),
currentPage + 1, pageCount, bookProgress);
progressLine = chapterProgressStr;
}
}
f.close();
@@ -251,18 +235,7 @@ std::string SleepActivity::getBookOverlayText(const std::string& bookPath) const
}
}
// Format overlay text (two lines)
std::string overlayText;
if (!title.empty()) {
overlayText = title;
if (!author.empty()) {
overlayText += " - " + author;
}
overlayText += "\n"; // New line
overlayText += progressLine;
}
return overlayText;
return info;
}
void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
@@ -286,7 +259,7 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
ratio = (1.0f - cropX) * static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
}
x = 0;
y = std::round((static_cast<float>(pageHeight) - static_cast<float>(pageWidth) / ratio) / 2);
y = 0;
LOG_DBG("SLP", "Centering with ratio %f to y=%d", ratio, y);
} else {
// image taller than viewport ratio, scaled down image needs to be centered horizontally
@@ -302,7 +275,7 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
} else {
// center the image
x = (pageWidth - bitmap.getWidth()) / 2;
y = (pageHeight - bitmap.getHeight()) / 2;
y = 0;
}
LOG_DBG("SLP", "drawing to %d x %d", x, y);
@@ -317,43 +290,70 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
renderer.invertScreen();
}
// Draw overlay text if provided
const std::string overlayText = SETTINGS.sleepCoverOverlay ? getBookOverlayText(APP_STATE.openEpubPath) : "";
const uint8_t overlayMode = SETTINGS.sleepCoverOverlay;
const BookOverlayInfo overlayInfo = overlayMode < 3 ? getBookOverlayInfo(APP_STATE.openEpubPath) : BookOverlayInfo{};
const auto drawOverlay = [&]() {
if (overlayText.empty()) {
const bool hasTitle = !overlayInfo.title.empty();
const bool hasProgress = !overlayInfo.progressText.empty();
const bool hasAuthor = !overlayInfo.author.empty();
if (!hasTitle && !hasAuthor && !hasProgress) {
return;
}
// Split text into lines
size_t newlinePos = overlayText.find('\n');
std::string line1 = (newlinePos != std::string::npos) ? overlayText.substr(0, newlinePos) : overlayText;
std::string line2 = (newlinePos != std::string::npos && newlinePos + 1 < overlayText.length())
? overlayText.substr(newlinePos + 1)
: "";
const int lineHeight12 = renderer.getLineHeight(BOOKERLY_12_FONT_ID);
const int lineHeight10 = renderer.getLineHeight(UI_10_FONT_ID);
constexpr int lineSpacing = 3;
constexpr int sectionSpacing = 10;
const int availableWidth = pageWidth - 20;
// Truncate lines if too long for the screen
line1 = renderer.truncatedText(UI_10_FONT_ID, line1.c_str(), pageWidth - 20);
line2 = renderer.truncatedText(UI_10_FONT_ID, line2.c_str(), pageWidth - 20);
int textBlockHeight = 0;
if (hasTitle) textBlockHeight += lineHeight12;
if (hasAuthor) textBlockHeight += lineSpacing + lineHeight10;
if (hasProgress) textBlockHeight += sectionSpacing + lineHeight10;
// Compute overlay height to exactly fit the text lines with equal top/bottom padding
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
constexpr int lineSpacing = 4;
constexpr int overlayPadding = 10;
const int numLines = line2.empty() ? 1 : 2;
const int textBlockHeight = numLines * lineHeight + (numLines - 1) * lineSpacing;
const int overlayHeight = textBlockHeight + overlayPadding * 2;
const int overlayY = pageHeight - overlayHeight - 20;
renderer.fillRect(0, overlayY, pageWidth, overlayHeight, false); // White background
const bool textBlack = (overlayMode != 2);
const int topPadding = lineHeight12 / 3;
const int bottomPadding = lineHeight10 * 2 / 3;
const int overlayHeight = textBlockHeight + topPadding + bottomPadding;
const int overlayY = pageHeight - overlayHeight;
// Center text block vertically in the overlay rectangle
const int textStartY = overlayY + (overlayHeight - textBlockHeight) / 2;
const int textX1 = (pageWidth - renderer.getTextWidth(UI_10_FONT_ID, line1.c_str())) / 2;
renderer.drawText(UI_10_FONT_ID, textX1, textStartY, line1.c_str(), true); // Black text
if (overlayMode == 1) {
renderer.fillRectDither(0, overlayY, pageWidth, overlayHeight, Color::LightGray);
} else {
renderer.fillRect(0, overlayY, pageWidth, overlayHeight, overlayMode == 2);
}
if (!line2.empty()) {
const int line2Y = textStartY + lineHeight + lineSpacing;
const int textX2 = (pageWidth - renderer.getTextWidth(UI_10_FONT_ID, line2.c_str())) / 2;
renderer.drawText(UI_10_FONT_ID, textX2, line2Y, line2.c_str(), true); // Black text
int currentY = overlayY + topPadding;
if (hasTitle) {
const std::string titleStr =
renderer.truncatedText(BOOKERLY_12_FONT_ID, overlayInfo.title.c_str(), availableWidth, EpdFontFamily::BOLD);
renderer.drawCenteredText(BOOKERLY_12_FONT_ID, currentY, titleStr.c_str(), textBlack, EpdFontFamily::BOLD);
currentY += lineHeight12 + lineSpacing;
}
if (hasAuthor) {
const std::string authorStr = renderer.truncatedText(UI_10_FONT_ID, overlayInfo.author.c_str(), availableWidth);
renderer.drawCenteredText(UI_10_FONT_ID, currentY, authorStr.c_str(), textBlack);
currentY += lineHeight10 + sectionSpacing;
}
if (hasProgress) {
std::string progressStr;
if (!overlayInfo.chapterName.empty()) {
const std::string prefix = "Chapter: ";
const int prefixWidth = renderer.getTextWidth(UI_10_FONT_ID, prefix.c_str());
const int suffixWidth = renderer.getTextWidth(UI_10_FONT_ID, overlayInfo.progressSuffix.c_str());
const int maxChapterWidth = availableWidth - prefixWidth - suffixWidth;
const std::string truncatedChapter =
maxChapterWidth > 0
? renderer.truncatedText(UI_10_FONT_ID, overlayInfo.chapterName.c_str(), maxChapterWidth)
: "";
progressStr = prefix + truncatedChapter + overlayInfo.progressSuffix;
} else {
progressStr = renderer.truncatedText(UI_10_FONT_ID, overlayInfo.progressText.c_str(), availableWidth);
}
renderer.drawCenteredText(UI_10_FONT_ID, currentY, progressStr.c_str(), textBlack);
}
};
+12 -1
View File
@@ -1,8 +1,19 @@
#pragma once
#include <string>
#include "../Activity.h"
class Bitmap;
struct BookOverlayInfo {
std::string title;
std::string author;
std::string progressText;
std::string chapterName;
std::string progressSuffix;
};
class SleepActivity final : public Activity {
public:
explicit SleepActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
@@ -15,5 +26,5 @@ class SleepActivity final : public Activity {
void renderCoverSleepScreen() const;
void renderBitmapSleepScreen(const Bitmap& bitmap) const;
void renderBlankSleepScreen() const;
std::string getBookOverlayText(const std::string& bookPath) const;
BookOverlayInfo getBookOverlayInfo(const std::string& bookPath) const;
};