Skip to content

Commit

Permalink
fix: SVG and images were inverted
Browse files Browse the repository at this point in the history
This is because I called commands with (x, y) instead of (y, x)
Fixes #47
  • Loading branch information
erwanvivien committed Dec 29, 2023
1 parent b736073 commit 152996e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
/// }
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/convert/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/tests/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}

0 comments on commit 152996e

Please sign in to comment.