animate/path/
options.rs

1/// A separator type for a list of values.
2///
3/// <https://www.w3.org/TR/SVG11/types.html#DataTypeList>
4#[derive(Clone, Copy, PartialEq, Debug)]
5pub enum ListSeparator {
6    /// `10,20`
7    Comma,
8    /// `10 20`
9    Space,
10    /// `10, 20`
11    CommaSpace,
12}
13
14/// Options for SVG types writing.
15#[derive(Clone, Copy, PartialEq, Debug)]
16pub struct WriteOptions {
17    /// Use #RGB color notation when possible.
18    ///
19    /// By default all colors written using #RRGGBB notation.
20    ///
21    /// # Examples
22    ///
23    /// `#ff0000` -> `#f00`, `#000000` -> `#000`, `#00aa00` -> `#0a0`
24    ///
25    /// Default: disabled
26    pub trim_hex_colors: bool,
27
28    /// Remove leading zero from numbers.
29    ///
30    /// # Examples
31    ///
32    /// - `0.1` -> `.1`
33    /// - `-0.1` -> `-.1`
34    ///
35    /// Default: disabled
36    pub remove_leading_zero: bool,
37
38    /// Use compact path notation.
39    ///
40    /// SVG allow us to remove some symbols from path notation without breaking parsing.
41    ///
42    /// # Examples
43    ///
44    /// `M 10 -20 A 5.5 0.3 -4 1 1 0 -0.1` -> `M10-20A5.5.3-4 1 1 0-.1`
45    ///
46    /// Default: disabled
47    pub use_compact_path_notation: bool,
48
49    /// Join ArcTo flags.
50    ///
51    /// Elliptical arc curve segment has flags parameters, which can have values of `0` or `1`.
52    /// Since we have fixed-width values, we can skip spaces between them.
53    ///
54    /// **Note:** Sadly, but most of the viewers doesn't support such notation,
55    /// even though it's valid according to the SVG spec.
56    ///
57    /// # Examples
58    ///
59    /// `A 5 5 30 1 1 10 10` -> `A 5 5 30 1110 10`
60    ///
61    /// Default: disabled
62    pub join_arc_to_flags: bool,
63
64    /// Remove duplicated commands.
65    ///
66    /// If a segment has the same type as a previous then we can skip command specifier.
67    ///
68    /// # Examples
69    ///
70    /// `M 10 10 L 20 20 L 30 30 L 40 40` -> `M 10 10 L 20 20 30 30 40 40`
71    ///
72    /// Default: disabled
73    pub remove_duplicated_path_commands: bool,
74
75    /// Use implicit LineTo commands.
76    ///
77    /// 'If a MoveTo is followed by multiple pairs of coordinates,
78    /// the subsequent pairs are treated as implicit LineTo commands.'
79    ///
80    /// # Examples
81    ///
82    /// `M 10 10 L 20 20 L 30 30` -> `M 10 10 20 20 30 30`
83    ///
84    /// Default: disabled
85    pub use_implicit_lineto_commands: bool,
86
87    /// Simplify transform matrices into short equivalent when possible.
88    ///
89    /// If not set - all transform will be saved as 'matrix'.
90    ///
91    /// # Examples
92    ///
93    /// ```text
94    /// matrix(1 0 0 1 10 20) -> translate(10 20)
95    /// matrix(1 0 0 1 10 0)  -> translate(10)
96    /// matrix(2 0 0 3 0 0)   -> scale(2 3)
97    /// matrix(2 0 0 2 0 0)   -> scale(2)
98    /// matrix(0 1 -1 0 0 0)  -> rotate(-90)
99    /// ```
100    ///
101    /// Default: disabled
102    pub simplify_transform_matrices: bool,
103
104    /// Set the separator type for list types.
105    ///
106    /// Affects `Points`, `LengthList`, `NumberList` and `Transform`.
107    ///
108    /// Default: `ListSeparator::Space`
109    pub list_separator: ListSeparator,
110}
111
112impl Default for WriteOptions {
113    #[inline]
114    fn default() -> WriteOptions {
115        WriteOptions {
116            trim_hex_colors: false,
117            remove_leading_zero: false,
118            use_compact_path_notation: false,
119            join_arc_to_flags: false,
120            remove_duplicated_path_commands: false,
121            use_implicit_lineto_commands: false,
122            simplify_transform_matrices: false,
123            list_separator: ListSeparator::Space,
124        }
125    }
126}
127
128impl WriteOptions {
129    /// Writes a selected separator to the output buffer.
130    ///
131    /// Uses `WriteOptions::list_separator` option.
132    #[inline]
133    pub fn write_separator(&self, out: &mut Vec<u8>) {
134        match self.list_separator {
135            ListSeparator::Space => out.push(b' '),
136            ListSeparator::Comma => out.push(b','),
137            ListSeparator::CommaSpace => out.extend_from_slice(b", "),
138        }
139    }
140}