write_fonts/
round.rs

1//! Rounding whose behavior is defined by the
2//! [font specification](https://learn.microsoft.com/en-us/typography/opentype/spec/otff).
3
4/// Floating-point rounding per the [OpenType spec][spec].
5///
6/// <https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166> captures the rationale
7/// for the current implementation.
8///
9/// Copied from <https://github.com/simoncozens/rust-font-tools/blob/105436d3a617ddbebd25f790b041ff506bd90d44/otmath/src/lib.rs#L17>,
10/// which is in turn copied from <https://github.com/fonttools/fonttools/blob/a55a545b12a9735e303568a9d4c7e75fe6dbd2be/Lib/fontTools/misc/roundTools.py#L23>.
11///
12/// [spec]: https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#coordinate-scales-and-normalization
13pub trait OtRound<U, T = Self> {
14    fn ot_round(self) -> U;
15}
16
17impl OtRound<i16> for f64 {
18    #[inline]
19    fn ot_round(self) -> i16 {
20        (self + 0.5).floor() as i16
21    }
22}
23
24impl OtRound<i16> for f32 {
25    #[inline]
26    fn ot_round(self) -> i16 {
27        (self + 0.5).floor() as i16
28    }
29}
30
31impl OtRound<u16> for f64 {
32    #[inline]
33    fn ot_round(self) -> u16 {
34        (self + 0.5).floor() as u16
35    }
36}
37
38impl OtRound<u16> for f32 {
39    #[inline]
40    fn ot_round(self) -> u16 {
41        (self + 0.5).floor() as u16
42    }
43}
44
45impl OtRound<f64> for f64 {
46    #[inline]
47    fn ot_round(self) -> f64 {
48        (self + 0.5).floor()
49    }
50}
51
52impl OtRound<f32> for f32 {
53    #[inline]
54    fn ot_round(self) -> f32 {
55        (self + 0.5).floor()
56    }
57}
58
59impl OtRound<(i16, i16)> for kurbo::Point {
60    #[inline]
61    fn ot_round(self) -> (i16, i16) {
62        (self.x.ot_round(), self.y.ot_round())
63    }
64}
65
66impl OtRound<kurbo::Vec2> for kurbo::Vec2 {
67    #[inline]
68    fn ot_round(self) -> kurbo::Vec2 {
69        kurbo::Vec2::new((self.x + 0.5).floor(), (self.y + 0.5).floor())
70    }
71}