Skip to content

Commit c1fd5b5

Browse files
authored
Add FXL EPUB fit preference (#681)
1 parent e49966e commit c1fd5b5

File tree

13 files changed

+170
-40
lines changed

13 files changed

+170
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ All notable changes to this project will be documented in this file. Take a look
1313
* This callback is called before executing any navigation action.
1414
* Useful for hiding UI elements when the user navigates, or implementing analytics.
1515
* Added swipe gesture support for navigating in PDF paginated spread mode.
16-
* Added `fit` preference for PDF documents to control how pages are scaled within the viewport.
17-
* Only effective in scroll mode. Paginated mode always uses page fit due to PDFKit limitations.
16+
* Added `fit` preference for fixed-layout publications (PDF and FXL EPUB) to control how pages are scaled within the viewport.
17+
* In the PDF navigator, it is only effective in scroll mode. Paginated mode always uses `page` fit due to PDFKit limitations.
1818

1919
### Deprecated
2020

@@ -32,7 +32,7 @@ All notable changes to this project will be documented in this file. Take a look
3232

3333
* The `Fit` enum has been redesigned to fit the PDF implementation.
3434
* **Breaking change:** Update any code using the old `Fit` enum values.
35-
* The PDF navigator's content inset behavior has changed:
35+
* The fixed-layout navigators (PDF and FXL EPUB)'s content inset behavior has changed:
3636
* iPhone: Continues to apply window safe area insets (to account for notch/Dynamic Island).
3737
* iPad/macOS: Now displays edge-to-edge with no automatic safe area insets.
3838
* You can customize this behavior with `VisualNavigatorDelegate.navigatorContentInset(_:)`.

Sources/Navigator/EPUB/Assets/Static/scripts/readium-fixed-wrapper-one.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/Navigator/EPUB/Assets/Static/scripts/readium-fixed-wrapper-two.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/Navigator/EPUB/EPUBFixedSpreadView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ final class EPUBFixedSpreadView: EPUBSpreadView {
8888
insets.right = horizontalInsets
8989

9090
let viewportSize = bounds.inset(by: insets).size
91+
let fitString = viewModel.settings.fit.rawValue
9192

9293
webView.evaluateJavaScript("""
9394
spread.setViewport(
9495
{'width': \(Int(viewportSize.width)), 'height': \(Int(viewportSize.height))},
95-
{'top': \(Int(insets.top)), 'left': \(Int(insets.left)), 'bottom': \(Int(insets.bottom)), 'right': \(Int(insets.right))}
96+
{'top': \(Int(insets.top)), 'left': \(Int(insets.left)), 'bottom': \(Int(insets.bottom)), 'right': \(Int(insets.right))},
97+
'\(fitString)'
9698
);
9799
""")
98100
}

Sources/Navigator/EPUB/EPUBNavigatorViewController.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,10 +1043,23 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate {
10431043
// the application's bars.
10441044
var insets = view.window?.safeAreaInsets ?? .zero
10451045

1046-
if publication.metadata.layout != .fixed {
1046+
switch publication.metadata.layout ?? .reflowable {
1047+
case .fixed:
1048+
// With iPadOS and macOS, we aim to display content edge-to-edge
1049+
// since there are no physical notches or Dynamic Island like on the
1050+
// iPhone.
1051+
if UIDevice.current.userInterfaceIdiom != .phone {
1052+
insets = .zero
1053+
}
1054+
1055+
case .reflowable:
10471056
let configInset = config.contentInset(for: view.traitCollection.verticalSizeClass)
10481057
insets.top = max(insets.top, configInset.top)
10491058
insets.bottom = max(insets.bottom, configInset.bottom)
1059+
1060+
case .scrolled:
1061+
// Not supported with EPUB.
1062+
break
10501063
}
10511064

10521065
return insets

Sources/Navigator/EPUB/EPUBNavigatorViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ final class EPUBNavigatorViewModel: Loggable {
220220
|| oldSettings.verticalText != newSettings.verticalText
221221
|| oldSettings.scroll != newSettings.scroll
222222
|| oldSettings.spread != newSettings.spread
223+
|| oldSettings.fit != newSettings.fit
223224

224225
// We don't commit the CSS changes if we invalidate the pagination, as
225226
// the resources will be reloaded anyway.

Sources/Navigator/EPUB/Preferences/EPUBPreferences.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public struct EPUBPreferences: ConfigurablePreferences {
1818
/// spread).
1919
public var columnCount: ColumnCount?
2020

21+
/// Method for fitting the content of a fixed-layout publication within the
22+
/// viewport.
23+
///
24+
/// - `auto` or `page`: Fit entire page within viewport (default).
25+
/// - `width`: Fit page width, allow vertical scrolling if needed.
26+
public var fit: Fit?
27+
2128
/// Default typeface for the text.
2229
public var fontFamily: FontFamily?
2330

@@ -97,6 +104,7 @@ public struct EPUBPreferences: ConfigurablePreferences {
97104
public init(
98105
backgroundColor: Color? = nil,
99106
columnCount: ColumnCount? = nil,
107+
fit: Fit? = nil,
100108
fontFamily: FontFamily? = nil,
101109
fontSize: Double? = nil,
102110
fontWeight: Double? = nil,
@@ -123,6 +131,7 @@ public struct EPUBPreferences: ConfigurablePreferences {
123131
) {
124132
self.backgroundColor = backgroundColor
125133
self.columnCount = columnCount
134+
self.fit = fit
126135
self.fontFamily = fontFamily
127136
self.fontSize = fontSize.map { max($0, 0) }
128137
self.fontWeight = fontWeight?.clamped(to: 0.0 ... 2.5)
@@ -152,6 +161,7 @@ public struct EPUBPreferences: ConfigurablePreferences {
152161
EPUBPreferences(
153162
backgroundColor: other.backgroundColor ?? backgroundColor,
154163
columnCount: other.columnCount ?? columnCount,
164+
fit: other.fit ?? fit,
155165
fontFamily: other.fontFamily ?? fontFamily,
156166
fontSize: other.fontSize ?? fontSize,
157167
fontWeight: other.fontWeight ?? fontWeight,

Sources/Navigator/EPUB/Preferences/EPUBPreferencesEditor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public final class EPUBPreferencesEditor: StatefulPreferencesEditor<EPUBPreferen
7070
supportedValues: [.auto, .one, .two]
7171
)
7272

73+
/// Method for fitting the content within the viewport.
74+
///
75+
/// Only effective with fixed-layout publications.
76+
public lazy var fit: AnyEnumPreference<Fit> =
77+
enumPreference(
78+
preference: \.fit,
79+
setting: \.fit,
80+
defaultEffectiveValue: defaults.fit ?? .auto,
81+
isEffective: { [layout] _ in layout == .fixed },
82+
supportedValues: [.auto, .page, .width]
83+
)
84+
7385
/// Default typeface for the text.
7486
///
7587
/// Only effective with reflowable publications.

Sources/Navigator/EPUB/Preferences/EPUBSettings.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ReadiumShared
1313
public struct EPUBSettings: ConfigurableSettings {
1414
public var backgroundColor: Color?
1515
public var columnCount: ColumnCount
16+
public var fit: Fit
1617
public var fontFamily: FontFamily?
1718
public var fontSize: Double
1819
public var fontWeight: Double?
@@ -46,6 +47,7 @@ public struct EPUBSettings: ConfigurableSettings {
4647
public init(
4748
backgroundColor: Color?,
4849
columnCount: ColumnCount,
50+
fit: Fit,
4951
fontFamily: FontFamily?,
5052
fontSize: Double,
5153
fontWeight: Double?,
@@ -72,6 +74,7 @@ public struct EPUBSettings: ConfigurableSettings {
7274
) {
7375
self.backgroundColor = backgroundColor
7476
self.columnCount = columnCount
77+
self.fit = fit
7578
self.fontFamily = fontFamily
7679
self.fontSize = fontSize
7780
self.fontWeight = fontWeight
@@ -139,6 +142,9 @@ public struct EPUBSettings: ConfigurableSettings {
139142
columnCount: preferences.columnCount
140143
?? defaults.columnCount
141144
?? .auto,
145+
fit: preferences.fit
146+
?? defaults.fit
147+
?? .auto,
142148
fontFamily: preferences.fontFamily,
143149
fontSize: preferences.fontSize
144150
?? defaults.fontSize
@@ -196,6 +202,7 @@ public struct EPUBSettings: ConfigurableSettings {
196202
/// See `EPUBPreferences`.
197203
public struct EPUBDefaults {
198204
public var columnCount: ColumnCount?
205+
public var fit: Fit?
199206
public var fontSize: Double?
200207
public var fontWeight: Double?
201208
public var hyphens: Bool?
@@ -218,6 +225,7 @@ public struct EPUBDefaults {
218225

219226
public init(
220227
columnCount: ColumnCount? = nil,
228+
fit: Fit? = nil,
221229
fontSize: Double? = nil,
222230
fontWeight: Double? = nil,
223231
hyphens: Bool? = nil,
@@ -239,6 +247,7 @@ public struct EPUBDefaults {
239247
wordSpacing: Double? = nil
240248
) {
241249
self.columnCount = columnCount
250+
self.fit = fit
242251
self.fontSize = fontSize
243252
self.fontWeight = fontWeight
244253
self.hyphens = hyphens

0 commit comments

Comments
 (0)