Skip to content

Commit d2d58dc

Browse files
authored
Add paragraphMargins user setting (#198)
Former-commit-id: 4a25c71
1 parent f891c92 commit d2d58dc

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

navigator/Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
github "cezheng/Fuzi" "3.1.3"
22
github "dexman/Minizip" "1.4.0"
3-
github "readium/r2-shared-swift" "f631efdb51ad6cb55fb173b4e80bbb1d1215fa76"
3+
github "readium/r2-shared-swift" "7c66c3b7eb8711946b4fca4a1cce8f5ae0bc6bfe"
44
github "scinfu/SwiftSoup" "2.3.2"

navigator/Package.resolved

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

navigator/r2-navigator-swift/EPUB/UserSettings.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class UserSettings {
3636
private var letterSpacing: Float
3737
private var pageMargins: Float
3838
private var lineHeight: Float
39+
private var paragraphMargins: Float?
3940

4041
public let userProperties = UserProperties()
4142

@@ -46,6 +47,12 @@ public class UserSettings {
4647
/// - Important: For each parameter, if a corresponding value is found in
4748
/// `UserDefaults`, that value will be used instead of the passed-in value.
4849
///
50+
/// - Note: When VoiceOver is enabled, depending on how the EPUB is
51+
/// formatted, moving to the next paragraph (swipe right) may bring
52+
/// the reading point to the end of the chapter instead of the next
53+
/// paragraph. This is a bug in WKWebView that can be worked around
54+
/// by setting the `paragraphMargins` parameter to 0.5 or more.
55+
///
4956
/// - Parameters:
5057
/// - hyphens: Whether words should be hyphenated by default.
5158
/// - fontSize: The default font size as a `%` value.
@@ -68,6 +75,11 @@ public class UserSettings {
6875
/// - letterSpacing: The default letter spacing as a `rem` value.
6976
/// - pageMargins: The default page margin value.
7077
/// - lineHeight: The default line height.
78+
/// - paragraphMargins: The default margin top and bottom `em` value to
79+
/// separate paragraphs. Passing `nil` will prevent Readium to forcibly
80+
/// modify paragraph spacing, ignoring what was previously set in
81+
/// UserDefaults. For most EPUBs, in order for this setting to work
82+
/// it may be required that `publisherDefaults` is set to `false`.
7183
public init(
7284
hyphens: Bool = false,
7385
fontSize: Float = 100,
@@ -80,7 +92,8 @@ public class UserSettings {
8092
wordSpacing: Float = 0,
8193
letterSpacing: Float = 0,
8294
pageMargins: Float = 1,
83-
lineHeight: Float = 1.5
95+
lineHeight: Float = 1.5,
96+
paragraphMargins: Float? = nil
8497
) {
8598

8699
/// Check if a given key is set in the UserDefaults.
@@ -181,6 +194,20 @@ public class UserSettings {
181194
self.lineHeight = lineHeight
182195
}
183196

197+
// Paragraph Margins
198+
// A nil `paragraphMargins` input parameter provides a way for client
199+
// apps to opt out of setting a value for paragraph spacing, which is
200+
// something that may not always be desirable. A use case is using
201+
// this parameter to work around WKWebView bugs with VoiceOver
202+
// (e.g. https://github.com/readium/r2-navigator-swift/issues/197 )
203+
// but when VoiceOver is not active it may be desirable to not change
204+
// paragraph spacing at all.
205+
if paragraphMargins != nil, isKeyPresentInUserDefaults(key: ReadiumCSSName.paragraphMargins) {
206+
self.paragraphMargins = userDefaults.float(forKey: ReadiumCSSName.paragraphMargins.rawValue)
207+
} else {
208+
self.paragraphMargins = paragraphMargins
209+
}
210+
184211
buildCssProperties()
185212

186213
}
@@ -285,6 +312,16 @@ public class UserSettings {
285312
reference: ReadiumCSSReference.lineHeight.rawValue,
286313
name: ReadiumCSSName.lineHeight.rawValue)
287314

315+
// Paragraph margins
316+
if let paragraphMargins = paragraphMargins {
317+
userProperties.addIncrementable(nValue: paragraphMargins,
318+
min: 0,
319+
max: 2,
320+
step: 0.1,
321+
suffix: "em",
322+
reference: ReadiumCSSReference.paragraphMargins.rawValue,
323+
name: ReadiumCSSName.paragraphMargins.rawValue)
324+
}
288325
}
289326

290327
// Save settings to UserDefaults
@@ -339,6 +376,10 @@ public class UserSettings {
339376
if let currentLineHeight = userProperties.getProperty(reference: ReadiumCSSReference.lineHeight.rawValue) as? Incrementable {
340377
userDefaults.set(currentLineHeight.value, forKey: ReadiumCSSName.lineHeight.rawValue)
341378
}
379+
380+
if let currentParagraphMargins = userProperties.getProperty(reference: ReadiumCSSReference.paragraphMargins.rawValue) as? Incrementable {
381+
userDefaults.set(currentParagraphMargins.value, forKey: ReadiumCSSName.paragraphMargins.rawValue)
382+
}
342383

343384
}
344385

0 commit comments

Comments
 (0)