Integrate PT 1455

This commit is contained in:
jpirnay
2026-03-26 11:16:53 +01:00
parent 33347d5b96
commit 1d3405318c
13 changed files with 2572 additions and 27 deletions
+175 -2
View File
@@ -4,6 +4,8 @@
#include <Logging.h>
#include <Serialization.h>
#include <algorithm>
#include "Epub/css/CssParser.h"
#include "Page.h"
#include "hyphenation/Hyphenator.h"
@@ -147,6 +149,10 @@ bool Section::loadSectionFile(const int fontId, const float lineCompression, con
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);
@@ -244,11 +250,24 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
}
}
// 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, tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight, hyphenationEnabled,
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
embeddedStyle, contentBase, imageBasePath, imageRendering, progressFn, cssParser);
embeddedStyle, contentBase, imageBasePath, imageRendering, std::move(tocAnchors), progressFn, cssParser);
Hyphenator::setPreferredLanguage(epub->getLanguage());
success = visitor.parseAndBuildPages();
@@ -281,7 +300,7 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
return false;
}
// Write anchor-to-page map for fragment navigation (e.g. footnote targets)
// 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()));
@@ -309,6 +328,8 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
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)) {
@@ -342,6 +363,158 @@ std::unique_ptr<Page> Section::loadPageFromSectionFile() {
// 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)) {
+22
View File
@@ -23,6 +23,15 @@ class Section {
bool embeddedStyle, uint8_t imageRendering);
uint32_t onPageComplete(std::unique_ptr<Page> page);
struct TocBoundary {
int tocIndex = 0;
uint16_t startPage = 0;
};
std::vector<TocBoundary> tocBoundaries;
void buildTocBoundaries(const std::vector<std::pair<std::string, uint16_t>>& anchors);
void buildTocBoundariesFromFile(FsFile& f);
public:
uint16_t pageCount = 0;
int currentPage = 0;
@@ -42,6 +51,19 @@ class Section {
uint8_t imageRendering, const std::function<void(int)>& progressFn = nullptr);
std::unique_ptr<Page> loadPageFromSectionFile();
// Given a page in this section, return the TOC index for that page.
int getTocIndexForPage(int page) const;
// Given a TOC index, return the start page in this section.
// Returns nullopt if the TOC index doesn't map to a boundary in this spine (e.g. belongs to a different spine).
std::optional<int> getPageForTocIndex(int tocIndex) const;
struct TocPageRange {
int startPage; // inclusive
int endPage; // exclusive
};
// Returns the page range [start, end) within this spine that belongs to the given TOC index.
std::optional<TocPageRange> getPageRangeForTocIndex(int tocIndex) const;
// Look up the page number for an anchor id from the section cache file.
std::optional<uint16_t> getPageForAnchor(const std::string& anchor) const;
@@ -7,6 +7,8 @@
#include <Utf8.h>
#include <expat.h>
#include <algorithm>
#include "../../Epub.h"
#include "../Page.h"
#include "../converters/ImageDecoderFactory.h"
@@ -146,15 +148,35 @@ void ChapterHtmlSlimParser::startNewTextBlock(const BlockStyle& blockStyle) {
currentTextBlock->setBlockStyle(currentTextBlock->getBlockStyle().getCombinedBlockStyle(incoming));
if (!pendingAnchorId.empty()) {
if (std::find(tocAnchors.begin(), tocAnchors.end(), pendingAnchorId) != tocAnchors.end()) {
if (currentPage && !currentPage->elements.empty()) {
completePageFn(std::move(currentPage));
completedPageCount++;
currentPage.reset(new Page());
currentPageNextY = 0;
}
}
anchorData.push_back({std::move(pendingAnchorId), static_cast<uint16_t>(completedPageCount)});
pendingAnchorId.clear();
}
wordsExtractedInBlock = 0;
return;
}
makePages();
}
// Record deferred anchor after previous block is flushed
// If the pending anchor is a TOC chapter boundary, force a page break after the previous
// block is flushed so the chapter starts on a fresh page.
if (!pendingAnchorId.empty() &&
std::find(tocAnchors.begin(), tocAnchors.end(), pendingAnchorId) != tocAnchors.end()) {
if (currentPage && !currentPage->elements.empty()) {
completePageFn(std::move(currentPage));
completedPageCount++;
currentPage.reset(new Page());
currentPageNextY = 0;
}
}
// Record deferred anchor after previous block is flushed (and any TOC page break)
if (!pendingAnchorId.empty()) {
anchorData.push_back({std::move(pendingAnchorId), static_cast<uint16_t>(completedPageCount)});
pendingAnchorId.clear();
@@ -182,7 +204,8 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
} else if (strcmp(atts[i], "style") == 0) {
styleAttr = atts[i + 1];
} else if (strcmp(atts[i], "id") == 0) {
// Defer recording until startNewTextBlock, after previous block is flushed to pages
// Defer both anchor recording and TOC page breaks until startNewTextBlock,
// after the previous block is flushed to pages via makePages().
self->pendingAnchorId = atts[i + 1];
}
}
@@ -74,6 +74,7 @@ class ChapterHtmlSlimParser {
int completedPageCount = 0;
std::vector<std::pair<std::string, uint16_t>> anchorData;
std::string pendingAnchorId; // deferred until after previous text block is flushed
std::vector<std::string> tocAnchors;
// Paragraph index tracking for XPath-to-page lookup table.
// Counts <p> sibling indices (1-based, matching XPath convention) during page building.
@@ -110,6 +111,7 @@ class ChapterHtmlSlimParser {
const std::function<void(std::unique_ptr<Page>)>& completePageFn,
const bool embeddedStyle, const std::string& contentBase,
const std::string& imageBasePath, const uint8_t imageRendering = 0,
std::vector<std::string> tocAnchors = {},
const std::function<void(int)>& progressFn = nullptr,
const CssParser* cssParser = nullptr)
@@ -129,7 +131,8 @@ class ChapterHtmlSlimParser {
embeddedStyle(embeddedStyle),
imageRendering(imageRendering),
contentBase(contentBase),
imageBasePath(imageBasePath) {}
imageBasePath(imageBasePath),
tocAnchors(std::move(tocAnchors)) {}
~ChapterHtmlSlimParser() = default;
bool parseAndBuildPages();