use crate::render::svglib::{att, att_to_string, Att, SvgElement, Value}; use std::fmt; pub fn svg() -> Svg { Svg::default() } #[derive(Default)] pub struct Svg { style: Option, elements: Vec>, atts: Vec, } impl Svg { pub fn style(&mut self, style: &str) { self.style = Some(style.to_string()); } pub fn add(&mut self, child: impl SvgElement + 'static) { self.elements.push(Box::new(child)); } pub fn width>(&mut self, width: V) { self.atts.push(att("width", width.into().to_string())); } pub fn height>(&mut self, height: V) { self.atts.push(att("height", height.into().to_string())); } pub fn viewbox>(&mut self, viewbox: V) { self.atts.push(att("viewBox", viewbox)); } pub fn preserveaspectratio>(&mut self, preserveaspectratio: V) { self.atts .push(att("preserveAspectRatio", preserveaspectratio)); } pub fn transform>(&mut self, transform: V) { self.atts.push(att("transform", transform)); } } impl fmt::Display for Svg { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, r#"{}"#, self.atts.iter().map(att_to_string).collect::(), self.style .as_ref() .map(|s| format!("", s)) .unwrap_or("".to_string()) )?; for e in &self.elements { write!(f, "{}", e.to_string().as_str())?; } write!(f, "") } } #[cfg(test)] mod tests { use super::*; use crate::render::svglib::rect::rect; #[test] fn style() { let mut svg = svg(); svg.style(".id { background-color: red; }"); assert_eq!( r#""#, svg.to_string() ) } #[test] fn add_rect() { let mut svg = svg(); svg.preserveaspectratio("none"); svg.add(rect().x(0).y(0).width(10).height(10)); assert_eq!( r#""#, svg.to_string() ) } }