diff --git a/lib/qr_code/matrix_helper.ex b/lib/qr_code/matrix_helper.ex index fea668e..f68f1fd 100644 --- a/lib/qr_code/matrix_helper.ex +++ b/lib/qr_code/matrix_helper.ex @@ -5,8 +5,10 @@ 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 + when is_integer(quiet_zone) and quiet_zone > 0 do {rows, cols} = Matrix.size(matrix) # Create new matrix with quiet zone diff --git a/test/svg_test.exs b/test/svg_test.exs index 6509f67..38385d5 100644 --- a/test/svg_test.exs +++ b/test/svg_test.exs @@ -151,5 +151,80 @@ defmodule SvgTest do assert Regex.match?(@rgx_qr_color, rv) end + + 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 + {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 + + 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 = List.first(matrix_with_quiet_1) + assert Enum.all?(first_row, &(&1 == 0)) + + # Check last row is all zeros (white margin) + 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 = 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 = Enum.map(matrix_with_quiet_1, &List.last/1) + assert Enum.all?(last_col, &(&1 == 0)) + end end end