This commit is contained in:
Uri Tauber 2026-01-30 09:10:15 +00:00 committed by GitHub
commit 7a1601c77d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 1782 additions and 434 deletions

View File

@ -208,6 +208,10 @@ bool Epub::parseTocNavFile() const {
bool Epub::load(const bool buildIfMissing) {
Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str());
if (!footnotePages) {
footnotePages = new std::unordered_set<std::string>();
}
// Initialize spine/TOC cache
bookMetadataCache.reset(new BookMetadataCache(cachePath));
@ -528,7 +532,8 @@ int Epub::getSpineItemsCount() const {
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
return 0;
}
return bookMetadataCache->getSpineCount();
int virtualCount = virtualSpineItems ? virtualSpineItems->size() : 0;
return bookMetadataCache->getSpineCount() + virtualCount;
}
size_t Epub::getCumulativeSpineItemSize(const int spineIndex) const { return getSpineItem(spineIndex).cumulativeSize; }
@ -539,6 +544,15 @@ BookMetadataCache::SpineEntry Epub::getSpineItem(const int spineIndex) const {
return {};
}
// Virtual spine item
if (isVirtualSpineItem(spineIndex)) {
int virtualIndex = spineIndex - bookMetadataCache->getSpineCount();
if (virtualSpineItems && virtualIndex >= 0 && virtualIndex < static_cast<int>(virtualSpineItems->size())) {
// Create a dummy spine entry for virtual item
return BookMetadataCache::SpineEntry((*virtualSpineItems)[virtualIndex], 0, -1);
}
}
if (spineIndex < 0 || spineIndex >= bookMetadataCache->getSpineCount()) {
Serial.printf("[%lu] [EBP] getSpineItem index:%d is out of range\n", millis(), spineIndex);
return bookMetadataCache->getSpineEntry(0);
@ -628,6 +642,83 @@ int Epub::getSpineIndexForTextReference() const {
return 0;
}
void Epub::markAsFootnotePage(const std::string& href) {
// Lazy initialization
if (!footnotePages) {
footnotePages = new std::unordered_set<std::string>();
}
// Extract filename from href (remove #anchor if present)
size_t hashPos = href.find('#');
std::string filename = (hashPos != std::string::npos) ? href.substr(0, hashPos) : href;
// Extract just the filename without path
size_t lastSlash = filename.find_last_of('/');
if (lastSlash != std::string::npos) {
filename = filename.substr(lastSlash + 1);
}
footnotePages->insert(filename);
Serial.printf("[%lu] [EPUB] Marked as footnote page: %s\n", millis(), filename.c_str());
}
bool Epub::isFootnotePage(const std::string& filename) const {
if (!footnotePages) return false;
return footnotePages->find(filename) != footnotePages->end();
}
bool Epub::shouldHideFromToc(int spineIndex) const {
// Always hide virtual spine items
if (isVirtualSpineItem(spineIndex)) {
return true;
}
BookMetadataCache::SpineEntry entry = getSpineItem(spineIndex);
const std::string& spineItem = entry.href;
// Extract filename from spine item
size_t lastSlash = spineItem.find_last_of('/');
std::string filename = (lastSlash != std::string::npos) ? spineItem.substr(lastSlash + 1) : spineItem;
return isFootnotePage(filename);
}
// Virtual spine items
int Epub::addVirtualSpineItem(const std::string& path) {
// Lazy initialization
if (!virtualSpineItems) {
virtualSpineItems = new std::vector<std::string>();
}
virtualSpineItems->push_back(path);
// Fix: use cache spine count instead of spine.size()
int currentSpineSize = bookMetadataCache ? bookMetadataCache->getSpineCount() : 0;
int newIndex = currentSpineSize + virtualSpineItems->size() - 1;
Serial.printf("[%lu] [EPUB] Added virtual spine item: %s (index %d)\n", millis(), path.c_str(), newIndex);
return newIndex;
}
bool Epub::isVirtualSpineItem(int spineIndex) const {
int currentSpineSize = bookMetadataCache ? bookMetadataCache->getSpineCount() : 0;
return spineIndex >= currentSpineSize;
}
int Epub::findVirtualSpineIndex(const std::string& filename) const {
if (!virtualSpineItems) return -1;
int currentSpineSize = bookMetadataCache ? bookMetadataCache->getSpineCount() : 0;
for (size_t i = 0; i < virtualSpineItems->size(); i++) {
std::string virtualPath = (*virtualSpineItems)[i];
size_t lastSlash = virtualPath.find_last_of('/');
std::string virtualFilename = (lastSlash != std::string::npos) ? virtualPath.substr(lastSlash + 1) : virtualPath;
if (virtualFilename == filename) {
return currentSpineSize + i;
}
}
return -1;
}
// Calculate progress in book (returns 0.0-1.0)
float Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) const {
const size_t bookSize = getBookSize();

View File

@ -5,6 +5,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "Epub/BookMetadataCache.h"
@ -20,22 +21,30 @@ class Epub {
std::string filepath;
// the base path for items in the EPUB file
std::string contentBasePath;
// Uniq cache key based on filepath
std::string cachePath;
// Spine and TOC cache
std::unique_ptr<BookMetadataCache> bookMetadataCache;
// Use pointers, allocate only if needed
std::unordered_set<std::string>* footnotePages;
std::vector<std::string>* virtualSpineItems;
bool findContentOpfFile(std::string* contentOpfFile) const;
bool parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata);
bool parseTocNcxFile() const;
bool parseTocNavFile() const;
public:
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
// create a cache key based on the filepath
explicit Epub(std::string filepath, const std::string& cacheDir)
: filepath(std::move(filepath)), footnotePages(nullptr), virtualSpineItems(nullptr) {
cachePath = cacheDir + "/epub_" + std::to_string(std::hash<std::string>{}(this->filepath));
}
~Epub() = default;
~Epub() {
delete footnotePages;
delete virtualSpineItems;
}
std::string& getBasePath() { return contentBasePath; }
bool load(bool buildIfMissing = true);
bool clearCache() const;
@ -62,6 +71,13 @@ class Epub {
size_t getCumulativeSpineItemSize(int spineIndex) const;
int getSpineIndexForTextReference() const;
void markAsFootnotePage(const std::string& href);
bool isFootnotePage(const std::string& filename) const;
bool shouldHideFromToc(int spineIndex) const;
int addVirtualSpineItem(const std::string& path);
bool isVirtualSpineItem(int spineIndex) const;
int findVirtualSpineIndex(const std::string& filename) const;
size_t getBookSize() const;
float calculateProgress(int currentSpineIndex, float currentSpineRead) const;
};

View File

@ -0,0 +1,12 @@
#pragma once
struct FootnoteEntry {
char number[3];
char href[64];
bool isInline;
FootnoteEntry() : isInline(false) {
number[0] = '\0';
href[0] = '\0';
}
};

View File

@ -43,6 +43,16 @@ bool Page::serialize(FsFile& file) const {
}
}
// Serialize footnotes
int32_t fCount = footnotes.size();
serialization::writePod(file, fCount);
for (const auto& fn : footnotes) {
file.write(fn.number, 3);
file.write(fn.href, 64);
uint8_t isInlineFlag = fn.isInline ? 1 : 0;
file.write(&isInlineFlag, 1);
}
return true;
}
@ -65,5 +75,18 @@ std::unique_ptr<Page> Page::deserialize(FsFile& file) {
}
}
int32_t footnoteCount;
serialization::readPod(file, footnoteCount);
for (int i = 0; i < footnoteCount; i++) {
FootnoteEntry entry;
file.read(entry.number, 3);
file.read(entry.href, 64);
uint8_t isInlineFlag = 0;
file.read(&isInlineFlag, 1);
entry.isInline = (isInlineFlag != 0);
page->footnotes.push_back(entry);
}
return page;
}

View File

