animate/path/
length.rs

1use std::str::FromStr;
2
3use super::{Error, FuzzyEq, Result, Stream, WriteBuffer, WriteOptions};
4
5/// List of all SVG length units.
6#[derive(Clone, Copy, Debug, PartialEq)]
7#[allow(missing_docs)]
8pub enum LengthUnit {
9    None,
10    Em,
11    Ex,
12    Px,
13    In,
14    Cm,
15    Mm,
16    Pt,
17    Pc,
18    Percent,
19}
20
21/// Representation of the [`<length>`] type.
22///
23/// [`<length>`]: https://www.w3.org/TR/SVG11/types.html#DataTypeLength
24#[derive(Clone, Copy, Debug, PartialEq)]
25#[allow(missing_docs)]
26pub struct Length {
27    pub num: f64,
28    pub unit: LengthUnit,
29}
30
31impl Length {
32    /// Constructs a new length.
33    #[inline]
34    pub fn new(num: f64, unit: LengthUnit) -> Length {
35        Length { num, unit }
36    }
37
38    /// Constructs a new length with `LengthUnit::None`.
39    #[inline]
40    pub fn new_number(num: f64) -> Length {
41        Length {
42            num,
43            unit: LengthUnit::None,
44        }
45    }
46
47    /// Constructs a new length with a zero number.
48    ///
49    /// Shorthand for: `Length::new(0.0, Unit::None)`.
50    #[inline]
51    pub fn zero() -> Length {
52        Length {
53            num: 0.0,
54            unit: LengthUnit::None,
55        }
56    }
57}
58
59impl Default for Length {
60    #[inline]
61    fn default() -> Self {
62        Length::zero()
63    }
64}
65
66impl FromStr for Length {
67    type Err = Error;
68
69    #[inline]
70    fn from_str(text: &str) -> Result<Self> {
71        let mut s = Stream::from(text);
72        let l = s.parse_length()?;
73
74        if !s.at_end() {
75            return Err(Error::UnexpectedData(s.calc_char_pos()));
76        }
77
78        Ok(Length::new(l.num, l.unit))
79    }
80}
81
82impl WriteBuffer for Length {
83    fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>) {
84        self.num.write_buf_opt(opt, buf);
85
86        let t: &[u8] = match self.unit {
87            LengthUnit::None => b"",
88            LengthUnit::Em => b"em",
89            LengthUnit::Ex => b"ex",
90            LengthUnit::Px => b"px",
91            LengthUnit::In => b"in",
92            LengthUnit::Cm => b"cm",
93            LengthUnit::Mm => b"mm",
94            LengthUnit::Pt => b"pt",
95            LengthUnit::Pc => b"pc",
96            LengthUnit::Percent => b"%",
97        };
98
99        buf.extend_from_slice(t);
100    }
101}
102
103impl ::std::fmt::Display for Length {
104    #[inline]
105    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
106        write!(f, "{}", self.with_write_opt(&WriteOptions::default()))
107    }
108}
109
110impl FuzzyEq for Length {
111    fn fuzzy_eq(&self, other: &Self) -> bool {
112        if self.unit != other.unit {
113            return false;
114        }
115
116        self.num.fuzzy_eq(&other.num)
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123    use std::str::FromStr;
124
125    macro_rules! test_p {
126        ($name:ident, $text:expr, $result:expr) => {
127            #[test]
128            fn $name() {
129                assert_eq!(Length::from_str($text).unwrap(), $result);
130            }
131        };
132    }
133
134    test_p!(parse_1, "1", Length::new(1.0, LengthUnit::None));
135    test_p!(parse_2, "1em", Length::new(1.0, LengthUnit::Em));
136    test_p!(parse_3, "1ex", Length::new(1.0, LengthUnit::Ex));
137    test_p!(parse_4, "1px", Length::new(1.0, LengthUnit::Px));
138    test_p!(parse_5, "1in", Length::new(1.0, LengthUnit::In));
139    test_p!(parse_6, "1cm", Length::new(1.0, LengthUnit::Cm));
140    test_p!(parse_7, "1mm", Length::new(1.0, LengthUnit::Mm));
141    test_p!(parse_8, "1pt", Length::new(1.0, LengthUnit::Pt));
142    test_p!(parse_9, "1pc", Length::new(1.0, LengthUnit::Pc));
143    test_p!(parse_10, "1%", Length::new(1.0, LengthUnit::Percent));
144    test_p!(parse_11, "1e0", Length::new(1.0, LengthUnit::None));
145    test_p!(parse_12, "1.0e0", Length::new(1.0, LengthUnit::None));
146    test_p!(parse_13, "1.0e0em", Length::new(1.0, LengthUnit::Em));
147
148    #[test]
149    fn err_1() {
150        let mut s = Stream::from("1q");
151        assert_eq!(
152            s.parse_length().unwrap(),
153            Length::new(1.0, LengthUnit::None)
154        );
155        assert_eq!(
156            s.parse_length().unwrap_err().to_string(),
157            "invalid number at position 2"
158        );
159    }
160
161    #[test]
162    fn err_2() {
163        assert_eq!(
164            Length::from_str("1mmx").unwrap_err().to_string(),
165            "unexpected data at position 4"
166        );
167    }
168
169    macro_rules! test_w {
170        ($name:ident, $len:expr, $unit:expr, $result:expr) => {
171            #[test]
172            fn $name() {
173                let l = Length::new($len, $unit);
174                assert_eq!(l.to_string(), $result);
175            }
176        };
177    }
178
179    test_w!(write_1, 1.0, LengthUnit::None, "1");
180    test_w!(write_2, 1.0, LengthUnit::Em, "1em");
181    test_w!(write_3, 1.0, LengthUnit::Ex, "1ex");
182    test_w!(write_4, 1.0, LengthUnit::Px, "1px");
183    test_w!(write_5, 1.0, LengthUnit::In, "1in");
184    test_w!(write_6, 1.0, LengthUnit::Cm, "1cm");
185    test_w!(write_7, 1.0, LengthUnit::Mm, "1mm");
186    test_w!(write_8, 1.0, LengthUnit::Pt, "1pt");
187    test_w!(write_9, 1.0, LengthUnit::Pc, "1pc");
188    test_w!(write_10, 1.0, LengthUnit::Percent, "1%");
189}