Enhance TOC parsing and chapter selection logic

- Update .gitignore to include additional paths
- Refactor Epub::parseContentOpf to improve NCX item retrieval
- Modify ContentOpfParser to store media type in ManifestItem
- Implement rebuildVisibleSpineIndices in EpubReaderChapterSelectionScreen for better chapter navigation
- Adjust rendering logic to handle empty chapter lists gracefully
This commit is contained in:
Arthur Tazhitdinov 2025-12-15 19:39:07 +03:00
parent 6989035ef8
commit 7e28af02d1
6 changed files with 130 additions and 25 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.pio .pio
.idea .idea
.DS_Store .DS_Store
.vscode
lib/EpdFont/fontsrc

View File

@ -69,16 +69,31 @@ bool Epub::parseContentOpf(const std::string& contentOpfFilePath) {
// Grab data from opfParser into epub // Grab data from opfParser into epub
title = opfParser.title; title = opfParser.title;
constexpr const char* NCX_MEDIA_TYPE = "application/x-dtbncx+xml";
if (opfParser.items.count("ncx")) { for (const auto& [id, manifestItem] : opfParser.items) {
tocNcxItem = opfParser.items.at("ncx"); (void)id;
} else if (opfParser.items.count("ncxtoc")) { if (manifestItem.mediaType == NCX_MEDIA_TYPE) {
tocNcxItem = opfParser.items.at("ncxtoc"); tocNcxItem = manifestItem.href;
break;
}
}
if (tocNcxItem.empty() && !opfParser.tocNcxPath.empty()) {
tocNcxItem = opfParser.tocNcxPath;
}
if (tocNcxItem.empty()) {
if (opfParser.items.count("ncx")) {
tocNcxItem = opfParser.items.at("ncx").href;
} else if (opfParser.items.count("ncxtoc")) {
tocNcxItem = opfParser.items.at("ncxtoc").href;
}
} }
for (auto& spineRef : opfParser.spineRefs) { for (auto& spineRef : opfParser.spineRefs) {
if (opfParser.items.count(spineRef)) { if (opfParser.items.count(spineRef)) {
spine.emplace_back(spineRef, opfParser.items.at(spineRef)); spine.emplace_back(spineRef, opfParser.items.at(spineRef).href);
} }
} }

View File

@ -96,17 +96,24 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
if (self->state == IN_MANIFEST && (strcmp(name, "item") == 0 || strcmp(name, "opf:item") == 0)) { if (self->state == IN_MANIFEST && (strcmp(name, "item") == 0 || strcmp(name, "opf:item") == 0)) {
std::string itemId; std::string itemId;
std::string href; ManifestItem item;
for (int i = 0; atts[i]; i += 2) { for (int i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], "id") == 0) { if (strcmp(atts[i], "id") == 0) {
itemId = atts[i + 1]; itemId = atts[i + 1];
} else if (strcmp(atts[i], "href") == 0) { } else if (strcmp(atts[i], "href") == 0) {
href = self->baseContentPath + atts[i + 1]; item.href = self->baseContentPath + atts[i + 1];
} else if (strcmp(atts[i], "media-type") == 0) {
item.mediaType = atts[i + 1];
} }
} }
self->items[itemId] = href; if (!itemId.empty()) {
self->items[itemId] = item;
if (item.mediaType == "application/x-dtbncx+xml" && self->tocNcxPath.empty()) {
self->tocNcxPath = item.href;
}
}
return; return;
} }

View File

