From 8b20b65b6405d7c6cd03151cc641981b8425f9f2 Mon Sep 17 00:00:00 2001 From: Moxley Stratton Date: Sun, 7 Sep 2025 14:21:06 -0700 Subject: [PATCH 1/4] Fix quiet_zone=0 --- lib/qr_code/matrix_helper.ex | 17 +++++++++++------ test/svg_test.exs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/qr_code/matrix_helper.ex b/lib/qr_code/matrix_helper.ex index fea668e..ff3ea7e 100644 --- a/lib/qr_code/matrix_helper.ex +++ b/lib/qr_code/matrix_helper.ex @@ -7,14 +7,19 @@ defmodule QRCode.MatrixHelper do @spec surround_matrix(Matrix.t(), integer(), integer()) :: Matrix.t() def surround_matrix(matrix, quiet_zone, value) when is_integer(quiet_zone) and quiet_zone >= 0 do - {rows, cols} = Matrix.size(matrix) + # If quiet_zone is 0, just return the original matrix + if quiet_zone == 0 do + matrix + else + {rows, cols} = Matrix.size(matrix) - # Create new matrix with quiet zone - {:ok, new_matrix} = Matrix.new({rows + 2 * quiet_zone, cols + 2 * quiet_zone}, value) + # Create new matrix with quiet zone + {:ok, new_matrix} = Matrix.new({rows + 2 * quiet_zone, cols + 2 * quiet_zone}, value) - # Copy qr code matrix to new matrix - {:ok, new_matrix} = Matrix.update_map(new_matrix, matrix, [{quiet_zone, quiet_zone}]) + # Copy qr code matrix to new matrix + {:ok, new_matrix} = Matrix.update_map(new_matrix, matrix, [{quiet_zone, quiet_zone}]) - new_matrix + new_matrix + end end end diff --git a/test/svg_test.exs b/test/svg_test.exs index 6509f67..5c2e3ad 100644 --- a/test/svg_test.exs +++ b/test/svg_test.exs @@ -151,5 +151,38 @@ defmodule SvgTest do assert Regex.match?(@rgx_qr_color, rv) end + + test "should render svg with no margin when quiet_zone is 0" do + {:ok, qr} = QRCode.create("A") # Simple QR code for predictable size + + # Get the original matrix size + {rows, cols} = MatrixReloaded.Matrix.size(qr.matrix) + + # Render with quiet_zone: 0 and scale: 10 + {:ok, svg_content} = + QRCode.render({:ok, qr}, :svg, %SvgSettings{quiet_zone: 0, scale: 10, structure: :readable}) + + # The SVG dimensions should match exactly the matrix size * scale + expected_size = rows * 10 + assert svg_content =~ ~r/width="#{expected_size}"/ + assert svg_content =~ ~r/height="#{expected_size}"/ + + # There should be rectangles starting at x="0" and y="0" (no margin) + assert svg_content =~ ~r/x="0"/ + assert svg_content =~ ~r/y="0"/ + + # Verify that MatrixHelper.surround_matrix works with quiet_zone: 0 + matrix_with_no_quiet = QRCode.MatrixHelper.surround_matrix(qr.matrix, 0, 0) + assert MatrixReloaded.Matrix.size(matrix_with_no_quiet) == {rows, cols} + + # Compare with quiet_zone: 1 to ensure the difference is clear + {:ok, svg_with_margin} = + QRCode.render({:ok, qr}, :svg, %SvgSettings{quiet_zone: 1, scale: 10, structure: :readable}) + + # With quiet_zone: 1, dimensions should be 2 units larger (1 unit margin on each side) + expected_size_with_margin = (rows + 2) * 10 + assert svg_with_margin =~ ~r/width="#{expected_size_with_margin}"/ + assert svg_with_margin =~ ~r/height="#{expected_size_with_margin}"/ + end end end From 51ad2e1b6a4b02a3c74a2ebe63627780089d462b Mon Sep 17 00:00:00 2001 From: Moxley Stratton Date: Sun, 7 Sep 2025 14:24:31 -0700 Subject: [PATCH 2/4] Add test for quiet_zone=1 --- test/svg_test.exs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/test/svg_test.exs b/test/svg_test.exs index 5c2e3ad..1b161e7 100644 --- a/test/svg_test.exs +++ b/test/svg_test.exs @@ -152,7 +152,7 @@ defmodule SvgTest do assert Regex.match?(@rgx_qr_color, rv) end - test "should render svg with no margin when quiet_zone is 0" do + test "render svg with no margin when quiet_zone is 0" do {:ok, qr} = QRCode.create("A") # Simple QR code for predictable size # Get the original matrix size @@ -184,5 +184,47 @@ defmodule SvgTest do assert svg_with_margin =~ ~r/width="#{expected_size_with_margin}"/ assert svg_with_margin =~ ~r/height="#{expected_size_with_margin}"/ end + + test "render svg with 1-unit margin when quiet_zone is 1" do + {:ok, qr} = QRCode.create("A") # Simple QR code for predictable size + + # Get the original matrix size + {rows, cols} = MatrixReloaded.Matrix.size(qr.matrix) + + # Render with quiet_zone: 1 and scale: 10 + {:ok, svg_content} = + QRCode.render({:ok, qr}, :svg, %SvgSettings{quiet_zone: 1, scale: 10, structure: :readable}) + + # The SVG dimensions should be matrix size + 2 units (1 on each side) * scale + expected_size = (rows + 2) * 10 + assert svg_content =~ ~r/width="#{expected_size}"/ + assert svg_content =~ ~r/height="#{expected_size}"/ + + # With 1-unit margin, the first QR rectangles should start at x="10" and y="10" (not x="0") + # because there's a 1-unit (10-pixel) margin on each side + assert svg_content =~ ~r/x="10"/ + assert svg_content =~ ~r/y="10"/ + + # Verify that MatrixHelper.surround_matrix works with quiet_zone: 1 + matrix_with_quiet_1 = QRCode.MatrixHelper.surround_matrix(qr.matrix, 1, 0) + assert MatrixReloaded.Matrix.size(matrix_with_quiet_1) == {rows + 2, cols + 2} + + # The surrounded matrix should have white (0) borders + # Check first row is all zeros (white margin) + first_row = matrix_with_quiet_1 |> List.first() + assert Enum.all?(first_row, &(&1 == 0)) + + # Check last row is all zeros (white margin) + last_row = matrix_with_quiet_1 |> List.last() + assert Enum.all?(last_row, &(&1 == 0)) + + # Check first column is all zeros (white margin) + first_col = matrix_with_quiet_1 |> Enum.map(&List.first/1) + assert Enum.all?(first_col, &(&1 == 0)) + + # Check last column is all zeros (white margin) + last_col = matrix_with_quiet_1 |> Enum.map(&List.last/1) + assert Enum.all?(last_col, &(&1 == 0)) + end end end From 228e3f399c9b9a1c03c57fd41a3bfa24da07f0ab Mon Sep 17 00:00:00 2001 From: Moxley Stratton Date: Sun, 7 Sep 2025 14:27:31 -0700 Subject: [PATCH 3/4] Elixir styling fix --- test/svg_test.exs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/svg_test.exs b/test/svg_test.exs index 1b161e7..38385d5 100644 --- a/test/svg_test.exs +++ b/test/svg_test.exs @@ -211,19 +211,19 @@ defmodule SvgTest do # The surrounded matrix should have white (0) borders # Check first row is all zeros (white margin) - first_row = matrix_with_quiet_1 |> List.first() + first_row = List.first(matrix_with_quiet_1) assert Enum.all?(first_row, &(&1 == 0)) # Check last row is all zeros (white margin) - last_row = matrix_with_quiet_1 |> List.last() + last_row = List.last(matrix_with_quiet_1) assert Enum.all?(last_row, &(&1 == 0)) # Check first column is all zeros (white margin) - first_col = matrix_with_quiet_1 |> Enum.map(&List.first/1) + first_col = Enum.map(matrix_with_quiet_1, &List.first/1) assert Enum.all?(first_col, &(&1 == 0)) # Check last column is all zeros (white margin) - last_col = matrix_with_quiet_1 |> Enum.map(&List.last/1) + last_col = Enum.map(matrix_with_quiet_1, &List.last/1) assert Enum.all?(last_col, &(&1 == 0)) end end From bf4d429163041decc6306ebecbd115fdd537e9fa Mon Sep 17 00:00:00 2001 From: Moxley Stratton Date: Sun, 7 Sep 2025 14:29:26 -0700 Subject: [PATCH 4/4] Elixir styling fix --- lib/qr_code/matrix_helper.ex | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/qr_code/matrix_helper.ex b/lib/qr_code/matrix_helper.ex index ff3ea7e..f68f1fd 100644 --- a/lib/qr_code/matrix_helper.ex +++ b/lib/qr_code/matrix_helper.ex @@ -5,21 +5,18 @@ defmodule QRCode.MatrixHelper do alias MatrixReloaded.Matrix @spec surround_matrix(Matrix.t(), integer(), integer()) :: Matrix.t() + def surround_matrix(matrix, 0, _value), do: matrix + def surround_matrix(matrix, quiet_zone, value) - when is_integer(quiet_zone) and quiet_zone >= 0 do - # If quiet_zone is 0, just return the original matrix - if quiet_zone == 0 do - matrix - else - {rows, cols} = Matrix.size(matrix) + when is_integer(quiet_zone) and quiet_zone > 0 do + {rows, cols} = Matrix.size(matrix) - # Create new matrix with quiet zone - {:ok, new_matrix} = Matrix.new({rows + 2 * quiet_zone, cols + 2 * quiet_zone}, value) + # Create new matrix with quiet zone + {:ok, new_matrix} = Matrix.new({rows + 2 * quiet_zone, cols + 2 * quiet_zone}, value) - # Copy qr code matrix to new matrix - {:ok, new_matrix} = Matrix.update_map(new_matrix, matrix, [{quiet_zone, quiet_zone}]) + # Copy qr code matrix to new matrix + {:ok, new_matrix} = Matrix.update_map(new_matrix, matrix, [{quiet_zone, quiet_zone}]) - new_matrix - end + new_matrix end end