-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.rs
53 lines (51 loc) · 1.65 KB
/
drawing.rs
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
use font::glyph::{Glyph, Segment};
use font::{Metrics, Number, Offset};
use svg::node::{element, Node};
pub fn draw(glyph: &Glyph) -> element::Group {
let mut group = element::Group::new();
let mut data = element::path::Data::new();
let mut a = Offset::default();
for contour in glyph.iter() {
a += contour.offset;
data = data.move_to(vec![a.0, a.1]);
for segment in contour.iter() {
match *segment {
Segment::Linear(b) => {
a += b;
data = data.line_by(vec![b.0, b.1]);
}
Segment::Quadratic(b, mut c) => {
c += b;
a += c;
data = data.quadratic_curve_by(vec![b.0, b.1, c.0, c.1]);
}
Segment::Cubic(b, mut c, mut d) => {
c += b;
d += c;
a += d;
data = data.cubic_curve_by(vec![b.0, b.1, c.0, c.1, d.0, d.1]);
}
}
}
data = data.close();
}
if !data.is_empty() {
group.append(element::Path::new().set("d", data));
}
group
}
pub fn transform(
glyph: &Glyph,
_: &Metrics,
reference: &Glyph,
document_size: Number,
) -> (Number, Number, Number) {
const BASELINE: Number = 0.75;
const MULTIPLIER: Number = 1.75;
let (left, _, right, _) = glyph.bounding_box;
let glyph_size = MULTIPLIER * reference.bounding_box.3;
let scale = document_size / glyph_size;
let x = -glyph.side_bearings.0 + (glyph_size - (right - left)) / 2.0;
let y = BASELINE * glyph_size;
(x, y, scale)
}