Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/Epub/Epub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,18 @@ float Epub::calculateProgress(const uint16_t currentSpineIndex, const float curr
return totalProgress / static_cast<float>(bookSize);
}

float Epub::calculateProgressForPage(const uint16_t spineIndex, const uint16_t pageInSpine,
const uint16_t spinePageCount) const {
if (spinePageCount == 0) {
return calculateProgress(spineIndex, 0.0f);
}
float intra = static_cast<float>(pageInSpine + 1) / static_cast<float>(spinePageCount);
if (intra > 1.0f) {
intra = 1.0f;
}
return calculateProgress(spineIndex, intra);
}

std::optional<uint16_t> Epub::resolveHrefToSpineIndex(const std::string& href) const {
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) return std::nullopt;

Expand Down
9 changes: 9 additions & 0 deletions lib/Epub/Epub.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,16 @@ class Epub {
uint16_t getSpineIndexForTextReference() const;

size_t getBookSize() const;

// Byte-weighted primitive: maps a [0,1] fraction through a spine to book
// progress. Page-based callers use calculateProgressForPage (public) instead.
float calculateProgress(uint16_t currentSpineIndex, float currentSpineRead) const;

// Book progress (0.0-1.0) for a 0-based page within a spine. The 1-based
// fraction ((pageInSpine + 1) / spinePageCount) makes the final page read as
// a full 1.0. Shared by all progress readouts so the convention can't drift.
float calculateProgressForPage(uint16_t spineIndex, uint16_t pageInSpine, uint16_t spinePageCount) const;

CssParser* getCssParser() const { return cssParser.get(); }
std::optional<uint16_t> resolveHrefToSpineIndex(const std::string& href) const;
};
26 changes: 11 additions & 15 deletions src/activities/reader/EpubReaderActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ void EpubReaderActivity::loop() {
const int totalPages = getChapterTotalPages();
float bookProgress = 0.0f;
if (epub->getBookSize() > 0 && spineItem && spineItem->getPageCount() > 0) {
const float chapterProgress =
static_cast<float>(spineItem->currentPage) / static_cast<float>(spineItem->getPageCount());
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
bookProgress =
epub->calculateProgressForPage(currentSpineIndex, spineItem->currentPage, spineItem->getPageCount()) *
100.0f;
}
const int bookProgressPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
startActivityForResult(std::make_unique<EpubReaderMenuActivity>(renderer, mappedInput, epub->getTitle(),
Expand Down Expand Up @@ -543,9 +543,9 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
case EpubReaderMenuActivity::MenuAction::GO_TO_PERCENT: {
float bookProgress = 0.0f;
if (epub && epub->getBookSize() > 0 && spineItem && spineItem->getPageCount() > 0) {
const float chapterProgress =
static_cast<float>(spineItem->currentPage) / static_cast<float>(spineItem->getPageCount());
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
bookProgress =
epub->calculateProgressForPage(currentSpineIndex, spineItem->currentPage, spineItem->getPageCount()) *
100.0f;
}
const int initialPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
startActivityForResult(std::make_unique<ReaderPercentSelectionActivity>(renderer, mappedInput, initialPercent),
Expand Down Expand Up @@ -999,10 +999,8 @@ void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const uint16

void EpubReaderActivity::renderStatusBar() const {
// Calculate progress in book
const int currentPage = spineItem->currentPage + 1;
const float pageCount = spineItem->getPageCount();
const float sectionChapterProg = (pageCount > 0) ? (static_cast<float>(currentPage) / pageCount) : 0;
const float bookProgress = epub->calculateProgress(currentSpineIndex, sectionChapterProg) * 100;
const float bookProgress =
epub->calculateProgressForPage(currentSpineIndex, spineItem->currentPage, spineItem->getPageCount()) * 100;

// Use chapter-relative page counts when available
const int chapterPage = getChapterRelativePage() + 1;
Expand Down Expand Up @@ -1253,11 +1251,9 @@ ScreenshotInfo EpubReaderActivity::getScreenshotInfo() const {
info.currentPage = spineItem->currentPage + 1;
info.totalPages = spineItem->getPageCount();
if (epub && epub->getBookSize() > 0 && info.totalPages > 0) {
const float chapterProgress = static_cast<float>(spineItem->currentPage) / static_cast<float>(info.totalPages);
int pct = static_cast<int>(epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f + 0.5f);
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
info.progressPercent = pct;
const int pct = static_cast<int>(
epub->calculateProgressForPage(currentSpineIndex, spineItem->currentPage, info.totalPages) * 100.0f + 0.5f);
info.progressPercent = clampPercent(pct);
}
}
return info;
Expand Down
Loading