gdritter repos gunpowder_treason / ec7c38d
Make all measurements in inches by default Getty Ritter 5 years ago
1 changed file(s) with 21 addition(s) and 6 deletion(s). Collapse all Expand all
11 use std::fmt::Display;
2 use std::fmt::{Formatter, Error};
23 use std::io::Write;
34
45 /// An SVG document
78 size: (f64, f64),
89 }
910
11 #[derive(Copy, Clone)]
12 pub struct Inches { amt: f64 }
13
14 impl Display for Inches {
15 fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
16 self.amt.fmt(f)?;
17 write!(f, "in")
18 }
19 }
20
21 fn inches(amt: f64) -> Inches { Inches { amt } }
22
1023 fn xml_tag(w: &mut Vec<u8>, name: &str, attrs: &[(&str, &dyn Display)]) {
1124 write!(w, "<{}", name);
1225 for (k, v) in attrs {
2437 }
2538
2639 fn xml_close(w: &mut Vec<u8>, name: &str) {
27 write!(w, "<{}/>", name);
40 write!(w, "</{}>", name);
2841 }
2942
3043 /// Create a new empty SVG document of the specified width and height
5265 pub fn to_bytes(self) -> Vec<u8> {
5366 let mut buf = Vec::new();
5467 let (w, h) = self.size;
55 writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
68 writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
5669 xml_open(
5770 &mut buf, "svg",
5871 &[("xmlns", &"http://www.w3.org/2000/svg"),
5972 ("version", &"1.1"),
60 ("width", &w),
61 ("height", &h),
73 ("width", &inches(w)),
74 ("height", &inches(h)),
75 ("viewBox", &format!("0 0 {} {}", w, h)),
76 ("stroke-width", &"0.0001in"),
6277 ],
6378 );
6479 for elem in self.stuff {
105120 buf, "circle",
106121 &[("cx", &x),
107122 ("cy", &y),
108 ("r", &"0.5"),
123 ("r", &"0.01in"),
109124 ("fill", &"black"),
110125 ],
111126 );
150165 }
151166
152167 /// Draw a line segment from this point to another point
153 pub fn draw_to(mut self, x: f64, y: f64) -> Line {
168 pub fn to(mut self, x: f64, y: f64) -> Line {
154169 self.points.push((x, y));
155170 self
156171 }