Skip to content

Commit

Permalink
[fontdrasil/coords] Avoid dups in CoordConverter::unmapped
Browse files Browse the repository at this point in the history
  • Loading branch information
anthrotype committed Nov 14, 2023
1 parent de0d78c commit 1d9e338
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions fontdrasil/src/coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<max
assert_eq!(
CoordConverter::unmapped(
UserCoord(0.0.into()),
UserCoord(0.0.into()),
UserCoord(100.0.into()),
)
.default_idx,
0
);
// min<default==max
assert_eq!(
CoordConverter::unmapped(
UserCoord(0.0.into()),
UserCoord(100.0.into()),
UserCoord(100.0.into()),
)
.default_idx,
1
);
// min<default<max
assert_eq!(
CoordConverter::unmapped(
UserCoord(0.0.into()),
UserCoord(50.0.into()),
UserCoord(100.0.into()),
)
.default_idx,
1
);
}
}

0 comments on commit 1d9e338

Please sign in to comment.