Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few fixes #13

Merged
merged 4 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions lib/eqrcode/svg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ defmodule EQRCode.SVG do
dimension = matrix_size * svg_options[:module_size]

xml_tag = ~s(<?xml version="1.0" standalone="yes"?>)
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 =
Expand Down Expand Up @@ -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

Expand Down
25 changes: 13 additions & 12 deletions test/png_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,29 @@ 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
png =
Enum.map(kw_png, fn {key, value} ->
~s{<p><strong>#{key}:</strong></p><image src="#{value}">}
~s{<p><strong>#{key}:</strong></p><img src="#{value}">}
end)

"""
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<head>
<title>EQRCode PNG Image Tests</title>
<style>
body { background: rgba(0,255,0); }
</style>
</head>
#{png}
</body>
<html>
<head>
<title>EQRCode PNG Image Tests</title>
<style>
body { background: rgba(0, 255, 0); }
</style>
</head>
<body>
#{png}
</body>
</html>
"""
end
end
18 changes: 9 additions & 9 deletions test/svg_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ defmodule EQRCode.SVGTest do

"""
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<head>
<title>EQRCode PNG Image Tests</title>
<style>
body { background: rgba(0,255,0); }
</style>
</head>
<html>
<head>
<title>EQRCode SVG Image Tests</title>
<style>
body { background: rgba(0, 255, 0); }
</style>
</head>
<body>
#{svg}
</body>
</body>
<html>
"""
end
Expand Down