33 lines
892 B
Rust
33 lines
892 B
Rust
use crate::qr::QrCode;
|
|
|
|
pub fn render_svg(qr: &QrCode) -> String {
|
|
let matrix_size = qr.size() as u32;
|
|
let margin = qr.margin as u32;
|
|
let total = matrix_size + 2 * margin;
|
|
|
|
let mut svg = String::new();
|
|
svg.push_str(&format!(
|
|
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" viewBox="0 0 {} {}">"#,
|
|
total, total, total, total
|
|
));
|
|
svg.push_str(&format!(
|
|
r#"<rect width="{}" height="{}" fill="white"/>"#,
|
|
total, total
|
|
));
|
|
|
|
for y in 0..matrix_size {
|
|
for x in 0..matrix_size {
|
|
if qr.modules()[y as usize][x as usize] {
|
|
svg.push_str(&format!(
|
|
r#"<rect x="{}" y="{}" width="1" height="1" fill="black"/>"#,
|
|
x + margin,
|
|
y + margin
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
svg.push_str("</svg>");
|
|
svg
|
|
}
|