mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-06 15:47:39 +03:00
dont round % and default to start of chapter not end
This commit is contained in:
parent
0620a46429
commit
f36d89cd18
@ -539,14 +539,15 @@ int Epub::getSpineIndexForTextReference() const {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate progress in book
|
// Calculate progress in book (returns 0.0-1.0)
|
||||||
uint8_t Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) const {
|
float Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) const {
|
||||||
const size_t bookSize = getBookSize();
|
const size_t bookSize = getBookSize();
|
||||||
if (bookSize == 0) {
|
if (bookSize == 0) {
|
||||||
return 0;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
const size_t prevChapterSize = (currentSpineIndex >= 1) ? getCumulativeSpineItemSize(currentSpineIndex - 1) : 0;
|
const size_t prevChapterSize = (currentSpineIndex >= 1) ? getCumulativeSpineItemSize(currentSpineIndex - 1) : 0;
|
||||||
const size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize;
|
const size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize;
|
||||||
const size_t sectionProgSize = currentSpineRead * curChapterSize;
|
const float sectionProgSize = currentSpineRead * static_cast<float>(curChapterSize);
|
||||||
return round(static_cast<float>(prevChapterSize + sectionProgSize) / bookSize * 100.0);
|
const float totalProgress = static_cast<float>(prevChapterSize) + sectionProgSize;
|
||||||
|
return totalProgress / static_cast<float>(bookSize);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,5 +60,5 @@ class Epub {
|
|||||||
int getSpineIndexForTextReference() const;
|
int getSpineIndexForTextReference() const;
|
||||||
|
|
||||||
size_t getBookSize() const;
|
size_t getBookSize() const;
|
||||||
uint8_t calculateProgress(int currentSpineIndex, float currentSpineRead) const;
|
float calculateProgress(int currentSpineIndex, float currentSpineRead) const;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -14,9 +14,8 @@ KOReaderPosition ProgressMapper::toKOReader(const std::shared_ptr<Epub>& epub, c
|
|||||||
intraSpineProgress = static_cast<float>(pos.pageNumber) / static_cast<float>(pos.totalPages);
|
intraSpineProgress = static_cast<float>(pos.pageNumber) / static_cast<float>(pos.totalPages);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate overall book progress (0-100 from Epub, convert to 0.0-1.0)
|
// Calculate overall book progress (0.0-1.0)
|
||||||
const uint8_t progressPercent = epub->calculateProgress(pos.spineIndex, intraSpineProgress);
|
result.percentage = epub->calculateProgress(pos.spineIndex, intraSpineProgress);
|
||||||
result.percentage = static_cast<float>(progressPercent) / 100.0f;
|
|
||||||
|
|
||||||
// Generate XPath with estimated paragraph position based on page
|
// Generate XPath with estimated paragraph position based on page
|
||||||
result.xpath = generateXPath(pos.spineIndex, pos.pageNumber, pos.totalPages);
|
result.xpath = generateXPath(pos.spineIndex, pos.pageNumber, pos.totalPages);
|
||||||
@ -48,9 +47,11 @@ CrossPointPosition ProgressMapper::toCrossPoint(const std::shared_ptr<Epub>& epu
|
|||||||
int xpathSpineIndex = parseDocFragmentIndex(koPos.xpath);
|
int xpathSpineIndex = parseDocFragmentIndex(koPos.xpath);
|
||||||
if (xpathSpineIndex >= 0 && xpathSpineIndex < epub->getSpineItemsCount()) {
|
if (xpathSpineIndex >= 0 && xpathSpineIndex < epub->getSpineItemsCount()) {
|
||||||
result.spineIndex = xpathSpineIndex;
|
result.spineIndex = xpathSpineIndex;
|
||||||
Serial.printf("[%lu] [ProgressMapper] Got spine index from XPath: %d\n", millis(), result.spineIndex);
|
// When we have XPath, go to page 0 of the spine - byte-based page calculation is unreliable
|
||||||
|
result.pageNumber = 0;
|
||||||
|
Serial.printf("[%lu] [ProgressMapper] Got spine index from XPath: %d (page=0)\n", millis(), result.spineIndex);
|
||||||
} else {
|
} else {
|
||||||
// Fall back to percentage-based lookup
|
// Fall back to percentage-based lookup for both spine and page
|
||||||
const size_t targetBytes = static_cast<size_t>(bookSize * koPos.percentage);
|
const size_t targetBytes = static_cast<size_t>(bookSize * koPos.percentage);
|
||||||
|
|
||||||
// Find the spine item that contains this byte position
|
// Find the spine item that contains this byte position
|
||||||
@ -63,26 +64,20 @@ CrossPointPosition ProgressMapper::toCrossPoint(const std::shared_ptr<Epub>& epu
|
|||||||
}
|
}
|
||||||
Serial.printf("[%lu] [ProgressMapper] Got spine index from percentage (%.2f%%): %d\n", millis(),
|
Serial.printf("[%lu] [ProgressMapper] Got spine index from percentage (%.2f%%): %d\n", millis(),
|
||||||
koPos.percentage * 100, result.spineIndex);
|
koPos.percentage * 100, result.spineIndex);
|
||||||
}
|
|
||||||
|
|
||||||
// Estimate page number within the spine item using percentage
|
// Estimate page number within the spine item using percentage (only when no XPath)
|
||||||
if (totalPagesInSpine > 0 && result.spineIndex < epub->getSpineItemsCount()) {
|
if (totalPagesInSpine > 0 && result.spineIndex < epub->getSpineItemsCount()) {
|
||||||
// Calculate what percentage through the spine item we should be
|
const size_t prevCumSize = (result.spineIndex > 0) ? epub->getCumulativeSpineItemSize(result.spineIndex - 1) : 0;
|
||||||
const size_t prevCumSize = (result.spineIndex > 0) ? epub->getCumulativeSpineItemSize(result.spineIndex - 1) : 0;
|
const size_t currentCumSize = epub->getCumulativeSpineItemSize(result.spineIndex);
|
||||||
const size_t currentCumSize = epub->getCumulativeSpineItemSize(result.spineIndex);
|
const size_t spineSize = currentCumSize - prevCumSize;
|
||||||
const size_t spineSize = currentCumSize - prevCumSize;
|
|
||||||
|
|
||||||
if (spineSize > 0) {
|
if (spineSize > 0) {
|
||||||
const size_t targetBytes = static_cast<size_t>(bookSize * koPos.percentage);
|
const size_t bytesIntoSpine = (targetBytes > prevCumSize) ? (targetBytes - prevCumSize) : 0;
|
||||||
const size_t bytesIntoSpine = (targetBytes > prevCumSize) ? (targetBytes - prevCumSize) : 0;
|
const float intraSpineProgress = static_cast<float>(bytesIntoSpine) / static_cast<float>(spineSize);
|
||||||
const float intraSpineProgress = static_cast<float>(bytesIntoSpine) / static_cast<float>(spineSize);
|
const float clampedProgress = std::max(0.0f, std::min(1.0f, intraSpineProgress));
|
||||||
|
result.pageNumber = static_cast<int>(clampedProgress * totalPagesInSpine);
|
||||||
// Clamp to valid range
|
result.pageNumber = std::max(0, std::min(result.pageNumber, totalPagesInSpine - 1));
|
||||||
const float clampedProgress = std::max(0.0f, std::min(1.0f, intraSpineProgress));
|
}
|
||||||
result.pageNumber = static_cast<int>(clampedProgress * totalPagesInSpine);
|
|
||||||
|
|
||||||
// Ensure page number is valid
|
|
||||||
result.pageNumber = std::max(0, std::min(result.pageNumber, totalPagesInSpine - 1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -438,11 +438,13 @@ void EpubReaderActivity::renderStatusBar(const int orientedMarginRight, const in
|
|||||||
if (showProgress) {
|
if (showProgress) {
|
||||||
// Calculate progress in book
|
// Calculate progress in book
|
||||||
const float sectionChapterProg = static_cast<float>(section->currentPage) / section->pageCount;
|
const float sectionChapterProg = static_cast<float>(section->currentPage) / section->pageCount;
|
||||||
const uint8_t bookProgress = epub->calculateProgress(currentSpineIndex, sectionChapterProg);
|
const float bookProgress = epub->calculateProgress(currentSpineIndex, sectionChapterProg) * 100;
|
||||||
|
|
||||||
// Right aligned text for progress counter
|
// Right aligned text for progress counter
|
||||||
const std::string progress = std::to_string(section->currentPage + 1) + "/" + std::to_string(section->pageCount) +
|
char progressStr[32];
|
||||||
" " + std::to_string(bookProgress) + "%";
|
snprintf(progressStr, sizeof(progressStr), "%d/%d %.1f%%", section->currentPage + 1, section->pageCount,
|
||||||
|
bookProgress);
|
||||||
|
const std::string progress = progressStr;
|
||||||
progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progress.c_str());
|
progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progress.c_str());
|
||||||
renderer.drawText(SMALL_FONT_ID, renderer.getScreenWidth() - orientedMarginRight - progressTextWidth, textY,
|
renderer.drawText(SMALL_FONT_ID, renderer.getScreenWidth() - orientedMarginRight - progressTextWidth, textY,
|
||||||
progress.c_str());
|
progress.c_str());
|
||||||
|
|||||||
@ -296,7 +296,7 @@ void KOReaderSyncActivity::render() {
|
|||||||
snprintf(remoteChapterStr, sizeof(remoteChapterStr), " %s", remoteChapter.c_str());
|
snprintf(remoteChapterStr, sizeof(remoteChapterStr), " %s", remoteChapter.c_str());
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, 185, remoteChapterStr);
|
renderer.drawText(UI_10_FONT_ID, 20, 185, remoteChapterStr);
|
||||||
char remotePageStr[64];
|
char remotePageStr[64];
|
||||||
snprintf(remotePageStr, sizeof(remotePageStr), " Page %d, %.0f%% overall", remotePosition.pageNumber + 1,
|
snprintf(remotePageStr, sizeof(remotePageStr), " Page %d, %.2f%% overall", remotePosition.pageNumber + 1,
|
||||||
remoteProgress.percentage * 100);
|
remoteProgress.percentage * 100);
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, 210, remotePageStr);
|
renderer.drawText(UI_10_FONT_ID, 20, 210, remotePageStr);
|
||||||
|
|
||||||
@ -312,7 +312,7 @@ void KOReaderSyncActivity::render() {
|
|||||||
snprintf(localChapterStr, sizeof(localChapterStr), " %s", localChapter.c_str());
|
snprintf(localChapterStr, sizeof(localChapterStr), " %s", localChapter.c_str());
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, 295, localChapterStr);
|
renderer.drawText(UI_10_FONT_ID, 20, 295, localChapterStr);
|
||||||
char localPageStr[64];
|
char localPageStr[64];
|
||||||
snprintf(localPageStr, sizeof(localPageStr), " Page %d/%d, %.0f%% overall", currentPage + 1, totalPagesInSpine,
|
snprintf(localPageStr, sizeof(localPageStr), " Page %d/%d, %.2f%% overall", currentPage + 1, totalPagesInSpine,
|
||||||
localProgress.percentage * 100);
|
localProgress.percentage * 100);
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, 320, localPageStr);
|
renderer.drawText(UI_10_FONT_ID, 20, 320, localPageStr);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user