Integrate PT 1455
This commit is contained in:
+175
-2
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user