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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/// To specify in which color to draw either a path or the svg background
#[derive(Debug,PartialEq)]
pub enum Color {
    None,
    White,
    Black,
    Blue,
    Green,
    Red,
    RGB(u8,u8,u8),
}

/// Create a path by specifying stroke, stroke-width, fill and rules such as Move To, etc.
pub struct Path {
    rules: Vec<String>,
    stroke: Color,
    stroke_width: usize,
    fill: Color,
}

impl Path {
    pub fn new() -> Path {
        Path {
            rules: Vec::new(),
            stroke: Color::None,
            stroke_width: 0,
            fill: Color::None,
        }
    }

    /// Sets `stroke=\"YourColor\"`. If unset it will remain as `stroke="none"`.
    pub fn set_stroke_color(&mut self, color: Color) {
        self.stroke = color;
    }

    /// Sets `stroke-width=\"YourWidth\"`. If unset it will remain as `stroke-width=\"0\"`.
    pub fn set_stroke_width(&mut self, width: usize) {
        self.stroke_width = width;
    }

    /// Sets fill=\"YourColor\". If unset it will remain as `fill=\"none\"`.
    pub fn set_fill_color(&mut self, color: Color) {
        self.fill = color;
    }

    /// Adds rule `"M x y"`
    pub fn move_to(&mut self, pos: [usize;2]) {
        self.rules.push(format!("M {} {} ",pos[0],pos[1]));
    }

    /// Adds rule `"l x y"`
    pub fn line_to(&mut self, pos: [usize;2]) {
        self.rules.push(format!("L {} {} ",pos[0],pos[1]));
    }

    /// Adds rule `"c x1 y1, x2 y2, x y"`
    pub fn bezier(&mut self, points: [usize;6]) {
        self.rules.push(format!("C {} {}, {} {}, {} {} ",points[0],points[1],points[2],points[3],points[4],points[5]));
    }

    /// Closes the path with `'Z'`
    pub fn close_path(&mut self) {
        self.rules.push("Z ".to_string());
    }

    /// Removes the last rule
    pub fn undo(&mut self) {
        self.rules.pop();
    }

    /// Returns a `String` with a path of type `"<path d=\"...\" stroke=\"...\" stroke-width=\"...\" fill=\"...\" />"`
    pub fn create(&mut self) -> String {
        let mut path = String::new();

        path.push_str(&format!("<path d=\""));

        for rule in &self.rules {
            path.push_str(&rule);
        }
        

        path.push_str("\" stroke=\"");

        match &self.stroke {
            Color::None => {
                path.push_str("none\" ");
            },
            Color::White => {
                path.push_str("white\" ");
            },
            Color::Black => {
                path.push_str("black\" ");
            },
            Color::Blue => {
                path.push_str("blue\" ");
            },
            Color::Green => {
                path.push_str("green\" ");
            },
            Color::Red => {
                path.push_str("red\" ");
            },
            Color::RGB(r,g,b) => {
                path.push_str(&format!("rgb({},{},{})\" ",r,g,b));
            },
        }

        path.push_str(&format!("stroke-width=\"{}\" fill=\"", self.stroke_width));

        match &self.fill {
            Color::None => {
                path.push_str("none\" />");
            },
            Color::White => {
                path.push_str("white\" />");
            },
            Color::Black => {
                path.push_str("black\" />");
            },
            Color::Blue => {
                path.push_str("blue\" />");
            },
            Color::Green => {
                path.push_str("green\" />");
            },
            Color::Red => {
                path.push_str("red\" />");
            },
            Color::RGB(r,g,b) => {
                path.push_str(&format!("rgb({},{},{})\" />",r,g,b));
            },
        }

        path
    }

    /// Adds (for example) `"M 0 0 ... L 100 100"` to the current path rules
    pub fn add_rule_raw(&mut self, rule: String) {
        self.rules.push(rule);
    }

    /// Returns a `String` with path information. Example: `"M 0 0 L 100 100 ..."`
    pub fn create_raw(&mut self) -> String {
        let mut path = String::new();

        for rule in &self.rules {
            path.push_str(rule);
        }

        path
    }
}

/// Create an svg structure to hold one or more paths with options to set the viewbox, xmlns, and background color
pub struct MinSVG {
    viewbox: [usize;4],
    xmlns: Option<String>,
    paths: Vec<Path>,
    background: Color,
}

impl MinSVG {
    /// Construct a new svg with the required viewBox
    /// 
    /// Example: `[0,0,100,100]` will result in `viewBox=\"0 0 100 100\"`
    pub fn new(viewbox: [usize;4]) -> MinSVG {
        MinSVG {
            viewbox,
            xmlns: None,
            paths: Vec::new(),
            background: Color::None,
        }
    }

    /// Set a custom namespace. If not invoked the namespace will be `http://www.w3.org/2000/svg`
    pub fn set_xmlns(&mut self, xmlns: String) {
        self.xmlns = Some(xmlns);
    }

    /// Set a background color. If not invoked there will be none.
    pub fn set_background_color(&mut self, color: Color) {
        self.background = color;
    }

