Skip to content

Commit 7f97ee4

Browse files
authored
Fix first spread of FXL EPUBs (#470)
1 parent 10984a7 commit 7f97ee4

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ All notable changes to this project will be documented in this file. Take a look
3535
#### Navigator
3636

3737
* Optimized scrolling to an EPUB text-based locator if it contains a CSS selector.
38+
* The first resource of a fixed-layout EPUB is now displayed on its own when spreads are enabled and the author has not set a `page-spread-*` property. This is the default behavior in major reading apps like Apple Books.
3839

3940

4041
## [3.0.0-alpha.1]

Sources/Navigator/EPUB/EPUBSpread.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct EPUBSpread: Loggable {
8383
/// - page [left|center|right]: (optional) Page position of the linked resource in the spread.
8484
func json(forBaseURL baseURL: HTTPURL) -> [[String: Any]] {
8585
func makeLinkJSON(_ link: Link, page: Presentation.Page? = nil) -> [String: Any]? {
86-
let page = page ?? link.properties.page ?? readingProgression.leadingPage
86+
let page = page ?? link.properties.page ?? readingProgression.startingPage
8787
return [
8888
"link": link.json,
8989
"url": link.url(relativeTo: baseURL).string,
@@ -147,7 +147,7 @@ struct EPUBSpread: Loggable {
147147
readingProgression: ReadingProgression
148148
) -> [EPUBSpread] {
149149
/// Builds two-pages spreads from a list of links and a spread accumulator.
150-
func makeSpreads(for links: [Link], in spreads: [EPUBSpread] = []) -> [EPUBSpread] {
150+
func makeSpreads(for links: [Link], index: Int, in spreads: [EPUBSpread] = []) -> [EPUBSpread] {
151151
var links = links
152152
var spreads = spreads
153153
guard !links.isEmpty else {
@@ -160,7 +160,7 @@ struct EPUBSpread: Loggable {
160160
if let second = links.first,
161161
layout == .fixed,
162162
layout == publication.metadata.presentation.layout(of: second),
163-
areConsecutive(first, second)
163+
areConsecutive(first, second, index: index)
164164
{
165165
spreads.append(EPUBSpread(
166166
spread: true,
@@ -176,11 +176,15 @@ struct EPUBSpread: Loggable {
176176
))
177177
}
178178

179-
return makeSpreads(for: links, in: spreads)
179+
return makeSpreads(for: links, index: index + 1, in: spreads)
180180
}
181181

182182
/// Two resources are consecutive if their position hint (Properties.Page) are paired according to the reading progression.
183-
func areConsecutive(_ first: Link, _ second: Link) -> Bool {
183+
func areConsecutive(_ first: Link, _ second: Link, index: Int) -> Bool {
184+
guard index > 0 || first.properties.page != nil else {
185+
return false
186+
}
187+
184188
// Here we use the default publication reading progression instead of the custom one provided, otherwise the page position hints might be wrong, and we could end up with only one-page spreads.
185189
switch publication.metadata.effectiveReadingProgression {
186190
case .ltr, .ttb, .auto:
@@ -194,7 +198,7 @@ struct EPUBSpread: Loggable {
194198
}
195199
}
196200

197-
return makeSpreads(for: readingOrder)
201+
return makeSpreads(for: readingOrder, index: 0)
198202
}
199203
}
200204

Sources/Navigator/Preferences/Types.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public enum ReadingProgression: String, Codable, Hashable {
5252
}
5353
}
5454

55-
/// Returns the leading Page for the reading progression.
56-
var leadingPage: Presentation.Page {
55+
/// Returns the starting page for the reading progression.
56+
var startingPage: Presentation.Page {
5757
switch self {
5858
case .ltr:
59-
return .left
60-
case .rtl:
6159
return .right
60+
case .rtl:
61+
return .left
6262
}
6363
}
6464
}

0 commit comments

Comments
 (0)