summaryrefslogtreecommitdiffstats
path: root/examples/dmtx_text.rs
blob: a8a4687ca51b70d8fcc6db9b95dd1e2bf2d7adb7 (plain)
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
use iec16022::DataMatrix;
use std::env::args_os;
use std::os::unix::ffi::OsStringExt;

fn main() {
    let data = args_os()
        .skip(1) // program name
        .map(|arg| arg.into_vec())
        .collect::<Vec<_>>()
        .join(&b' ');
    let dmtx = DataMatrix::encode(&data).unwrap();

    let capacity = (dmtx.width() + 1) * dmtx.height();
    let mut out = String::with_capacity(capacity);
    for row in (0..dmtx.height()).rev() {
        for col in 0..dmtx.width() {
            if dmtx.get(row, col) {
                out.push('*');
            } else {
                out.push(' ');
            }
        }
        out.push('\n');
    }
    debug_assert!(
        capacity == out.capacity(),
        "caculated capacity is not correct"
    );

    print!("{}", out);
}