//! Lays a whole French novel out and writes it as one PDF.
//!
//! The twin of `write_book`, on a text that draws the accented letters as well
//! as the plain ones. It shows what a wider alphabet costs: more outlines to
//! carry, and metrics still stated only for the glyphs the book draws.
//!
//! Usage: `cargo run --example write_french_book -- tmp/french_book.pdf
//! [font.ttf]`
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use hqf_pdf::{Document, Font};
#[path = "shared/out.rs"]
mod out;
#[path = "shared/licence.rs"]
mod licence;
#[path = "shared/book.rs"]
mod book;
/// The font the example draws with when none is given on the command line.
fn default_font() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fonts")
.join("DejaVuSans.ttf")
}
/// The novel this lays out, carried in full.
const BOOK: book::Book = book::Book {
title: "Le tour du monde en quatre-vingts jours",
author: "Jules Verne",
file: "tourdumonde.txt",
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args().skip(1);
let out = args
.next()
.unwrap_or_else(|| out::default_path("french_book"));
let font_path = args.next().map_or_else(default_font, PathBuf::from);
let mut doc = Document::new();
doc.set_license(licence::licensed());
let handle = doc.add_font(Font::parse(fs::read(&font_path)?)?);
let text = fs::read_to_string(BOOK.path())?;
book::lay_out(&mut doc, &handle, &BOOK, &text)?;
let bytes = doc.to_bytes()?;
if let Some(parent) = Path::new(&out).parent() {
fs::create_dir_all(parent)?;
}
fs::write(&out, &bytes)?;
println!("wrote {out}: {} bytes", bytes.len());
Ok(())
}