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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::io::Write;

use crate::escape::escape_xml;
pub use circle::*;
pub use color::*;
use common_attributes::*;
pub use element::*;
pub use group::*;
pub use id::*;
pub use path::*;
pub use rect::*;
pub use script::*;
pub use stroke::*;
pub use style::*;
pub use svg_use::*;
pub use symbol::*;
pub use transform::*;
pub use view_box::*;

pub mod circle;
pub mod color;
pub mod common_attributes;
pub mod element;
pub(crate) mod escape;
pub mod group;
pub mod id;
pub mod path;
pub mod rect;
pub mod script;
pub mod stroke;
pub mod style;
pub mod svg_use;
pub mod symbol;
pub mod transform;
pub mod view_box;

pub type Coordinate = f64;

pub type SvgInteger = i64;

enum OptionalSvgId {
    None,
    Some(SvgId),
    Def(SvgId),
}

pub struct SvgImage {
    id_sequence: u32,
    dimensions: Option<(SvgInteger, SvgInteger)>,
    view_box: Option<ViewBox>,
    elements: Vec<(OptionalSvgId, SvgElement)>,
    data_attributes: Vec<(String, String)>,
    common_attributes: CommonAttributes,
}

implement_common_attributes!(SvgImage);

impl SvgImage {
    pub const fn new() -> Self {
        Self {
            id_sequence: 0,
            dimensions: None,
            view_box: None,
            elements: Vec::new(),
            data_attributes: Vec::new(),
            common_attributes: CommonAttributes::new(),
        }
    }

    pub const fn dimensions(mut self, width: SvgInteger, height: SvgInteger) -> Self {
        self.dimensions = Some((width, height));
        self
    }

    pub fn data_attribute(mut self, name: String, value: String) -> Self {
        self.data_attributes.push((name, value));
        self
    }

    pub fn view_box<V: Into<ViewBox>>(mut self, view_box: V) -> Self {
        self.view_box = Some(view_box.into());
        self
    }

    pub fn add<E: Into<SvgElement>>(&mut self, element: E) -> &mut Self {
        self.elements.push((OptionalSvgId::None, element.into()));
        self
    }

    pub fn add_with_id<E: Into<SvgElement>>(&mut self, element: E) -> SvgId {
        let new_id = SvgId {
            value: self.id_sequence,
        };
        self.id_sequence += 1;
        self.elements
            .push((OptionalSvgId::Some(new_id), element.into()));
        new_id
    }

    pub fn define<E: Into<SvgElement>>(&mut self, element: E) -> SvgId {
        let new_id = SvgId {
            value: self.id_sequence,
        };
        self.id_sequence += 1;
        self.elements
            .push((OptionalSvgId::Def(new_id), element.into()));
        new_id
    }

    pub fn to_svg_string(&self) -> String {
        #![allow(clippy::unwrap_used)]
        let mut buffer = Vec::new();
        buffer
            .write_all(b"<svg xmlns=\"http://www.w3.org/2000/svg\"")
            .unwrap();
        if let Some((x, y)) = &self.dimensions {
            buffer
                .write_all(format!(" x=\"{x}\" y=\"{y}\"").as_bytes())
                .unwrap();
        }
        if let Some(view_box) = &self.view_box {
            let s = format!(
                " viewBox=\"{} {} {} {}\" preserveAspectRatio=\"xMidYMid\"",
                view_box.min_x, view_box.min_y, view_box.width, view_box.height
            );
            buffer.write_all(s.as_bytes()).unwrap();
        }
        for (name, value) in &self.data_attributes {
            buffer
                .write_all(
                    format!("data-{}=\"{}\"", escape_xml(name), escape_xml(value)).as_bytes(),
                )
                .unwrap();
        }
        self.common_attributes.write(&mut buffer);
        buffer.write_all(&[b'>', b'\n']).unwrap();

        let mut first = true;
        for (id, element) in &self.elements {
            if let OptionalSvgId::Def(id) = id {
                if first {
                    first = false;
                    buffer.write_all(b"<defs>").unwrap();
                }
                element.write(Some(*id), &mut buffer);
            }
        }
        if !first {
            buffer.write_all(b"</defs>").unwrap();
        }

        for (id, element) in &self.elements {
            match id {
                OptionalSvgId::None => {
                    element.write(None, &mut buffer);
                }
                OptionalSvgId::Some(id) => {
                    element.write(Some(*id), &mut buffer);
                }
                OptionalSvgId::Def(_) => {}
            }
        }

        buffer.write_all(b"</svg>").unwrap();
        String::from_utf8(buffer).unwrap()
    }
}

#[test]
fn test() {
    let mut image = SvgImage::new()
        .dimensions(200, 00)
        .view_box((-100, -100, 200, 200))
        .style("--step: 0");
    for (offset_x, offset_y, color) in [
        (-100, -100, (0xFF, 0, 0)),
        (0, -100, (0, 0xFF, 0)),
        (-100, 0, (0, 0, 0xFF)),
        (0, 0, (0xFF, 0xFF, 0xFF)),
    ] {
        let id = image.add_with_id(
            SvgGroup::with_elements(vec![SvgRect::default()
                .x(offset_x)
                .y(offset_y)
                .width(100)
                .height(100)
                .fill(SvgColor::Rgb(color.0, color.1, color.2))])
            .style("opacity: 0"),
        );
        if color.0 == 0xff {
            image.add(SvgScript::new(format!(
                "setTimeout(() => {{ document.getElementById('{id}').remove(); }}, 1000);"
            )));
        }
    }
    image.add(SvgPath {
        stroke: Some(SvgColor::Rgb(0xFF, 0xFF, 0)),
        shape: SvgShape::at(10.6, 10.).close(),
        ..Default::default()
    });
    println!("###:\n{}\n###", image.to_svg_string());
    /*

    */
}