|
1 |
| -# Adding custom fonts to the EPUB navigator |
| 1 | +# The FontFamily type |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ```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) |
16 | 7 | ```
|
17 | 8 |
|
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 |
19 | 10 |
|
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`. |
21 | 12 |
|
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: |
40 | 14 |
|
41 | 15 | * [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/). |
45 | 18 |
|
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`. |
47 | 20 |
|
48 | 21 | ```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 | +) |
50 | 41 | ```
|
51 | 42 |
|
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 |
67 | 44 |
|
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. |
69 | 46 |
|
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 | +``` |
79 | 56 |
|
80 | 57 | ## Android Downloadable Fonts
|
81 | 58 |
|
|
0 commit comments