Skip to content

Fix first spread of FXL EPUBs #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ All notable changes to this project will be documented in this file. Take a look
#### Navigator

* Optimized scrolling to an EPUB text-based locator if it contains a CSS selector.
* 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.


## [3.0.0-alpha.1]
Expand Down
16 changes: 10 additions & 6 deletions Sources/Navigator/EPUB/EPUBSpread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct EPUBSpread: Loggable {
/// - page [left|center|right]: (optional) Page position of the linked resource in the spread.
func json(forBaseURL baseURL: HTTPURL) -> [[String: Any]] {
func makeLinkJSON(_ link: Link, page: Presentation.Page? = nil) -> [String: Any]? {
let page = page ?? link.properties.page ?? readingProgression.leadingPage
let page = page ?? link.properties.page ?? readingProgression.startingPage
return [
"link": link.json,
"url": link.url(relativeTo: baseURL).string,
Expand Down Expand Up @@ -147,7 +147,7 @@ struct EPUBSpread: Loggable {
readingProgression: ReadingProgression
) -> [EPUBSpread] {
/// Builds two-pages spreads from a list of links and a spread accumulator.
func makeSpreads(for links: [Link], in spreads: [EPUBSpread] = []) -> [EPUBSpread] {
func makeSpreads(for links: [Link], index: Int, in spreads: [EPUBSpread] = []) -> [EPUBSpread] {
var links = links
var spreads = spreads
guard !links.isEmpty else {
Expand All @@ -160,7 +160,7 @@ struct EPUBSpread: Loggable {
if let second = links.first,
layout == .fixed,
layout == publication.metadata.presentation.layout(of: second),
areConsecutive(first, second)
areConsecutive(first, second, index: index)
{
spreads.append(EPUBSpread(
spread: true,
Expand All @@ -176,11 +176,15 @@ struct EPUBSpread: Loggable {
))
}

return makeSpreads(for: links, in: spreads)
return makeSpreads(for: links, index: index + 1, in: spreads)
}

/// Two resources are consecutive if their position hint (Properties.Page) are paired according to the reading progression.
func areConsecutive(_ first: Link, _ second: Link) -> Bool {
func areConsecutive(_ first: Link, _ second: Link, index: Int) -> Bool {
guard index > 0 || first.properties.page != nil else {
return false
}

// 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.
switch publication.metadata.effectiveReadingProgression {
case .ltr, .ttb, .auto:
Expand All @@ -194,7 +198,7 @@ struct EPUBSpread: Loggable {
}
}

return makeSpreads(for: readingOrder)
return makeSpreads(for: readingOrder, index: 0)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/Navigator/Preferences/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public enum ReadingProgression: String, Codable, Hashable {
}
}

/// Returns the leading Page for the reading progression.
var leadingPage: Presentation.Page {
/// Returns the starting page for the reading progression.
var startingPage: Presentation.Page {
switch self {
case .ltr:
return .left
case .rtl:
return .right
case .rtl:
return .left
}
}
}
Expand Down
Loading