feat: 顶层 API + PNG/SVG/ASCII 渲染器
This commit is contained in:
+32
-1
@@ -1 +1,32 @@
|
||||
// FIXME: SVG 渲染器 — Task 10
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user