|
type PathData = Vec<(f64, f64)>; |
|
|
|
struct SVGDataLayer { |
|
color: String, |
|
name: String, |
|
stroke_width: f64, |
|
routes: Vec<PathData>, |
|
} |
|
|
|
struct SVGData { |
|
layers: Vec<SVGDataLayer>, |
|
width: f64, |
|
height: f64, |
|
background: Option<String>, |
|
} |
|
|
|
fn make_layer(l: &SVGDataLayer, index: usize) -> String { |
|
let body = l.routes |
|
.iter() |
|
.map(|route| { |
|
let path_data = route |
|
.iter() |
|
.enumerate() |
|
.map(|(i, (x, y))| { |
|
format!("{} {}, {}", if i == 0 { "M" } else { "L" }, x.to_string(), y.to_string()) |
|
}) |
|
.collect::<Vec<String>>() |
|
.join(" "); |
|
format!("<path d=\"{}\" fill=\"none\" stroke=\"{}\" stroke-width=\"{}\" style=\"mix-blend-mode: multiply;\" />", path_data, l.color, l.stroke_width) |
|
}) |
|
.collect::<Vec<String>>() |
|
.join("\n"); |
|
format!("<g inkscape:groupmode=\"layer\" inkscape:label=\"{} {}\">{}</g>", index, l.name, body) |
|
} |
|
|
|
|
|
fn make_svg(a: &SVGData) -> String { |
|
let body = a.layers |
|
.iter() |
|
.enumerate() |
|
.map(|(i, l)| make_layer(l, i)) |
|
.collect::<Vec<String>>() |
|
.join("\n"); |
|
let background = match &a.background { |
|
Some(color) => color, |
|
None => "white" |
|
}; |
|
format!("<svg style=\"background:{}\" viewBox=\"0 0 {} {}\" width=\"{}mm\" height=\"{}mm\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\">\n{}\n</svg>", background, a.width, a.height, a.width, a.height, body) |
|
} |
|
|
|
/* |
|
fn main() { |
|
let data = SVGData { |
|
width: 100.0, |
|
height: 100.0, |
|
layers: vec![ |
|
SVGDataLayer { |
|
color: "#e22".to_string(), |
|
name: "Diamine Brilliant Red".to_string(), |
|
stroke_width: 0.5, |
|
routes: (0..40) |
|
.map(|i| vec![(10.0 + i as f64, 10.0), (80.0 + i as f64 / 4.0, 90.0)]) |
|
.collect::<Vec<PathData>>(), |
|
}, |
|
SVGDataLayer { |
|
color: "deepskyblue".to_string(), |
|
name: "Diamine Turquoise".to_string(), |
|
stroke_width: 0.5, |
|
routes: (0..40) |
|
.map(|i| vec![(90.0 - i as f64, 10.0), (20.0 - i as f64 / 4.0, 90.0)]) |
|
.collect::<Vec<PathData>>(), |
|
}, |
|
], |
|
background: None, |
|
}; |
|
|
|
println!("{}", make_svg(&data)); |
|
} |
|
*/ |