gdritter repos gunpowder_treason / ed0f021
Merge branch 'master' of rosencrantz:/srv/git/gunpowder_treason Getty Ritter 5 years ago
1 changed file(s) with 28 addition(s) and 6 deletion(s). Collapse all Expand all
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};
54
65 /// An SVG document
76 pub struct SVG {
1312 pub struct Inches { amt: f64 }
1413
1514 impl Display for Inches {
16 fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
15 fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
1716 self.amt.fmt(f)?;
1817 write!(f, "in")
1918 }
6463 Ok(buf)
6564 }
6665
67 pub fn write_svg<W: Write>(self, buf: &mut W) -> io::Result<()> {
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<()> {
6890 let (w, h) = self.size;
6991 writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")?;
7092 xml_open(