Calculate the progress in the book by file sizes of each chapter.

This commit is contained in:
Jonas Diemer 2025-12-16 18:01:46 +01:00
parent 11f01d3a41
commit 9561bab51f
3 changed files with 32 additions and 2 deletions

View File

@ -148,6 +148,18 @@ bool Epub::load() {
return false;
}
// determine size of spine items
size_t spineItemsCount = getSpineItemsCount();
size_t spineItemsSize = 0;
for (size_t i = 0; i < spineItemsCount; i++) {
std::string spineItem = getSpineItem(i);
size_t s = 0;
getItemSize(spineItem, &s);
spineItemsSize += s;
cumulativeSpineItemSize.emplace_back(spineItemsSize);
}
Serial.printf("[%lu] [EBP] Book size: %u\n", millis(), spineItemsSize);
Serial.printf("[%lu] [EBP] Loaded ePub: %s\n", millis(), filepath.c_str());
return true;
@ -255,6 +267,8 @@ bool Epub::getItemSize(const std::string& itemHref, size_t* size) const {
int Epub::getSpineItemsCount() const { return spine.size(); }
size_t Epub::getCumulativeSpineItemSize(const int spineIndex) const { return cumulativeSpineItemSize.at(spineIndex); }
std::string& Epub::getSpineItem(const int spineIndex) {
if (spineIndex < 0 || spineIndex >= spine.size()) {
Serial.printf("[%lu] [EBP] getSpineItem index:%d is out of range\n", millis(), spineIndex);

View File

@ -20,6 +20,8 @@ class Epub {
std::string filepath;
// the spine of the EPUB file
std::vector<std::pair<std::string, std::string>> spine;
// the file size of the spine items (proxy to book progress)
std::vector<size_t> cumulativeSpineItemSize;
// the toc of the EPUB file
std::vector<EpubTocEntry> toc;
// the base path for items in the EPUB file
@ -51,7 +53,8 @@ class Epub {
bool getItemSize(const std::string& itemHref, size_t* size) const;
std::string& getSpineItem(int spineIndex);
int getSpineItemsCount() const;
EpubTocEntry& getTocItem(int tocTndex);
size_t getCumulativeSpineItemSize(const int spineIndex) const;
EpubTocEntry& getTocItem(int tocIndex);
int getTocItemsCount() const;
int getSpineIndexForTocIndex(int tocIndex) const;
int getTocIndexForSpineIndex(int spineIndex) const;

View File

@ -324,8 +324,21 @@ void EpubReaderScreen::renderContents(std::unique_ptr<Page> page) {
void EpubReaderScreen::renderStatusBar() const {
constexpr auto textY = 776;
// Calculate progress in book
size_t prevChapterSize = epub->getCumulativeSpineItemSize(currentSpineIndex - 1);
size_t curChapterSize = epub->getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize;
size_t bookSize = epub->getCumulativeSpineItemSize(epub->getSpineItemsCount() - 1);
size_t sectionProgSize = static_cast<size_t>(static_cast<float>(section->currentPage) / section->pageCount * curChapterSize);
float bookProgress = static_cast<float>(prevChapterSize + sectionProgSize) / bookSize * 100.0;
char bookProgressStr[6] = "--.-";
std:snprintf(bookProgressStr, 6, "%.1f", bookProgress);
// Serial.printf("[%lu] [EBP] prevChapterSize: %u bookSize: %u sectionProgSize: %u bookSize:%u Book progress: %s %%\n", millis(),
// prevChapterSize, bookSize, sectionProgSize, bookSize, bookProgressStr);
// Right aligned text for progress counter
const std::string progress = std::to_string(section->currentPage + 1) + " / " + std::to_string(section->pageCount);
const std::string progress = std::to_string(section->currentPage + 1) + "/" + std::to_string(section->pageCount)
+ " " + bookProgressStr + "%";
const auto progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progress.c_str());
renderer.drawText(SMALL_FONT_ID, GfxRenderer::getScreenWidth() - marginRight - progressTextWidth, textY,
progress.c_str());