From 8cae186cc12f41d001ac7b6cefa58ea17b742734 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Sun, 17 May 2026 12:29:56 +0200 Subject: [PATCH] Fix css mismatch after koreader sync --- lib/Epub/Epub/Section.cpp | 11 ++++++++- lib/Epub/Epub/Section.h | 2 ++ src/activities/reader/EpubReaderActivity.cpp | 24 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 40d40993..bd6df71f 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -60,7 +60,7 @@ constexpr uint32_t FNV_OFFSET_BASIS = 0x811C9DC5; // 2166136261 #endif #ifndef SCT_EMBEDDED_STYLE_MIN_CONTIG_HEAP_BYTES -#define SCT_EMBEDDED_STYLE_MIN_CONTIG_HEAP_BYTES (56 * 1024) +#define SCT_EMBEDDED_STYLE_MIN_CONTIG_HEAP_BYTES (36 * 1024) #endif constexpr uint32_t EMBEDDED_STYLE_MIN_FREE_HEAP_BYTES = SCT_EMBEDDED_STYLE_MIN_FREE_HEAP_BYTES; @@ -244,6 +244,7 @@ bool Section::loadSectionFile(const int fontId, const float lineCompression, con const uint16_t viewportHeight, const bool hyphenationEnabled, const bool embeddedStyle, const bool bionicReadingEnabled, const uint8_t imageRendering) { truncatedCache = false; + embeddedStyleFallback = false; uint32_t propertyHash = calculatePropertyHash(fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth, viewportHeight, hyphenationEnabled, embeddedStyle, bionicReadingEnabled, imageRendering); @@ -260,6 +261,7 @@ bool Section::loadSectionFile(const int fontId, const float lineCompression, con if (Storage.openFileForRead("SCT", fallbackPath, file)) { filePath = fallbackPath; usingEmbeddedStyleFallback = true; + embeddedStyleFallback = true; LOG_INF("SCT", "Using no-CSS section cache fallback: %s", filePath.c_str()); } else { return false; @@ -419,6 +421,13 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c return false; } + // Reset build state — createSectionFile may be called on a Section that previously + // loaded a cache (e.g. fallback no-CSS file). pageCount must start at 0 so that + // onPageComplete() numbering and paragraphLutPerPage stay in lockstep. + file.close(); + pageCount = 0; + this->lut.clear(); + if (!Storage.openFileForWrite("SCT", filePath, file)) { return false; } diff --git a/lib/Epub/Epub/Section.h b/lib/Epub/Epub/Section.h index 08be6a35..5a0fec43 100644 --- a/lib/Epub/Epub/Section.h +++ b/lib/Epub/Epub/Section.h @@ -18,6 +18,7 @@ class Section { FsFile file; std::vector lut; // Cached page byte-offsets; loaded once, avoids per-page LUT seek bool truncatedCache = false; + bool embeddedStyleFallback = false; void writeSectionFileHeader(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment, uint16_t viewportWidth, uint16_t viewportHeight, bool hyphenationEnabled, @@ -70,6 +71,7 @@ class Section { const std::function& progressFn = nullptr, bool skipEviction = false); std::unique_ptr loadPageFromSectionFile(); bool isTruncatedCache() const { return truncatedCache; } + bool isEmbeddedStyleFallback() const { return embeddedStyleFallback; } // Given a page in this section, return the TOC index for that page. int getTocIndexForPage(int page) const; diff --git a/src/activities/reader/EpubReaderActivity.cpp b/src/activities/reader/EpubReaderActivity.cpp index af332c66..3eb31507 100644 --- a/src/activities/reader/EpubReaderActivity.cpp +++ b/src/activities/reader/EpubReaderActivity.cpp @@ -1604,6 +1604,30 @@ void EpubReaderActivity::render(RenderLock&& lock) { section.reset(); return; } + } else if (section->isEmbeddedStyleFallback()) { + LOG_INF("ERS", "No-CSS fallback loaded; rebuilding with embedded CSS..."); + lastRenderStats.cacheRebuilt = true; + + Rect popupRect{}; + const auto progressFn = [this, &popupRect](int progress) { + if (popupRect.width == 0) { + popupRect = GUI.drawPopup(renderer, tr(STR_INDEXING)); + return; + } + GUI.fillPopupProgress(renderer, popupRect, progress); + }; + + renderer.clearSdCardFontAccumulation(); + if (!section->createSectionFile(getEffectiveReaderFontId(), getEffectiveReaderLineCompression(), + SETTINGS.extraParagraphSpacing, getEffectiveParagraphAlignment(), viewportWidth, + viewportHeight, SETTINGS.hyphenationEnabled, embeddedStyle, + bookBionicReadingOverride, imageRendering, progressFn)) { + LOG_ERR("ERS", "Failed to rebuild CSS section cache; keeping fallback"); + section->loadSectionFile(getEffectiveReaderFontId(), getEffectiveReaderLineCompression(), + SETTINGS.extraParagraphSpacing, getEffectiveParagraphAlignment(), viewportWidth, + viewportHeight, SETTINGS.hyphenationEnabled, embeddedStyle, bookBionicReadingOverride, + imageRendering); + } } else { LOG_DBG("ERS", "Cache found, skipping build..."); }