@ -2,6 +2,8 @@
#include <Print.h> #include <Print.h>
#include <map> #include <map>
#include <string>
#include <vector>
#include "Epub.h" #include "Epub.h"
#include "expat.h" #include "expat.h"
@ -26,9 +28,14 @@ class ContentOpfParser final : public Print {
static void endElement(void* userData, const XML_Char* name); static void endElement(void* userData, const XML_Char* name);
public: public:
struct ManifestItem {
std::string href;
std::string mediaType;
};
std::string title; std::string title;
std::string tocNcxPath; std::string tocNcxPath;
std::map<std::string, std::string> items; std::map<std::string, ManifestItem> items;
std::vector<std::string> spineRefs; std::vector<std::string> spineRefs;
explicit ContentOpfParser(const std::string& baseContentPath, const size_t xmlSize) explicit ContentOpfParser(const std::string& baseContentPath, const size_t xmlSize)

View File

@ -13,13 +13,41 @@ void EpubReaderChapterSelectionScreen::taskTrampoline(void* param) {
self->displayTaskLoop(); self->displayTaskLoop();
} }
void EpubReaderChapterSelectionScreen::rebuildVisibleSpineIndices() {
visibleSpineIndices.clear();
if (!epub) {
return;
}
const int spineCount = epub->getSpineItemsCount();
visibleSpineIndices.reserve(spineCount);
for (int i = 0; i < spineCount; i++) {
if (epub->getTocIndexForSpineIndex(i) != -1) {
visibleSpineIndices.push_back(i);
}
}
}
void EpubReaderChapterSelectionScreen::onEnter() { void EpubReaderChapterSelectionScreen::onEnter() {
if (!epub) { if (!epub) {
return; return;
} }
renderingMutex = xSemaphoreCreateMutex(); renderingMutex = xSemaphoreCreateMutex();
selectorIndex = currentSpineIndex; rebuildVisibleSpineIndices();
selectorIndex = 0;
if (!visibleSpineIndices.empty()) {
for (size_t i = 0; i < visibleSpineIndices.size(); i++) {
if (visibleSpineIndices[i] == currentSpineIndex) {
selectorIndex = static_cast<int>(i);
break;
}
}
if (selectorIndex >= static_cast<int>(visibleSpineIndices.size())) {
selectorIndex = static_cast<int>(visibleSpineIndices.size()) - 1;
}
}
// Trigger first update // Trigger first update
updateRequired = true; updateRequired = true;
@ -40,6 +68,7 @@ void EpubReaderChapterSelectionScreen::onExit() {
} }
vSemaphoreDelete(renderingMutex); vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr; renderingMutex = nullptr;
visibleSpineIndices.clear();
} }
void EpubReaderChapterSelectionScreen::handleInput() { void EpubReaderChapterSelectionScreen::handleInput() {
@ -51,22 +80,53 @@ void EpubReaderChapterSelectionScreen::handleInput() {
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS; const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) { if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
onSelectSpineIndex(selectorIndex); if (!visibleSpineIndices.empty()) {
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) { if (selectorIndex >= static_cast<int>(visibleSpineIndices.size())) {
selectorIndex = static_cast<int>(visibleSpineIndices.size()) - 1;
}
onSelectSpineIndex(visibleSpineIndices[selectorIndex]);
}
return;
}
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
onGoBack(); onGoBack();
} else if (prevReleased) { return;
}
const int chapterCount = static_cast<int>(visibleSpineIndices.size());
if (chapterCount == 0) {
return;
}
if (selectorIndex >= chapterCount) {
selectorIndex = chapterCount - 1;
}
if (prevReleased) {
if (skipPage) { if (skipPage) {
selectorIndex = const int totalPages = (chapterCount + PAGE_ITEMS - 1) / PAGE_ITEMS;
((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + epub->getSpineItemsCount()) % epub->getSpineItemsCount(); int currentPage = selectorIndex / PAGE_ITEMS;
currentPage = (currentPage + totalPages - 1) % totalPages;
selectorIndex = currentPage * PAGE_ITEMS;
if (selectorIndex >= chapterCount) {
selectorIndex = chapterCount - 1;
}
} else { } else {
selectorIndex = (selectorIndex + epub->getSpineItemsCount() - 1) % epub->getSpineItemsCount(); selectorIndex = (selectorIndex + chapterCount - 1) % chapterCount;
} }
updateRequired = true; updateRequired = true;
} else if (nextReleased) { } else if (nextReleased) {
if (skipPage) { if (skipPage) {
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % epub->getSpineItemsCount(); const int totalPages = (chapterCount + PAGE_ITEMS - 1) / PAGE_ITEMS;
int currentPage = selectorIndex / PAGE_ITEMS;
currentPage = (currentPage + 1) % totalPages;
selectorIndex = currentPage * PAGE_ITEMS;
if (selectorIndex >= chapterCount) {
selectorIndex = chapterCount - 1;
}
} else { } else {
selectorIndex = (selectorIndex + 1) % epub->getSpineItemsCount(); selectorIndex = (selectorIndex + 1) % chapterCount;
} }
updateRequired = true; updateRequired = true;
} }
@ -90,17 +150,28 @@ void EpubReaderChapterSelectionScreen::renderScreen() {
const auto pageWidth = renderer.getScreenWidth(); const auto pageWidth = renderer.getScreenWidth();
renderer.drawCenteredText(READER_FONT_ID, 10, "Select Chapter", true, BOLD); renderer.drawCenteredText(READER_FONT_ID, 10, "Select Chapter", true, BOLD);
const int chapterCount = static_cast<int>(visibleSpineIndices.size());
if (chapterCount == 0) {
renderer.drawText(UI_FONT_ID, 20, 60, "No chapters available");
renderer.displayBuffer();
return;
}
if (selectorIndex >= chapterCount) {
selectorIndex = chapterCount - 1;
}
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS; const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30); renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30);
for (int i = pageStartIndex; i < epub->getSpineItemsCount() && i < pageStartIndex + PAGE_ITEMS; i++) { for (int i = pageStartIndex; i < chapterCount && i < pageStartIndex + PAGE_ITEMS; i++) {
const int tocIndex = epub->getTocIndexForSpineIndex(i); const int spineIndex = visibleSpineIndices[i];
const int tocIndex = epub->getTocIndexForSpineIndex(spineIndex);
if (tocIndex == -1) { if (tocIndex == -1) {
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, "Unnamed", i != selectorIndex); continue; // Filtered chapters should not reach here, but skip defensively.
} else {
auto item = epub->getTocItem(tocIndex);
renderer.drawText(UI_FONT_ID, 20 + (item.level - 1) * 15, 60 + (i % PAGE_ITEMS) * 30, item.title.c_str(),
i != selectorIndex);
} }
auto item = epub->getTocItem(tocIndex);
renderer.drawText(UI_FONT_ID, 20 + (item.level - 1) * 15, 60 + (i % PAGE_ITEMS) * 30, item.title.c_str(),
i != selectorIndex);
} }
renderer.displayBuffer(); renderer.displayBuffer();

View File

@ -5,6 +5,7 @@
#include <freertos/task.h> #include <freertos/task.h>
#include <memory> #include <memory>
#include <vector>
#include "Screen.h" #include "Screen.h"
@ -15,11 +16,13 @@ class EpubReaderChapterSelectionScreen final : public Screen {
int currentSpineIndex = 0; int currentSpineIndex = 0;
int selectorIndex = 0; int selectorIndex = 0;
bool updateRequired = false; bool updateRequired = false;
std::vector<int> visibleSpineIndices;
const std::function<void()> onGoBack; const std::function<void()> onGoBack;
const std::function<void(int newSpineIndex)> onSelectSpineIndex; const std::function<void(int newSpineIndex)> onSelectSpineIndex;
static void taskTrampoline(void* param); static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop(); [[noreturn]] void displayTaskLoop();
void rebuildVisibleSpineIndices();
void renderScreen(); void renderScreen();
public: public: