From bec2c671406ecb7b20738a93a99a423e684c110c Mon Sep 17 00:00:00 2001 From: Mariusz Gumienny Date: Fri, 27 Dec 2019 18:18:13 +0100 Subject: [PATCH 1/4] Fix broken HTML structure --- test/png_test.exs | 22 +++++++++++----------- test/svg_test.exs | 18 +++++++++--------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/test/png_test.exs b/test/png_test.exs index 437b1ff..e04c520 100644 --- a/test/png_test.exs +++ b/test/png_test.exs @@ -50,22 +50,22 @@ defmodule EQRCode.PNGTest do defp gen_html(kw_png) when is_list(kw_png) do png = Enum.map(kw_png, fn {key, value} -> - ~s{

#{key}:

} + ~s{

#{key}:

} end) """ - - - - EQRCode PNG Image Tests - - - #{png} - + + EQRCode PNG Image Tests + + + + #{png} + + """ end end diff --git a/test/svg_test.exs b/test/svg_test.exs index 74ed32d..f52d87d 100644 --- a/test/svg_test.exs +++ b/test/svg_test.exs @@ -53,16 +53,16 @@ defmodule EQRCode.SVGTest do """ - - - - EQRCode PNG Image Tests - - + + + EQRCode SVG Image Tests + + + #{svg} - + """ end From 527d3b1878bf6118e073595e60507a4ce7c19868 Mon Sep 17 00:00:00 2001 From: Mariusz Gumienny Date: Fri, 27 Dec 2019 18:20:13 +0100 Subject: [PATCH 2/4] Change absolute paths to relative paths in tests --- test/png_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/png_test.exs b/test/png_test.exs index e04c520..cb8009c 100644 --- a/test/png_test.exs +++ b/test/png_test.exs @@ -44,7 +44,8 @@ defmodule EQRCode.PNGTest do EQRCode.png(qr, opts) |> write_png_to_file(png_path) - png_list ++ [{label, png_path}] + png_relative_path = "../images/" <> Path.basename(png_path) + png_list ++ [{label, png_relative_path}] end defp gen_html(kw_png) when is_list(kw_png) do From a3285326a3d920cefc806dccf3230354ab29bf80 Mon Sep 17 00:00:00 2001 From: Mariusz Gumienny Date: Fri, 27 Dec 2019 18:49:27 +0100 Subject: [PATCH 3/4] Use `viewport` attribute in a better way --- lib/eqrcode/svg.ex | 47 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/lib/eqrcode/svg.ex b/lib/eqrcode/svg.ex index 2ea64ab..4c736d1 100644 --- a/lib/eqrcode/svg.ex +++ b/lib/eqrcode/svg.ex @@ -33,12 +33,13 @@ defmodule EQRCode.SVG do dimension = matrix_size * svg_options[:module_size] xml_tag = ~s() + viewbox_attr = ~s(viewBox="0 0 #{matrix_size} #{matrix_size}") dimension_attrs = if Keyword.get(options, :viewbox, false) do - ~s(viewBox="0 0 #{dimension} #{dimension}") + viewbox_attr else - ~s(width="#{dimension}" height="#{dimension}") + ~s(width="#{dimension}" height="#{dimension}" #{viewbox_attr}") end open_tag = @@ -94,54 +95,50 @@ defmodule EQRCode.SVG do |> Enum.to_list() end - defp substitute(data, row_num, col_num, %{module_size: module_size}) + defp substitute(data, row_num, col_num, %{}) when is_nil(data) or data == 0 do %{} - |> Map.put(:height, module_size) + |> Map.put(:height, 1) |> Map.put(:style, "fill: transparent;") - |> Map.put(:width, module_size) - |> Map.put(:x, row_num * module_size) - |> Map.put(:y, col_num * module_size) + |> Map.put(:width, 1) + |> Map.put(:x, row_num) + |> Map.put(:y, col_num) |> draw_rect end # This pattern match ensures that the QR Codes positional markers are drawn # as rectangles, regardless of the shape - defp substitute(1, row_num, col_num, %{color: color, module_size: module_size, size: size}) + defp substitute(1, row_num, col_num, %{color: color, size: size}) when (row_num <= 8 and col_num <= 8) or (row_num >= size - 9 and col_num <= 8) or (row_num <= 8 and col_num >= size - 9) do %{} - |> Map.put(:height, module_size) + |> Map.put(:height, 1) |> Map.put(:style, "fill:#{color};") - |> Map.put(:width, module_size) - |> Map.put(:x, col_num * module_size) - |> Map.put(:y, row_num * module_size) + |> Map.put(:width, 1) + |> Map.put(:x, col_num) + |> Map.put(:y, row_num) |> draw_rect end - defp substitute(1, row_num, col_num, %{ - color: color, - module_size: module_size, - shape: "circle" - }) do - radius = module_size / 2.0 + defp substitute(1, row_num, col_num, %{color: color, shape: "circle"}) do + radius = 0.5 %{} - |> Map.put(:cx, row_num * module_size + radius) - |> Map.put(:cy, col_num * module_size + radius) + |> Map.put(:cx, row_num + radius) + |> Map.put(:cy, col_num + radius) |> Map.put(:r, radius) |> Map.put(:style, "fill:#{color};") |> draw_circle end - defp substitute(1, row_num, col_num, %{color: color, module_size: module_size}) do + defp substitute(1, row_num, col_num, %{color: color}) do %{} - |> Map.put(:height, module_size) + |> Map.put(:height, 1) |> Map.put(:style, "fill:#{color};") - |> Map.put(:width, module_size) - |> Map.put(:x, row_num * module_size) - |> Map.put(:y, col_num * module_size) + |> Map.put(:width, 1) + |> Map.put(:x, row_num) + |> Map.put(:y, col_num) |> draw_rect end From f82c55a4e493e7161bd22e9f5e5c41e92963c06e Mon Sep 17 00:00:00 2001 From: Mariusz Gumienny Date: Sat, 28 Dec 2019 11:00:02 +0100 Subject: [PATCH 4/4] Remove `"` character --- lib/eqrcode/svg.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/eqrcode/svg.ex b/lib/eqrcode/svg.ex index 4c736d1..a9cbe33 100644 --- a/lib/eqrcode/svg.ex +++ b/lib/eqrcode/svg.ex @@ -39,7 +39,7 @@ defmodule EQRCode.SVG do if Keyword.get(options, :viewbox, false) do viewbox_attr else - ~s(width="#{dimension}" height="#{dimension}" #{viewbox_attr}") + ~s(width="#{dimension}" height="#{dimension}" #{viewbox_attr}) end open_tag =