Merge branch 'master' of rosencrantz:/srv/git/gunpowder_treason
Getty Ritter
6 years ago
1 | use std::fmt::Display; | |
2 | use std::fmt::{Formatter, Error}; | |
3 | use std::io; | |
4 | use std::io::Write; | |
1 | use std::fmt::{self, Display, Formatter}; | |
2 | use std::fs::OpenOptions; | |
3 | use std::io::{self, Write}; | |
5 | 4 | |
6 | 5 | /// An SVG document |
7 | 6 | pub struct SVG { |
13 | 12 | pub struct Inches { amt: f64 } |
14 | 13 | |
15 | 14 | impl Display for Inches { |
16 |
fn fmt(&self, f: &mut Formatter) -> Result<(), |
|
15 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { | |
17 | 16 | self.amt.fmt(f)?; |
18 | 17 | write!(f, "in") |
19 | 18 | } |
64 | 63 | Ok(buf) |
65 | 64 | } |
66 | 65 | |
67 |
|
|
66 | /// Print this SVG document to stdout | |
67 | pub fn output(self, p: &str) -> io::Result<()> { | |
68 | let mut file = { | |
69 | let mut n = 0u32; | |
70 | let mut path = format!("output/{}{:05}.svg", p, n); | |
71 | let mut f = OpenOptions::new().write(true).create_new(true).open(&path); | |
72 | loop { | |
73 | match f { | |
74 | Ok(_) => break, | |
75 | Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => | |
76 | return Err(io::Error::new(e.kind(), "failed to create file")), | |
77 | _ => (), | |
78 | } | |
79 | n += 1; | |
80 | path = format!("output/{}{:05}.svg", p, n); | |
81 | f = OpenOptions::new().write(true).create_new(true).open(&path); | |
82 | } | |
83 | f.unwrap() | |
84 | }; | |
85 | self.write_svg(&mut file) | |
86 | } | |
87 | ||
88 | ||
89 | pub fn write_svg<W: Write>(self, buf: &mut W) -> io::Result<()> { | |
68 | 90 | let (w, h) = self.size; |
69 | 91 | writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")?; |
70 | 92 | xml_open( |