1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! Module for parsing [`<transform-list>`] data.
//!
//! [`<transform-list>`]: https://www.w3.org/TR/SVG/coords.html#TransformAttribute
use error::{
Result,
};
use {
ErrorKind,
FromSpan,
Stream,
StreamExt,
StrSpan,
};
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum Token {
Matrix {
a: f64,
b: f64,
c: f64,
d: f64,
e: f64,
f: f64,
},
Translate {
tx: f64,
ty: f64,
},
Scale {
sx: f64,
sy: f64,
},
Rotate {
angle: f64,
},
SkewX {
angle: f64,
},
SkewY {
angle: f64,
},
}
/// Transform tokenizer.
pub struct Tokenizer<'a> {
stream: Stream<'a>,
rotate_ts: Option<(f64, f64)>,
last_angle: Option<f64>,
}
impl<'a> FromSpan<'a> for Tokenizer<'a> {
fn from_span(span: StrSpan<'a>) -> Self {
Tokenizer {
stream: Stream::from_span(span),
rotate_ts: None,
last_angle: None,
}
}
}
impl<'a> Iterator for Tokenizer<'a> {
type Item = Result<Token>;
/// Extracts next transform from the stream.
///
/// # Errors
///
/// - Most of the `Error` types can occur.
///
/// # Notes
///
/// - There are no separate `rotate(<rotate-angle> <cx> <cy>)` type.
/// It will be automatically split into three `Transform` tokens:
/// `translate(<cx> <cy>) rotate(<rotate-angle>) translate(-<cx> -<cy>)`.
/// Just like the spec is stated.
fn next(&mut self) -> Option<Self::Item> {
if let Some(a) = self.last_angle {
self.last_angle = None;
return Some(Ok(Token::Rotate {
angle: a,
}));
}
if let Some((x, y)) = self.rotate_ts {
self.rotate_ts = None;
return Some(Ok(Token::Translate {
tx: -x,
ty: -y,
}));
}
self.stream.skip_spaces();
if self.stream.at_end() {
// empty attribute is still a valid value
return None;
}
let ts = self.parse_next();
if ts.is_err() {
self.stream.jump_to_end();
}
Some(ts)
}
}
impl<'a> Tokenizer<'a> {
fn parse_next(&mut self) -> Result<Token> {
let s = &mut self.stream;
let start = s.pos();
let name = s.consume_name()?;
s.skip_spaces();
s.consume_byte(b'(')?;
let t = match name.as_bytes() {
b"matrix" => {
Token::Matrix {
a: s.parse_list_number()?,
b: s.parse_list_number()?,
c: s.parse_list_number()?,
d: s.parse_list_number()?,
e: s.parse_list_number()?,
f: s.parse_list_number()?,
}
}
b"translate" => {
let x = s.parse_list_number()?;
s.skip_spaces();
let y = if s.is_curr_byte_eq(b')') {
// 'If <ty> is not provided, it is assumed to be zero.'
0.0
} else {
s.parse_list_number()?
};
Token::Translate {
tx: x,
ty: y,
}
}
b"scale" => {
let x = s.parse_list_number()?;
s.skip_spaces();
let y = if s.is_curr_byte_eq(b')') {
// 'If <sy> is not provided, it is assumed to be equal to <sx>.'
x
} else {
s.parse_list_number()?
};
Token::Scale {
sx: x,
sy: y,
}
}
b"rotate" => {
let a = s.parse_list_number()?;
s.skip_spaces();
if !s.is_curr_byte_eq(b')') {
// 'If optional parameters <cx> and <cy> are supplied, the rotate is about the
// point (cx, cy). The operation represents the equivalent of the following
// specification:
// translate(<cx>, <cy>) rotate(<rotate-angle>) translate(-<cx>, -<cy>).'
let cx = s.parse_list_number()?;
let cy = s.parse_list_number()?;
self.rotate_ts = Some((cx, cy));
self.last_angle = Some(a);
Token::Translate {
tx: cx,
ty: cy,
}
} else {
Token::Rotate {
angle: a,
}
}
}
b"skewX" => {
Token::SkewX {
angle: s.parse_list_number()?,
}
}
b"skewY" => {
Token::SkewY {
angle: s.parse_list_number()?,
}
}
_ => {
let pos = s.gen_error_pos_from(start);
return Err(ErrorKind::InvalidTransform(pos).into());
}
};
s.skip_spaces();
s.consume_byte(b')')?;
s.skip_spaces();
if s.is_curr_byte_eq(b',') {
s.advance(1);
}
Ok(t)
}
}