From 152996ea20b7bb90c840c6556fba41220936f009 Mon Sep 17 00:00:00 2001 From: Erwan Vivien Date: Fri, 29 Dec 2023 21:28:25 +0100 Subject: [PATCH] fix: SVG and images were inverted This is because I called commands with (x, y) instead of (y, x) Fixes #47 --- src/convert/mod.rs | 2 +- src/convert/svg.rs | 2 +- src/tests/svg.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/convert/mod.rs b/src/convert/mod.rs index 0e608e0..9127720 100644 --- a/src/convert/mod.rs +++ b/src/convert/mod.rs @@ -22,7 +22,7 @@ use crate::Module; /// For the square shape, the svg is `M{x},{y}h1v1h-1` /// /// ```rust -/// fn square(y: usize, x: usize) -> String { +/// fn square(y: usize, x: usize, _module: Module) -> String { /// format!("M{x},{y}h1v1h-1") /// } /// ``` diff --git a/src/convert/svg.rs b/src/convert/svg.rs index 8d80ad4..9994588 100644 --- a/src/convert/svg.rs +++ b/src/convert/svg.rs @@ -276,7 +276,7 @@ impl SvgBuilder { } for (i, command) in commands.iter().enumerate() { - paths[i].push_str(&command(x + self.margin, y + self.margin, cell)); + paths[i].push_str(&command(y + self.margin, x + self.margin, cell)); } } } diff --git a/src/tests/svg.rs b/src/tests/svg.rs index 8f46da0..c1284f7 100644 --- a/src/tests/svg.rs +++ b/src/tests/svg.rs @@ -20,3 +20,33 @@ fn it_embeds_an_image_via_data_uri() { let expected_href = format!(r#"href="{data_uri}""#); assert!(svg.contains(&expected_href)); } + +#[cfg(feature = "svg")] +#[test] +fn check_svg_is_not_inverted() { + use crate::convert::svg::SvgBuilder; + use crate::convert::Builder; + use crate::{QRBuilder, Version, ECL}; + + let qrcode = QRBuilder::new("Test") + .ecl(ECL::M) + .version(Version::V01) + .build() + .unwrap(); + + const MARGIN: usize = 4; + let svg = SvgBuilder::default().margin(MARGIN).to_str(&qrcode); + + let size = qrcode.size; + for y in 0..size { + for x in 0..size { + let index = y * size + x; + let expected = qrcode.data[index]; + if expected.value() { + dbg!((y, x)); + let expected = format!(r#"M{x},{y}"#, x = x + MARGIN, y = y + MARGIN); + assert!(svg.contains(&expected)); + } + } + } +}