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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// 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/.
use std::f64;
// external
use svgdom;
use lyon_geom;
// self
use tree;
use tree::prelude::*;
use super::prelude::*;
use super::{
fill,
stroke,
};
pub fn convert(
node: &svgdom::Node,
d: svgdom::Path,
mut parent: tree::Node,
rtree: &mut tree::Tree,
) {
let d = convert_path(d);
if d.len() < 2 {
return;
}
let has_bbox = has_bbox(&d);
let attrs = node.attributes();
let fill = fill::convert(rtree, &attrs, has_bbox);
let stroke = stroke::convert(rtree, &attrs, has_bbox);
let transform = attrs.get_transform(AId::Transform).unwrap_or_default();
// Shapes without a bbox cannot be filled,
// and if there is no stroke than there is nothing to render.
if !has_bbox && stroke.is_none() {
return;
}
if fill.is_none() && stroke.is_none() {
return;
}
parent.append_kind(tree::NodeKind::Path(tree::Path {
id: node.id().clone(),
transform,
fill,
stroke,
segments: d,
}));
}
fn convert_path(mut path: svgdom::Path) -> Vec<tree::PathSegment> {
let mut new_path = Vec::with_capacity(path.len());
path.conv_to_absolute();
// Previous MoveTo coordinates.
let mut pmx = 0.0;
let mut pmy = 0.0;
// Previous coordinates.
let mut px = 0.0;
let mut py = 0.0;
// Previous SmoothQuadratic coordinates.
let mut ptx = None;
let mut pty = None;
for seg in path.iter() {
match *seg {
svgdom::PathSegment::MoveTo { x, y, .. } => {
new_path.push(tree::PathSegment::MoveTo { x, y });
}
svgdom::PathSegment::LineTo { x, y, .. } => {
new_path.push(tree::PathSegment::LineTo { x, y });
}
svgdom::PathSegment::HorizontalLineTo { x, .. } => {
new_path.push(tree::PathSegment::LineTo { x, y: py });
}
svgdom::PathSegment::VerticalLineTo { y, .. } => {
new_path.push(tree::PathSegment::LineTo { x: px, y });
}
svgdom::PathSegment::CurveTo { x1, y1, x2, y2, x, y, .. } => {
new_path.push(tree::PathSegment::CurveTo { x1, y1, x2, y2, x, y });
}
svgdom::PathSegment::SmoothCurveTo { x2, y2, x, y, .. } => {
// 'The first control point is assumed to be the reflection of the second control
// point on the previous command relative to the current point.
// (If there is no previous command or if the previous command
// was not an C, c, S or s, assume the first control point is
// coincident with the current point.)'
let new_x1;
let new_y1;
if let Some(seg) = new_path.last().cloned() {
match seg {
tree::PathSegment::CurveTo { x2, y2, x, y, .. } => {
new_x1 = x * 2.0 - x2;
new_y1 = y * 2.0 - y2;
}
_ => {
new_x1 = px;
new_y1 = py;
}
}
new_path.push(tree::PathSegment::CurveTo { x1: new_x1, y1: new_y1, x2, y2, x, y });
}
}
svgdom::PathSegment::Quadratic { x1, y1, x, y, .. } => {
// Remember last control point.
ptx = Some(x * 2.0 - x1);
pty = Some(y * 2.0 - y1);
new_path.push(quad_to_curve(px, py, x1, y1, x, y));
}
svgdom::PathSegment::SmoothQuadratic { x, y, .. } => {
// 'The control point is assumed to be the reflection of
// the control point on the previous command relative to
// the current point. (If there is no previous command or
// if the previous command was not a Q, q, T or t, assume
// the control point is coincident with the current point.)'
let new_x1;
let new_y1;
if let (Some(tx), Some(ty)) = (ptx, pty) {
new_x1 = tx;
new_y1 = ty;
// Reset control point.
ptx = Some(x * 2.0 - tx);
pty = Some(y * 2.0 - ty);
} else {
new_x1 = px;
new_y1 = py;
}
new_path.push(quad_to_curve(px, py, new_x1, new_y1, x, y));
}
svgdom::PathSegment::EllipticalArc { rx, ry, x_axis_rotation, large_arc, sweep, x, y, .. } => {
let arc = lyon_geom::SvgArc {
from: [px as f32, py as f32].into(),
to: [x as f32, y as f32].into(),
radii: [rx as f32, ry as f32].into(),
x_rotation: lyon_geom::math::Angle::degrees(x_axis_rotation as f32),
flags: lyon_geom::ArcFlags { large_arc, sweep },
};
arc.for_each_quadratic_bezier(&mut |quad| {
let cubic = quad.to_cubic();
let curve = tree::PathSegment::CurveTo {
x1: cubic.ctrl1.x as f64, y1: cubic.ctrl1.y as f64,
x2: cubic.ctrl2.x as f64, y2: cubic.ctrl2.y as f64,
x: cubic.to.x as f64, y: cubic.to.y as f64,
};
new_path.push(curve);
});
}
svgdom::PathSegment::ClosePath { .. } => {
new_path.push(tree::PathSegment::ClosePath);
}
}
// Remember last position.
if let Some(seg) = new_path.last() {
match *seg {
tree::PathSegment::MoveTo { x, y } => {
px = x;
py = y;
pmx = x;
pmy = y;
}
tree::PathSegment::LineTo { x, y } => {
px = x;
py = y;
}
tree::PathSegment::CurveTo { x, y, .. } => {
px = x;
py = y;
}
tree::PathSegment::ClosePath => {
// ClosePath moves us to the last MoveTo coordinate,
// not previous.
px = pmx;
py = pmy;
}
}
}
}
// TODO: find a better way
// if stroke.is_some() {
// // If the controls point coordinate is too close to the end point
// // we have to snap it to the end point. Otherwise, it will produce rendering errors.
// //
// // See e-path-044.svg
//
// // Just a magic/heuristic number.
// let sw = 0.25;
//
// for seg in &mut new_path {
// if let &mut tree::PathSegment::CurveTo
// { ref mut x1, ref mut y1,ref mut x2, ref mut y2, x, y } = seg
// {
// if (x - *x1).abs() < sw { *x1 = x; }
// if (y - *y1).abs() < sw { *y1 = y; }
// if (x - *x2).abs() < sw { *x2 = x; }
// if (y - *y2).abs() < sw { *y2 = y; }
// }
// }
// }
new_path
}
fn quad_to_curve(
px: f64,
py: f64,
x1: f64,
y1: f64,
x: f64,
y: f64
) -> tree::PathSegment {
let quad = lyon_geom::QuadraticBezierSegment {
from: [px as f32, py as f32].into(),
ctrl: [x1 as f32, y1 as f32].into(),
to: [x as f32, y as f32].into(),
};
let cubic = quad.to_cubic();
tree::PathSegment::CurveTo {
x1: cubic.ctrl1.x as f64, y1: cubic.ctrl1.y as f64,
x2: cubic.ctrl2.x as f64, y2: cubic.ctrl2.y as f64,
x: cubic.to.x as f64, y: cubic.to.y as f64,
}
}
fn has_bbox(segments: &[tree::PathSegment]) -> bool {
debug_assert!(!segments.is_empty());
let (mut prev_x, mut prev_y, mut minx, mut miny, mut maxx, mut maxy) = {
if let tree::PathSegment::MoveTo { x, y } = segments[0] {
(x as f32, y as f32, x as f32, y as f32, x as f32, y as f32)
} else {
unreachable!();
}
};
for seg in segments {
match *seg {
tree::PathSegment::MoveTo { x, y }
| tree::PathSegment::LineTo { x, y } => {
let x = x as f32;
let y = y as f32;
prev_x = x;
prev_y = y;
if x > maxx { maxx = x; }
else if x < minx { minx = x; }
if y > maxy { maxy = y; }
else if y < miny { miny = y; }
}
tree::PathSegment::CurveTo { x1, y1, x2, y2, x, y } => {
let x = x as f32;
let y = y as f32;
let curve = lyon_geom::CubicBezierSegment {
from: lyon_geom::math::Point::new(prev_x, prev_y),
ctrl1: lyon_geom::math::Point::new(x1 as f32, y1 as f32),
ctrl2: lyon_geom::math::Point::new(x2 as f32, y2 as f32),
to: lyon_geom::math::Point::new(x, y),
};
prev_x = x;
prev_y = y;
let r = curve.bounding_rect();
let right = r.max_x();
let bottom = r.max_y();
if r.min_x() < minx { minx = r.min_x(); }
if right > maxx { maxx = right; }
if r.min_y() < miny { miny = r.min_y(); }
if bottom > maxy { maxy = bottom; }
}
tree::PathSegment::ClosePath => {}
}
let width = (maxx - minx) as f64;
let height = (maxy - miny) as f64;
if !(width.is_fuzzy_zero() || height.is_fuzzy_zero()) {
return true;
}
}
false
}