Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlcctrlv committed Jan 26, 2022
1 parent 53de262 commit 227b966
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 81 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 145
43 changes: 21 additions & 22 deletions src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bezier>;

pub fn clap_app() -> clap::App<'static> {
App::new("BOOLEAN")
.setting(AppSettings::DeriveDisplayOrder)
Expand Down Expand Up @@ -43,22 +46,21 @@ pub fn clap_app() -> clap::App<'static> {
}

fn apply_flo<PD: glifparser::PointData>(pathop: FloPathOp, operand: Option<&str>, outline: &glifparser::Outline<PD>) -> glifparser::Outline<()> {
let pw: Piecewise<Piecewise<Bezier>> = Piecewise::from(outline);
let o_pw: Option<Piecewise<Piecewise<Bezier>>> = {
let pw: Piecewise<PwBez> = Piecewise::from(outline);
let o_pw: Option<Piecewise<PwBez>> = {
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 <outline> in operand glif"))
})
};

let out = match pathop {
FloPathOp::RemoveInterior => flo::bezier::path::path_remove_interior_points::<Piecewise<Bezier>, Piecewise<Bezier>>(&pw.segs, 1.),
FloPathOp::RemoveOverlapping => flo::bezier::path::path_remove_overlapped_points::<Piecewise<Bezier>, Piecewise<Bezier>>(&pw.segs, 1.),
FloPathOp::Intersect => flo::bezier::path::path_intersect::<Piecewise<Bezier>, Piecewise<Bezier>, Piecewise<Bezier>>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.),
FloPathOp::Add => flo::bezier::path::path_add::<Piecewise<Bezier>, Piecewise<Bezier>, Piecewise<Bezier>>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.),
FloPathOp::Sub => flo::bezier::path::path_sub::<Piecewise<Bezier>, Piecewise<Bezier>, Piecewise<Bezier>>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.),
FloPathOp::RemoveInterior => flopath::path_remove_interior_points::<PwBez, PwBez>(&pw.segs, 1.),
FloPathOp::RemoveOverlapping => flopath::path_remove_overlapped_points::<PwBez, PwBez>(&pw.segs, 1.),
FloPathOp::Intersect => flopath::path_intersect::<PwBez, PwBez, PwBez>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.),
FloPathOp::Add => flopath::path_add::<PwBez, PwBez, PwBez>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.),
FloPathOp::Sub => flopath::path_sub::<PwBez, PwBez, PwBez>(&pw.segs, &o_pw.expect("mode requires operand").segs, 1.),
};

Piecewise::new(out, None).to_outline()
Expand All @@ -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 <outline> 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 <outline> in glif")
.to_skia_paths(None)
.combined()
});

if pathop == skia::PathOp::Union && operand.is_none() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
41 changes: 23 additions & 18 deletions src/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <copypasteⒶkittens.ph>; MFEK Authors")
.arg(Arg::new("input")
.setting(AppSettings::DeriveDisplayOrder)
.about("Delete all contours in glyph")
.version("0.0.0")
.author("Fredrick Brennan <copypasteⒶkittens.ph>; 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!(),
}
}
5 changes: 2 additions & 3 deletions src/fit_to_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
55 changes: 28 additions & 27 deletions src/refigure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <copypasteⒶkittens.ph>; 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 <copypasteⒶkittens.ph>; 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!(),
}
}
12 changes: 2 additions & 10 deletions src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ enum ValidatorRange {
fn arg_validator_f64_impl(v: &str, range: ValidatorRange) -> Result<(), String> {
match v.parse::<f64>() {
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 => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 227b966

Please sign in to comment.