    /// Add a path created with `svg_minimal::Path` to the svg.
    pub fn add_path(&mut self, path: Path) {
        self.paths.push(path);
    }

    /// Will return a complete svg with all the requirements.
    pub fn create(&mut self) -> String {
        let mut svg = String::new();

        match &self.xmlns {
            None => {
                svg.push_str(&format!("<svg viewBox=\"{} {} {} {}\" xmlns=\"http://www.w3.org/2000/svg\">",
                                self.viewbox[0],self.viewbox[1],self.viewbox[2],self.viewbox[3]));
            },
            Some(xmlns) => {
                svg.push_str(&format!("<svg viewBox=\"{} {} {} {}\" xmlns=\"{}\">",
                                self.viewbox[0],self.viewbox[1],self.viewbox[2],self.viewbox[3],xmlns));
            }
        }

        match &self.background {
            Color::None => {

            },
            Color::White => {
                svg.push_str(&format!("<rect width=\"{}\" height=\"{}\" style=\"fill:{}\" />", self.viewbox[2], self.viewbox[3],"white"));
            },
            Color::Black => {
                svg.push_str(&format!("<rect width=\"{}\" height=\"{}\" style=\"fill:{}\" />", self.viewbox[2], self.viewbox[3],"black"));
            },
            Color::Blue => {
                svg.push_str(&format!("<rect width=\"{}\" height=\"{}\" style=\"fill:{}\" />", self.viewbox[2], self.viewbox[3],"blue"));
            },
            Color::Green => {
                svg.push_str(&format!("<rect width=\"{}\" height=\"{}\" style=\"fill:{}\" />", self.viewbox[2], self.viewbox[3],"green"));
            },
            Color::Red => {
                svg.push_str(&format!("<rect width=\"{}\" height=\"{}\" style=\"fill:{}\" />", self.viewbox[2], self.viewbox[3],"red"));
            },
            Color::RGB(r,g,b) => {
                svg.push_str(&format!("<rect width=\"{}\" height=\"{}\" style=\"fill:rgb({},{},{})\" />", self.viewbox[2], self.viewbox[3],r,g,b));
            },
        }

        for path in &mut self.paths {
            svg.push_str(&*path.create());
        }

        svg.push_str("</svg>");

        svg
    }

}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_construct_path() {
        let path = Path::new();
        assert_eq!(0,path.rules.len());
        assert_eq!(Color::None,path.stroke);
        assert_eq!(0,path.stroke_width);
        assert_eq!(Color::None,path.fill);
    }

    #[test]
    fn test_add_path_rule() {
        let mut path = Path::new();
        path.move_to([0,0]);
        path.line_to([100,100]);
        assert_eq!("<path d=\"M 0 0 L 100 100 \" stroke=\"none\" stroke-width=\"0\" fill=\"none\" />".to_string(),path.create());
    }

    #[test]
    fn test_create_raw_path() {
        let mut path = Path::new();
        path.move_to([0,0]);
        path.line_to([100,100]);
        assert_eq!("M 0 0 L 100 100 ".to_string(),path.create_raw());
    }

    #[test]
    fn test_construct_svg() {
        let svg = MinSVG::new([0,0,100,100]);
        assert_eq!([0,0,100,100], svg.viewbox);
        assert_eq!(None, svg.xmlns);
    }

    #[test]
    fn test_add_xmlns() {
        let mut svg = MinSVG::new([0,0,100,100]);
        svg.set_xmlns("Some namespace".to_string());
        assert_eq!(Some("Some namespace".to_string()),svg.xmlns);
    }

    #[test]
    fn test_without_xmlns() {
        let svg = MinSVG::new([0,0,100,100]);
        assert_eq!(None,svg.xmlns);
    }

    #[test]
    fn test_svg_and_path_rgb() {
        let mut svg = MinSVG::new([0,0,100,100]);
        svg.set_background_color(Color::RGB(0,0,0));

        let mut path = Path::new();
        path.set_stroke_color(Color::RGB(10,100,50));

        svg.add_path(path);

        assert_eq!("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"100\" height=\"100\" style=\"fill:rgb(0,0,0)\" /><path d=\"\" stroke=\"rgb(10,100,50)\" stroke-width=\"0\" fill=\"none\" /></svg>".to_string(),svg.create());
    }

    #[test]
    fn test_create_svg() {
        let mut svg = MinSVG::new([0,0,500,500]);

        let mut path = Path::new();
        path.set_stroke_color(Color::Black);
        path.set_fill_color(Color::Black);
        path.set_stroke_width(3);
        path.move_to([0,0]);
        path.line_to([0,50]);
        path.line_to([450,500]);
        path.line_to([500,500]);
        path.line_to([0,0]);

        svg.add_path(path);

        svg.set_background_color(Color::Green);

        assert_eq!("<svg viewBox=\"0 0 500 500\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"500\" height=\"500\" style=\"fill:green\" /><path d=\"M 0 0 L 0 50 L 450 500 L 500 500 L 0 0 \" stroke=\"black\" stroke-width=\"3\" fill=\"black\" /></svg>".to_string(),svg.create());

    }

 }