Skip to content

Commit

Permalink
Remove examples/open_ufo, improve examples/load_save
Browse files Browse the repository at this point in the history
I'll be honest, I didn't see that load_save existed and so I updated
open_ufo to include saving, and now we have two identical examples. The
new code is modestly better, so let's keep that and delete the extremely
redundant open_ufo example?
  • Loading branch information
cmyr committed Jul 29, 2021
1 parent 39ca112 commit 44778cb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 82 deletions.
94 changes: 57 additions & 37 deletions examples/load_save.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
//! A small program that times the loading and saving of a UFO file.

use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::time::{Duration, Instant};

use norad::Font;

fn main() {
let (input, output) = get_path_or_exit();

let start_load = Instant::now();
let mut my_ufo = match Font::load(input) {
Ok(v) => v,
Err(e) => {
eprintln!("Loading the UFO failed: {}", e);
std::process::exit(1);
}
};
let duration_load = start_load.elapsed();
let duration_load_str = format_time(duration_load);

my_ufo.meta.creator = "org.linebender.norad".to_string();
static HELP: &str = "
USAGE:
open_ufo PATH [OUTPATH]
let start_write = Instant::now();
my_ufo.save(output).unwrap();
let duration_write = start_write.elapsed();
let duration_write_str = format_time(duration_write);
If an OUTPATH is provided, the UFO will be saved after opening.
";

println!("Loaded UFO in {}, wrote it in {}.", duration_load_str, duration_write_str);
macro_rules! exit_err {
($($arg:tt)*) => ({
eprintln!($($arg)*);
eprintln!("{}", HELP);
std::process::exit(1);
})
}

fn get_path_or_exit() -> (PathBuf, PathBuf) {
let mut args = env::args().skip(1);
fn main() {
let args = Args::get_from_env_or_exit();

let input = match args.next().map(PathBuf::from) {
Some(ref p) if p.exists() && p.extension() == Some(OsStr::new("ufo")) => p.to_owned(),
_ => {
eprintln!("Please supply a path to a UFO to read from");
std::process::exit(1);
}
};
let output = match args.next().map(PathBuf::from) {
Some(ref p) if p.extension() == Some(OsStr::new("ufo")) => p.to_owned(),
_ => {
eprintln!("Please supply a path to write the UFO to");
std::process::exit(1);
}
};
let start = Instant::now();
let ufo = Font::load(&args.path).expect("failed to load file");

let duration = start.elapsed();
let time_str = format_time(duration);
let font_name = ufo
.font_info
.as_ref()
.and_then(|f| f.family_name.clone())
.unwrap_or_else(|| "an unnamed font".into());

(input, output)
println!("loaded {} glyphs from {} in {}.", ufo.glyph_count(), font_name, time_str);

if let Some(outpath) = args.outpath {
let start = Instant::now();
ufo.save(outpath).expect("failed to save UFO");
let duration = start.elapsed();
let time_str = format_time(duration);
println!("wrote UFO to disk in {}", time_str);
}
}

fn format_time(duration: Duration) -> String {
let secs = duration.as_secs();
let millis = duration.subsec_millis();
format!("{}.{}s", secs, millis)
}

struct Args {
path: PathBuf,
outpath: Option<PathBuf>,
}

impl Args {
fn get_from_env_or_exit() -> Self {
let mut args = env::args().skip(1);
let path = match args.next().map(PathBuf::from) {
Some(ref p) if p.exists() && p.extension() == Some(OsStr::new("ufo")) => p.to_owned(),
Some(ref p) => exit_err!("path {:?} is not an existing .ufo file, exiting", p),
None => exit_err!("Please supply a path to a .ufo file"),
};

let outpath = args.next().map(PathBuf::from);
if outpath.as_ref().map(|p| p.exists()).unwrap_or(false) {
exit_err!("outpath {} already exists, exiting", outpath.unwrap().display());
}

Args { path, outpath }
}
}
45 changes: 0 additions & 45 deletions examples/open_ufo.rs

This file was deleted.

0 comments on commit 44778cb

Please sign in to comment.