Skip to content

Commit df8b8d0

Browse files
authored
EPUB: Option to revert to the 2.2.0 font size strategy (#323)
1 parent cf3a4f5 commit df8b8d0

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ All notable changes to this project will be documented in this file. Take a look
44

55
**Warning:** Features marked as *experimental* may change or be removed in a future release without notice. Use with caution.
66

7-
<!--## [Unreleased]-->
7+
## [Unreleased]
8+
9+
### Added
10+
11+
#### Navigator
12+
13+
* New `EPUBNavigatorFragment.Configuration.useReadiumCssFontSize` option to revert to the 2.2.0 strategy for setting the font size of reflowable EPUB publications.
14+
* The native font size strategy introduced in 2.3.0 uses the Android web view's [`WebSettings.textZoom`](https://developer.android.com/reference/android/webkit/WebSettings#setTextZoom(int)) property to adjust the font size. 2.2.0 was using Readium CSS's [`--USER__fontSize` variable](https://readium.org/readium-css/docs/CSS12-user_prefs.html#font-size).
15+
* `WebSettings.textZoom` will work with more publications than `--USER__fontSize`, even the ones poorly authored. However the page width is not adjusted when changing the font size to keep the optimal line length.
816

917
## [2.3.0]
1018

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typealias JavascriptInterfaceFactory = (resource: Link) -> Any?
8686
*
8787
* To use this [Fragment], create a factory with `EpubNavigatorFragment.createFactory()`.
8888
*/
89-
@OptIn(ExperimentalDecorator::class, ExperimentalReadiumApi::class)
89+
@OptIn(ExperimentalDecorator::class, ExperimentalReadiumApi::class, DelicateReadiumApi::class)
9090
class EpubNavigatorFragment internal constructor(
9191
override val publication: Publication,
9292
private val baseUrl: String?,
@@ -132,6 +132,22 @@ class EpubNavigatorFragment internal constructor(
132132
@ExperimentalReadiumApi
133133
var readiumCssRsProperties: RsProperties,
134134

135+
/**
136+
* When disabled, the Android web view's `WebSettings.textZoom` will be used to adjust the
137+
* font size, instead of using the Readium CSS's `--USER__fontSize` variable.
138+
*
139+
* `WebSettings.textZoom` will work with more publications than `--USER__fontSize`, even the
140+
* ones poorly authored. However, the page width is not adjusted when changing the font
141+
* size to keep the optimal line length.
142+
*
143+
* See:
144+
* - https://github.com/readium/mobile/issues/5
145+
* - https://github.com/readium/mobile/issues/1#issuecomment-652431984
146+
*/
147+
@ExperimentalReadiumApi
148+
@DelicateReadiumApi
149+
var useReadiumCssFontSize: Boolean = true,
150+
135151
/**
136152
* Supported HTML decoration templates.
137153
*/
@@ -503,18 +519,20 @@ class EpubNavigatorFragment internal constructor(
503519
}
504520

505521
private fun R2PagerAdapter.setFontSize(fontSize: Double) {
506-
r2PagerAdapter?.mFragments?.forEach { _, fragment ->
522+
if (config.useReadiumCssFontSize) return
523+
524+
mFragments.forEach { _, fragment ->
507525
(fragment as? R2EpubPageFragment)?.setFontSize(fontSize)
508526
}
509527
}
510528

511529
private inner class PagerAdapterListener : R2PagerAdapter.Listener {
512530
override fun onCreatePageFragment(fragment: Fragment) {
513-
if (viewModel.layout == EpubLayout.FIXED) {
514-
return
531+
if (viewModel.layout == EpubLayout.REFLOWABLE) {
532+
if (!config.useReadiumCssFontSize) {
533+
(fragment as? R2EpubPageFragment)?.setFontSize(settings.value.fontSize)
534+
}
515535
}
516-
517-
(fragment as? R2EpubPageFragment)?.setFontSize(settings.value.fontSize)
518536
}
519537
}
520538

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorViewModel.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.readium.r2.navigator.html.HtmlDecorationTemplates
3030
import org.readium.r2.navigator.preferences.*
3131
import org.readium.r2.navigator.util.createViewModelFactory
3232
import org.readium.r2.shared.COLUMN_COUNT_REF
33+
import org.readium.r2.shared.DelicateReadiumApi
3334
import org.readium.r2.shared.ExperimentalReadiumApi
3435
import org.readium.r2.shared.SCROLL_REF
3536
import org.readium.r2.shared.extensions.addPrefix
@@ -44,7 +45,7 @@ internal enum class DualPage {
4445
AUTO, OFF, ON
4546
}
4647

47-
@OptIn(ExperimentalReadiumApi::class, ExperimentalDecorator::class)
48+
@OptIn(ExperimentalReadiumApi::class, ExperimentalDecorator::class, DelicateReadiumApi::class)
4849
internal class EpubNavigatorViewModel(
4950
application: Application,
5051
val publication: Publication,
@@ -118,7 +119,7 @@ internal class EpubNavigatorViewModel(
118119
fontFamilyDeclarations = config.fontFamilyDeclarations,
119120
googleFonts = googleFonts,
120121
assetsBaseHref = WebViewServer.assetsBaseHref
121-
).update(settings.value)
122+
).update(settings.value, useReadiumCssFontSize = config.useReadiumCssFontSize)
122123
)
123124

124125
init {
@@ -230,7 +231,7 @@ internal class EpubNavigatorViewModel(
230231

231232
val newSettings = settingsPolicy.settings(preferences)
232233
_settings.value = newSettings
233-
css.update { it.update(newSettings) }
234+
css.update { it.update(newSettings, useReadiumCssFontSize = config.useReadiumCssFontSize) }
234235

235236
val needsInvalidation: Boolean = (
236237
oldSettings.readingProgression != newSettings.readingProgression ||

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubSettings.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ data class EpubSettings(
6565
) : Configurable.Settings
6666

6767
@OptIn(ExperimentalReadiumApi::class)
68-
internal fun ReadiumCss.update(settings: EpubSettings): ReadiumCss {
68+
internal fun ReadiumCss.update(settings: EpubSettings, useReadiumCssFontSize: Boolean): ReadiumCss {
6969

7070
fun resolveFontStack(fontFamily: String): List<String> = buildList {
7171
add(fontFamily)
@@ -109,9 +109,8 @@ internal fun ReadiumCss.update(settings: EpubSettings): ReadiumCss {
109109
backgroundColor = backgroundColor?.toCss(),
110110
fontOverride = (fontFamily != null || textNormalization),
111111
fontFamily = fontFamily?.toCss(),
112-
// Font size is handled natively with WebSettings.textZoom.
113-
// See https://github.com/readium/mobile/issues/1#issuecomment-652431984
114-
// fontSize = fontSize?.let { Length.Percent(it) },
112+
fontSize = if (useReadiumCssFontSize) Length.Percent(fontSize)
113+
else null,
115114
advancedSettings = !publisherStyles,
116115
typeScale = typeScale,
117116
textAlign = when (textAlign) {

0 commit comments

Comments
 (0)