diff options
author | Thomas Letan <contact@thomasletan.fr> | 2019-08-04 14:24:22 +0200 |
---|---|---|
committer | Thomas Letan <contact@thomasletan.fr> | 2019-08-04 14:24:22 +0200 |
commit | e869b6b3acea2c1bcf9ccbc390c4da349ef389c7 (patch) | |
tree | ef8b6575ac4f1323b91e7659e2ef479f331df496 | |
parent | feature: Embed a CCS file in the generated EPUB (diff) |
feature: Add fonts support and make assets relocatable
Default font is et-book, from https://github.com/edwardtufte/et-book.
It is a very beautiful font, but there is no support for bold+italic.
Therefore, so we will probably have to change it in the future.
-rw-r--r-- | fonts/et-book-bold-line-figures.ttf | bin | 0 -> 70912 bytes | |||
-rw-r--r-- | fonts/et-book-display-italic-old-style-figures.ttf | bin | 0 -> 74464 bytes | |||
-rw-r--r-- | fonts/et-book-license | 20 | ||||
-rw-r--r-- | fonts/et-book-roman-line-figures.ttf | bin | 0 -> 71688 bytes | |||
-rw-r--r-- | src/epub.rs | 64 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | templates/chapter.xhtml | 4 | ||||
-rw-r--r-- | templates/content.opf | 5 | ||||
-rw-r--r-- | templates/main.css | 25 |
9 files changed, 110 insertions, 19 deletions
diff --git a/fonts/et-book-bold-line-figures.ttf b/fonts/et-book-bold-line-figures.ttf Binary files differnew file mode 100644 index 0000000..9798360 --- /dev/null +++ b/fonts/et-book-bold-line-figures.ttf diff --git a/fonts/et-book-display-italic-old-style-figures.ttf b/fonts/et-book-display-italic-old-style-figures.ttf Binary files differnew file mode 100644 index 0000000..9da91de --- /dev/null +++ b/fonts/et-book-display-italic-old-style-figures.ttf diff --git a/fonts/et-book-license b/fonts/et-book-license new file mode 100644 index 0000000..1d409da --- /dev/null +++ b/fonts/et-book-license @@ -0,0 +1,20 @@ +Copyright (c) 2015 Dmitry Krasny, Bonnie Scranton, Edward Tufte. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/fonts/et-book-roman-line-figures.ttf b/fonts/et-book-roman-line-figures.ttf Binary files differnew file mode 100644 index 0000000..daceffb --- /dev/null +++ b/fonts/et-book-roman-line-figures.ttf diff --git a/src/epub.rs b/src/epub.rs index 4356f3d..33afa09 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -62,20 +62,71 @@ fn create_chapters(tera : &Tera, chapters : &Vec<Chapter<String>>) -> Result<Vec .collect::<Result<Vec<String>, Error>>() } -pub fn generate(project : &Project<String>) -> Result<(), Error> { +fn template_dir(assets : &PathBuf) -> Result<String, Error> { + let mut res = assets.clone(); - let tera = compile_templates!("../templates/**/*"); + res.push("templates"); + res.push("**"); + res.push("*"); + + res.to_str().map(String::from).ok_or(Error(format!("Compute template dir"))) +} + +fn fonts_dir(assets : &PathBuf) -> Result<PathBuf, Error> { + let mut res = assets.clone(); + + res.push("fonts"); + + Ok(res) +} + +fn install_fonts(assets : &PathBuf, fonts : &Vec<&str>) -> Result<(), Error> { + create_dir_all("OEBPS/Fonts/") + .map_err(|_| Error(String::from("cannot create directory OEBPS/Fonts/")))?; + + for f in fonts { + let mut src = fonts_dir(assets)?; + src.push(f); + let mut dst = PathBuf::from("OEBPS/Fonts"); + dst.push(f); + + std::fs::copy(src, dst) + .map_err(|_| Error(format!("cannot copy {}", f)))?; + + } + + Ok(()) +} + +pub fn generate(project : &Project<String>, assets : &PathBuf) -> Result<(), Error> { + + let tera = compile_templates!(template_dir(assets)?.as_str()); create_mimetype()?; create_container(&tera)?; let files = create_chapters(&tera, &project.chapters)?; + write_template_to( + &tera, + "main.css", + &Context::new(), + &PathBuf::from("OEBPS/Style/main.css") + )?; + + let fonts = vec![ + "et-book-roman-line-figures.ttf", + "et-book-bold-line-figures.ttf", + "et-book-display-italic-old-style-figures.ttf", + ]; + + install_fonts(assets, &fonts)?; + let mut ctx = Context::new(); ctx.insert("title", &project.title); ctx.insert("author", &project.author); ctx.insert("files", &files); - + ctx.insert("fonts", &fonts); write_template_to( &tera, "content.opf", @@ -83,12 +134,5 @@ pub fn generate(project : &Project<String>) -> Result<(), Error> { &PathBuf::from("OEBPS/content.opf") )?; - write_template_to( - &tera, - "main.css", - &Context::new(), - &PathBuf::from("OEBPS/Style/main.css") - )?; - Ok(()) } diff --git a/src/main.rs b/src/main.rs index 956bb78..06410d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ pub mod epub; use project::{Project, Error}; use ogmarkup::typography::FRENCH; +use std::path::PathBuf; use std::fs::{create_dir, remove_dir_all}; const BUILD_DIR : &'static str = "_build"; @@ -31,7 +32,7 @@ fn cd_clean_build_dir() -> Result<(), Error> { Ok(()) } -pub fn build() -> Result<(), Error> { +pub fn build(assets : &PathBuf) -> Result<(), Error> { Project::cd_root()?; let project = Project::find_project()? @@ -39,7 +40,7 @@ pub fn build() -> Result<(), Error> { cd_clean_build_dir()?; - epub::generate(&project)?; + epub::generate(&project, assets)?; Ok(()) } @@ -57,8 +58,12 @@ fn main() -> Result<(), Error> { let (subcommand, _args) = matches.subcommand(); + // TODO: in release mode, look for /usr/share/celtchar/assets + let assets: PathBuf = std::env::current_dir() + .map_err(|_| Error(String::from("cannot get current directory")))?; + match subcommand { - "build" => build()?, + "build" => build(&assets)?, _ => unimplemented!(), } diff --git a/templates/chapter.xhtml b/templates/chapter.xhtml index d4f7c62..8ced676 100644 --- a/templates/chapter.xhtml +++ b/templates/chapter.xhtml @@ -4,9 +4,7 @@ </head> <body> {% if chapter.title %} - <h1> - {{ chapter.title }} - </h1> + <h1>{% filter upper %}{{ chapter.title }}{% endfilter %}</h1> {% endif %} <div class="ogmarkup"> {{ chapter.content }} diff --git a/templates/content.opf b/templates/content.opf index 90201bb..4dcd262 100644 --- a/templates/content.opf +++ b/templates/content.opf @@ -8,8 +8,11 @@ <dc:description>Ceci est une description</dc:description> </metadata> <manifest> - <item href="Style/main.css" id="main.css" media-type="text/css" /> <item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml" /> + <item href="Style/main.css" id="main.css" media-type="text/css" /> + {% for f in fonts %} + <item href="Fonts/{{ f }}" id="{{ f }}" media-type="application/x-font-ttf" /> + {% endfor %} {% for f in files %} <item href="Text/{{ f }}" id="{{ f }}" media-type="application/xhtml+xml" /> {% endfor %} diff --git a/templates/main.css b/templates/main.css index 6e7eb83..eae7896 100644 --- a/templates/main.css +++ b/templates/main.css @@ -1,6 +1,27 @@ +@font-face { + font-family: "et-book"; + font-weight: normal; + font-style: normal; + src: url(../Fonts/et-book-roman-line-figures.ttf); +} + +@font-face { + font-family: "et-book"; + font-weight: bold; + font-style: normal; + src: url(../Fonts/et-book-bold-line-figures.ttf); +} + +@font-face { + font-family: "et-book"; + font-weight: normal; + font-style: italic; + src: url(../Fonts/et-book-display-italic-old-style-figures.ttf); +} + html, body { font-size: 100%; - font-family: serif; + font-family: 'et-book', serif; color: #222; hyphens: auto; } @@ -12,7 +33,7 @@ h1 { } p { - text-indent: 1rem; + text-indent: .9rem; padding: 0; margin: 0; line-height: 1.5em; |