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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::{Color, Style, ToSvgStr, ViewBox};
use std::fmt::{Display, Formatter, Result};

#[derive(Clone)]
pub struct Svg<'a> {
    pub items: Vec<&'a dyn ToSvgStr>,
    pub siblings: Vec<Svg<'a>>,
    pub viewbox: ViewBox,
    pub style: Style,
}

impl<'a> Svg<'a> {
    pub fn and(mut self, sibling: Svg<'a>) -> Self {
        self.siblings.push(sibling);
        self
    }

    pub fn with_style(mut self, style: &Style) -> Self {
        self.style = style.clone();
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_style(style);
        }
        self
    }

    pub fn with_margin(mut self, margin: f32) -> Self {
        self.viewbox = self.viewbox.with_margin(margin);
        self
    }

    pub fn with_color(mut self, color: Color) -> Self {
        self.style.fill = Some(color);
        self.style.stroke_color = Some(color);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_color(color);
        }
        self
    }

    pub fn with_opacity(mut self, opacity: f32) -> Self {
        self.style.opacity = Some(opacity);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_opacity(opacity);
        }
        self
    }

    pub fn with_fill_color(mut self, fill: Color) -> Self {
        self.style.fill = Some(fill);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_fill_color(fill);
        }
        self
    }

    pub fn with_fill_opacity(mut self, fill_opacity: f32) -> Self {
        self.style.fill_opacity = Some(fill_opacity);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_fill_opacity(fill_opacity);
        }
        self
    }

    pub fn with_stroke_width(mut self, stroke_width: f32) -> Self {
        self.style.stroke_width = Some(stroke_width);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_stroke_width(stroke_width);
        }
        self
    }

    pub fn with_stroke_opacity(mut self, stroke_opacity: f32) -> Self {
        self.style.stroke_opacity = Some(stroke_opacity);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_stroke_opacity(stroke_opacity);
        }
        self
    }

    pub fn with_stroke_color(mut self, stroke_color: Color) -> Self {
        self.style.stroke_color = Some(stroke_color);
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_stroke_color(stroke_color);
        }
        self
    }

    pub fn with_radius(mut self, radius: f32) -> Self {
        self.style.radius = radius;
        for sibling in &mut self.siblings {
            *sibling = sibling.clone().with_radius(radius);
        }
        self
    }

    pub fn svg_str(&self) -> String {
        self.items
            .iter()
            .map(|item| item.to_svg_str(&self.style))
            .chain(self.siblings.iter().map(Svg::svg_str))
            .collect()
    }

    pub fn viewbox(&self) -> ViewBox {
        self.items
            .iter()
            .map(|item| item.viewbox(&self.style))
            .chain(self.siblings.iter().map(Svg::viewbox))
            .fold(self.viewbox, |viewbox, other_viewbox| {
                viewbox.add(&other_viewbox)
            })
    }
}

impl<'a> Display for Svg<'a> {
    fn fmt(&self, fmt: &mut Formatter) -> Result {
        let viewbox = self.viewbox();
        write!(
            fmt,
            r#"<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid meet" viewBox="{x} {y} {w} {h}">{content}</svg>"#,
            x = viewbox.min_x(),
            y = viewbox.min_y(),
            w = viewbox.width(),
            h = viewbox.height(),
            content = self.items
                .iter()
                .map(|item| item.to_svg_str(&self.style))
                .chain(self.siblings.iter().map(Svg::svg_str))
                .collect::<String>()
        )
    }
}