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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Functionality for generating SVG representations of barcodes.
//!
//! An SVG can be constructed via the standard constructor pattern
//! or via a constructor method if you want default values.
//!
//! For example:
//!
//! ```rust
//! use barcoders::generators::svg::*;
//!
//! // Specify your own struct fields.
//! let svg = SVG{height: 80,
//!               xdim: 1,
//!               background: Color{rgba: [255, 0, 0, 255]},
//!               foreground: Color::black(),
//!               xmlns: Some(String::from("http://www.w3.org/2000/svg"))};
//!
//! // Or use the constructor for defaults (you must specify the height).
//! let svg = SVG::new(100)
//!               .xdim(2)
//!               .background(Color::white())
//!               .foreground(Color::black())
//!               .xmlns(String::from("http://www.w3.org/2000/svg"));
//! ```

use crate::error::Result;
#[cfg(not(feature = "std"))]
use alloc::{
    format,
    string::{String, ToString},
};

trait ToHex {
    fn to_hex(self) -> String;

    fn format_hex(n: u8) -> String {
        format!(
            "{}{}",
            Self::to_hex_digit(n / 16),
            Self::to_hex_digit(n % 16)
        )
    }

    fn to_hex_digit(n: u8) -> char {
        match n {
            d if d < 10 => (d + 48) as char,
            d if d < 16 => (d + 87) as char,
            _ => '0',
        }
    }
}

/// Represents a RGBA color for the barcode foreground and background.
#[derive(Copy, Clone, Debug)]
pub struct Color {
    /// Reg, Green, Blue, Alpha value.
    pub rgba: [u8; 4],
}

impl Color {
    /// Constructor.
    pub fn new(rgba: [u8; 4]) -> Color {
        Color { rgba }
    }

    /// Constructor for black (#000000).
    pub fn black() -> Color {
        Color::new([0, 0, 0, 255])
    }

    /// Constructor for white (#FFFFFF).
    pub fn white() -> Color {
        Color::new([255, 255, 255, 255])
    }

    fn to_opacity(self) -> String {
        format!("{:.*}", 2, (self.rgba[3] as f64 / 255.0))
    }
}

impl ToHex for Color {
    fn to_hex(self) -> String {
        self.rgba
            .iter()
            .take(3)
            .map(|&c| Self::format_hex(c))
            .collect()
    }
}

/// The SVG barcode generator type.
#[derive(Clone, Debug)]
pub struct SVG {
    /// The height of the barcode (```self.height``` pixels high for SVG).
    pub height: u32,
    /// The X dimension. Specifies the width of the "narrow" bars.
    /// For SVG, each will be ```self.xdim``` pixels wide.
    pub xdim: u32,
    /// The RGBA color for the foreground.
    pub foreground: Color,
    /// The RGBA color for the foreground.
    pub background: Color,
    /// The XML namespace
    pub xmlns: Option<String> 
}

impl SVG {
    /// Returns a new SVG with default values.
    pub fn new(height: u32) -> SVG {
        SVG {
            height,
            xdim: 1,
            foreground: Color {
                rgba: [0, 0, 0, 255],
            },
            background: Color {
                rgba: [255, 255, 255, 255],
            },
            xmlns: None 
        }
    }

    /// Set the xml namespace (xmlns) of the SVG
    pub fn xmlns(mut self, xmlns_uri: String) -> Self {
        self.xmlns = Some(xmlns_uri);
        self
    }

    /// Set the x dimensional bar width
    pub fn xdim(mut self, xdim: u32) -> Self {
        self.xdim = xdim;
        self
    }

    /// Set the foreground (bar) color
    pub fn foreground(mut self, color: Color) -> Self {
        self.foreground = color;
        self
    }

    /// Set the background color
    pub fn background(mut self, color: Color) -> Self {
        self.background= color;
        self
    }

    fn rect(&self, style: u8, offset: u32, width: u32) -> String {
        let fill = match style {
            1 => self.foreground,
            _ => self.background,
        };

        let opacity = match &fill.to_opacity()[..] {
            "1.00" | "1" => "".to_string(),
            o => format!(" fill-opacity=\"{}\" ", o),
        };

        format!(
            "<rect x=\"{}\" y=\"0\" width=\"{}\" height=\"{}\" fill=\"#{}\"{}/>",
            offset,
            width,
            self.height,
            fill.to_hex(),
            opacity
        )
    }

    /// Generates the given barcode. Returns a `Result<String, Error>` of the SVG data or an
    /// error message.
    pub fn generate<T: AsRef<[u8]>>(&self, barcode: T) -> Result<String> {
        let barcode = barcode.as_ref();
        let width = (barcode.len() as u32) * self.xdim;
        let rects: String = barcode
            .iter()
            .enumerate()
            .filter(|&(_, &n)| n == 1)
            .map(|(i, &n)| self.rect(n, i as u32 * self.xdim, self.xdim))
            .collect();

        let xmlns = match &self.xmlns {
            Some(xmlns) => format!("xmlns=\"{xmlns}\" "),
            None => "".to_string() 
        };

        Ok(format!(
            "<svg version=\"1.1\" {x}viewBox=\"0 0 {w} {h}\">{s}{r}</svg>",
            x = xmlns,
            w = width,
            h = self.height,
            s = self.rect(0, 0, width),
            r = rects
        ))
    }
}

