From 03e7ca085ce5e59f7eccc286cecd9385c63f1d53 Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Wed, 9 Aug 2017 17:19:02 -0400 Subject: [PATCH] Fixes for page counting --- package.json | 2 +- src/layout.js | 7 +++++-- src/managers/default/index.js | 27 ++++++++++++++++----------- src/managers/views/iframe.js | 14 +++++--------- src/rendition.js | 2 +- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 7ff2b3c..cbb645e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "epubjs", - "version": "0.3.44", + "version": "0.3.45", "description": "Parse and Render Epubs", "main": "lib/index.js", "module": "src/index.js", diff --git a/src/layout.js b/src/layout.js index 209a2ab..566484f 100644 --- a/src/layout.js +++ b/src/layout.js @@ -105,6 +105,7 @@ class Layout { var colWidth; var spreadWidth; + var pageWidth; var delta; if (this._spread && width >= this._minSpreadWidth) { @@ -135,14 +136,15 @@ class Layout { width = colWidth; } - spreadWidth = colWidth * divisor; + spreadWidth = (colWidth * divisor) + gap; + pageWidth = colWidth + (gap / 2); delta = width; - // console.log(delta, width); this.width = width; this.height = _height; this.spreadWidth = spreadWidth; + this.pageWidth = pageWidth; this.delta = delta; this.columnWidth = colWidth; @@ -152,6 +154,7 @@ class Layout { this.props.width = width; this.props.height = _height; this.props.spreadWidth = spreadWidth; + this.props.pageWidth = pageWidth; this.props.delta = delta; this.props.columnWidth = colWidth; diff --git a/src/managers/default/index.js b/src/managers/default/index.js index 0fca053..5adac01 100644 --- a/src/managers/default/index.js +++ b/src/managers/default/index.js @@ -547,6 +547,7 @@ class DefaultViewManager { var container = this.container.getBoundingClientRect(); var left = 0; + let used = 0; if(this.fullsize) { left = window.scrollX; @@ -558,26 +559,30 @@ class DefaultViewManager { let position = view.position().left; let width = view.width(); - let startPos = left + offset; - let endPos = startPos + this.layout.spreadWidth + this.layout.gap; - if (endPos > left + offset + width) { - endPos = left + offset + width; + // Find mapping + let start = left + container.left - position + used; + let end = start + this.layout.spreadWidth - used; + + let mapping = this.mapping.page(view.contents, view.section.cfiBase, start, end); + + // Find displayed pages + let startPos = left + used; + let endPos = startPos + this.layout.spreadWidth - used; + if (endPos > offset + width) { + endPos = offset + width; + used = this.layout.pageWidth; } let totalPages = this.layout.count(width).pages; - let currPage = Math.ceil((startPos - offset) / this.layout.spreadWidth); + let currPage = Math.floor((startPos - offset) / this.layout.pageWidth); let pages = []; - let numPages = (endPos - startPos) / (this.layout.columnWidth + (this.layout.gap / 2)); + let numPages = Math.floor((endPos - startPos) / this.layout.pageWidth); + for (var i = 1; i <= numPages; i++) { let pg = currPage + i; pages.push(pg); } - let start = left + container.left - position; - let end = start + this.layout.spreadWidth + this.layout.gap; - - let mapping = this.mapping.page(view.contents, view.section.cfiBase, start, end); - return { index, href, diff --git a/src/managers/views/iframe.js b/src/managers/views/iframe.js index 4e99c69..0108bb4 100644 --- a/src/managers/views/iframe.js +++ b/src/managers/views/iframe.js @@ -253,11 +253,12 @@ class IframeView { // Get the contentWidth by resizing the iframe // Check with a min reset of the textWidth - // width = this.contentWidth(textWidth); - + // Add padding back + if (width % this.layout.width > 0) { + width += this.layout.gap / 2; + } /* - columns = Math.ceil(width / (this.settings.layout.columnWidth)); - + columns = Math.ceil(width / this.settings.layout.delta); if ( this.settings.layout.divisor > 1 && this.settings.layout.name === "reflowable" && (columns % 2 > 0)) { @@ -266,11 +267,6 @@ class IframeView { } */ - // Add padding back - if (width % this.layout.width > 0) { - width += this.layout.gap / 2; - } - // Save the textWdith this._textWidth = textWidth; diff --git a/src/rendition.js b/src/rendition.js index 0a4496a..79196f0 100644 --- a/src/rendition.js +++ b/src/rendition.js @@ -620,7 +620,7 @@ class Rendition { } if (end.index === this.book.spine.last().index && - located.end.displayed.page === located.end.displayed.totalPages) { + located.end.displayed.page >= located.end.displayed.totalPages) { located.atEnd = true; }