Skip to content

Commit 1360a08

Browse files
EliasBinders-m-i-t-a
authored andcommitted
Fix tests, add quiet_zone property to render settings for png and svg
1 parent 5ce6c91 commit 1360a08

11 files changed

+289
-9
lines changed

.tool-versions

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
elixir 1.17.2-otp-27
2+
erlang 27.0.1

hello.svg

+1
Loading

lib/qr_code/matrix_helper.ex

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule QRCode.MatrixHelper do
2+
@moduledoc """
3+
Helper functions for matrix manipulation.
4+
"""
5+
alias MatrixReloaded.Matrix
6+
7+
@spec surround_matrix(Matrix.t(), integer(), integer()) :: Matrix.t()
8+
def surround_matrix(matrix, quiet_zone, value) when is_integer(quiet_zone) and quiet_zone >= 0 do
9+
{rows, cols} = Matrix.size(matrix)
10+
11+
# Create new matrix with quiet zone
12+
{:ok, new_matrix} = Matrix.new({rows + 2 * quiet_zone, cols + 2 * quiet_zone}, value)
13+
14+
# Copy matrix to new matrix
15+
new_matrix = Enum.reduce(0..rows - 1, new_matrix, fn row, acc_matrix ->
16+
Enum.reduce(0..cols - 1, acc_matrix, fn col, acc_matrix_inner ->
17+
{:ok, element} = Matrix.get_element(matrix, {row, col})
18+
{:ok, updated_matrix} = Matrix.update_element(acc_matrix_inner, element, {row + quiet_zone, col + quiet_zone})
19+
updated_matrix
20+
end)
21+
end)
22+
23+
new_matrix
24+
end
25+
end

lib/qr_code/render/png.ex

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule QRCode.Render.Png do
66
alias MatrixReloaded.Matrix
77
alias QRCode.QR
88
alias QRCode.Render.PngSettings
9+
alias QRCode.MatrixHelper
910

1011
@doc """
1112
Create Png image from QR matrix as binary.
@@ -26,15 +27,17 @@ defmodule QRCode.Render.Png do
2627
%PngSettings{
2728
scale: scale,
2829
background_color: background_color,
29-
qrcode_color: qrcode_color
30+
qrcode_color: qrcode_color,
31+
quiet_zone: quiet_zone
3032
}
3133
) do
3234
{rows, cols} = Matrix.size(matrix)
33-
height = rows * scale
34-
width = cols * scale
35+
height = (rows + 2 * quiet_zone) * scale
36+
width = (cols + 2 * quiet_zone) * scale
3537

3638
bitmap =
3739
matrix
40+
|> MatrixHelper.surround_matrix(quiet_zone, 0)
3841
|> rescale_rows(scale)
3942
|> List.flatten()
4043
|> rescale_cols_and_put_colors(scale, to_rgb(background_color), to_rgb(qrcode_color))
@@ -71,4 +74,6 @@ defmodule QRCode.Render.Png do
7174
Base.decode16!(c, case: :mixed)
7275
|> :binary.decode_unsigned()
7376
end
77+
78+
7479
end

lib/qr_code/render/png_settings.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ defmodule QRCode.Render.PngSettings do
55

66
@type background_color :: String.t() | tuple
77
@type qrcode_color :: String.t() | tuple
8+
@type scale :: integer
9+
@type quiet_zone :: integer
810

911
@type t :: %__MODULE__{
1012
scale: integer,
1113
background_color: background_color,
12-
qrcode_color: qrcode_color
14+
qrcode_color: qrcode_color,
15+
quiet_zone: quiet_zone
1316
}
1417

1518
defstruct scale: 10,
1619
background_color: "#ffffff",
17-
qrcode_color: "#000000"
20+
qrcode_color: "#000000",
21+
quiet_zone: 4
1822
end

lib/qr_code/render/svg.ex

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule QRCode.Render.Svg do
66
alias MatrixReloaded.Matrix
77
alias QRCode.QR
88
alias QRCode.Render.SvgSettings
9+
alias QRCode.MatrixHelper
910

1011
@type t :: %__MODULE__{
1112
xmlns: String.t(),
@@ -30,6 +31,7 @@ defmodule QRCode.Render.Svg do
3031
@spec create(Result.t(String.t(), QR.t()), SvgSettings.t()) :: Result.t(String.t(), binary())
3132
def create({:ok, %QR{matrix: matrix}}, settings) do
3233
matrix
34+
|> MatrixHelper.surround_matrix(settings.quiet_zone, 0)
3335
|> create_svg(settings)
3436
|> Result.ok()
3537
end

lib/qr_code/render/svg_settings.ex

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule QRCode.Render.SvgSettings do
1919
@type qrcode_color :: String.t() | tuple
2020
@type flatten :: boolean()
2121
@type structure :: :minify | :readable
22+
@type quiet_zone :: integer
2223

2324
@type t :: %__MODULE__{
2425
scale: integer,
@@ -27,7 +28,8 @@ defmodule QRCode.Render.SvgSettings do
2728
background_color: background_color,
2829
qrcode_color: qrcode_color,
2930
flatten: flatten,
30-
structure: structure
31+
structure: structure,
32+
quiet_zone: quiet_zone
3133
}
3234

3335
defstruct scale: 10,
@@ -36,5 +38,6 @@ defmodule QRCode.Render.SvgSettings do
3638
background_color: "#ffffff",
3739
qrcode_color: "#000000",
3840
flatten: false,
39-
structure: :minify
41+
structure: :minify,
42+
quiet_zone: 4
4043
end

test.png

3.17 KB
Loading

0 commit comments

Comments
 (0)