#[cfg(test)]
mod tests {
    use crate::generators::svg::*;
    use crate::sym::codabar::*;
    use crate::sym::code11::*;
    use crate::sym::code128::*;
    use crate::sym::code39::*;
    use crate::sym::code93::*;
    use crate::sym::ean13::*;
    use crate::sym::ean8::*;
    use crate::sym::ean_supp::*;
    use crate::sym::tf::*;
    #[cfg(feature = "std")]
    use std::fs::File;
    #[cfg(feature = "std")]
    use std::io::prelude::*;
    #[cfg(feature = "std")]
    use std::io::BufWriter;
    #[cfg(feature = "std")]
    use std::path::Path;

    const TEST_DATA_BASE: &str = "./target/debug";
    const WRITE_TO_FILE: bool = true;

    #[cfg(feature = "std")]
    fn write_file(data: &str, file: &'static str) {
        let path = open_file(file);
        let mut writer = BufWriter::new(path);
        writer.write(data.as_bytes()).unwrap();
    }

    #[cfg(not(feature = "std"))]
    fn write_file(_data: &str, _file: &'static str) {}

    #[cfg(feature = "std")]
    fn open_file(name: &'static str) -> File {
        File::create(&Path::new(&format!("{}/{}", TEST_DATA_BASE, name)[..])).unwrap()
    }

    #[test]
    fn ean_13_as_svg() {
        let ean13 = EAN13::new("750103131130").unwrap();
        let svg = SVG::new(80);
        let generated = svg.generate(&ean13.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "ean13.svg");
        }

        assert_eq!(generated.len(), 2890);
    }

    #[test]
    fn colored_ean_13_as_svg() {
        let ean13 = EAN13::new("750103131130").unwrap();
        let svg = SVG {
            height: 80,
            xdim: 1,
            background: Color {
                rgba: [255, 0, 0, 255],
            },
            foreground: Color {
                rgba: [0, 0, 255, 255],
            },
            xmlns: None
        };
        let generated = svg.generate(&ean13.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "ean13_colored.svg");
        }

        assert_eq!(generated.len(), 2890);
    }

    #[test]
    fn colored_semi_transparent_ean_13_as_svg() {
        let ean13 = EAN13::new("750103131130").unwrap();
        let svg = SVG {
            height: 70,
            xdim: 1,
            background: Color {
                rgba: [255, 0, 0, 128],
            },
            foreground: Color {
                rgba: [0, 0, 255, 128],
            },
            xmlns: None
        };
        let generated = svg.generate(&ean13.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "ean13_colored_semi_transparent.svg");
        }

        assert_eq!(generated.len(), 3940);
    }

    #[test]
    fn ean_8_as_svg() {
        let ean8 = EAN8::new("9998823").unwrap();
        let svg = SVG::new(80).xmlns("http://www.w3.org/2000/svg".to_string());
        let generated = svg.generate(&ean8.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "ean8.svg");
        }

        assert_eq!(generated.len(), 1956);
    }

    #[test]
    fn code39_as_svg() {
        let code39 = Code39::new("IGOT99PROBLEMS").unwrap();
        let svg = SVG::new(80).xmlns("http://www.w3.org/2000/svg".to_string());
        let generated = svg.generate(&code39.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "code39.svg");
        }

        assert_eq!(generated.len(), 6574);
    }

    #[test]
    fn code93_as_svg() {
        let code93 = Code93::new("IGOT99PROBLEMS").unwrap();
        let svg = SVG::new(80).xmlns("http://www.w3.org/2000/svg".to_string());
        let generated = svg.generate(&code93.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "code93.svg");
        }

        assert_eq!(generated.len(), 4493);
    }

    #[test]
    fn codabar_as_svg() {
        let codabar = Codabar::new("A12----34A").unwrap();
        let svg = SVG::new(80).xmlns("http://www.w3.org/2000/svg".to_string());
        let generated = svg.generate(&codabar.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "codabar.svg");
        }

        assert_eq!(generated.len(), 2985);
    }

    #[test]
    fn code128_as_svg() {
        let code128 = Code128::new("ÀHIĆ345678").unwrap();
        let svg = SVG::new(80).xmlns("http://www.w3.org/2000/svg".to_string());
        let generated = svg.generate(&code128.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "code128.svg");
        }

        assert_eq!(generated.len(), 2758);
    }

    #[test]
    fn ean_2_as_svg() {
        let ean2 = EANSUPP::new("78").unwrap();
        let svg = SVG::new(80).xmlns("http://www.w3.org/2000/svg".to_string());
        let generated = svg.generate(&ean2.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "ean2.svg");
        }

        assert_eq!(generated.len(), 760);
    }

    #[test]
    fn itf_as_svg() {
        let itf = TF::interleaved("1234123488993344556677118").unwrap();
        let svg = SVG {
            height: 80,
            xdim: 1,
            background: Color::black(),
            foreground: Color::white(),
            xmlns: None
        };
        let generated = svg.generate(&itf.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "itf.svg");
        }

        assert_eq!(generated.len(), 7123);
    }

    #[test]
    fn code11_as_svg() {
        let code11 = Code11::new("9988-45643201").unwrap();
        let svg = SVG {
            height: 80,
            xdim: 1,
            background: Color::black(),
            foreground: Color::white(),
            xmlns: None
        };
        let generated = svg.generate(&code11.encode()[..]).unwrap();

        if WRITE_TO_FILE {
            write_file(&generated[..], "code11.svg");
        }

        assert_eq!(generated.len(), 4219);
    }
}