diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..7f8371a --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 145 diff --git a/src/boolean.rs b/src/boolean.rs index 67b0c11..6eb6737 100644 --- a/src/boolean.rs +++ b/src/boolean.rs @@ -2,11 +2,14 @@ use clap::{App, AppSettings, Arg, ArgMatches}; use skia_safe as skia; use std::fs; +use flo::bezier::path as flopath; use flo_curves as flo; use glifparser::outline::skia::{FromSkiaPath as _, ToSkiaPaths as _}; use glifparser::outline::RefigurePointTypes as _; use MFEKmath::{Bezier, Piecewise}; +type PwBez = Piecewise; + pub fn clap_app() -> clap::App<'static> { App::new("BOOLEAN") .setting(AppSettings::DeriveDisplayOrder) @@ -43,22 +46,21 @@ pub fn clap_app() -> clap::App<'static> { } fn apply_flo(pathop: FloPathOp, operand: Option<&str>, outline: &glifparser::Outline) -> glifparser::Outline<()> { - let pw: Piecewise> = Piecewise::from(outline); - let o_pw: Option>> = { + let pw: Piecewise = Piecewise::from(outline); + let o_pw: Option> = { operand.map(|operand| { - let operand: glifparser::Glif<()> = - glifparser::read(&fs::read_to_string(operand).expect("Failed to read operand path file!")) - .expect("glifparser couldn't parse operand path glif. Invalid glif?"); + let operand: glifparser::Glif<()> = glifparser::read(&fs::read_to_string(operand).expect("Failed to read operand path file!")) + .expect("glifparser couldn't parse operand path glif. Invalid glif?"); Piecewise::from(&operand.outline.expect("No in operand glif")) }) }; let out = match pathop { - FloPathOp::RemoveInterior => flo::bezier::path::path_remove_interior_points::, Piecewise>(&pw.segs, 1.), - FloPathOp::RemoveOverlapping => flo::bezier::path::path_remove_overlapped_points::, Piecewise>(&pw.segs, 1.), - FloPathOp::Intersect => flo::bezier::path::path_intersect::, Piecewise, Piecewise>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.), - FloPathOp::Add => flo::bezier::path::path_add::, Piecewise, Piecewise>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.), - FloPathOp::Sub => flo::bezier::path::path_sub::, Piecewise, Piecewise>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.), + FloPathOp::RemoveInterior => flopath::path_remove_interior_points::(&pw.segs, 1.), + FloPathOp::RemoveOverlapping => flopath::path_remove_overlapped_points::(&pw.segs, 1.), + FloPathOp::Intersect => flopath::path_intersect::(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.), + FloPathOp::Add => flopath::path_add::(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.), + FloPathOp::Sub => flopath::path_sub::(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.), }; Piecewise::new(out, None).to_outline() @@ -69,14 +71,12 @@ fn apply_skia(pathop: skia::PathOp, operand: Option<&str>, outline: &glifparser: let mut final_skpath; let operand = operand.map(|oper| { - glifparser::read::<()>( - &fs::read_to_string(oper).expect("Failed to read operand path file!"), - ) - .expect("glifparser couldn't parse operand path glif. Invalid glif?") - .outline - .expect("no in glif") - .to_skia_paths(None) - .combined() + glifparser::read::<()>(&fs::read_to_string(oper).expect("Failed to read operand path file!")) + .expect("glifparser couldn't parse operand path glif. Invalid glif?") + .outline + .expect("no in glif") + .to_skia_paths(None) + .combined() }); if pathop == skia::PathOp::Union && operand.is_none() { @@ -104,7 +104,7 @@ enum FloPathOp { #[derive(Clone, Debug, PartialEq, Eq)] enum EngineOp { Skia(skia::PathOp), - FloCurves(FloPathOp) + FloCurves(FloPathOp), } pub fn cli(matches: &ArgMatches) { @@ -126,9 +126,8 @@ pub fn cli(matches: &ArgMatches) { s => panic!("flo_curves mode {} unavailable", s), }; - let mut path: glifparser::Glif<()> = - glifparser::read(&fs::read_to_string(path_string).expect("Failed to read path file!")) - .expect("glifparser couldn't parse input path glif. Invalid glif?"); + let mut path: glifparser::Glif<()> = glifparser::read(&fs::read_to_string(path_string).expect("Failed to read path file!")) + .expect("glifparser couldn't parse input path glif. Invalid glif?"); if let Some(ref outline) = path.outline.as_ref() { let mut final_output = match engine_op { diff --git a/src/clear.rs b/src/clear.rs index ddfe189..0f13f56 100644 --- a/src/clear.rs +++ b/src/clear.rs @@ -7,49 +7,54 @@ use glifparser::glif::mfek::traits::*; pub fn clap_app() -> clap::App<'static> { App::new("CLEAR") - .setting(AppSettings::DeriveDisplayOrder) - .about("Delete all contours in glyph") - .version("0.0.0") - .author("Fredrick Brennan ; MFEK Authors") - .arg(Arg::new("input") + .setting(AppSettings::DeriveDisplayOrder) + .about("Delete all contours in glyph") + .version("0.0.0") + .author("Fredrick Brennan ; MFEK Authors") + .arg( + Arg::new("input") .long("input") .short('i') .takes_value(true) .required(true) .allow_invalid_utf8(true) - .help("The path to the input UFO `.glif` file. (will be overwritten!)")) - .arg(Arg::new("prune-contour-ops") + .help("The path to the input UFO `.glif` file. (will be overwritten!)"), + ) + .arg( + Arg::new("prune-contour-ops") .long("prune-contour-ops") .short('P') .takes_value(false) .required(false) - .help("Prune contour ops?")) + .help("Prune contour ops?"), + ) } pub fn cli(matches: &ArgMatches) { let path_string: path::PathBuf = matches.value_of_os("input").unwrap().into(); // required options shouldn't panic let prune_contour_ops = matches.is_present("prune-contour-ops"); - let ext = path_string.extension().map(|e|e.to_ascii_lowercase().to_string_lossy().to_string()).unwrap_or(String::from("glif")); + let ext = path_string + .extension() + .map(|e| e.to_ascii_lowercase().to_string_lossy().to_string()) + .unwrap_or(String::from("glif")); match ext.as_str() { "glif" => { - let mut glif: glifparser::Glif<()> = - glifparser::read(&fs::read_to_string(&path_string).expect("Failed to read path file!")) - .expect("glifparser couldn't parse input path glif. Invalid glif?"); + let mut glif: glifparser::Glif<()> = glifparser::read(&fs::read_to_string(&path_string).expect("Failed to read path file!")) + .expect("glifparser couldn't parse input path glif. Invalid glif?"); glif.outline = None; glifparser::write_to_filename(&glif, path_string).unwrap(); - }, + } "glifjson" => { - let mut glif: glifparser::MFEKGlif<()> = - serde_json::from_str(&fs::read_to_string(&path_string).expect("Could not open file")) - .expect("Could not deserialize JSON MFEKGlif"); + let mut glif: glifparser::MFEKGlif<()> = serde_json::from_str(&fs::read_to_string(&path_string).expect("Could not open file")) + .expect("Could not deserialize JSON MFEKGlif"); if prune_contour_ops { glif.downgrade_contour_ops(); } else { glif.layers = vec![]; } fs::write(&path_string, serde_json::to_vec_pretty(&glif).unwrap()).expect("Failed to write file"); - }, - _ => unreachable!() + } + _ => unreachable!(), } } diff --git a/src/fit_to_points.rs b/src/fit_to_points.rs index 8e63c8e..586ad7a 100644 --- a/src/fit_to_points.rs +++ b/src/fit_to_points.rs @@ -8,9 +8,8 @@ pub fn cli(matches: &ArgMatches) { let path_string = matches.value_of("input").unwrap(); // required options shouldn't panic let out_string = matches.value_of("output").unwrap(); - let mut glif: Glif<()> = - read(&fs::read_to_string(path_string).expect("Failed to read the path file!")) - .expect("glifparser couldn't parse input path gliph. Invalid gliph?"); + let mut glif: Glif<()> = read(&fs::read_to_string(path_string).expect("Failed to read the path file!")) + .expect("glifparser couldn't parse input path gliph. Invalid gliph?"); let final_result = fit_to_points::fit(glif.outline.unwrap()); glif.outline = Some(final_result); glifparser::write_to_filename(&glif, out_string).expect("Failed to write "); diff --git a/src/main.rs b/src/main.rs index 894c927..65d84bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ use env_logger; #[allow(unused)] mod validators; -mod clear; mod boolean; +mod clear; mod fit_to_points; mod refigure; fn main() { diff --git a/src/refigure.rs b/src/refigure.rs index 3e39802..0735aaa 100644 --- a/src/refigure.rs +++ b/src/refigure.rs @@ -7,56 +7,57 @@ use std::path; pub fn clap_app() -> clap::App<'static> { App::new("REFIGURE") - .setting(AppSettings::DeriveDisplayOrder) - .about("Fix point type/handle colocation errors in .glif files.") - .version("0.0.1") - .author("Fredrick Brennan ; MFEK Authors") - .arg(Arg::new("input") + .setting(AppSettings::DeriveDisplayOrder) + .about("Fix point type/handle colocation errors in .glif files.") + .version("0.0.1") + .author("Fredrick Brennan ; MFEK Authors") + .arg( + Arg::new("input") .long("input") .short('i') .takes_value(true) .required(true) .allow_invalid_utf8(true) - .help("The path to the input UFO `.glif` file. (will be overwritten!)")) - .arg(Arg::new("remove-single-points") + .help("The path to the input UFO `.glif` file. (will be overwritten!)"), + ) + .arg( + Arg::new("remove-single-points") .long("remove-single-points") .short('1') .takes_value(false) .required(false) - .help("Remove contours with single points")) + .help("Remove contours with single points"), + ) } pub fn cli(matches: &ArgMatches) { let path_string: path::PathBuf = matches.value_of_os("input").unwrap().into(); // required options shouldn't panic - let retain_threshold = if matches.is_present("remove-single-points") { - 2 - } else { - 1 - }; + let retain_threshold = if matches.is_present("remove-single-points") { 2 } else { 1 }; - let ext = path_string.extension().map(|e|e.to_ascii_lowercase().to_string_lossy().to_string()).unwrap_or(String::from("glif")); + let ext = path_string + .extension() + .map(|e| e.to_ascii_lowercase().to_string_lossy().to_string()) + .unwrap_or(String::from("glif")); match ext.as_str() { "glif" => { - let mut glif: glifparser::Glif<()> = - glifparser::read(&fs::read_to_string(&path_string).expect("Failed to read path file!")) - .expect("glifparser couldn't parse input path glif. Invalid glif?"); - glif.outline.as_mut().map(|o|o.assert_colocated_within(0.01)); - glif.outline.as_mut().map(|o|o.refigure_point_types()); - glif.outline.as_mut().map(|o|o.retain(|c|c.len() >= retain_threshold)); + let mut glif: glifparser::Glif<()> = glifparser::read(&fs::read_to_string(&path_string).expect("Failed to read path file!")) + .expect("glifparser couldn't parse input path glif. Invalid glif?"); + glif.outline.as_mut().map(|o| o.assert_colocated_within(0.01)); + glif.outline.as_mut().map(|o| o.refigure_point_types()); + glif.outline.as_mut().map(|o| o.retain(|c| c.len() >= retain_threshold)); glifparser::write_to_filename(&glif, path_string).unwrap(); - }, + } "glifjson" => { - let mut glif: glifparser::MFEKGlif<()> = - serde_json::from_str(&fs::read_to_string(&path_string).expect("Could not open file")) - .expect("Could not deserialize JSON MFEKGlif"); + let mut glif: glifparser::MFEKGlif<()> = serde_json::from_str(&fs::read_to_string(&path_string).expect("Could not open file")) + .expect("Could not deserialize JSON MFEKGlif"); for layer in glif.layers.iter_mut() { - layer.outline.retain(|c|c.inner.len() > 1); + layer.outline.retain(|c| c.inner.len() > 1); for outline in layer.outline.iter_mut() { outline.inner.refigure_point_types(); } } fs::write(&path_string, serde_json::to_vec_pretty(&glif).unwrap()).expect("Failed to write file"); - }, - _ => unreachable!() + } + _ => unreachable!(), } } diff --git a/src/validators.rs b/src/validators.rs index cd991b2..107e264 100644 --- a/src/validators.rs +++ b/src/validators.rs @@ -8,12 +8,7 @@ enum ValidatorRange { fn arg_validator_f64_impl(v: &str, range: ValidatorRange) -> Result<(), String> { match v.parse::() { Ok(i) => { - let err = || { - Err(String::from(&format!( - "Value outside allowed range ({:?})", - range - ))) - }; + let err = || Err(String::from(&format!("Value outside allowed range ({:?})", range))); match range { ValidatorRange::All => Ok(()), ValidatorRange::PositiveNotZero => { @@ -62,10 +57,7 @@ pub fn arg_validator_usize(v: &str) -> Result<(), String> { } } -pub fn arg_validator_suffix( - f: &impl Fn(String) -> Result<(), String>, - suffix: char, -) -> impl Fn(String) -> Result<(), String> + '_ { +pub fn arg_validator_suffix(f: &impl Fn(String) -> Result<(), String>, suffix: char) -> impl Fn(String) -> Result<(), String> + '_ { move |mut v| { while v.ends_with(suffix) { assert_eq!(v.pop().unwrap(), suffix);