Skip to content

Commit ac42983

Browse files
authored
Rewrite Settings API and introduce PDF settings (#293)
1 parent 9c093e9 commit ac42983

File tree

108 files changed

+5052
-5953
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+5052
-5953
lines changed

docs/guides/epub-custom-fonts.md

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,58 @@
1-
# Adding custom fonts to the EPUB navigator
1+
# The FontFamily type
22

3-
The `EpubNavigatorFragment` supports a limited number of font families by default. To change the available typefaces and add new ones, you need to provide font family declarations when initializing the `EpubNavigatorFragment` factory:
3+
The `FontFamily` type represents a font family that can be picked up by users when they are reading reflowable EPUBs. Some are predefined in the navigator (though typefaces are not necessarily available for them) and you can create new ones using a name of your choice. Providing an `alternate` font family as a fallback is a good idea in case the font file is unreachable.
44

55
```kotlin
6-
EpubNavigatorFragment.createFactory(
7-
...,
8-
config = EpubNavigatorFragment.Configuration(
9-
fontFamilies = listOf(
10-
FontFamily.LITERATA.from(FontFamilySource.GoogleFonts),
11-
FontFamily.ACCESSIBLE_DFA.from(FontFamilySource.ReadiumCss),
12-
FontFamily.OPEN_DYSLEXIC.from(FontFamilySource.Assets("fonts/OpenDyslexic-Regular.otf")),
13-
)
14-
)
15-
)
6+
val openDyslexic = FontFamily(name = "OpenDyslexic", alternate = FontFamily.ACCESSIBLE_DFA)
167
```
178

18-
To use the default list of typefaces, modify or pass the `EpubNavigatorFragment.Configuration.DEFAULT_FONT_FAMILIES` list.
9+
# Adding custom typefaces to the EPUB navigator
1910

20-
## Typefaces available offline
11+
By default, Android doesn't guarantee the availability of any specific typeface on devices. Only generic font families can reliably be used: `sans-serif`, `serif`, `cursive` and `monospace`.
2112

22-
If you want to expose only the font families that are available offline, use this configuration:
23-
24-
```kotlin
25-
EpubNavigatorFragment.Configuration(
26-
fontFamilies = listOf(
27-
FontFamily.SERIF.from(FontFamilySource.System),
28-
FontFamily.SANS_SERIF.from(FontFamilySource.System),
29-
FontFamily.CURSIVE.from(FontFamilySource.System),
30-
FontFamily.MONOSPACE.from(FontFamilySource.System),
31-
FontFamily.ACCESSIBLE_DFA.from(FontFamilySource.ReadiumCss),
32-
FontFamily.IA_WRITER_DUOSPACE.from(FontFamilySource.ReadiumCss),
33-
)
34-
)
35-
```
36-
37-
By default, Android ships with a very limited set of font families. Only these generic ones are available offline: `sans-serif`, `serif`, `cursive` and `monospace`.
38-
39-
Additionally, [Readium CSS](https://github.com/readium/readium-css) embeds two accessible typefaces:
13+
Additionally, the `EpubNavigatorFragment` comes with three font families that you don't have to provide typefaces for:
4014

4115
* [AccessibleDfA](https://github.com/Orange-OpenSource/font-accessible-dfa), by Orange;
42-
* [IA Writer Duospace](https://github.com/iaolo/iA-Fonts/tree/master/iA%20Writer%20Duospace), by iA.
43-
44-
## Typefaces downloaded from Google Fonts
16+
* [IA Writer Duospace](https://github.com/iaolo/iA-Fonts/tree/master/iA%20Writer%20Duospace), by iA;
17+
* [OpenDyslexic](https://opendyslexic.org/).
4518

46-
You can also use font families hosted by [Google Fonts](https://fonts.google.com/) using the `GoogleFonts` font family source. :warning: The device will need an Internet connection to use them.
19+
To add new typefaces, you need to provide font family declarations when initializing the `EpubNavigatorFragment` factory. Make sure that one of the patterns that you provide in `servedAssets` is matching the asset path you pass to `æddSource`.
4720

4821
```kotlin
49-
FontFamily.LITERATA.from(FontFamilySource.GoogleFonts)
22+
EpubNavigatorFragment(
23+
...,
24+
configuration = EpubNavigatorFactory.Configuration(
25+
servedAssets = listOf(
26+
"fonts/.*"
27+
)
28+
).apply {
29+
addFontFamilyDeclaration(FontFamily.LITERATA) {
30+
addFontFace {
31+
addSource("fonts/Literata-VariableFont_opsz,wght.ttf")
32+
setFontStyle(FontStyle.NORMAL)
33+
}
34+
addFontFace {
35+
addSource("fonts/Literata-Italic-VariableFont_opsz,wght.ttf")
36+
setFontStyle(FontStyle.ITALIC)
37+
}
38+
}
39+
}
40+
)
5041
```
5142

52-
Readium declares [a set of open source font families](https://github.com/readium/readium-css/blob/develop/docs/CSS10-libre_fonts.md) which were vetted for reading purposes. Most of them are hosted by Google Fonts.
53-
54-
But you can also declare a custom font:
55-
56-
1. Check out if it's available on Google Fonts and copy its name, for example "[Great Vibes](https://fonts.google.com/betterspecimen/Great+Vibes)".
57-
2. In your app, declare the new `FontFamily` using its exact name. Providing an `alternate` font family as a fallback is a good idea in case the device is offline.
58-
```kotlin
59-
val greatVibes = FontFamily(name = "Great Vibes", alternate = FontFamily.CURSIVE)
60-
```
61-
3. Finally, use the font family with the `GoogleFonts` source in the `Configuration` object.
62-
```kotlin
63-
greatVibes.from(FontFamilySource.GoogleFonts)
64-
```
65-
66-
## Embed typefaces in your app assets
43+
# Customizing the list of fonts available through the EPUB preferences editor
6744

68-
A better way to support new font families is to embed them directly in your app to have them available offline.
45+
If you have added custom typefaces to the EPUB navigator, you probably want to customize the list of fonts available through the preferences editor as well.
6946

70-
1. Add the font files in the [`assets/` directory](https://developer.android.com/guide/topics/resources/providing-resources#OriginalFiles).
71-
2. In your app, declare the new `FontFamily` using a name of your choice. Providing an `alternate` font family as a fallback is a good idea in case the font file is unreachable.
72-
```kotlin
73-
val openDyslexic = FontFamily(name = "OpenDyslexic", alternate = FontFamily.ACCESSIBLE_DFA)
74-
```
75-
3. Finally, use the font family with the `Assets` source in the `Configuration` object, giving the file path relative to the `assets/` directory.
76-
```kotlin
77-
openDyslexic.from(FontFamilySource.Assets("fonts/OpenDyslexic-Regular.otf"))
78-
```
47+
```kotlin
48+
EpubNavigatorFactory(
49+
...,
50+
preferencesEditorConfiguration = EpubPreferencesEditor.Configuration(
51+
fontFamilies = listOf(FontFamily.LITERATA, FontFamily.OPEN_DYSLEXIC, FontFamily.ACCESSIBLE_DFA)
52+
)
53+
)
54+
)
55+
```
7956

8057
## Android Downloadable Fonts
8158

0 commit comments

Comments
 (0)