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
// 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 svgdom::{
Color,
PaintFallback,
};
use super::prelude::*;
/// Resolve 'currentColor' attribute.
///
/// The function will fallback to a default value when possible.
//
// a-fill-022.svg
pub fn resolve_current_color(doc: &Document) {
let mut ids = Vec::new();
for (_, mut node) in doc.root().descendants().svg() {
ids.clear();
{
let attrs = node.attributes();
for (aid, attr) in attrs.iter().svg() {
match attr.value {
AValue::CurrentColor => {
ids.push(aid);
}
AValue::Paint(_, fallback) => {
if let Some(PaintFallback::CurrentColor) = fallback {
ids.push(aid);
}
}
_ => {}
}
}
}
for id in &ids {
match resolve_color(&node, *id) {
Some(v) => {
let av = node.attributes().get_value(*id).cloned().unwrap();
match av {
AValue::CurrentColor => {
node.set_attribute((*id, v));
}
AValue::Paint(link, _) => {
let fallback = Some(PaintFallback::Color(v));
node.set_attribute((*id, (link.clone(), fallback)));
}
_ => {}
}
}
None => {
warn!("Failed to resolve currentColor for: {}. Removing it.", id);
node.remove_attribute(*id);
}
}
}
}
}
fn resolve_color(node: &Node, aid: AId) -> Option<Color> {
if let Some(n) = node.ancestors().find(|n| n.has_attribute(AId::Color)) {
n.attributes().get_color(AId::Color)
} else {
match aid {
AId::Fill
| AId::FloodColor
| AId::StopColor => Some(Color::new(0, 0, 0)),
AId::LightingColor => Some(Color::new(255, 255, 255)),
_ => None,
}
}
}