From 5702ee859d7ba04506f1772f822c2a262db32c14 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 | 3 ++- src/convert/svg.rs | 2 +- src/tests/bytes.rs | 2 +- src/tests/svg.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/convert/mod.rs b/src/convert/mod.rs index 0e608e0..8517faf 100644 --- a/src/convert/mod.rs +++ b/src/convert/mod.rs @@ -22,7 +22,8 @@ use crate::Module; /// For the square shape, the svg is `M{x},{y}h1v1h-1` /// /// ```rust -/// fn square(y: usize, x: usize) -> String { +/// # use fast_qr::Module; +/// 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/bytes.rs b/src/tests/bytes.rs index c5687ec..4da88c8 100644 --- a/src/tests/bytes.rs +++ b/src/tests/bytes.rs @@ -8,7 +8,7 @@ fn it_can_output_to_bytes_from_image() { use crate::{QRBuilder, ECL}; // Expected - let image_base64 = "iVBORw0KGgoAAAANSUhEUgAAACUAAAAlCAYAAADFniADAAACsklEQVR4Ae2SUW4bQQxD8+5/6NQvAjsaza7rNB9NgRQgJFEUh96U98e/t2/27yfUq3+Q/+NLAbc/qP/3g9KFgzVD9TFSAzuXndW9NTi+FPA2RYrhnncvvIN7nXt1HXDqb0PB+mWaQR3DqprPnZwI3/vOTd45+FKoPJKq6VUfLlWdyGx1Dv46lAawvprGsL6uezmrgKV1Fu6heO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QVD59MrP+p389ijohS6CemaoeLcMSofun15nWUPr4eTF7VGKAfx5EbTnsuTQMmtGfVB9k78cBWWC+pI02jRz6zvElzh9crPe5S+PcgBcj3PQTptcPEaoGebxzCg/cTlKEfq10iwfhDPKQfnNz4A6SC+0D4rXr2YMbo/SoBmq2VwOevCsoXioGM0YOAN6HnQej/FyFLQRagGsnI0iS4zWsPrk4TpD7ytsj8qwNFonh14sB7XUPID2wKrDvfbsPxYu/0tQgLVRThxewwes+o5/GD9/wP4w5fSaB2+9lIOgFsF6oIPVjQJKN5/QA6WZT+32KE3QTVBHhHcQFAcd1cXUrWE/S+2M26OgFtkAlUNH+cADzKEWm8tB1cln1AM1z3zi5VEaM2jmk5M/46u6vnOv9eUoyVeA9eugauiYfpdC8eGgX08OVv3BHY3Lvz6oJsUzDu+TghoGq18P7LloxuegTXJ5KahlZy80D+tSvS6C4mcezSigPdY73B4F1WxTliSGM0Ifm9oopt/6jOjGqf3VUdCHO3QOh6sG9QFQ2vTbH7x9lAMcCr0otdorTJ/52Xt71MX4uVwe6hDzAOrrU5+XQfVA+dShuPQkbo+KeI4OCgc1PHViPLAuhKphjemb8XLUFP9V/nPUV1/+W77Ub25RML9l49+tAAAAAElFTkSuQmCC"; let expected_data_uri = format!("data:image/png;base64,{image_base64}"); // Source diff --git a/src/tests/svg.rs b/src/tests/svg.rs index 8f46da0..7b9f8e1 100644 --- a/src/tests/svg.rs +++ b/src/tests/svg.rs @@ -20,3 +20,32 @@ 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() { + let expected = format!(r#"M{x},{y}"#, x = x + MARGIN, y = y + MARGIN); + assert!(svg.contains(&expected)); + } + } + } +}