From 5e4f78f7bef3c796108e530ea2e9afd60aba0e2f Mon Sep 17 00:00:00 2001 From: jpirnay Date: Mon, 20 Apr 2026 12:28:00 +0200 Subject: [PATCH] Finetuning --- lib/Epub/Epub.cpp | 9 +++++---- lib/Epub/Epub.h | 2 +- lib/Epub/Epub/Section.cpp | 10 ++++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index baf1b2e7..4ee69bae 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -45,7 +45,7 @@ bool Epub::findContentOpfFile(std::string* contentOpfFile) const { return true; } -bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata) { +bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata, bool useCache) { std::string contentOpfFilePath; if (!findContentOpfFile(&contentOpfFilePath)) { LOG_ERR("EBP", "Could not find content.opf in zip"); @@ -62,7 +62,8 @@ bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata) { return false; } - ContentOpfParser opfParser(getCachePath(), getBasePath(), contentOpfSize, bookMetadataCache.get()); + ContentOpfParser opfParser(getCachePath(), getBasePath(), contentOpfSize, + useCache ? bookMetadataCache.get() : nullptr); if (!opfParser.setup()) { LOG_ERR("EBP", "Could not setup content.opf parser"); return false; @@ -352,7 +353,7 @@ bool Epub::load(const bool buildIfMissing, const bool skipLoadingCss) { LOG_DBG("EBP", "CSS rules cache missing or stale, attempting to parse CSS files"); cssParser->deleteCache(); - if (!parseContentOpf(bookMetadataCache->coreMetadata)) { + if (!parseContentOpf(bookMetadataCache->coreMetadata, false)) { LOG_ERR("EBP", "Could not parse content.opf from cached bookMetadata for CSS files"); // continue anyway - book will work without CSS and we'll still load any inline style CSS } @@ -389,7 +390,7 @@ bool Epub::load(const bool buildIfMissing, const bool skipLoadingCss) { LOG_ERR("EBP", "Could not begin writing content.opf pass"); return false; } - if (!parseContentOpf(bookMetadata)) { + if (!parseContentOpf(bookMetadata, true)) { LOG_ERR("EBP", "Could not parse content.opf"); return false; } diff --git a/lib/Epub/Epub.h b/lib/Epub/Epub.h index 77d52a04..201188f3 100644 --- a/lib/Epub/Epub.h +++ b/lib/Epub/Epub.h @@ -35,7 +35,7 @@ class Epub { bool syntheticTocFallbackEnabled = false; bool findContentOpfFile(std::string* contentOpfFile) const; - bool parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata); + bool parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata, bool useCache = true); bool parseTocNcxFile() const; bool parseTocNavFile() const; void parseCssFiles() const; diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 788498db..06ff3420 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -584,17 +584,19 @@ std::optional Section::getPageForParagraphIndex(const uint16_t pIndex) return std::nullopt; } - // Find the first page whose paragraph index >= pIndex. - uint16_t resultPage = count - 1; // default to last page + // Find the page that contains the requested paragraph index. + // Each entry stores the first paragraph index for that page, so the page + // containing pIndex is the last page whose start paragraph is <= pIndex. + uint16_t resultPage = 0; for (uint16_t i = 0; i < count; i++) { uint32_t byteOffset; uint16_t pagePIdx; serialization::readPod(f, byteOffset); serialization::readPod(f, pagePIdx); - if (pagePIdx >= pIndex) { - resultPage = i; + if (pagePIdx > pIndex) { break; } + resultPage = i; } f.close();