From 1d9e338c5c582b0226286762e81ca873743a70f0 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 13 Nov 2023 13:28:45 +0000 Subject: [PATCH] [fontdrasil/coords] Avoid dups in CoordConverter::unmapped --- fontdrasil/src/coords.rs | 60 ++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/fontdrasil/src/coords.rs b/fontdrasil/src/coords.rs index c0a3b82bd..025bae04d 100644 --- a/fontdrasil/src/coords.rs +++ b/fontdrasil/src/coords.rs @@ -101,14 +101,14 @@ impl CoordConverter { /// Initialize a converter from just min/default/max user coords, e.g. a source with no mapping pub fn unmapped(min: UserCoord, default: UserCoord, max: UserCoord) -> CoordConverter { - CoordConverter::new( - vec![ - (min, DesignCoord::new(min.into_inner())), - (default, DesignCoord::new(default.into_inner())), - (max, DesignCoord::new(max.into_inner())), - ], - 1, - ) + let mut mappings = vec![ + (min, DesignCoord::new(min.into_inner())), + (default, DesignCoord::new(default.into_inner())), + (max, DesignCoord::new(max.into_inner())), + ]; + mappings.dedup(); + let default_idx = mappings.iter().position(|(u, _)| *u == default).unwrap(); + CoordConverter::new(mappings, default_idx) } /// Walk the vertices of the mappings, viewing the user/design/normalized value at each stop. @@ -550,4 +550,48 @@ mod tests { .into_inner() ); } + + #[test] + fn unmapped_coords_get_deduped() { + // min==default==max + assert_eq!( + CoordConverter::unmapped( + UserCoord(100.0.into()), + UserCoord(100.0.into()), + UserCoord(100.0.into()), + ) + .default_idx, + 0 + ); + // min==default