Files
Crosspoint/lib/Epub/Epub/Section.cpp
T
2026-05-01 16:53:41 +02:00

834 lines
32 KiB
C++

#include "Section.h"
#include <HalStorage.h>
#include <Logging.h>
#include <Serialization.h>
#include <algorithm>
#include "Epub/css/CssParser.h"
#include "Page.h"
#include "hyphenation/Hyphenator.h"
#include "parsers/ChapterHtmlSlimParser.h"
namespace {
constexpr uint8_t SECTION_FILE_VERSION = 24;
constexpr uint32_t HEADER_SIZE = sizeof(uint8_t) + // SECTION_FILE_VERSION
sizeof(int) + // fontId
sizeof(float) + // lineCompression
sizeof(bool) + // extraParagraphSpacing
sizeof(uint8_t) + // paragraphAlignment
sizeof(uint16_t) + // viewportWidth
sizeof(uint16_t) + // viewportHeight
sizeof(uint16_t) + // pageCount (stored as 16-bit in header)
sizeof(bool) + // hyphenationEnabled
sizeof(bool) + // embeddedStyle
sizeof(bool) + // bionicReadingEnabled
sizeof(uint8_t) + // imageRendering
sizeof(uint32_t) + // page LUT offset
sizeof(uint32_t) + // anchor map offset
sizeof(uint32_t); // paragraph LUT offset
// On-disk paragraph LUT entry: u32 xhtmlByteOffset + u16 paragraphIndex.
constexpr uint32_t PARAGRAPH_LUT_ENTRY_SIZE = sizeof(uint32_t) + sizeof(uint16_t);
inline uint32_t paragraphLutEntryOffset(uint32_t lutStart, uint16_t page) {
return lutStart + page * PARAGRAPH_LUT_ENTRY_SIZE;
}
} // namespace
#include <algorithm>
namespace {
constexpr uint32_t FNV_PRIME = 0x01000193; // 16777619
constexpr uint32_t FNV_OFFSET_BASIS = 0x811C9DC5; // 2166136261
uint32_t fnv1a(const uint8_t* data, size_t length) {
uint32_t hash = FNV_OFFSET_BASIS;
for (size_t i = 0; i < length; ++i) {
hash ^= data[i];
hash *= FNV_PRIME;
}
return hash;
}
} // namespace
uint32_t Section::calculatePropertyHash(int fontId, float lineCompression, bool extraParagraphSpacing,
uint8_t paragraphAlignment, uint16_t viewportWidth, uint16_t viewportHeight,
bool hyphenationEnabled, bool embeddedStyle, bool bionicReadingEnabled,
uint8_t imageRendering) {
uint8_t buffer[64];
size_t offset = 0;
auto append = [&](const void* ptr, size_t size) {
memcpy(buffer + offset, ptr, size);
offset += size;
};
append(&fontId, sizeof(fontId));
append(&lineCompression, sizeof(lineCompression));
append(&extraParagraphSpacing, sizeof(extraParagraphSpacing));
append(&paragraphAlignment, sizeof(paragraphAlignment));
append(&viewportWidth, sizeof(viewportWidth));
append(&viewportHeight, sizeof(viewportHeight));
append(&hyphenationEnabled, sizeof(hyphenationEnabled));
append(&embeddedStyle, sizeof(embeddedStyle));
append(&bionicReadingEnabled, sizeof(bionicReadingEnabled));
append(&imageRendering, sizeof(imageRendering));
return fnv1a(buffer, offset);
}
std::string Section::getSectionFilePath(uint32_t propertyHash) const {
char buf[32];
snprintf(buf, sizeof(buf), "%d_%08x", spineIndex, propertyHash);
return epub->getCachePath() + "/sections/" + buf + ".bin";
}
std::string Section::getImageBasePath(uint32_t propertyHash) const {
char buf[32];
snprintf(buf, sizeof(buf), "img_%d_%08x_", spineIndex, propertyHash);
return epub->getCachePath() + "/" + buf;
}
struct SectionVariant {
std::string filename;
uint16_t date;
uint16_t time;
};
void Section::evictOldVariants() const {
// We keep up to 5 most recently accessed/modified variants to prevent SD card bloat
constexpr size_t MAX_VARIANTS = 5;
std::string sectionsDir = epub->getCachePath() + "/sections";
auto files = Storage.listFiles(sectionsDir.c_str(), 100);
std::vector<SectionVariant> variants;
// Find all cache variants belonging to this spineIndex
char prefix[16];
snprintf(prefix, sizeof(prefix), "%d_", spineIndex);
size_t prefixLen = strlen(prefix);
for (const auto& file : files) {
if (file.startsWith(prefix) && file.endsWith(".bin")) {
HalFile hf = Storage.open((sectionsDir + "/" + file.c_str()).c_str(), O_RDONLY);
if (hf) {
uint16_t md, mt;
if (hf.getModifyDateTime(&md, &mt)) {
variants.push_back({file.c_str(), md, mt});
} else {
// If we can't get modified time, assume it's very old to evict it
variants.push_back({file.c_str(), 0, 0});
}
}
}
}
if (variants.size() <= MAX_VARIANTS) return;
// Sort descending by modified date and time
std::sort(variants.begin(), variants.end(), [](const SectionVariant& a, const SectionVariant& b) {
if (a.date != b.date) return a.date > b.date;
return a.time > b.time;
});
// Delete everything after MAX_VARIANTS limit
for (size_t i = MAX_VARIANTS; i < variants.size(); ++i) {
std::string targetPath = sectionsDir + "/" + variants[i].filename;
Storage.remove(targetPath.c_str());
LOG_DBG("SCT", "Evicted old section cache: %s", targetPath.c_str());
// Extract the hash to also clean up associated images
// Filename format: spineIndex_hash.bin
size_t underscore = variants[i].filename.find('_');
size_t dot = variants[i].filename.find('.');
if (underscore != std::string::npos && dot != std::string::npos && dot > underscore) {
std::string hashStr = variants[i].filename.substr(underscore + 1, dot - underscore - 1);
uint32_t parsedHash = strtoul(hashStr.c_str(), nullptr, 16);
if (parsedHash != 0 || hashStr == "00000000") {
std::string imgBasePath = getImageBasePath(parsedHash);
// Find and delete matching images
auto rootFiles = Storage.listFiles(epub->getCachePath().c_str(), 100);
size_t lastSlash = imgBasePath.find_last_of('/');
std::string imgPrefix = (lastSlash != std::string::npos) ? imgBasePath.substr(lastSlash + 1) : imgBasePath;
for (const auto& rf : rootFiles) {
if (rf.startsWith(imgPrefix.c_str())) {
Storage.remove((epub->getCachePath() + "/" + rf.c_str()).c_str());
LOG_DBG("SCT", "Evicted old image cache: %s", rf.c_str());
}
}
}
}
}
}
uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
if (!file) {
LOG_ERR("SCT", "File not open for writing page %d", pageCount);
return 0;
}
const uint32_t position = file.position();
if (!page->serialize(file)) {
LOG_ERR("SCT", "Failed to serialize page %d", pageCount);
return 0;
}
LOG_DBG("SCT", "Page %d processed", pageCount);
pageCount++;
return position;
}
void Section::writeSectionFileHeader(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
const uint16_t viewportHeight, const bool hyphenationEnabled,
const bool embeddedStyle, const bool bionicReadingEnabled,
const uint8_t imageRendering) {
if (!file) {
LOG_DBG("SCT", "File not open for writing header");
return;
}
static_assert(HEADER_SIZE == sizeof(SECTION_FILE_VERSION) + sizeof(fontId) + sizeof(lineCompression) +
sizeof(extraParagraphSpacing) + sizeof(paragraphAlignment) + sizeof(viewportWidth) +
sizeof(viewportHeight) + sizeof(pageCount) + sizeof(hyphenationEnabled) +
sizeof(embeddedStyle) + sizeof(bionicReadingEnabled) + sizeof(imageRendering) +
sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t),
"Header size mismatch");
serialization::writePod(file, SECTION_FILE_VERSION);
serialization::writePod(file, fontId);
serialization::writePod(file, lineCompression);
serialization::writePod(file, extraParagraphSpacing);
serialization::writePod(file, paragraphAlignment);
serialization::writePod(file, viewportWidth);
serialization::writePod(file, viewportHeight);
serialization::writePod(file, hyphenationEnabled);
serialization::writePod(file, embeddedStyle);
serialization::writePod(file, bionicReadingEnabled);
serialization::writePod(file, imageRendering);
serialization::writePod(file, pageCount); // Placeholder for page count (will be initially 0, patched later)
serialization::writePod(file, static_cast<uint32_t>(0)); // Placeholder for LUT offset (patched later)
serialization::writePod(file, static_cast<uint32_t>(0)); // Placeholder for anchor map offset (patched later)
serialization::writePod(file, static_cast<uint32_t>(0)); // Placeholder for paragraph LUT offset (patched later)
}
bool Section::loadSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
const uint16_t viewportHeight, const bool hyphenationEnabled, const bool embeddedStyle,
const bool bionicReadingEnabled, const uint8_t imageRendering) {
uint32_t propertyHash =
calculatePropertyHash(fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight, hyphenationEnabled, embeddedStyle, bionicReadingEnabled, imageRendering);
filePath = getSectionFilePath(propertyHash);
if (!Storage.openFileForRead("SCT", filePath, file)) {
return false;
}
// Match parameters
{
uint8_t version;
serialization::readPod(file, version);
if (version != SECTION_FILE_VERSION) {
LOG_ERR("SCT", "Deserialization failed: Unknown version %u", version);
clearCache(); // closes file before removal
return false;
}
int fileFontId;
uint16_t fileViewportWidth, fileViewportHeight;
float fileLineCompression;
bool fileExtraParagraphSpacing;
uint8_t fileParagraphAlignment;
bool fileHyphenationEnabled;
bool fileEmbeddedStyle;
bool fileBionicReadingEnabled;
uint8_t fileImageRendering;
serialization::readPod(file, fileFontId);
serialization::readPod(file, fileLineCompression);
serialization::readPod(file, fileExtraParagraphSpacing);
serialization::readPod(file, fileParagraphAlignment);
serialization::readPod(file, fileViewportWidth);
serialization::readPod(file, fileViewportHeight);
serialization::readPod(file, fileHyphenationEnabled);
serialization::readPod(file, fileEmbeddedStyle);
serialization::readPod(file, fileBionicReadingEnabled);
serialization::readPod(file, fileImageRendering);
if (fontId != fileFontId || lineCompression != fileLineCompression ||
extraParagraphSpacing != fileExtraParagraphSpacing || paragraphAlignment != fileParagraphAlignment ||
viewportWidth != fileViewportWidth || viewportHeight != fileViewportHeight ||
hyphenationEnabled != fileHyphenationEnabled || embeddedStyle != fileEmbeddedStyle ||
bionicReadingEnabled != fileBionicReadingEnabled || imageRendering != fileImageRendering) {
LOG_ERR("SCT", "Deserialization failed: Parameters do not match");
clearCache(); // closes file before removal
return false;
}
}
serialization::readPod(file, pageCount);
// Sanity check: same upper bound used by TextBlock::deserialize for word count
if (pageCount > 10000) {
LOG_ERR("SCT", "Deserialization failed: page count %u exceeds maximum", pageCount);
clearCache();
return false;
}
// Load LUT into memory (file is now positioned at the lutOffset field)
uint32_t lutOffset;
serialization::readPod(file, lutOffset);
lut.resize(pageCount);
if (!file.seek(lutOffset)) {
LOG_ERR("SCT", "Deserialization failed: seek to LUT offset %u failed", lutOffset);
clearCache();
return false;
}
for (uint32_t& pos : lut) {
serialization::readPod(file, pos);
if (pos < HEADER_SIZE || pos >= lutOffset) {
LOG_ERR("SCT", "Deserialization failed: LUT entry %u out of range [%u, %u)", pos, HEADER_SIZE, lutOffset);
clearCache();
return false;
}
}
// Build TOC boundaries by scanning anchor data from the still-open file,
// matching only the TOC anchors we need (avoids loading all anchors into memory).
buildTocBoundariesFromFile(file);
// File is intentionally left open; subsequent loadPageFromSectionFile() calls
// seek within this handle instead of re-opening the file each time.
LOG_DBG("SCT", "Deserialization succeeded: %d pages, LUT cached", pageCount);
return true;
}
bool Section::clearCache() {
file.close(); // Must be closed before removal on FAT32
lut.clear();
pageCount = 0;
currentPage = 0;
if (!Storage.exists(filePath.c_str())) {
LOG_DBG("SCT", "Cache does not exist, no action needed");
return true;
}
if (!Storage.remove(filePath.c_str())) {
LOG_ERR("SCT", "Failed to clear cache");
return false;
}
LOG_DBG("SCT", "Cache cleared successfully");
return true;
}
bool Section::createSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
const uint16_t viewportHeight, const bool hyphenationEnabled, const bool embeddedStyle,
const bool bionicReadingEnabled, const uint8_t imageRendering,
const std::function<void(int)>& progressFn) {
uint32_t propertyHash =
calculatePropertyHash(fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight, hyphenationEnabled, embeddedStyle, bionicReadingEnabled, imageRendering);
filePath = getSectionFilePath(propertyHash);
const uint32_t phaseTotalStart = millis();
const auto localPath = epub->getSpineItem(spineIndex).href;
// Create cache directory if it doesn't exist
{
const auto sectionsDir = epub->getCachePath() + "/sections";
Storage.mkdir(sectionsDir.c_str());
}
// Get inflated size up-front so the parser can choose progress granularity.
const uint32_t phaseSetupStart = millis();
size_t inflatedSize = 0;
if (!epub->getItemSize(localPath, &inflatedSize)) {
LOG_ERR("SCT", "Failed to get inflated size for %s", localPath.c_str());
return false;
}
if (!Storage.openFileForWrite("SCT", filePath, file)) {
return false;
}
writeSectionFileHeader(fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight, hyphenationEnabled, embeddedStyle, bionicReadingEnabled, imageRendering);
std::vector<uint32_t> lut = {};
// Derive the content base directory and image cache path prefix for the parser
size_t lastSlash = localPath.find_last_of('/');
std::string contentBase = (lastSlash != std::string::npos) ? localPath.substr(0, lastSlash + 1) : "";
std::string imageBasePath = getImageBasePath(propertyHash);
// Evict old variants for this spine to keep cache size controlled
evictOldVariants();
CssParser* cssParser = nullptr;
if (embeddedStyle) {
cssParser = epub->getCssParser();
if (cssParser) {
if (!cssParser->loadFromCache()) {
LOG_ERR("SCT", "Failed to load CSS from cache");
}
}
}
// Collect TOC anchors for this spine so the parser can insert page breaks at chapter boundaries
std::vector<std::string> tocAnchors;
const int startTocIndex = epub->getTocIndexForSpineIndex(spineIndex);
if (startTocIndex >= 0) {
for (int i = startTocIndex; i < epub->getTocItemsCount(); i++) {
auto entry = epub->getTocItem(i);
if (entry.spineIndex != spineIndex) break;
if (!entry.anchor.empty()) {
tocAnchors.push_back(std::move(entry.anchor));
}
}
}
ChapterHtmlSlimParser visitor(
epub, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth, viewportHeight,
hyphenationEnabled, bionicReadingEnabled,
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
embeddedStyle, contentBase, imageBasePath, imageRendering, std::move(tocAnchors), progressFn, cssParser);
Hyphenator::setPreferredLanguage(epub->getLanguage());
if (!visitor.setup(inflatedSize)) {
LOG_ERR("SCT", "Failed to set up chapter parser");
file.close();
Storage.remove(filePath.c_str());
if (cssParser) {
cssParser->clear();
}
return false;
}
const uint32_t setupMs = millis() - phaseSetupStart;
// Stream EPUB item content directly into the parser — no temp file, no second SD pass.
const uint32_t phaseParseStart = millis();
const bool streamOk = epub->readItemContentsToStream(localPath, visitor, 1024);
const bool finalizeOk = visitor.finalize();
bool success = streamOk && finalizeOk && visitor.streamSucceeded();
const uint32_t parseMs = millis() - phaseParseStart;
// streamMs is no longer a separate phase (SD-write of temp file is gone); keep the
// log breakdown stable by reporting it as 0.
constexpr uint32_t streamMs = 0;
const uint32_t phaseFinalizeStart = millis();
if (!success) {
LOG_ERR("SCT", "Failed to parse XML and build pages (stream=%d finalize=%d)", streamOk ? 1 : 0, finalizeOk ? 1 : 0);
file.close();
Storage.remove(filePath.c_str());
if (cssParser) {
cssParser->clear();
}
return false;
}
const uint32_t fileSize = static_cast<uint32_t>(inflatedSize);
const uint32_t lutOffset = file.position();
bool hasFailedLutRecords = false;
// Write LUT
for (const uint32_t& pos : lut) {
if (pos == 0) {
hasFailedLutRecords = true;
break;
}
serialization::writePod(file, pos);
}
if (hasFailedLutRecords) {
LOG_ERR("SCT", "Failed to write LUT due to invalid page positions");
file.close();
Storage.remove(filePath.c_str());
return false;
}
// Write anchor-to-page map for fragment navigation (TOC + footnote targets)
const uint32_t anchorMapOffset = file.position();
const auto& anchors = visitor.getAnchors();
serialization::writePod(file, static_cast<uint16_t>(anchors.size()));
for (const auto& [anchor, page] : anchors) {
serialization::writeString(file, anchor);
serialization::writePod(file, page);
}
// Write per-page paragraph LUT: count + array of {xhtmlByteOffset(u32), paragraphIndex(u16)}.
// The byte offset lets findXPathForParagraph seek near the target paragraph without scanning
// from the beginning of the XHTML file, reducing SD reads on large chapters.
const uint32_t paragraphLutOffset = file.position();
const auto& paragraphLut = visitor.getParagraphLutPerPage();
if (paragraphLut.size() != static_cast<size_t>(pageCount)) {
LOG_ERR("SCT", "Paragraph LUT size mismatch: lut=%u pageCount=%u", static_cast<uint32_t>(paragraphLut.size()),
static_cast<uint32_t>(pageCount));
file.close();
Storage.remove(filePath.c_str());
return false;
}
serialization::writePod(file, static_cast<uint16_t>(paragraphLut.size()));
for (const auto& entry : paragraphLut) {
serialization::writePod(file, entry.xhtmlByteOffset);
serialization::writePod(file, entry.paragraphIndex);
}
// Patch header with final pageCount, lutOffset, anchorMapOffset, and paragraphLutOffset
file.seek(HEADER_SIZE - sizeof(uint32_t) * 3 - sizeof(pageCount));
serialization::writePod(file, pageCount);
serialization::writePod(file, lutOffset);
serialization::writePod(file, anchorMapOffset);
serialization::writePod(file, paragraphLutOffset);
file.close();
if (cssParser) {
cssParser->clear();
}
buildTocBoundaries(anchors);
// Cache the LUT in memory and open the file for reading so that
// subsequent loadPageFromSectionFile() calls can seek directly without re-opening.
if (!Storage.openFileForRead("SCT", filePath, file)) {
LOG_ERR("SCT", "Failed to open section file for reading after creation");
return false;
}
this->lut = std::move(lut);
const uint32_t finalizeMs = millis() - phaseFinalizeStart;
const uint32_t totalMs = millis() - phaseTotalStart;
LOG_DBG("SCT", "createSectionFile spine=%d total=%ums (stream=%u setup=%u parse=%u finalize=%u) pages=%u bytes=%u",
spineIndex, totalMs, streamMs, setupMs, parseMs, finalizeMs, pageCount, fileSize);
return true;
}
std::unique_ptr<Page> Section::loadPageFromSectionFile() {
if (currentPage < 0 || currentPage >= static_cast<int>(lut.size())) {
LOG_ERR("SCT", "loadPageFromSectionFile: page %d out of LUT range (%u entries)", currentPage,
static_cast<uint32_t>(lut.size()));
return nullptr;
}
if (!file) {
// Safety fallback: file was closed unexpectedly; reopen
LOG_ERR("SCT", "loadPageFromSectionFile: file not open, reopening");
if (!Storage.openFileForRead("SCT", filePath, file)) {
return nullptr;
}
}
if (!file.seek(lut[currentPage])) {
LOG_ERR("SCT", "loadPageFromSectionFile: seek to page %d offset %u failed", currentPage, lut[currentPage]);
return nullptr;
}
return Page::deserialize(file);
// File is intentionally NOT closed; stays open for the next page load
}
// Resolve TOC anchor-to-page mappings from the parser's in-memory anchor vector.
// Called after createSectionFile when anchors are already in memory.
// See buildTocBoundariesFromFile for the on-disk variant; the two are kept separate
// because the anchor resolution has fundamentally different iteration patterns
// (scan in-memory vector vs. stream from file with early exit).
void Section::buildTocBoundaries(const std::vector<std::pair<std::string, uint16_t>>& anchors) {
const int startTocIndex = epub->getTocIndexForSpineIndex(spineIndex);
if (startTocIndex < 0) return;
// Count TOC entries for this spine and how many have anchors to resolve
const int tocCount = epub->getTocItemsCount();
uint16_t totalEntries = 0;
uint16_t unresolvedCount = 0;
for (int i = startTocIndex; i < tocCount; i++) {
const auto entry = epub->getTocItem(i);
if (entry.spineIndex != spineIndex) break;
totalEntries++;
if (!entry.anchor.empty()) unresolvedCount++;
}
// If no TOC entries have anchors, all chapters start at page 0 and
// getTocIndexForPage falls back to epub->getTocIndexForSpineIndex,
// so there's nothing to resolve and no value in storing boundaries.
if (totalEntries == 0 || unresolvedCount == 0) return;
tocBoundaries.reserve(totalEntries);
for (int i = startTocIndex; i < startTocIndex + totalEntries; i++) {
const auto entry = epub->getTocItem(i);
uint16_t page = 0;
if (!entry.anchor.empty()) {
for (const auto& [key, val] : anchors) {
if (key == entry.anchor) {
page = val;
break;
}
}
}
tocBoundaries.push_back({i, page});
}
// Defensive sort in case TOC entries are out of document order in a malformed epub
std::sort(tocBoundaries.begin(), tocBoundaries.end(),
[](const TocBoundary& a, const TocBoundary& b) { return a.startPage < b.startPage; });
}
// Resolve TOC anchor-to-page mappings by scanning the section cache's on-disk anchor data.
// Called from loadSectionFile when anchors are not in memory. Caches the small set of
// TOC anchor strings first (since getTocItem does file I/O to BookMetadataCache), then
// streams through on-disk anchors matching only those, stopping as soon as all are found.
// See buildTocBoundaries for the in-memory variant.
void Section::buildTocBoundariesFromFile(FsFile& f) {
const int startTocIndex = epub->getTocIndexForSpineIndex(spineIndex);
if (startTocIndex < 0) return;
// Count TOC entries for this spine, then reserve and populate
const int tocCount = epub->getTocItemsCount();
uint16_t totalEntries = 0;
uint16_t unresolvedCount = 0;
for (int i = startTocIndex; i < tocCount; i++) {
const auto entry = epub->getTocItem(i);
if (entry.spineIndex != spineIndex) break;
totalEntries++;
if (!entry.anchor.empty()) unresolvedCount++;
}
// If no TOC entries have anchors, all chapters start at page 0 and
// getTocIndexForPage falls back to epub->getTocIndexForSpineIndex,
// so there's nothing to resolve and no value in storing boundaries.
if (totalEntries == 0 || unresolvedCount == 0) return;
// Cache TOC anchor strings before scanning disk, since getTocItem() does file I/O
struct TocAnchorEntry {
int tocIndex;
std::string anchor;
};
std::vector<TocAnchorEntry> tocAnchorsToResolve;
tocAnchorsToResolve.reserve(unresolvedCount);
tocBoundaries.reserve(totalEntries);
for (int i = startTocIndex; i < startTocIndex + totalEntries; i++) {
const auto entry = epub->getTocItem(i);
tocBoundaries.push_back({i, 0});
if (!entry.anchor.empty()) {
tocAnchorsToResolve.push_back({i, std::move(entry.anchor)});
}
}
// Single pass through on-disk anchors, matching against cached TOC anchors.
// Stop early once all TOC anchors are resolved.
// Header layout: ... | lutOffset (u32) | anchorMapOffset (u32) | paragraphLutOffset (u32) |
f.seek(HEADER_SIZE - sizeof(uint32_t) * 2);
uint32_t anchorMapOffset;
serialization::readPod(f, anchorMapOffset);
if (anchorMapOffset != 0) {
f.seek(anchorMapOffset);
uint16_t count;
serialization::readPod(f, count);
std::string key;
for (uint16_t i = 0; i < count && unresolvedCount > 0; i++) {
uint16_t page;
serialization::readString(f, key);
serialization::readPod(f, page);
for (auto& tocAnchor : tocAnchorsToResolve) {
if (!tocAnchor.anchor.empty() && key == tocAnchor.anchor) {
tocBoundaries[tocAnchor.tocIndex - startTocIndex].startPage = page;
tocAnchor.anchor.clear(); // mark resolved
unresolvedCount--;
break;
}
}
}
}
// Defensive sort in case TOC entries are out of document order in a malformed epub
std::sort(tocBoundaries.begin(), tocBoundaries.end(),
[](const TocBoundary& a, const TocBoundary& b) { return a.startPage < b.startPage; });
}
int Section::getTocIndexForPage(const int page) const {
if (tocBoundaries.empty()) {
return epub->getTocIndexForSpineIndex(spineIndex);
}
// Find the first boundary AFTER page, then step back one
auto it = std::upper_bound(tocBoundaries.begin(), tocBoundaries.end(), static_cast<uint16_t>(page),
[](uint16_t page, const TocBoundary& boundary) { return page < boundary.startPage; });
if (it == tocBoundaries.begin()) {
return tocBoundaries[0].tocIndex;
}
return std::prev(it)->tocIndex;
}
std::optional<int> Section::getPageForTocIndex(const int tocIndex) const {
for (const auto& boundary : tocBoundaries) {
if (boundary.tocIndex == tocIndex) {
return boundary.startPage;
}
}
return std::nullopt;
}
std::optional<Section::TocPageRange> Section::getPageRangeForTocIndex(const int tocIndex) const {
for (size_t i = 0; i < tocBoundaries.size(); i++) {
if (tocBoundaries[i].tocIndex == tocIndex) {
const int startPage = tocBoundaries[i].startPage;
const int endPage = (i + 1 < tocBoundaries.size()) ? static_cast<int>(tocBoundaries[i + 1].startPage) : pageCount;
return TocPageRange{startPage, endPage};
}
}
return std::nullopt;
}
std::optional<uint16_t> Section::getPageForAnchor(const std::string& anchor) const {
FsFile f;
if (!Storage.openFileForRead("SCT", filePath, f)) {
return std::nullopt;
}
const uint32_t fileSize = f.size();
f.seek(HEADER_SIZE - sizeof(uint32_t) * 2);
uint32_t anchorMapOffset;
serialization::readPod(f, anchorMapOffset);
if (anchorMapOffset == 0 || anchorMapOffset >= fileSize) {
f.close();
return std::nullopt;
}
f.seek(anchorMapOffset);
uint16_t count;
serialization::readPod(f, count);
for (uint16_t i = 0; i < count; i++) {
std::string key;
uint16_t page;
serialization::readString(f, key);
serialization::readPod(f, page);
if (key == anchor) {
f.close();
return page;
}
}
f.close();
return std::nullopt;
}
bool Section::readParagraphLutHeader(FsFile& outFile, uint16_t& outCount, uint32_t& outLutStart) const {
if (!Storage.openFileForRead("SCT", filePath, outFile)) {
return false;
}
const uint32_t fileSize = outFile.size();
outFile.seek(HEADER_SIZE - sizeof(uint32_t));
uint32_t paragraphLutOffset;
serialization::readPod(outFile, paragraphLutOffset);
if (fileSize < sizeof(uint16_t) || paragraphLutOffset == 0 || paragraphLutOffset > fileSize - sizeof(uint16_t)) {
outFile.close();
return false;
}
outFile.seek(paragraphLutOffset);
serialization::readPod(outFile, outCount);
if (outCount == 0) {
outFile.close();
return false;
}
const uint64_t remainingBytes = static_cast<uint64_t>(fileSize) - paragraphLutOffset;
const uint64_t requiredBytes = sizeof(uint16_t) + static_cast<uint64_t>(outCount) * PARAGRAPH_LUT_ENTRY_SIZE;
if (remainingBytes < requiredBytes) {
outFile.close();
return false;
}
outLutStart = paragraphLutOffset + sizeof(uint16_t);
return true;
}
std::optional<uint16_t> Section::getPageForParagraphIndex(const uint16_t pIndex) const {
FsFile f;
uint16_t count = 0;
uint32_t lutStart = 0;
if (!readParagraphLutHeader(f, count, lutStart)) {
return std::nullopt;
}
const uint32_t fileSize = f.size();
// Each LUT entry stores the paragraph index at page-break time — i.e. the last
// <p> whose start tag had been seen while page i was being laid out. Paragraph
// P therefore first appears on the smallest i where storedPIdx[i] >= P.
for (uint16_t i = 0; i < count; i++) {
const uint32_t entryOffset = paragraphLutEntryOffset(lutStart, i) + sizeof(uint32_t);
const uint64_t requiredOffset = static_cast<uint64_t>(entryOffset) + sizeof(uint16_t);
if (requiredOffset > fileSize) {
f.close();
return std::nullopt;
}
f.seek(entryOffset);
uint16_t pagePIdx;
serialization::readPod(f, pagePIdx);
if (pagePIdx >= pIndex) {
f.close();
return i;
}
}
f.close();
return static_cast<uint16_t>(count - 1);
}
std::optional<uint16_t> Section::getParagraphIndexForPage(const uint16_t page) const {
FsFile f;
uint16_t count = 0;
uint32_t lutStart = 0;
if (!readParagraphLutHeader(f, count, lutStart)) {
return std::nullopt;
}
if (page >= count) {
f.close();
return std::nullopt;
}
const uint32_t fileSize = f.size();
const uint32_t entryOffset = paragraphLutEntryOffset(lutStart, page) + sizeof(uint32_t);
const uint64_t requiredOffset = static_cast<uint64_t>(entryOffset) + sizeof(uint16_t);
if (requiredOffset > fileSize) {
f.close();
return std::nullopt;
}
// Seek directly to the paragraphIndex field of the requested entry (skip xhtmlByteOffset)
f.seek(entryOffset);
uint16_t pIdx;
serialization::readPod(f, pIdx);
f.close();
return pIdx;
}
std::optional<uint32_t> Section::getXhtmlByteOffsetForPage(const uint16_t page) const {
FsFile f;
uint16_t count = 0;
uint32_t lutStart = 0;
if (!readParagraphLutHeader(f, count, lutStart)) {
return std::nullopt;
}
if (page >= count) {
f.close();
return std::nullopt;
}
const uint32_t fileSize = f.size();
const uint32_t entryOffset = paragraphLutEntryOffset(lutStart, page);
const uint64_t requiredOffset = static_cast<uint64_t>(entryOffset) + sizeof(uint32_t);
if (requiredOffset > fileSize) {
f.close();
return std::nullopt;
}
f.seek(entryOffset);
uint32_t byteOffset;
serialization::readPod(f, byteOffset);
f.close();
// A zero offset means the entry was recorded post-parse (last page), so it's unusable as a hint.
return byteOffset > 0 ? std::optional<uint32_t>{byteOffset} : std::nullopt;
}