From ead39fd04b9035fc75f102e0f2f4d2f1ecd7ff83 Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Sat, 13 Dec 2025 21:17:22 +1100 Subject: [PATCH] Return -1 from getTocIndexForSpineIndex if TOC item does not exist --- lib/Epub/Epub.cpp | 3 +-- src/screens/EpubReaderScreen.cpp | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index 80a28d9..f9273a9 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -299,6 +299,5 @@ int Epub::getTocIndexForSpineIndex(const int spineIndex) const { } Serial.printf("[%lu] [EBP] TOC item not found\n", millis()); - // not found - default to first item - return 0; + return -1; } diff --git a/src/screens/EpubReaderScreen.cpp b/src/screens/EpubReaderScreen.cpp index b9542d1..f9ef87f 100644 --- a/src/screens/EpubReaderScreen.cpp +++ b/src/screens/EpubReaderScreen.cpp @@ -328,12 +328,21 @@ void EpubReaderScreen::renderStatusBar() const { const int titleMarginLeft = 20 + percentageTextWidth + 30 + marginLeft; const int titleMarginRight = progressTextWidth + 30 + marginRight; const int availableTextWidth = GfxRenderer::getScreenWidth() - titleMarginLeft - titleMarginRight; - const auto tocItem = epub->getTocItem(epub->getTocIndexForSpineIndex(currentSpineIndex)); - auto title = tocItem.title; - int titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str()); - while (titleWidth > availableTextWidth) { - title = title.substr(0, title.length() - 8) + "..."; + const int tocIndex = epub->getTocIndexForSpineIndex(currentSpineIndex); + + std::string title; + int titleWidth; + if (tocIndex == -1) { + title = "Unnamed"; + titleWidth = renderer.getTextWidth(SMALL_FONT_ID, "Unnamed"); + } else { + const auto tocItem = epub->getTocItem(tocIndex); + title = tocItem.title; titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str()); + while (titleWidth > availableTextWidth) { + title = title.substr(0, title.length() - 8) + "..."; + titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str()); + } } renderer.drawText(SMALL_FONT_ID, titleMarginLeft + (availableTextWidth - titleWidth) / 2, textY, title.c_str());