Return -1 from getTocIndexForSpineIndex if TOC item does not exist

This commit is contained in:
Dave Allie 2025-12-13 21:17:22 +11:00
parent 5a7381a0eb
commit ead39fd04b
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
2 changed files with 15 additions and 7 deletions

View File

@ -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;
}

View File

@ -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());