From 91a067750323af75d26073032959f5acb159ad2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Launay?= Date: Wed, 17 Dec 2025 23:46:01 +0100 Subject: [PATCH] =?UTF-8?q?Fix:=20Footnotes=20crashes=20after=20Screen?= =?UTF-8?q?=E2=86=92Activity=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/activities/reader/EpubReaderActivity.cpp | 10 +++++-- .../reader/EpubReaderFootnotesActivity.h | 29 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/activities/reader/EpubReaderActivity.cpp b/src/activities/reader/EpubReaderActivity.cpp index 636dc84..4eb6c71 100644 --- a/src/activities/reader/EpubReaderActivity.cpp +++ b/src/activities/reader/EpubReaderActivity.cpp @@ -46,7 +46,6 @@ void EpubReaderActivity::onEnter() { nextPageNumber = data[2] + (data[3] << 8); Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber); f.close(); - f.close(); } // Trigger first update @@ -322,11 +321,16 @@ void EpubReaderActivity::renderScreen() { return renderScreen(); } + Serial.printf("[%lu] [ERS] Page loaded: %d elements, %d footnotes\n", + millis(), p->elementCount, p->footnoteCount); + // Copy footnotes from page to currentPageFootnotes currentPageFootnotes.clear(); - for (int i = 0; i < p->footnoteCount && i < 16; i++) { + int maxFootnotes = (p->footnoteCount < 16) ? p->footnoteCount : 16; + + for (int i = 0; i < maxFootnotes; i++) { FootnoteEntry* footnote = p->getFootnote(i); - if (footnote) { + if (footnote && footnote->href[0] != '\0') { currentPageFootnotes.addFootnote(footnote->number, footnote->href); } } diff --git a/src/activities/reader/EpubReaderFootnotesActivity.h b/src/activities/reader/EpubReaderFootnotesActivity.h index 7542633..a771f69 100644 --- a/src/activities/reader/EpubReaderFootnotesActivity.h +++ b/src/activities/reader/EpubReaderFootnotesActivity.h @@ -7,15 +7,20 @@ #include "../Activity.h" class FootnotesData { - private: - FootnoteEntry entries[32]; +private: + FootnoteEntry entries[16]; int count; - public: - FootnotesData() : count(0) {} +public: + FootnotesData() : count(0) { + for (int i = 0; i < 16; i++) { + entries[i].number[0] = '\0'; + entries[i].href[0] = '\0'; + } + } void addFootnote(const char* number, const char* href) { - if (count < 32) { + if (count < 16 && number && href) { strncpy(entries[count].number, number, 2); entries[count].number[2] = '\0'; strncpy(entries[count].href, href, 63); @@ -24,7 +29,13 @@ class FootnotesData { } } - void clear() { count = 0; } + void clear() { + count = 0; + for (int i = 0; i < 16; i++) { + entries[i].number[0] = '\0'; + entries[i].href[0] = '\0'; + } + } int getCount() const { return count; } @@ -42,7 +53,7 @@ class EpubReaderFootnotesActivity final : public Activity { const std::function onSelectFootnote; int selectedIndex; - public: +public: EpubReaderFootnotesActivity(GfxRenderer& renderer, InputManager& inputManager, const FootnotesData& footnotes, const std::function& onGoBack, const std::function& onSelectFootnote) @@ -56,6 +67,6 @@ class EpubReaderFootnotesActivity final : public Activity { void onExit() override; void loop() override; - private: +private: void render(); -}; +}; \ No newline at end of file