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
use svg::{Document, Node};
use crate::canvas::Canvas;
use crate::render::Renderer;
use crate::shape::{LinePoint, Shape};
use crate::style::Style;
use crate::{Drawing, Position, RGB};
use svg::node::element::path::Data;
use svg::node::element::{tag, Element};
pub struct SvgRenderer {}
impl SvgRenderer {
pub fn new() -> SvgRenderer {
SvgRenderer {}
}
}
impl Renderer for SvgRenderer {
fn render(&self, canvas: &Canvas) -> Vec<u8> {
let mut document = Document::new().set("viewBox", (0, 0, canvas.width, canvas.height));
if let Some(shape) = &canvas.background {
let origin = Position::new(0.0, 0.0);
document = render_shape(shape, &origin, &Style::default(), document);
}
for drawing in canvas.drawings() {
document = render_drawing(drawing, document)
}
document.to_string().into_bytes()
}
}
fn render_drawing(drawing: &Drawing, mut document: Document) -> Document {
document = render_shape(&drawing.shape, &drawing.position, &drawing.style, document);
for drawing in &drawing.display_list.drawings {
document = render_drawing(drawing, document);
}
document
}
fn render_shape(shape: &Shape, position: &Position, style: &Style, document: Document) -> Document {
let mut element;
match shape {
Shape::Rectangle { width, height } => {
element = rect(*width, *height, &position);
}
Shape::Circle { radius } => {
element = circle(*radius, &position);
}
Shape::Line { start, points } => {
element = line(*start, points);
}
}
if let Some(fill) = &style.fill {
element.assign("fill", rgb_to_str(&fill.color));
} else {
element.assign("fill", "transparent");
}
if let Some(stroke) = &style.stroke {
element.assign("stroke", rgb_to_str(&stroke.color));
element.assign("stroke-width", format!("{}", stroke.width));
}
document.add(element)
}
fn rgb_to_str(color: &RGB) -> String {
format!("rgb({},{},{})", color.r, color.g, color.b)
}
fn rect(width: u32, height: u32, position: &Position) -> Element {
let mut element = Element::new(tag::Rectangle);
element.assign("width", width);
element.assign("height", height);
element.assign("x", position.x);
element.assign("y", position.y);
element
}
fn circle(radius: u32, position: &Position) -> Element {
let mut element = Element::new(tag::Circle);
element.assign("r", radius);
element.assign("cx", position.x);
element.assign("cy", position.y);
element
}
fn line(start: Position, points: &Vec<LinePoint>) -> Element {
let mut data = Data::new().move_to((start.x, start.y));
for point in points {
match *point {
LinePoint::Straight { point } => {
data = data.line_to((point.x, point.y));
}
LinePoint::QuadraticBezierCurve { point, curve } => {
data = data.quadratic_curve_to((curve.x, curve.y, point.x, point.y));
}
_ => unimplemented!(),
}
}
let mut element = Element::new(tag::Path);
element.assign("d", data);
element
}