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
// 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 super::prelude::*;
fn bound<T: ::std::cmp::Ord>(min: T, val: T, max: T) -> T {
use std::cmp;
cmp::max(min, cmp::min(max, val))
}
pub fn resolve_font_weight(doc: &Document) {
for (_, mut node) in doc.root().descendants().svg() {
let parent = match node.parent() {
Some(p) => p,
None => continue,
};
let av = node.attributes().get_value(AId::FontWeight).cloned();
if let Some(AValue::String(name)) = av {
match name.as_str() {
"bolder" => {
// By the CSS2 spec the default value should be 400
// so `bolder` will result in 500.
// But Chrome and Inkscape will give us 700.
// Have no idea is it a bug or something, but
// we will follow such behavior for now.
let weight = find_font_weight(&parent, 600);
let weight = bound(100, weight + 100, 900);
node.set_attribute((AId::FontWeight, weight.to_string()));
}
"lighter" => {
// By the CSS2 spec the default value should be 400
// so `lighter` will result in 300.
// But Chrome and Inkscape will give us 200.
// Have no idea is it a bug or something, but
// we will follow such behavior for now.
let weight = find_font_weight(&parent, 300);
let weight = bound(100, weight - 100, 900);
node.set_attribute((AId::FontWeight, weight.to_string()));
}
_ => {}
}
}
}
}
fn find_font_weight(node: &Node, default: i32) -> i32 {
for n in node.ancestors() {
if let Some(v) = n.attributes().get_str(AId::FontWeight) {
return v.parse().unwrap_or(default);
}
}
default
}