-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove examples/open_ufo, improve examples/load_save
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
Showing
2 changed files
with
57 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.