@ -1,16 +1,15 @@
#pragma once
#include <SdFat.h>
#include <utility>
#include <vector>
#include "FootnoteEntry.h"
#include "blocks/TextBlock.h"
enum PageElementTag : uint8_t {
TAG_PageLine = 1,
};
// represents something that has been added to a page
class PageElement {
public:
int16_t xPos;
@ -21,7 +20,6 @@ class PageElement {
virtual bool serialize(FsFile& file) = 0;
};
// a line from a block element
class PageLine final : public PageElement {
std::shared_ptr<TextBlock> block;
@ -37,6 +35,19 @@ class Page {
public:
// the list of block index and line numbers on this page
std::vector<std::shared_ptr<PageElement>> elements;
std::vector<FootnoteEntry> footnotes;
void addFootnote(const char* number, const char* href) {
FootnoteEntry entry;
// ensure null termination and bounds
strncpy(entry.number, number, 2);
entry.number[2] = '\0';
strncpy(entry.href, href, 63);
entry.href[63] = '\0';
entry.isInline = false; // Default
footnotes.push_back(entry);
}
void render(GfxRenderer& renderer, int fontId, int xOffset, int yOffset) const;
bool serialize(FsFile& file) const;
static std::unique_ptr<Page> deserialize(FsFile& file);

View File

@ -49,16 +49,24 @@ uint16_t measureWordWidth(const GfxRenderer& renderer, const int fontId, const s
} // namespace
void ParsedText::addWord(std::string word, const EpdFontFamily::Style fontStyle) {
void ParsedText::addWord(std::string word, const EpdFontFamily::Style fontStyle,
std::unique_ptr<FootnoteEntry> footnote) {
if (word.empty()) return;
words.push_back(std::move(word));
wordStyles.push_back(fontStyle);
if (footnote) {
wordHasFootnote.push_back(1);
footnoteQueue.push_back(*footnote);
} else {
wordHasFootnote.push_back(0);
}
}
// Consumes data to minimize memory usage
void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, const uint16_t viewportWidth,
const std::function<void(std::shared_ptr<TextBlock>)>& processLine,
void ParsedText::layoutAndExtractLines(
const GfxRenderer& renderer, const int fontId, const uint16_t viewportWidth,
const std::function<void(std::shared_ptr<TextBlock>, const std::vector<FootnoteEntry>&)>& processLine,
const bool includeLastLine) {
if (words.empty()) {
return;
@ -255,8 +263,8 @@ std::vector<size_t> ParsedText::computeHyphenatedLineBreaks(const GfxRenderer& r
return lineBreakIndices;
}
// Splits words[wordIndex] into prefix (adding a hyphen only when needed) and remainder when a legal breakpoint fits the
// available width.
// Splits words[wordIndex] into prefix (adding a hyphen only when needed)
// and remainder when a legal breakpoint fits the available width.
bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availableWidth, const GfxRenderer& renderer,
const int fontId, std::vector<uint16_t>& wordWidths,
const bool allowFallbackBreaks) {
@ -320,6 +328,13 @@ bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availabl
words.insert(insertWordIt, remainder);
wordStyles.insert(insertStyleIt, style);
// Split wordHasFootnote as well. The footnote (if any) is associated with the remainder word.
auto wordHasFootnoteIt = wordHasFootnote.begin();
std::advance(wordHasFootnoteIt, wordIndex);
uint8_t hasFootnote = *wordHasFootnoteIt;
*wordHasFootnoteIt = 0; // First part doesn't have it anymore
wordHasFootnote.insert(std::next(wordHasFootnoteIt), hasFootnote);
// Update cached widths to reflect the new prefix/remainder pairing.
wordWidths[wordIndex] = static_cast<uint16_t>(chosenWidth);
const uint16_t remainderWidth = measureWordWidth(renderer, fontId, remainder, style);
@ -327,9 +342,10 @@ bool ParsedText::hyphenateWordAtIndex(const size_t wordIndex, const int availabl
return true;
}
void ParsedText::extractLine(const size_t breakIndex, const int pageWidth, const int spaceWidth,
const std::vector<uint16_t>& wordWidths, const std::vector<size_t>& lineBreakIndices,
const std::function<void(std::shared_ptr<TextBlock>)>& processLine) {
void ParsedText::extractLine(
const size_t breakIndex, const int pageWidth, const int spaceWidth, const std::vector<uint16_t>& wordWidths,
const std::vector<size_t>& lineBreakIndices,
const std::function<void(std::shared_ptr<TextBlock>, const std::vector<FootnoteEntry>&)>& processLine) {
const size_t lineBreak = lineBreakIndices[breakIndex];
const size_t lastBreakAt = breakIndex > 0 ? lineBreakIndices[breakIndex - 1] : 0;
const size_t lineWordCount = lineBreak - lastBreakAt;
@ -372,17 +388,35 @@ void ParsedText::extractLine(const size_t breakIndex, const int pageWidth, const
std::advance(wordEndIt, lineWordCount);
std::advance(wordStyleEndIt, lineWordCount);
// *** CRITICAL STEP: CONSUME DATA USING SPLICE ***
std::list<std::string> lineWords;
lineWords.splice(lineWords.begin(), words, words.begin(), wordEndIt);
std::list<EpdFontFamily::Style> lineWordStyles;
lineWordStyles.splice(lineWordStyles.begin(), wordStyles, wordStyles.begin(), wordStyleEndIt);
// Extract footnote flags from deque
std::vector<FootnoteEntry> lineFootnotes;
for (size_t i = 0; i < lineWordCount; i++) {
if (!wordHasFootnote.empty()) {
uint8_t hasFn = wordHasFootnote.front();
wordHasFootnote.pop_front();
if (hasFn) {
if (footnoteQueue.empty()) {
Serial.printf("[%lu] [ERROR] Footnote flag set but queue empty! Flags/queue out of sync.\n", millis());
break;
}
lineFootnotes.push_back(footnoteQueue.front());
footnoteQueue.pop_front();
}
}
}
for (auto& word : lineWords) {
if (containsSoftHyphen(word)) {
stripSoftHyphensInPlace(word);
}
}
processLine(std::make_shared<TextBlock>(std::move(lineWords), std::move(lineXPos), std::move(lineWordStyles), style));
processLine(std::make_shared<TextBlock>(std::move(lineWords), std::move(lineXPos), std::move(lineWordStyles), style),
lineFootnotes);
}

View File

@ -2,12 +2,14 @@
#include <EpdFontFamily.h>
#include <deque>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "FootnoteEntry.h"
#include "blocks/TextBlock.h"
class GfxRenderer;
@ -15,6 +17,8 @@ class GfxRenderer;
class ParsedText {
std::list<std::string> words;
std::list<EpdFontFamily::Style> wordStyles;
std::deque<uint8_t> wordHasFootnote;
std::deque<FootnoteEntry> footnoteQueue;
TextBlock::Style style;
bool extraParagraphSpacing;
bool hyphenationEnabled;
@ -26,9 +30,10 @@ class ParsedText {
int spaceWidth, std::vector<uint16_t>& wordWidths);
bool hyphenateWordAtIndex(size_t wordIndex, int availableWidth, const GfxRenderer& renderer, int fontId,
std::vector<uint16_t>& wordWidths, bool allowFallbackBreaks);
void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths,
void extractLine(
size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths,
const std::vector<size_t>& lineBreakIndices,
const std::function<void(std::shared_ptr<TextBlock>)>& processLine);
const std::function<void(std::shared_ptr<TextBlock>, const std::vector<FootnoteEntry>&)>& processLine);
std::vector<uint16_t> calculateWordWidths(const GfxRenderer& renderer, int fontId);
public:
@ -37,12 +42,13 @@ class ParsedText {
: style(style), extraParagraphSpacing(extraParagraphSpacing), hyphenationEnabled(hyphenationEnabled) {}
~ParsedText() = default;
void addWord(std::string word, EpdFontFamily::Style fontStyle);
void addWord(std::string word, EpdFontFamily::Style fontStyle, std::unique_ptr<FootnoteEntry> footnote = nullptr);
void setStyle(const TextBlock::Style style) { this->style = style; }
TextBlock::Style getStyle() const { return style; }
size_t size() const { return words.size(); }
bool isEmpty() const { return words.empty(); }
void layoutAndExtractLines(const GfxRenderer& renderer, int fontId, uint16_t viewportWidth,
const std::function<void(std::shared_ptr<TextBlock>)>& processLine,
void layoutAndExtractLines(
const GfxRenderer& renderer, int fontId, uint16_t viewportWidth,
const std::function<void(std::shared_ptr<TextBlock>, const std::vector<FootnoteEntry>&)>& processLine,
bool includeLastLine = true);
};

View File

@ -3,6 +3,10 @@
#include <SDCardManager.h>
#include <Serialization.h>
#include <fstream>
#include <set>
#include "FsHelpers.h"
#include "Page.h"
#include "hyphenation/Hyphenator.h"
#include "parsers/ChapterHtmlSlimParser.h"
@ -14,6 +18,60 @@ constexpr uint32_t HEADER_SIZE = sizeof(uint8_t) + sizeof(int) + sizeof(float) +
sizeof(uint32_t);
} // namespace
// Helper function to write XML-escaped text directly to file
static bool writeEscapedXml(FsFile& file, const char* text) {
if (!text) return true;
// Use a static buffer to avoid heap allocation
static char buffer[2048];
int bufferPos = 0;
while (*text && bufferPos < sizeof(buffer) - 10) { // Leave margin for entities
unsigned char c = (unsigned char)*text;
// Only escape the 5 XML special characters
if (c == '<') {
if (bufferPos + 4 < sizeof(buffer)) {
memcpy(&buffer[bufferPos], "&lt;", 4);
bufferPos += 4;
}
} else if (c == '>') {
if (bufferPos + 4 < sizeof(buffer)) {
memcpy(&buffer[bufferPos], "&gt;", 4);
bufferPos += 4;
}
} else if (c == '&') {
if (bufferPos + 5 < sizeof(buffer)) {
memcpy(&buffer[bufferPos], "&amp;", 5);
bufferPos += 5;
}
} else if (c == '"') {
if (bufferPos + 6 < sizeof(buffer)) {
memcpy(&buffer[bufferPos], "&quot;", 6);
bufferPos += 6;
}
} else if (c == '\'') {
if (bufferPos + 6 < sizeof(buffer)) {
memcpy(&buffer[bufferPos], "&apos;", 6);
bufferPos += 6;
}
} else {
// Keep everything else (include UTF8)
buffer[bufferPos++] = (char)c;
}
text++;
}
buffer[bufferPos] = '\0';
// Write all at once
size_t written = file.write((const uint8_t*)buffer, bufferPos);
file.flush();
return written == bufferPos;
}
uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
if (!file) {
Serial.printf("[%lu] [SCT] File not open for writing page %d\n", millis(), pageCount);
@ -25,7 +83,8 @@ uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
Serial.printf("[%lu] [SCT] Failed to serialize page %d\n", millis(), pageCount);
return 0;
}
Serial.printf("[%lu] [SCT] Page %d processed\n", millis(), pageCount);
// Debug reduce log spam
// Serial.printf("[%lu] [SCT] Page %d processed\n", millis(), pageCount);
pageCount++;
return position;
@ -104,7 +163,6 @@ bool Section::loadSectionFile(const int fontId, const float lineCompression, con
return true;
}
// Your updated class method (assuming you are using the 'SD' object, which is a wrapper for a specific filesystem)
bool Section::clearCache() const {
if (!SdMan.exists(filePath.c_str())) {
Serial.printf("[%lu] [SCT] Cache does not exist, no action needed\n", millis());
@ -126,7 +184,9 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
const std::function<void()>& progressSetupFn,
const std::function<void(int)>& progressFn) {
constexpr uint32_t MIN_SIZE_FOR_PROGRESS = 50 * 1024; // 50KB
const auto localPath = epub->getSpineItem(spineIndex).href;
BookMetadataCache::SpineEntry spineEntry = epub->getSpineItem(spineIndex);
const std::string localPath = spineEntry.href;
const auto tmpHtmlPath = epub->getCachePath() + "/.tmp_" + std::to_string(spineIndex) + ".html";
// Create cache directory if it doesn't exist
@ -135,43 +195,43 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
SdMan.mkdir(sectionsDir.c_str());
}
// Retry logic for SD card timing issues
bool isVirtual = epub->isVirtualSpineItem(spineIndex);
bool success = false;
uint32_t fileSize = 0;
for (int attempt = 0; attempt < 3 && !success; attempt++) {
if (attempt > 0) {
Serial.printf("[%lu] [SCT] Retrying stream (attempt %d)...\n", millis(), attempt + 1);
delay(50); // Brief delay before retry
}
std::string fileToParse = tmpHtmlPath;
// Remove any incomplete file from previous attempt before retrying
if (SdMan.exists(tmpHtmlPath.c_str())) {
SdMan.remove(tmpHtmlPath.c_str());
}
if (isVirtual) {
Serial.printf("[%lu] [SCT] Processing virtual spine item: %s\n", millis(), localPath.c_str());
// For virtual items, the path is already on SD, e.g. /sd/cache/...
// But we need to make sure the parser can read it.
// If it starts with /sd/, we might need to strip it if using SdFat with root?
// Assuming absolute path is fine.
fileToParse = localPath;
success = true;
fileSize = 0; // Don't check size for progress bar on virtual items
} else {
// Normal file - stream from zip
for (int attempt = 0; attempt < 3 && !success; attempt++) {
if (attempt > 0) delay(50);
if (SdMan.exists(tmpHtmlPath.c_str())) SdMan.remove(tmpHtmlPath.c_str());
FsFile tmpHtml;
if (!SdMan.openFileForWrite("SCT", tmpHtmlPath, tmpHtml)) {
continue;
}
if (!SdMan.openFileForWrite("SCT", tmpHtmlPath, tmpHtml)) continue;
success = epub->readItemContentsToStream(localPath, tmpHtml, 1024);
fileSize = tmpHtml.size();
tmpHtml.close();
// If streaming failed, remove the incomplete file immediately
if (!success && SdMan.exists(tmpHtmlPath.c_str())) {
SdMan.remove(tmpHtmlPath.c_str());
Serial.printf("[%lu] [SCT] Removed incomplete temp file after failed attempt\n", millis());
}
if (!success && SdMan.exists(tmpHtmlPath.c_str())) SdMan.remove(tmpHtmlPath.c_str());
}
if (!success) {
Serial.printf("[%lu] [SCT] Failed to stream item contents to temp file after retries\n", millis());
Serial.printf("[%lu] [SCT] Failed to stream item contents\n", millis());
return false;
}
}
Serial.printf("[%lu] [SCT] Streamed temp HTML to %s (%d bytes)\n", millis(), tmpHtmlPath.c_str(), fileSize);
// Only show progress bar for larger chapters where rendering overhead is worth it
// Only show progress bar for larger chapters
if (progressSetupFn && fileSize >= MIN_SIZE_FOR_PROGRESS) {
progressSetupFn();
}
@ -183,15 +243,44 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
viewportHeight, hyphenationEnabled);
std::vector<uint32_t> lut = {};
ChapterHtmlSlimParser visitor(
tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
std::unique_ptr<ChapterHtmlSlimParser> visitor(new ChapterHtmlSlimParser(
fileToParse, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight, hyphenationEnabled,
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
progressFn);
Hyphenator::setPreferredLanguage(epub->getLanguage());
success = visitor.parseAndBuildPages();
progressFn));
Hyphenator::setPreferredLanguage(epub->getLanguage());
// Track which inline footnotes AND paragraph notes are actually referenced in this file
std::set<std::string> rewrittenInlineIds;
int noterefCount = 0;
visitor->setNoterefCallback([this, &noterefCount, &rewrittenInlineIds](Noteref& noteref) {
// Extract the ID from the href for tracking
std::string href(noteref.href);
// Check if this was rewritten to an inline or paragraph note
if (href.find("inline_") == 0 || href.find("pnote_") == 0) {
size_t underscorePos = href.find('_');
size_t dotPos = href.find('.');
if (underscorePos != std::string::npos && dotPos != std::string::npos) {
std::string noteId = href.substr(underscorePos + 1, dotPos - underscorePos - 1);
rewrittenInlineIds.insert(noteId);
}
} else {
// Normal external footnote
epub->markAsFootnotePage(noteref.href);
}
noterefCount++;
});
success = visitor->parseAndBuildPages();
if (!isVirtual) {
SdMan.remove(tmpHtmlPath.c_str());
}
if (!success) {
Serial.printf("[%lu] [SCT] Failed to parse XML and build pages\n", millis());
file.close();
@ -199,9 +288,77 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
return false;
}
// --- Footnote Generation Logic (Merged from HEAD) ---
// Inline footnotes
for (int i = 0; i < visitor->inlineFootnoteCount; i++) {
const char* inlineId = visitor->inlineFootnotes[i].id;
const char* inlineText = visitor->inlineFootnotes[i].text;
if (rewrittenInlineIds.find(std::string(inlineId)) == rewrittenInlineIds.end()) continue;
if (!inlineText || strlen(inlineText) == 0) continue;
char inlineFilename[64];
snprintf(inlineFilename, sizeof(inlineFilename), "inline_%s.html", inlineId);
std::string fullPath = epub->getCachePath() + "/" + std::string(inlineFilename);
FsFile file;
if (SdMan.openFileForWrite("SCT", fullPath, file)) {
file.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
file.println("<!DOCTYPE html>");
file.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
file.println("<head><meta charset=\"UTF-8\"/><title>Footnote</title></head>");
file.println("<body>");
file.print("<p id=\"");
file.print(inlineId);
file.print("\">");
writeEscapedXml(file, inlineText);
file.println("</p></body></html>");
file.close();
int virtualIndex = epub->addVirtualSpineItem(fullPath);
char newHref[128];
snprintf(newHref, sizeof(newHref), "%s#%s", inlineFilename, inlineId);
epub->markAsFootnotePage(newHref);
}
}
// Paragraph notes
for (int i = 0; i < visitor->paragraphNoteCount; i++) {
const char* pnoteId = visitor->paragraphNotes[i].id;
const char* pnoteText = visitor->paragraphNotes[i].text;
if (!pnoteText || strlen(pnoteText) == 0) continue;
if (rewrittenInlineIds.find(std::string(pnoteId)) == rewrittenInlineIds.end()) continue;
char pnoteFilename[64];
snprintf(pnoteFilename, sizeof(pnoteFilename), "pnote_%s.html", pnoteId);
std::string fullPath = epub->getCachePath() + "/" + std::string(pnoteFilename);
FsFile file;
if (SdMan.openFileForWrite("SCT", fullPath, file)) {
file.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
file.println("<!DOCTYPE html>");
file.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
file.println("<head><meta charset=\"UTF-8\"/><title>Note</title></head>");
file.println("<body>");
file.print("<p id=\"");
file.print(pnoteId);
file.print("\">");
writeEscapedXml(file, pnoteText);
file.println("</p></body></html>");
file.close();
int virtualIndex = epub->addVirtualSpineItem(fullPath);
char newHref[128];
snprintf(newHref, sizeof(newHref), "%s#%s", pnoteFilename, pnoteId);
epub->markAsFootnotePage(newHref);
}
}
// Write LUT (master)
const uint32_t lutOffset = file.position();
bool hasFailedLutRecords = false;
// Write LUT
for (const uint32_t& pos : lut) {
if (pos == 0) {
hasFailedLutRecords = true;

View File

@ -30,7 +30,6 @@ constexpr int NUM_SKIP_TAGS = sizeof(SKIP_TAGS) / sizeof(SKIP_TAGS[0]);
bool isWhitespace(const char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }
// given the start and end of a tag, check to see if it matches a known tag
bool matches(const char* tag_name, const char* possible_tags[], const int possible_tag_count) {
for (int i = 0; i < possible_tag_count; i++) {
if (strcmp(tag_name, possible_tags[i]) == 0) {
@ -40,46 +39,332 @@ bool matches(const char* tag_name, const char* possible_tags[], const int possib
return false;
}
const char* getAttribute(const XML_Char** atts, const char* attrName) {
if (!atts) return nullptr;
for (int i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], attrName) == 0) {
return atts[i + 1];
}
}
return nullptr;
}
// Simple HTML entity replacement for noteref text
std::string replaceHtmlEntities(const char* text) {
if (!text) return "";
std::string s(text);
// Replace common entities
size_t pos = 0;
while ((pos = s.find("&lt;", pos)) != std::string::npos) {
s.replace(pos, 4, "<");
pos += 1;
}
pos = 0;
while ((pos = s.find("&gt;", pos)) != std::string::npos) {
s.replace(pos, 4, ">");
pos += 1;
}
pos = 0;
while ((pos = s.find("&amp;", pos)) != std::string::npos) {
s.replace(pos, 5, "&");
pos += 1;
}
pos = 0;
while ((pos = s.find("&quot;", pos)) != std::string::npos) {
s.replace(pos, 6, "\"");
pos += 1;
}
pos = 0;
while ((pos = s.find("&apos;", pos)) != std::string::npos) {
s.replace(pos, 6, "'");
pos += 1;
}
return s;
}
EpdFontFamily::Style ChapterHtmlSlimParser::getCurrentFontStyle() const {
if (boldUntilDepth < depth && italicUntilDepth < depth) {
return EpdFontFamily::BOLD_ITALIC;
} else if (boldUntilDepth < depth) {
return EpdFontFamily::BOLD;
} else if (italicUntilDepth < depth) {
return EpdFontFamily::ITALIC;
}
return EpdFontFamily::REGULAR;
}
// flush the contents of partWordBuffer to currentTextBlock
void ChapterHtmlSlimParser::flushPartWordBuffer() {
// determine font style
EpdFontFamily::Style fontStyle = EpdFontFamily::REGULAR;
if (boldUntilDepth < depth && italicUntilDepth < depth) {
fontStyle = EpdFontFamily::BOLD_ITALIC;
} else if (boldUntilDepth < depth) {
fontStyle = EpdFontFamily::BOLD;
} else if (italicUntilDepth < depth) {
fontStyle = EpdFontFamily::ITALIC;
}
EpdFontFamily::Style fontStyle = getCurrentFontStyle();
// flush the buffer
partWordBuffer[partWordBufferIndex] = '\0';
currentTextBlock->addWord(partWordBuffer, fontStyle);
currentTextBlock->addWord(std::move(replaceHtmlEntities(partWordBuffer)), fontStyle);
partWordBufferIndex = 0;
}
// start a new text block if needed
void ChapterHtmlSlimParser::startNewTextBlock(const TextBlock::Style style) {
if (currentTextBlock) {
// already have a text block running and it is empty - just reuse it
if (currentTextBlock->isEmpty()) {
currentTextBlock->setStyle(style);
return;
}
makePages();
}
currentTextBlock.reset(new ParsedText(style, extraParagraphSpacing, hyphenationEnabled));
}
std::unique_ptr<FootnoteEntry> ChapterHtmlSlimParser::createFootnoteEntry(const char* number, const char* href) {
auto entry = std::unique_ptr<FootnoteEntry>(new FootnoteEntry());
Serial.printf("[%lu] [ADDFT] Creating footnote: num=%s, href=%s\n", millis(), number, href);
// Copy number
strncpy(entry->number, number, 2);
entry->number[2] = '\0';
// Check if this is an inline footnote reference
const char* hashPos = strchr(href, '#');
if (hashPos) {
const char* inlineId = hashPos + 1; // Skip the '#'
// Check if we have this inline footnote
bool foundInline = false;
for (int i = 0; i < inlineFootnoteCount; i++) {
if (strcmp(inlineFootnotes[i].id, inlineId) == 0) {
// This is an inline footnote! Rewrite the href
char rewrittenHref[64];
snprintf(rewrittenHref, sizeof(rewrittenHref), "inline_%s.html#%s", inlineId, inlineId);
strncpy(entry->href, rewrittenHref, 63);
entry->href[63] = '\0';
Serial.printf("[%lu] [ADDFT] Rewrote inline href to: %s\n", millis(), rewrittenHref);
foundInline = true;
break;
}
}
// Check if we have this as a paragraph note
if (!foundInline) {
for (int i = 0; i < paragraphNoteCount; i++) {
if (strcmp(paragraphNotes[i].id, inlineId) == 0) {
char rewrittenHref[64];
snprintf(rewrittenHref, sizeof(rewrittenHref), "pnote_%s.html#%s", inlineId, inlineId);
strncpy(entry->href, rewrittenHref, 63);
entry->href[63] = '\0';
Serial.printf("[%lu] [ADDFT] Rewrote paragraph note href to: %s\n", millis(), rewrittenHref);
foundInline = true;
break;
}
}
}
if (!foundInline) {
// Normal href, just copy it
strncpy(entry->href, href, 63);
entry->href[63] = '\0';
}
} else {
// No anchor, just copy
strncpy(entry->href, href, 63);
entry->href[63] = '\0';
}
Serial.printf("[%lu] [ADDFT] Created as: num=%s, href=%s\n", millis(), entry->number, entry->href);
return entry;
}
void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char* name, const XML_Char** atts) {
auto* self = static_cast<ChapterHtmlSlimParser*>(userData);
// ============================================================================
// PASS 1: Detect and collect <p class="note">
// ============================================================================
if (strcmp(name, "p") == 0 && self->isPass1CollectingAsides) {
const char* classAttr = getAttribute(atts, "class");
if (classAttr && (strcmp(classAttr, "note") == 0 || strstr(classAttr, "note"))) {
Serial.printf("[%lu] [PNOTE] Found paragraph note (pass1=1)\n", millis());
self->insideParagraphNote = true;
self->paragraphNoteDepth = self->depth;
self->currentParagraphNoteTextLen = 0;
self->currentParagraphNoteText[0] = '\0';
self->currentParagraphNoteId[0] = '\0';
self->depth += 1;
return;
}
}
// Inside paragraph note in Pass 1, look for <a id="rnoteX">
if (self->insideParagraphNote && self->isPass1CollectingAsides && strcmp(name, "a") == 0) {
const char* id = getAttribute(atts, "id");
if (id && strncmp(id, "rnote", 5) == 0) {
strncpy(self->currentParagraphNoteId, id, 15);
self->currentParagraphNoteId[15] = '\0';
Serial.printf("[%lu] [PNOTE] Found note ID: %s\n", millis(), id);
}
self->depth += 1;
return;
}
// ============================================================================
// PASS 1: Detect and collect <aside epub:type="footnote">
// ============================================================================
if (strcmp(name, "aside") == 0) {
const char* epubType = getAttribute(atts, "epub:type");
const char* id = getAttribute(atts, "id");
if (epubType && strcmp(epubType, "footnote") == 0 && id) {
if (self->isPass1CollectingAsides) {
// Pass 1: Collect aside
Serial.printf("[%lu] [ASIDE] Found inline footnote: id=%s (pass1=%d)\n", millis(), id,
self->isPass1CollectingAsides);
self->insideAsideFootnote = true;
self->asideDepth = self->depth;
self->currentAsideTextLen = 0;
self->currentAsideText[0] = '\0';
strncpy(self->currentAsideId, id, 2);
self->currentAsideId[2] = '\0';
} else {
// Pass 2: Skip the aside (we already have it from Pass 1)
Serial.printf("[%lu] [ASIDE] Skipping aside in Pass 2: id=%s\n", millis(), id);
// Find the inline footnote text
for (int i = 0; i < self->inlineFootnoteCount; i++) {
if (strcmp(self->inlineFootnotes[i].id, id) == 0 && self->inlineFootnotes[i].text) {
// Output the footnote text as normal text
const char* text = self->inlineFootnotes[i].text;
int textLen = strlen(text);
// Process it through characterData
self->characterData(self, text, textLen);
Serial.printf("[%lu] [ASIDE] Rendered aside text: %.80s...\n", millis(), text);
break;
}
}
// Skip the aside element itself
self->skipUntilDepth = self->depth;
}
self->depth += 1;
return;
}
}
// ============================================================================
// PASS 1: Skip everything else
// ============================================================================
if (self->isPass1CollectingAsides) {
self->depth += 1;
return;
}
// ============================================================================
// PASS 2: Skip <p class="note"> (we already have them from Pass 1)
// ============================================================================
if (strcmp(name, "p") == 0) {
const char* classAttr = getAttribute(atts, "class");
if (classAttr && (strcmp(classAttr, "note") == 0 || strstr(classAttr, "note"))) {
Serial.printf("[%lu] [PNOTE] Skipping paragraph note in Pass 2\n", millis());
self->skipUntilDepth = self->depth;
self->depth += 1;
return;
}
}
// ============================================================================
// PASS 2: Normal parsing
// ============================================================================
// Middle of skip
if (self->skipUntilDepth < self->depth) {
self->depth += 1;
return;
}
// Rest of startElement logic for pass 2...
if (strcmp(name, "sup") == 0) {
self->supDepth = self->depth;
// Case A: Found <sup> inside a normal <a> (which wasn't marked as a note yet)
// Example: <a href="..."><sup>*</sup></a>
if (self->anchorDepth != -1 && !self->insideNoteref) {
Serial.printf("[%lu] [NOTEREF] Found <sup> inside <a>, promoting to noteref\n", millis());
// Flush the current word buffer (text before the sup is normal text)
if (self->partWordBufferIndex > 0) {
self->flushPartWordBuffer();
}
// Activate footnote mode
self->insideNoteref = true;
self->currentNoterefTextLen = 0;
self->currentNoterefText[0] = '\0';
// Note: The href was already saved to currentNoterefHref when the <a> was opened (see below)
}
}
// === Update the existing A block ===
if (strcmp(name, "a") == 0) {
const char* epubType = getAttribute(atts, "epub:type");
const char* href = getAttribute(atts, "href");
// Save Anchor state
self->anchorDepth = self->depth;
// Optimistically save the href, in case this becomes a footnote later (via internal <sup>)
if (!self->insideNoteref) {
if (href) {
strncpy(self->currentNoterefHref, href, 127);
self->currentNoterefHref[127] = '\0';
} else {
self->currentNoterefHref[0] = '\0';
}
}
// Footnote detection: via epub:type, rnote pattern, or if we are already inside a <sup>
// Case B: Found <a> inside <sup>
// Example: <sup><a href="...">1</a></sup>
bool isNoteref = (epubType && strcmp(epubType, "noteref") == 0);
if (!isNoteref && href && href[0] == '#' && strncmp(href + 1, "rnote", 5) == 0) {
isNoteref = true;
}
// New detection: if we are inside SUP, this link is a footnote
if (!isNoteref && self->supDepth != -1) {
isNoteref = true;
Serial.printf("[%lu] [NOTEREF] Found <a> inside <sup>, treating as noteref\n", millis());
}
if (isNoteref) {
Serial.printf("[%lu] [NOTEREF] Found noteref: href=%s\n", millis(), href ? href : "null");
// Flush word buffer
if (self->partWordBufferIndex > 0) {
self->flushPartWordBuffer();
}
self->insideNoteref = true;
self->currentNoterefTextLen = 0;
self->currentNoterefText[0] = '\0';
self->depth += 1;
return;
}
}
// Special handling for tables - show placeholder text instead of dropping silently
if (strcmp(name, "table") == 0) {
// Add placeholder text
@ -123,7 +408,6 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
}
if (matches(name, SKIP_TAGS, NUM_SKIP_TAGS)) {
// start skip
self->skipUntilDepth = self->depth;
self->depth += 1;
return;
@ -187,21 +471,97 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char* s, const int len) {
auto* self = static_cast<ChapterHtmlSlimParser*>(userData);
// Middle of skip
// Collect paragraph note text in Pass 1
if (self->insideParagraphNote && self->isPass1CollectingAsides) {
for (int i = 0; i < len; i++) {
if (self->currentParagraphNoteTextLen >= self->MAX_PNOTE_BUFFER - 2) {
if (self->currentParagraphNoteTextLen == self->MAX_PNOTE_BUFFER - 2) {
Serial.printf("[%lu] [PNOTE] WARNING: Note text truncated at %d chars\n", millis(),
self->MAX_PNOTE_BUFFER - 2);
}
break;
}
unsigned char c = (unsigned char)s[i];
if (isWhitespace(c)) {
if (self->currentParagraphNoteTextLen > 0 &&
self->currentParagraphNoteText[self->currentParagraphNoteTextLen - 1] != ' ') {
self->currentParagraphNoteText[self->currentParagraphNoteTextLen++] = ' ';
}
} else if (c >= 32 || c >= 0x80) { // Accept printable ASCII AND UTF-8
self->currentParagraphNoteText[self->currentParagraphNoteTextLen++] = c;
}
}
self->currentParagraphNoteText[self->currentParagraphNoteTextLen] = '\0';
return;
}
// If inside aside, collect the text ONLY in pass 1
if (self->insideAsideFootnote) {
if (!self->isPass1CollectingAsides) {
return;
}
for (int i = 0; i < len; i++) {
if (self->currentAsideTextLen >= self->MAX_ASIDE_BUFFER - 2) {
if (self->currentAsideTextLen == self->MAX_ASIDE_BUFFER - 2) {
Serial.printf("[%lu] [ASIDE] WARNING: Footnote text truncated at %d chars (id=%s)\n", millis(),
self->MAX_ASIDE_BUFFER - 2, self->currentAsideId);
}
break;
}
unsigned char c = (unsigned char)s[i]; // Cast to unsigned char
if (isWhitespace(c)) {
if (self->currentAsideTextLen > 0 && self->currentAsideText[self->currentAsideTextLen - 1] != ' ') {
self->currentAsideText[self->currentAsideTextLen++] = ' ';
}
} else if (c >= 32 || c >= 0x80) { // Accept printable ASCII AND UTF-8 bytes
self->currentAsideText[self->currentAsideTextLen++] = c;
}
// Skip control characters (0x00-0x1F) except whitespace
}
self->currentAsideText[self->currentAsideTextLen] = '\0';
return;
}
// During pass 1, skip all other content
if (self->isPass1CollectingAsides) {
return;
}
// Rest of characterData logic for pass 2...
if (self->insideNoteref) {
for (int i = 0; i < len; i++) {
unsigned char c = (unsigned char)s[i];
// Skip whitespace and brackets []
if (!isWhitespace(c) && c != '[' && c != ']' && self->currentNoterefTextLen < 15) {
self->currentNoterefText[self->currentNoterefTextLen++] = c;
self->currentNoterefText[self->currentNoterefTextLen] = '\0';
}
}
return;
}
if (self->skipUntilDepth < self->depth) {
return;
}
for (int i = 0; i < len; i++) {
if (isWhitespace(s[i])) {
// Currently looking at whitespace, if there's anything in the partWordBuffer, flush it
if (self->partWordBufferIndex > 0) {
self->flushPartWordBuffer();
}
// Skip the whitespace char
continue;
}
// If we're about to run out of space, then cut the word off and start a new one
if (self->partWordBufferIndex >= MAX_WORD_SIZE) {
self->flushPartWordBuffer();
}
// Skip Zero Width No-Break Space / BOM (U+FEFF) = 0xEF 0xBB 0xBF
const XML_Char FEFF_BYTE_1 = static_cast<XML_Char>(0xEF);
const XML_Char FEFF_BYTE_2 = static_cast<XML_Char>(0xBB);
@ -215,8 +575,6 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char
continue; // Move to the next iteration
}
}
// If we're about to run out of space, then cut the word off and start a new one
if (self->partWordBufferIndex >= MAX_WORD_SIZE) {
self->flushPartWordBuffer();
}
@ -232,18 +590,154 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char
Serial.printf("[%lu] [EHP] Text block too long, splitting into multiple pages\n", millis());
self->currentTextBlock->layoutAndExtractLines(
self->renderer, self->fontId, self->viewportWidth,
[self](const std::shared_ptr<TextBlock>& textBlock) { self->addLineToPage(textBlock); }, false);
[self](const std::shared_ptr<TextBlock>& textBlock, const std::vector<FootnoteEntry>& footnotes) {
self->addLineToPage(textBlock, footnotes);
},
false);
}
}
void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* name) {
auto* self = static_cast<ChapterHtmlSlimParser*>(userData);
// Closing paragraph note in Pass 1
if (strcmp(name, "p") == 0 && self->insideParagraphNote && self->depth - 1 == self->paragraphNoteDepth) {
if (self->isPass1CollectingAsides && self->currentParagraphNoteTextLen > 0 && self->paragraphNoteCount < 32 &&
self->currentParagraphNoteId[0] != '\0') {
// Copy ID
strncpy(self->paragraphNotes[self->paragraphNoteCount].id, self->currentParagraphNoteId, 15);
self->paragraphNotes[self->paragraphNoteCount].id[15] = '\0';
// Allocate memory for text
size_t textLen = strlen(self->currentParagraphNoteText);
self->paragraphNotes[self->paragraphNoteCount].text = static_cast<char*>(malloc(textLen + 1));
if (self->paragraphNotes[self->paragraphNoteCount].text) {
strcpy(self->paragraphNotes[self->paragraphNoteCount].text, self->currentParagraphNoteText);
Serial.printf("[%lu] [PNOTE] Stored: %s -> %.80s... (allocated %d bytes)\n", millis(),
self->currentParagraphNoteId, self->currentParagraphNoteText, textLen + 1);
self->paragraphNoteCount++;
}
}
self->insideParagraphNote = false;
self->depth -= 1;
return;
}
// Closing aside - handle differently for Pass 1 vs Pass 2
if (strcmp(name, "aside") == 0 && self->insideAsideFootnote && self->depth - 1 == self->asideDepth) {
// Store footnote ONLY in Pass 1
if (self->isPass1CollectingAsides && self->currentAsideTextLen > 0 && self->inlineFootnoteCount < 16) {
// Copy ID (max 2 digits)
strncpy(self->inlineFootnotes[self->inlineFootnoteCount].id, self->currentAsideId, 2);
self->inlineFootnotes[self->inlineFootnoteCount].id[2] = '\0';
// DYNAMIC ALLOCATION: allocate exactly the needed size + 1
size_t textLen = strlen(self->currentAsideText);
self->inlineFootnotes[self->inlineFootnoteCount].text = static_cast<char*>(malloc(textLen + 1));
if (self->inlineFootnotes[self->inlineFootnoteCount].text) {
strcpy(self->inlineFootnotes[self->inlineFootnoteCount].text, self->currentAsideText);
Serial.printf("[%lu] [ASIDE] Stored: %s -> %.80s... (allocated %d bytes)\n", millis(), self->currentAsideId,
self->currentAsideText, textLen + 1);
self->inlineFootnoteCount++;
} else {
Serial.printf("[%lu] [ASIDE] ERROR: Failed to allocate %d bytes for footnote %s\n", millis(), textLen + 1,
self->currentAsideId);
}
}
// Reset state AFTER processing
self->insideAsideFootnote = false;
self->depth -= 1;
return;
}
// During pass 1, skip all other processing
if (self->isPass1CollectingAsides) {
self->depth -= 1;
return;
}
// ---------------------------------------------------------
// PASS 2: Normal Parsing Logic
// ---------------------------------------------------------
// [NEW] 1. Reset Superscript State
// We must ensure we know when we are leaving a <sup> tag
if (strcmp(name, "sup") == 0) {
if (self->supDepth == self->depth) {
self->supDepth = -1;
}
}
// [MODIFIED] 2. Handle 'a' tags (Anchors/Footnotes)
// We check "a" generally now, to handle both Noterefs AND resetting regular links
if (strcmp(name, "a") == 0) {
// Track if this was a noteref so we can return early later
bool wasNoteref = self->insideNoteref;
if (self->insideNoteref) {
self->insideNoteref = false;
if (self->currentNoterefTextLen > 0) {
Serial.printf("[%lu] [NOTEREF] %s -> %s\n", millis(), self->currentNoterefText, self->currentNoterefHref);
// Create the footnote entry (this does the rewriting)
std::unique_ptr<FootnoteEntry> footnote =
self->createFootnoteEntry(self->currentNoterefText, self->currentNoterefHref);
// Then call callback with the REWRITTEN href
if (self->noterefCallback && footnote) {
Noteref noteref;
strncpy(noteref.number, self->currentNoterefText, 15);
noteref.number[15] = '\0';
strncpy(noteref.href, footnote->href, 127);
noteref.href[127] = '\0';
self->noterefCallback(noteref);
}
// Ensure [1] appears inline after the word it references
EpdFontFamily::Style fontStyle = self->getCurrentFontStyle();
// Format the noteref text with brackets
char formattedNoteref[32];
snprintf(formattedNoteref, sizeof(formattedNoteref), "[%s]", self->currentNoterefText);
// Add it as a word to the current text block with the footnote attached
if (self->currentTextBlock) {
self->currentTextBlock->addWord(formattedNoteref, fontStyle, std::move(footnote));
}
}
self->currentNoterefTextLen = 0;
self->currentNoterefText[0] = '\0';
self->currentNoterefHrefLen = 0;
// Note: We do NOT clear currentNoterefHref here yet, we do it below
}
// [NEW] Reset Anchor Depth
// This runs for BOTH footnotes and regular links to ensure state is clean
if (self->anchorDepth == self->depth) {
self->anchorDepth = -1;
self->currentNoterefHref[0] = '\0';
}
// If it was a noteref, we are done with this tag, return early
if (wasNoteref) {
self->depth -= 1;
return;
}
}
if (self->partWordBufferIndex > 0) {
// Only flush out part word buffer if we're closing a block tag or are at the top of the HTML file.
// We don't want to flush out content when closing inline tags like <span>.
// Currently this also flushes out on closing <b> and <i> tags, but they are line tags so that shouldn't happen,
// text styling needs to be overhauled to fix it.
const bool shouldBreakText =
matches(name, BLOCK_TAGS, NUM_BLOCK_TAGS) || matches(name, HEADER_TAGS, NUM_HEADER_TAGS) ||
matches(name, BOLD_TAGS, NUM_BOLD_TAGS) || matches(name, ITALIC_TAGS, NUM_ITALIC_TAGS) ||
@ -256,56 +750,56 @@ void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* n
self->depth -= 1;
// Leaving skip
if (self->skipUntilDepth == self->depth) {
self->skipUntilDepth = INT_MAX;
}
// Leaving bold
if (self->boldUntilDepth == self->depth) {
self->boldUntilDepth = INT_MAX;
}
// Leaving italic
if (self->italicUntilDepth == self->depth) {
self->italicUntilDepth = INT_MAX;
}
}
bool ChapterHtmlSlimParser::parseAndBuildPages() {
startNewTextBlock((TextBlock::Style)this->paragraphAlignment);
// ============================================================================
// PASS 1: Extract all inline footnotes (aside elements) FIRST
// ============================================================================
Serial.printf("[%lu] [PARSER] === PASS 1: Extracting inline footnotes ===\n", millis());
const XML_Parser parser = XML_ParserCreate(nullptr);
int done;
// Reset state for pass 1
depth = 0;
skipUntilDepth = INT_MAX;
insideAsideFootnote = false;
insideParagraphNote = false;
inlineFootnoteCount = 0;
paragraphNoteCount = 0;
isPass1CollectingAsides = true;
if (!parser) {
XML_Parser parser1 = XML_ParserCreate(nullptr);
if (!parser1) {
Serial.printf("[%lu] [EHP] Couldn't allocate memory for parser\n", millis());
return false;
}
XML_SetUserData(parser1, this);
XML_SetElementHandler(parser1, startElement, endElement);
XML_SetCharacterDataHandler(parser1, characterData);
FsFile file;
if (!SdMan.openFileForRead("EHP", filepath, file)) {
XML_ParserFree(parser);
XML_ParserFree(parser1);
return false;
}
// Get file size for progress calculation
const size_t totalSize = file.size();
size_t bytesRead = 0;
int lastProgress = -1;
XML_SetUserData(parser, this);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, characterData);
bool done = false;
do {
void* const buf = XML_GetBuffer(parser, 1024);
void* const buf = XML_GetBuffer(parser1, 1024);
if (!buf) {
Serial.printf("[%lu] [EHP] Couldn't allocate memory for buffer\n", millis());
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
XML_SetCharacterDataHandler(parser, nullptr);
XML_ParserFree(parser);
XML_ParserFree(parser1);
file.close();
return false;
}
@ -314,10 +808,87 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() {
if (len == 0 && file.available() > 0) {
Serial.printf("[%lu] [EHP] File read error\n", millis());
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
XML_SetCharacterDataHandler(parser, nullptr);
XML_ParserFree(parser);
XML_ParserFree(parser1);
file.close();
return false;
}
done = file.available() == 0;
if (XML_ParseBuffer(parser1, static_cast<int>(len), done) == XML_STATUS_ERROR) {
Serial.printf("[%lu] [EHP] Parse error at line %lu:\n%s\n", millis(), XML_GetCurrentLineNumber(parser1),
XML_ErrorString(XML_GetErrorCode(parser1)));
XML_ParserFree(parser1);
file.close();
return false;
}
} while (!done);
XML_ParserFree(parser1);
file.close();
Serial.printf("[%lu] [PARSER] Pass 1 complete: found %d inline footnotes\n", millis(), inlineFootnoteCount);
for (int i = 0; i < inlineFootnoteCount; i++) {
Serial.printf("[%lu] [PARSER] - %s: %.80s\n", millis(), inlineFootnotes[i].id, inlineFootnotes[i].text);
}
// ============================================================================
// PASS 2: Build pages with inline footnotes already available
// ============================================================================
Serial.printf("[%lu] [PARSER] === PASS 2: Building pages ===\n", millis());
// Reset parser state for pass 2
depth = 0;
skipUntilDepth = INT_MAX;
boldUntilDepth = INT_MAX;
italicUntilDepth = INT_MAX;
partWordBufferIndex = 0;
insideNoteref = false;
insideAsideFootnote = false;
isPass1CollectingAsides = false;
supDepth = -1;
anchorDepth = -1;
startNewTextBlock((TextBlock::Style)this->paragraphAlignment);
const XML_Parser parser2 = XML_ParserCreate(nullptr);
if (!parser2) {
Serial.printf("[%lu] [EHP] Couldn't allocate memory for parser\n", millis());
return false;
}
XML_SetUserData(parser2, this);
XML_SetElementHandler(parser2, startElement, endElement);
XML_SetCharacterDataHandler(parser2, characterData);
if (!SdMan.openFileForRead("EHP", filepath, file)) {
XML_ParserFree(parser2);
return false;
}
// Get file size for progress calculation
const size_t totalSize = file.size();
size_t bytesRead = 0;
int lastProgress = -1;
do {
void* const buf = XML_GetBuffer(parser2, 1024);
if (!buf) {
Serial.printf("[%lu] [EHP] Couldn't allocate memory for buffer\n", millis());
XML_ParserFree(parser2);
file.close();
return false;
}
const size_t len = file.read(buf, 1024);
if (len == 0 && file.available() > 0) {
Serial.printf("[%lu] [EHP] File read error\n", millis());
XML_StopParser(parser2, XML_FALSE); // Stop any pending processing
XML_SetElementHandler(parser2, nullptr, nullptr); // Clear callbacks
XML_SetCharacterDataHandler(parser2, nullptr);
XML_ParserFree(parser2);
file.close();
return false;
}
@ -335,28 +906,29 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() {
done = file.available() == 0;
if (XML_ParseBuffer(parser, static_cast<int>(len), done) == XML_STATUS_ERROR) {
Serial.printf("[%lu] [EHP] Parse error at line %lu:\n%s\n", millis(), XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
XML_SetCharacterDataHandler(parser, nullptr);
XML_ParserFree(parser);
if (XML_ParseBuffer(parser2, static_cast<int>(len), done) == XML_STATUS_ERROR) {
Serial.printf("[%lu] [EHP] Parse error at line %lu:\n%s\n", millis(), XML_GetCurrentLineNumber(parser2),
XML_ErrorString(XML_GetErrorCode(parser2)));
XML_StopParser(parser2, XML_FALSE); // Stop any pending processing
XML_SetElementHandler(parser2, nullptr, nullptr); // Clear callbacks
XML_SetCharacterDataHandler(parser2, nullptr);
XML_ParserFree(parser2);
file.close();
return false;
}
} while (!done);
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
XML_SetCharacterDataHandler(parser, nullptr);
XML_ParserFree(parser);
XML_ParserFree(parser2);
file.close();
// Process last page if there is still text
if (currentTextBlock) {
makePages();
if (currentPage) {
completePageFn(std::move(currentPage));
}
currentPage.reset();
currentTextBlock.reset();
}
@ -364,7 +936,8 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() {
return true;
}
void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr<TextBlock> line) {
void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr<TextBlock> line,
const std::vector<FootnoteEntry>& footnotes) {
const int lineHeight = renderer.getLineHeight(fontId) * lineCompression;
if (currentPageNextY + lineHeight > viewportHeight) {
@ -373,8 +946,17 @@ void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr<TextBlock> line) {
currentPageNextY = 0;
}
if (currentPage && currentPage->elements.size() < 24) { // Assuming generic capacity check or vector size
currentPage->elements.push_back(std::make_shared<PageLine>(line, 0, currentPageNextY));
currentPageNextY += lineHeight;
// Add footnotes for this line to the current page
for (const auto& fn : footnotes) {
currentPage->addFootnote(fn.number, fn.href);
}
} else if (currentPage) {
Serial.printf("[%lu] [EHP] WARNING: Page element capacity reached, skipping element\n", millis());
}
}
void ChapterHtmlSlimParser::makePages() {
@ -391,7 +973,9 @@ void ChapterHtmlSlimParser::makePages() {
const int lineHeight = renderer.getLineHeight(fontId) * lineCompression;
currentTextBlock->layoutAndExtractLines(
renderer, fontId, viewportWidth,
[this](const std::shared_ptr<TextBlock>& textBlock) { addLineToPage(textBlock); });
[this](const std::shared_ptr<TextBlock>& textBlock, const std::vector<FootnoteEntry>& footnotes) {
addLineToPage(textBlock, footnotes);
});
// Extra paragraph spacing if enabled
if (extraParagraphSpacing) {
currentPageNextY += lineHeight / 2;

View File

@ -3,9 +3,11 @@
#include <expat.h>
#include <climits>
#include <cstring>
#include <functional>
#include <memory>
#include "../FootnoteEntry.h"
#include "../ParsedText.h"
#include "../blocks/TextBlock.h"
@ -14,6 +16,37 @@ class GfxRenderer;
#define MAX_WORD_SIZE 200
struct Noteref {
char number[16];
char href[128];
};
// Struct to store collected inline footnotes (aside elements)
struct InlineFootnote {
char id[3];
char* text;
InlineFootnote() : text(nullptr) { id[0] = '\0'; }
};
// Struct to store collected inline footnotes from <p class="note">
struct ParagraphNote {
char id[16]; // ID from <a id="rnote1">
char* text; // Pointer to dynamically allocated text
ParagraphNote() : text(nullptr) { id[0] = '\0'; }
~ParagraphNote() {
if (text) {
free(text);
text = nullptr;
}
}
ParagraphNote(const ParagraphNote&) = delete;
ParagraphNote& operator=(const ParagraphNote&) = delete;
};
class ChapterHtmlSlimParser {
const std::string& filepath;
GfxRenderer& renderer;
@ -23,8 +56,6 @@ class ChapterHtmlSlimParser {
int skipUntilDepth = INT_MAX;
int boldUntilDepth = INT_MAX;
int italicUntilDepth = INT_MAX;
// buffer for building up words from characters, will auto break if longer than this
// leave one char at end for null pointer
char partWordBuffer[MAX_WORD_SIZE + 1] = {};
int partWordBufferIndex = 0;
std::unique_ptr<ParsedText> currentTextBlock = nullptr;
@ -38,15 +69,58 @@ class ChapterHtmlSlimParser {
uint16_t viewportHeight;
bool hyphenationEnabled;
// Noteref tracking
bool insideNoteref = false;
char currentNoterefText[16] = {0};
int currentNoterefTextLen = 0;
char currentNoterefHref[128] = {0};
int currentNoterefHrefLen = 0;
std::function<void(Noteref&)> noterefCallback = nullptr;
// Inline footnotes (aside) tracking
bool insideAsideFootnote = false;
int asideDepth = 0;
char currentAsideId[3] = {0};
// Paragraph note tracking
bool insideParagraphNote = false;
int paragraphNoteDepth = 0;
char currentParagraphNoteId[16] = {0};
static constexpr int MAX_PNOTE_BUFFER = 256;
char currentParagraphNoteText[MAX_PNOTE_BUFFER] = {0};
int currentParagraphNoteTextLen = 0;
// Temporary buffer for accumulation, will be copied to dynamic allocation
static constexpr int MAX_ASIDE_BUFFER = 1024;
char currentAsideText[MAX_ASIDE_BUFFER] = {0};
int currentAsideTextLen = 0;
// Flag to indicate we're in Pass 1 (collecting asides only)
bool isPass1CollectingAsides = false;
// Track superscript depth
int supDepth = -1;
int anchorDepth = -1;
std::unique_ptr<FootnoteEntry> createFootnoteEntry(const char* number, const char* href);
void startNewTextBlock(TextBlock::Style style);
EpdFontFamily::Style getCurrentFontStyle() const;
void flushPartWordBuffer();
void makePages();
// XML callbacks
static void XMLCALL startElement(void* userData, const XML_Char* name, const XML_Char** atts);
static void XMLCALL characterData(void* userData, const XML_Char* s, int len);
static void XMLCALL endElement(void* userData, const XML_Char* name);
public:
// inline footnotes
InlineFootnote inlineFootnotes[16];
int inlineFootnoteCount = 0;
// paragraph notes
ParagraphNote paragraphNotes[16];
int paragraphNoteCount = 0;
explicit ChapterHtmlSlimParser(const std::string& filepath, GfxRenderer& renderer, const int fontId,
const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
@ -55,6 +129,7 @@ class ChapterHtmlSlimParser {
const std::function<void(int)>& progressFn = nullptr)
: filepath(filepath),
renderer(renderer),
completePageFn(completePageFn),
fontId(fontId),
lineCompression(lineCompression),
extraParagraphSpacing(extraParagraphSpacing),
@ -62,9 +137,27 @@ class ChapterHtmlSlimParser {
viewportWidth(viewportWidth),
viewportHeight(viewportHeight),
hyphenationEnabled(hyphenationEnabled),
completePageFn(completePageFn),
progressFn(progressFn) {}
~ChapterHtmlSlimParser() = default;
progressFn(progressFn),
inlineFootnoteCount(0) {
// Initialize all footnote pointers to null
for (int i = 0; i < 16; i++) {
inlineFootnotes[i].text = nullptr;
inlineFootnotes[i].id[0] = '\0';
}
}
~ChapterHtmlSlimParser() {
// Manual cleanup of inline footnotes
for (int i = 0; i < inlineFootnoteCount; i++) {
if (inlineFootnotes[i].text) {
free(inlineFootnotes[i].text);
inlineFootnotes[i].text = nullptr;
}
}
}
bool parseAndBuildPages();
void addLineToPage(std::shared_ptr<TextBlock> line);
void addLineToPage(std::shared_ptr<TextBlock> line, const std::vector<FootnoteEntry>& footnotes);
void setNoterefCallback(const std::function<void(Noteref&)>& callback) { noterefCallback = callback; }
};

View File

@ -7,7 +7,7 @@
#include "CrossPointSettings.h"
#include "CrossPointState.h"
#include "EpubReaderChapterSelectionActivity.h"
#include "EpubReaderTocActivity.h"
#include "MappedInputManager.h"
#include "RecentBooksStore.h"
#include "ScreenComponents.h"
@ -53,7 +53,6 @@ void EpubReaderActivity::onEnter() {
}
renderingMutex = xSemaphoreCreateMutex();
epub->setupCacheDir();
FsFile f;
@ -91,7 +90,7 @@ void EpubReaderActivity::onEnter() {
updateRequired = true;
xTaskCreate(&EpubReaderActivity::taskTrampoline, "EpubReaderActivityTask",
8192, // Stack size
24576, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
@ -123,20 +122,25 @@ void EpubReaderActivity::loop() {
return;
}
// Enter chapter selection activity
// Enter chapter selection activity or menu
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
// Don't start activity transition while rendering
xSemaphoreTake(renderingMutex, portMAX_DELAY);
const int currentPage = section ? section->currentPage : 0;
const int totalPages = section ? section->pageCount : 0;
// Show consolidated TOC activity (Chapters and Footnotes)
exitActivity();
enterNewActivity(new EpubReaderChapterSelectionActivity(
enterNewActivity(new EpubReaderTocActivity(
this->renderer, this->mappedInput, epub, epub->getPath(), currentSpineIndex, currentPage, totalPages,
currentPageFootnotes,
[this] {
// onGoBack
exitActivity();
updateRequired = true;
},
[this](const int newSpineIndex) {
[this](int newSpineIndex) {
// onSelectSpineIndex
if (currentSpineIndex != newSpineIndex) {
currentSpineIndex = newSpineIndex;
nextPageNumber = 0;
@ -145,8 +149,14 @@ void EpubReaderActivity::loop() {
exitActivity();
updateRequired = true;
},
[this](const int newSpineIndex, const int newPage) {
// Handle sync position
[this](const char* href) {
// onSelectFootnote
navigateToHref(href, true);
exitActivity();
updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
// onSyncPosition
if (currentSpineIndex != newSpineIndex || (section && section->currentPage != newPage)) {
currentSpineIndex = newSpineIndex;
nextPageNumber = newPage;
@ -166,9 +176,15 @@ void EpubReaderActivity::loop() {
// Short press BACK goes to file selection
if (mappedInput.wasReleased(MappedInputManager::Button::Back) && mappedInput.getHeldTime() < goHomeMs) {
if (isViewingFootnote) {
restoreSavedPosition();
updateRequired = true;
return;
} else {
onGoBack();
return;
}
}
// When long-press chapter skip is disabled, turn pages on press instead of release.
const bool usePressForPageTurn = !SETTINGS.longPressChapterSkip;
@ -188,7 +204,7 @@ void EpubReaderActivity::loop() {
return;
}
// any botton press when at end of the book goes back to the last page
// any button press when at end of the book goes back to the last page
if (currentSpineIndex > 0 && currentSpineIndex >= epub->getSpineItemsCount()) {
currentSpineIndex = epub->getSpineItemsCount() - 1;
nextPageNumber = UINT16_MAX;
@ -254,17 +270,16 @@ void EpubReaderActivity::displayTaskLoop() {
}
}
// TODO: Failure handling
void EpubReaderActivity::renderScreen() {
if (!epub) {
return;
}
// edge case handling for sub-zero spine index
// Edge case handling for sub-zero spine index
if (currentSpineIndex < 0) {
currentSpineIndex = 0;
}
// based bounds of book, show end of book screen
// Based bounds of book, show end of book screen
if (currentSpineIndex > epub->getSpineItemsCount()) {
currentSpineIndex = epub->getSpineItemsCount();
}
@ -403,6 +418,22 @@ void EpubReaderActivity::renderScreen() {
section.reset();
return renderScreen();
}
Serial.printf("[%lu] [ERS] Page loaded: %d elements, %d footnotes\n", millis(), p->elements.size(),
p->footnotes.size());
// Copy footnotes from page to currentPageFootnotes
currentPageFootnotes.clear();
int maxFootnotes = (p->footnotes.size() < 8) ? p->footnotes.size() : 8;
for (int i = 0; i < maxFootnotes; i++) {
const FootnoteEntry& footnote = p->footnotes[i];
if (footnote.href[0] != '\0') {
currentPageFootnotes.addFootnote(footnote.number, footnote.href);
}
}
Serial.printf("[%lu] [ERS] Loaded %d footnotes for current page\n", millis(), p->footnotes.size());
const auto start = millis();
renderContents(std::move(p), orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
Serial.printf("[%lu] [ERS] Rendered page in %dms\n", millis(), millis() - start);
@ -553,3 +584,119 @@ void EpubReaderActivity::renderStatusBar(const int orientedMarginRight, const in
title.c_str());
}
}
void EpubReaderActivity::navigateToHref(const char* href, bool savePosition) {
if (!epub || !href) return;
// Save current position if requested
if (savePosition && section) {
savedSpineIndex = currentSpineIndex;
savedPageNumber = section->currentPage;
isViewingFootnote = true;
Serial.printf("[%lu] [ERS] Saved position: spine %d, page %d\n", millis(), savedSpineIndex, savedPageNumber);
}
// Parse href: "filename.html#anchor"
std::string hrefStr(href);
std::string filename;
std::string anchor;
size_t hashPos = hrefStr.find('#');
if (hashPos != std::string::npos) {
filename = hrefStr.substr(0, hashPos);
anchor = hrefStr.substr(hashPos + 1);
} else {
filename = hrefStr;
}
// Extract just filename without path
size_t lastSlash = filename.find_last_of('/');
if (lastSlash != std::string::npos) {
filename = filename.substr(lastSlash + 1);
}
Serial.printf("[%lu] [ERS] Navigate to: %s (anchor: %s)\n", millis(), filename.c_str(), anchor.c_str());
int targetSpineIndex = -1;
// FIRST: Check if we have an inline footnote or paragraph note for this anchor
if (!anchor.empty()) {
// Try inline footnote first
std::string inlineFilename = "inline_" + anchor + ".html";
Serial.printf("[%lu] [ERS] Looking for inline footnote: %s\n", millis(), inlineFilename.c_str());
targetSpineIndex = epub->findVirtualSpineIndex(inlineFilename);
// If not found, try paragraph note
if (targetSpineIndex == -1) {
std::string pnoteFilename = "pnote_" + anchor + ".html";
Serial.printf("[%lu] [ERS] Looking for paragraph note: %s\n", millis(), pnoteFilename.c_str());
targetSpineIndex = epub->findVirtualSpineIndex(pnoteFilename);
}
if (targetSpineIndex != -1) {
Serial.printf("[%lu] [ERS] Found note at virtual index: %d\n", millis(), targetSpineIndex);
// Navigate to the note
xSemaphoreTake(renderingMutex, portMAX_DELAY);
currentSpineIndex = targetSpineIndex;
nextPageNumber = 0;
section.reset();
xSemaphoreGive(renderingMutex);
updateRequired = true;
return;
} else {
Serial.printf("[%lu] [ERS] No virtual note found, trying normal navigation\n", millis());
}
}
// FALLBACK: Try to find the file in normal spine items
for (int i = 0; i < epub->getSpineItemsCount(); i++) {
if (epub->isVirtualSpineItem(i)) continue;
BookMetadataCache::SpineEntry entry = epub->getSpineItem(i);
std::string spineItem = entry.href;
size_t lastslash = spineItem.find_last_of('/');
std::string spineFilename = (lastslash != std::string::npos) ? spineItem.substr(lastslash + 1) : spineItem;
if (spineFilename == filename) {
targetSpineIndex = i;
break;
}
}
if (targetSpineIndex == -1) {
Serial.printf("[%lu] [ERS] Could not find spine index for: %s\n", millis(), filename.c_str());
return;
}
// Navigate to the target chapter
xSemaphoreTake(renderingMutex, portMAX_DELAY);
currentSpineIndex = targetSpineIndex;
nextPageNumber = 0;
section.reset();
xSemaphoreGive(renderingMutex);
updateRequired = true;
Serial.printf("[%lu] [ERS] Navigated to spine index: %d\n", millis(), targetSpineIndex);
}
void EpubReaderActivity::restoreSavedPosition() {
if (savedSpineIndex >= 0 && savedPageNumber >= 0) {
Serial.printf("[%lu] [ERS] Restoring position: spine %d, page %d\n", millis(), savedSpineIndex, savedPageNumber);
xSemaphoreTake(renderingMutex, portMAX_DELAY);
currentSpineIndex = savedSpineIndex;
nextPageNumber = savedPageNumber;
section.reset();
xSemaphoreGive(renderingMutex);
savedSpineIndex = -1;
savedPageNumber = -1;
isViewingFootnote = false;
updateRequired = true;
}
}

View File

@ -5,6 +5,7 @@
#include <freertos/semphr.h>
#include <freertos/task.h>
#include "FootnotesData.h"
#include "activities/ActivityWithSubactivity.h"
class EpubReaderActivity final : public ActivityWithSubactivity {
@ -21,6 +22,11 @@ class EpubReaderActivity final : public ActivityWithSubactivity {
const std::function<void()> onGoBack;
const std::function<void()> onGoHome;
FootnotesData currentPageFootnotes;
int savedSpineIndex = -1;
int savedPageNumber = -1;
bool isViewingFootnote = false;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void renderScreen();
@ -28,6 +34,10 @@ class EpubReaderActivity final : public ActivityWithSubactivity {
int orientedMarginBottom, int orientedMarginLeft);
void renderStatusBar(int orientedMarginRight, int orientedMarginBottom, int orientedMarginLeft) const;
// Footnote navigation methods
void navigateToHref(const char* href, bool savePosition = false);
void restoreSavedPosition();
public:
explicit EpubReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Epub> epub,
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)

View File

@ -1,215 +0,0 @@
#include "EpubReaderChapterSelectionActivity.h"
#include <GfxRenderer.h>
#include "KOReaderCredentialStore.h"
#include "KOReaderSyncActivity.h"
#include "MappedInputManager.h"
#include "fontIds.h"
namespace {
// Time threshold for treating a long press as a page-up/page-down
constexpr int SKIP_PAGE_MS = 700;
} // namespace
bool EpubReaderChapterSelectionActivity::hasSyncOption() const { return KOREADER_STORE.hasCredentials(); }
int EpubReaderChapterSelectionActivity::getTotalItems() const {
// Add 2 for sync options (top and bottom) if credentials are configured
const int syncCount = hasSyncOption() ? 2 : 0;
return epub->getTocItemsCount() + syncCount;
}
bool EpubReaderChapterSelectionActivity::isSyncItem(int index) const {
if (!hasSyncOption()) return false;
// First item and last item are sync options
return index == 0 || index == getTotalItems() - 1;
}
int EpubReaderChapterSelectionActivity::tocIndexFromItemIndex(int itemIndex) const {
// Account for the sync option at the top
const int offset = hasSyncOption() ? 1 : 0;
return itemIndex - offset;
}
int EpubReaderChapterSelectionActivity::getPageItems() const {
// Layout constants used in renderScreen
constexpr int startY = 60;
constexpr int lineHeight = 30;
const int screenHeight = renderer.getScreenHeight();
const int endY = screenHeight - lineHeight;
const int availableHeight = endY - startY;
int items = availableHeight / lineHeight;
// Ensure we always have at least one item per page to avoid division by zero
if (items < 1) {
items = 1;
}
return items;
}
void EpubReaderChapterSelectionActivity::taskTrampoline(void* param) {
auto* self = static_cast<EpubReaderChapterSelectionActivity*>(param);
self->displayTaskLoop();
}
void EpubReaderChapterSelectionActivity::onEnter() {
ActivityWithSubactivity::onEnter();
if (!epub) {
return;
}
renderingMutex = xSemaphoreCreateMutex();
// Account for sync option offset when finding current TOC index
const int syncOffset = hasSyncOption() ? 1 : 0;
selectorIndex = epub->getTocIndexForSpineIndex(currentSpineIndex);
if (selectorIndex == -1) {
selectorIndex = 0;
}
selectorIndex += syncOffset; // Offset for top sync option
// Trigger first update
updateRequired = true;
xTaskCreate(&EpubReaderChapterSelectionActivity::taskTrampoline, "EpubReaderChapterSelectionActivityTask",
4096, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void EpubReaderChapterSelectionActivity::onExit() {
ActivityWithSubactivity::onExit();
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void EpubReaderChapterSelectionActivity::launchSyncActivity() {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity();
enterNewActivity(new KOReaderSyncActivity(
renderer, mappedInput, epub, epubPath, currentSpineIndex, currentPage, totalPagesInSpine,
[this]() {
// On cancel
exitActivity();
updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
// On sync complete
exitActivity();
onSyncPosition(newSpineIndex, newPage);
}));
xSemaphoreGive(renderingMutex);
}
void EpubReaderChapterSelectionActivity::loop() {
if (subActivity) {
subActivity->loop();
return;
}
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::Up) ||
mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
const int pageItems = getPageItems();
const int totalItems = getTotalItems();
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
// Check if sync option is selected (first or last item)
if (isSyncItem(selectorIndex)) {
launchSyncActivity();
return;
}
// Get TOC index (account for top sync offset)
const int tocIndex = tocIndexFromItemIndex(selectorIndex);
const auto newSpineIndex = epub->getSpineIndexForTocIndex(tocIndex);
if (newSpineIndex == -1) {
onGoBack();
} else {
onSelectSpineIndex(newSpineIndex);
}
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
} else if (prevReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + totalItems) % totalItems;
} else {
selectorIndex = (selectorIndex + totalItems - 1) % totalItems;
}
updateRequired = true;
} else if (nextReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % totalItems;
} else {
selectorIndex = (selectorIndex + 1) % totalItems;
}
updateRequired = true;
}
}
void EpubReaderChapterSelectionActivity::displayTaskLoop() {
while (true) {
if (updateRequired && !subActivity) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
renderScreen();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void EpubReaderChapterSelectionActivity::renderScreen() {
renderer.clearScreen();
const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getPageItems();
const int totalItems = getTotalItems();
const std::string title =
renderer.truncatedText(UI_12_FONT_ID, epub->getTitle().c_str(), pageWidth - 40, EpdFontFamily::BOLD);
renderer.drawCenteredText(UI_12_FONT_ID, 15, title.c_str(), true, EpdFontFamily::BOLD);
const auto pageStartIndex = selectorIndex / pageItems * pageItems;
renderer.fillRect(0, 60 + (selectorIndex % pageItems) * 30 - 2, pageWidth - 1, 30);
for (int i = 0; i < pageItems; i++) {
int itemIndex = pageStartIndex + i;
if (itemIndex >= totalItems) break;
const int displayY = 60 + i * 30;
const bool isSelected = (itemIndex == selectorIndex);
if (isSyncItem(itemIndex)) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, ">> Sync Progress", !isSelected);
} else {
const int tocIndex = tocIndexFromItemIndex(itemIndex);
auto item = epub->getTocItem(tocIndex);
const int indentSize = 20 + (item.level - 1) * 15;
const std::string chapterName =
renderer.truncatedText(UI_10_FONT_ID, item.title.c_str(), pageWidth - 40 - indentSize);
renderer.drawText(UI_10_FONT_ID, indentSize, displayY, chapterName.c_str(), !isSelected);
}
}
const auto labels = mappedInput.mapLabels("« Back", "Select", "Up", "Down");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
}

View File

@ -1,65 +0,0 @@
#pragma once
#include <Epub.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <memory>
#include "../ActivityWithSubactivity.h"
class EpubReaderChapterSelectionActivity final : public ActivityWithSubactivity {
std::shared_ptr<Epub> epub;
std::string epubPath;
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
int currentSpineIndex = 0;
int currentPage = 0;
int totalPagesInSpine = 0;
int selectorIndex = 0;
bool updateRequired = false;
const std::function<void()> onGoBack;
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
const std::function<void(int newSpineIndex, int newPage)> onSyncPosition;
// Number of items that fit on a page, derived from logical screen height.
// This adapts automatically when switching between portrait and landscape.
int getPageItems() const;
// Total items including sync options (top and bottom)
int getTotalItems() const;
// Check if sync option is available (credentials configured)
bool hasSyncOption() const;
// Check if given item index is a sync option (first or last)
bool isSyncItem(int index) const;
// Convert item index to TOC index (accounting for top sync option offset)
int tocIndexFromItemIndex(int itemIndex) const;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void renderScreen();
void launchSyncActivity();
public:
explicit EpubReaderChapterSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::shared_ptr<Epub>& epub, const std::string& epubPath,
const int currentSpineIndex, const int currentPage,
const int totalPagesInSpine, const std::function<void()>& onGoBack,
const std::function<void(int newSpineIndex)>& onSelectSpineIndex,
const std::function<void(int newSpineIndex, int newPage)>& onSyncPosition)
: ActivityWithSubactivity("EpubReaderChapterSelection", renderer, mappedInput),
epub(epub),
epubPath(epubPath),
currentSpineIndex(currentSpineIndex),
currentPage(currentPage),
totalPagesInSpine(totalPagesInSpine),
onGoBack(onGoBack),
onSelectSpineIndex(onSelectSpineIndex),
onSyncPosition(onSyncPosition) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@ -0,0 +1,309 @@
#include "EpubReaderTocActivity.h"
#include <EpdFontFamily.h>
#include <GfxRenderer.h>
#include "KOReaderCredentialStore.h"
#include "KOReaderSyncActivity.h"
#include "MappedInputManager.h"
#include "ScreenComponents.h"
#include "fontIds.h"
namespace {
constexpr int TAB_BAR_Y = 15;
constexpr int CONTENT_START_Y = 60;
constexpr int CHAPTER_LINE_HEIGHT = 30;
constexpr int FOOTNOTE_LINE_HEIGHT = 40;
constexpr int SKIP_PAGE_MS = 700;
} // namespace
void EpubReaderTocActivity::taskTrampoline(void* param) {
auto* self = static_cast<EpubReaderTocActivity*>(param);
self->displayTaskLoop();
}
void EpubReaderTocActivity::onEnter() {
ActivityWithSubactivity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
// Init chapters state
buildFilteredChapterList();
chaptersSelectorIndex = 0;
for (size_t i = 0; i < filteredSpineIndices.size(); i++) {
if (filteredSpineIndices[i] == currentSpineIndex) {
chaptersSelectorIndex = i;
break;
}
}
if (hasSyncOption()) {
chaptersSelectorIndex += 1;
}
// Init footnotes state
footnotesSelectedIndex = 0;
updateRequired = true;
xTaskCreate(&EpubReaderTocActivity::taskTrampoline, "EpubReaderTocTask", 4096, this, 1, &displayTaskHandle);
}
void EpubReaderTocActivity::onExit() {
ActivityWithSubactivity::onExit();
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void EpubReaderTocActivity::launchSyncActivity() {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity();
enterNewActivity(new KOReaderSyncActivity(
renderer, mappedInput, this->epub, epubPath, currentSpineIndex, currentPage, totalPagesInSpine,
[this]() {
exitActivity();
this->updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
exitActivity();
this->onSyncPosition(newSpineIndex, newPage);
}));
xSemaphoreGive(renderingMutex);
}
void EpubReaderTocActivity::loop() {
if (subActivity) {
subActivity->loop();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
return;
}
const bool leftReleased = mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool rightReleased = mappedInput.wasReleased(MappedInputManager::Button::Right);
if (leftReleased && currentTab == Tab::FOOTNOTES) {
currentTab = Tab::CHAPTERS;
updateRequired = true;
return;
}
if (rightReleased && currentTab == Tab::CHAPTERS) {
currentTab = Tab::FOOTNOTES;
updateRequired = true;
return;
}
if (currentTab == Tab::CHAPTERS) {
loopChapters();
} else {
loopFootnotes();
}
}
void EpubReaderTocActivity::loopChapters() {
const bool upReleased = mappedInput.wasReleased(MappedInputManager::Button::Up);
const bool downReleased = mappedInput.wasReleased(MappedInputManager::Button::Down);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
const int totalItems = getChaptersTotalItems();
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (hasSyncOption() && (chaptersSelectorIndex == 0 || chaptersSelectorIndex == totalItems - 1)) {
launchSyncActivity();
return;
}
int filteredIndex = chaptersSelectorIndex;
if (hasSyncOption()) filteredIndex -= 1;
if (filteredIndex >= 0 && filteredIndex < static_cast<int>(filteredSpineIndices.size())) {
onSelectSpineIndex(filteredSpineIndices[filteredIndex]);
}
} else if (upReleased) {
if (totalItems > 0) {
if (skipPage) {
// TODO: implement page-skip navigation once page size is available
}
chaptersSelectorIndex = (chaptersSelectorIndex + totalItems - 1) % totalItems;
updateRequired = true;
}
} else if (downReleased) {
if (totalItems > 0) {
if (skipPage) {
// TODO: implement page-skip navigation once page size is available
}
chaptersSelectorIndex = (chaptersSelectorIndex + 1) % totalItems;
updateRequired = true;
}
}
}
void EpubReaderTocActivity::loopFootnotes() {
bool needsRedraw = false;
if (mappedInput.wasPressed(MappedInputManager::Button::Up)) {
if (footnotesSelectedIndex > 0) {
footnotesSelectedIndex--;
needsRedraw = true;
}
}
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
if (footnotesSelectedIndex < footnotes.getCount() - 1) {
footnotesSelectedIndex++;
needsRedraw = true;
}
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const FootnoteEntry* entry = footnotes.getEntry(footnotesSelectedIndex);
if (entry) {
onSelectFootnote(entry->href);
}
}
if (needsRedraw) {
updateRequired = true;
}
}
void EpubReaderTocActivity::displayTaskLoop() {
while (true) {
if (updateRequired && !subActivity) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
renderScreen();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void EpubReaderTocActivity::renderScreen() {
renderer.clearScreen();
std::vector<TabInfo> tabs = {{"Chapters", currentTab == Tab::CHAPTERS}, {"Footnotes", currentTab == Tab::FOOTNOTES}};
ScreenComponents::drawTabBar(renderer, TAB_BAR_Y, tabs);
const int screenHeight = renderer.getScreenHeight();
const int contentHeight = screenHeight - CONTENT_START_Y - 60;
if (currentTab == Tab::CHAPTERS) {
renderChapters(CONTENT_START_Y, contentHeight);
} else {
renderFootnotes(CONTENT_START_Y, contentHeight);
}
ScreenComponents::drawScrollIndicator(renderer, getCurrentPage(), getTotalPages(), CONTENT_START_Y, contentHeight);
const auto labels = mappedInput.mapLabels("« Back", "Select", "< Tab", "Tab >");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.drawSideButtonHints(UI_10_FONT_ID, ">", "<");
renderer.displayBuffer();
}
void EpubReaderTocActivity::renderChapters(int contentTop, int contentHeight) {
const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getChaptersPageItems(contentHeight);
const int totalItems = getChaptersTotalItems();
const auto pageStartIndex = chaptersSelectorIndex / pageItems * pageItems;
renderer.fillRect(0, contentTop + (chaptersSelectorIndex % pageItems) * CHAPTER_LINE_HEIGHT - 2, pageWidth - 1,
CHAPTER_LINE_HEIGHT);
for (int i = 0; i < pageItems; i++) {
int itemIndex = pageStartIndex + i;
if (itemIndex >= totalItems) break;
const int displayY = contentTop + i * CHAPTER_LINE_HEIGHT;
const bool isSelected = (itemIndex == chaptersSelectorIndex);
if (isSyncItem(itemIndex)) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, ">> Sync Progress", !isSelected);
} else {
int filteredIndex = itemIndex;
if (hasSyncOption()) filteredIndex -= 1;
if (filteredIndex >= 0 && filteredIndex < static_cast<int>(filteredSpineIndices.size())) {
int spineIndex = filteredSpineIndices[filteredIndex];
int tocIndex = this->epub->getTocIndexForSpineIndex(spineIndex);
if (tocIndex == -1) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, "Unnamed", !isSelected);
} else {
auto item = this->epub->getTocItem(tocIndex);
const int indentSize = 20 + (item.level - 1) * 15;
const std::string chapterName =
renderer.truncatedText(UI_10_FONT_ID, item.title.c_str(), pageWidth - 40 - indentSize);
renderer.drawText(UI_10_FONT_ID, indentSize, displayY, chapterName.c_str(), !isSelected);
}
}
}
}
}
void EpubReaderTocActivity::renderFootnotes(int contentTop, int contentHeight) {
const int marginLeft = 20;
if (footnotes.getCount() == 0) {
renderer.drawText(SMALL_FONT_ID, marginLeft, contentTop + 20, "No footnotes on this page");
return;
}
for (int i = 0; i < footnotes.getCount(); i++) {
const FootnoteEntry* entry = footnotes.getEntry(i);
if (!entry) continue;
const int y = contentTop + i * FOOTNOTE_LINE_HEIGHT;
if (i == footnotesSelectedIndex) {
renderer.drawText(UI_12_FONT_ID, marginLeft - 10, y, ">", EpdFontFamily::BOLD);
renderer.drawText(UI_12_FONT_ID, marginLeft + 10, y, entry->number, EpdFontFamily::BOLD);
} else {
renderer.drawText(UI_12_FONT_ID, marginLeft + 10, y, entry->number);
}
}
}
void EpubReaderTocActivity::buildFilteredChapterList() {
filteredSpineIndices.clear();
for (int i = 0; i < this->epub->getSpineItemsCount(); i++) {
if (this->epub->shouldHideFromToc(i)) continue;
int tocIndex = this->epub->getTocIndexForSpineIndex(i);
if (tocIndex == -1) continue;
filteredSpineIndices.push_back(i);
}
}
bool EpubReaderTocActivity::hasSyncOption() const { return KOREADER_STORE.hasCredentials(); }
bool EpubReaderTocActivity::isSyncItem(int index) const {
if (!hasSyncOption()) return false;
return index == 0 || index == getChaptersTotalItems() - 1;
}
int EpubReaderTocActivity::getChaptersTotalItems() const {
const int syncCount = hasSyncOption() ? 2 : 0;
return filteredSpineIndices.size() + syncCount;
}
int EpubReaderTocActivity::getChaptersPageItems(int contentHeight) const {
int items = contentHeight / CHAPTER_LINE_HEIGHT;
return (items < 1) ? 1 : items;
}
int EpubReaderTocActivity::getCurrentPage() const {
if (currentTab == Tab::CHAPTERS) {
const int availableHeight = renderer.getScreenHeight() - 120;
const int itemsPerPage = availableHeight / CHAPTER_LINE_HEIGHT;
return chaptersSelectorIndex / (itemsPerPage > 0 ? itemsPerPage : 1) + 1;
}
return 1;
}
int EpubReaderTocActivity::getTotalPages() const {
if (currentTab == Tab::CHAPTERS) {
const int availableHeight = renderer.getScreenHeight() - 120;
const int itemsPerPage = availableHeight / CHAPTER_LINE_HEIGHT;
const int totalItems = getChaptersTotalItems();
if (totalItems == 0) return 1;
return (totalItems + itemsPerPage - 1) / (itemsPerPage > 0 ? itemsPerPage : 1);
}
return 1;
}

View File

@ -0,0 +1,90 @@
#pragma once
#include <Epub.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <memory>
#include <vector>
#include "../ActivityWithSubactivity.h"
#include "FootnotesData.h"
class EpubReaderTocActivity final : public ActivityWithSubactivity {
public:
enum class Tab { CHAPTERS, FOOTNOTES };
private:
std::shared_ptr<Epub> epub;
std::string epubPath;
const FootnotesData& footnotes;
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
int currentSpineIndex = 0;
int currentPage = 0;
int totalPagesInSpine = 0;
Tab currentTab = Tab::CHAPTERS;
bool updateRequired = false;
// Chapters tab state
int chaptersSelectorIndex = 0;
std::vector<int> filteredSpineIndices;
// Footnotes tab state
int footnotesSelectedIndex = 0;
// Callbacks
const std::function<void()> onGoBack;
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
const std::function<void(const char* href)> onSelectFootnote;
const std::function<void(int newSpineIndex, int newPage)> onSyncPosition;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void renderScreen();
// Tab-specific methods
void loopChapters();
void loopFootnotes();
void renderChapters(int contentTop, int contentHeight);
void renderFootnotes(int contentTop, int contentHeight);
// Chapters helpers
void buildFilteredChapterList();
bool hasSyncOption() const;
bool isSyncItem(int index) const;
int getChaptersTotalItems() const;
int getChaptersPageItems(int contentHeight) const;
int tocIndexFromItemIndex(int itemIndex) const;
// Indicator helpers
int getCurrentPage() const;
int getTotalPages() const;
public:
EpubReaderTocActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::shared_ptr<Epub>& epub_ptr,
const std::string& epubPath, int currentSpineIndex, int currentPage, int totalPagesInSpine,
const FootnotesData& footnotes, std::function<void()> onGoBack,
std::function<void(int)> onSelectSpineIndex, std::function<void(const char*)> onSelectFootnote,
std::function<void(int, int)> onSyncPosition)
: ActivityWithSubactivity("EpubReaderToc", renderer, mappedInput),
epub(epub_ptr),
epubPath(epubPath),
currentSpineIndex(currentSpineIndex),
currentPage(currentPage),
totalPagesInSpine(totalPagesInSpine),
footnotes(footnotes),
onGoBack(onGoBack),
onSelectSpineIndex(onSelectSpineIndex),
onSelectFootnote(onSelectFootnote),
onSyncPosition(onSyncPosition) {}
void onEnter() override;
void onExit() override;
void loop() override;
void launchSyncActivity();
};

View File

@ -0,0 +1,45 @@
#pragma once
#include <Epub/FootnoteEntry.h>
#include <cstring>
class FootnotesData {
private:
FootnoteEntry entries[16];
int count;
public:
FootnotesData() : count(0) {
for (int i = 0; i < 16; i++) {
entries[i].number[0] = '\0';
entries[i].href[0] = '\0';
}
}
void addFootnote(const char* number, const char* href) {
if (count < 16 && number && href) {
strncpy(entries[count].number, number, 2);
entries[count].number[2] = '\0';
strncpy(entries[count].href, href, 63);
entries[count].href[63] = '\0';
count++;
}
}
void clear() {
count = 0;
for (int i = 0; i < 16; i++) {
entries[i].number[0] = '\0';
entries[i].href[0] = '\0';
}
}
int getCount() const { return count; }
const FootnoteEntry* getEntry(int index) const {
if (index >= 0 && index < count) {
return &entries[index];
}
return nullptr;
}
};