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

Misc doc changes #20

Merged
merged 1 commit into from
Sep 2, 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
9 changes: 4 additions & 5 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"],
line_length: 180
]
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
Expand All @@ -22,9 +22,12 @@ erl_crash.dump
# Ignore package tarball (built via "mix hex.build").
eqrcode-*.tar

# Temporary files, for example, from tests.
/tmp/

# Misc.
**/.DS_Store
.elixir_ls

/test/images/
/test/html/
.mix_tasks
File renamed without changes.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ qr_code_content
|> EQRCode.encode()
|> EQRCode.png()
```
<img src="./screenshots/default.png" width="300">

![screenshot-defaults](./assets/default.png)

Note that the PNG format is only the binary. You still have to write the data to a file:

Expand All @@ -56,15 +57,21 @@ qr_code_content
|> EQRCode.svg(color: "#03B6AD", shape: "circle", width: 300, background_color: "#FFF")
```

<img src="./screenshots/circle-color.png" width="300">
![screenshot-circle-color](./assets/circle-color.png)

You can specify the following attributes of the QR code:

* `color`: In hexadecimal format. The default is `#000`

* `background_color`: In hexadecimal format or `:transparent`. The default is `#FFF`.

* `shape`: Only `square` or `circle`. The default is `square`
* `width`: The width of the QR code in pixel. Without the width attribute, the QR code size will be dynamically generated based on the input string.
* `viewbox`: When set to `true`, the SVG element will specify its height and width using `viewBox`, instead of explicit `height` and `width` tags.

* `width`: The width of the QR code in pixel. Without the width attribute, the
QR code size will be dynamically generated based on the input string.

* `viewbox`: When set to `true`, the SVG element will specify its height and
width using `viewBox`, instead of explicit `height` and `width` tags.

Default options are `[color: "#000", shape: "square", background_color: "#FFF"]`.

Expand All @@ -73,8 +80,12 @@ Default options are `[color: "#000", shape: "square", background_color: "#FFF"]`
You can specify the following attributes of the QR code:

* `color`: In binary format in the RGB order. The default is `<<0, 0, 0>>`
* `background_color`: In binary format or `:transparent`. The default is `<<255, 255, 255>>`
* `width`: The width of the QR code in pixel. (the actual size may vary, due to the number of modules in the code)

* `background_color`: In binary format or `:transparent`. The default is
`<<255, 255, 255>>`

* `width`: The width of the QR code in pixel. (the actual size may vary, due to
the number of modules in the code)

By default, QR code size will be dynamically generated based on the input string.

Expand All @@ -92,6 +103,9 @@ qr_code_content

We reused most of the code from [sunboshan/qrcode](https://github.com/sunboshan/qrcode) to generate the matrix required to render the QR Code. We also reference [rqrcode](https://github.com/whomwah/rqrcode) on how to generate SVG from the QR Code matrix.

## License
## Copyright and License

Copyright (c) 2014 Silicon Jungles

This project is Licensed under the [MIT License](https://github.com/SiliconJungles/eqrcode/blob/master/LICENSE).
This library is released under the MIT License. See the
[LICENSE.md](./LICENSE.md) file.
Binary file added assets/circle-color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 3 additions & 50 deletions lib/eqrcode.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
defmodule EQRCode do
@moduledoc """
Simple QR Code Generator written in Elixir with no other dependencies.

To generate the SVG QR code:

```elixir
qr_code_content = "your_qr_code_content"

qr_code_content
|> EQRCode.encode()
|> EQRCode.svg()
```
"""

alias EQRCode.{Encode, ReedSolomon, Matrix}
Expand Down Expand Up @@ -50,7 +40,9 @@ defmodule EQRCode do
do: raise(ArgumentError, message: "your input is too long. keep it under 2952 characters")

@doc """
Encode the binary with custom pattern bits. Only supports version 5.
Encode the binary with custom pattern bits.

Only supports version 5.
"""
@spec encode(binary, error_correction_level(), bitstring) :: Matrix.t()
def encode(bin, error_correction_level, bits) when byte_size(bin) <= 106 do
Expand All @@ -72,48 +64,9 @@ defmodule EQRCode do

def encode(_, _, _), do: IO.puts("Binary too long.")

@doc """
```elixir
qr_code_content
|> EQRCode.encode()
|> EQRCode.svg(color: "#cc6600", shape: "circle", width: 300)
```

You can specify the following attributes of the QR code:

* `background_color`: In hexadecimal format or `:transparent`. The default is `#FFF`
* `color`: In hexadecimal format. The default is `#000`
* `shape`: Only `square` or `circle`. The default is `square`
* `width`: The width of the QR code in pixel. Without the width attribute, the QR code size will be dynamically generated based on the input string.
* `viewbox`: When set to `true`, the SVG element will specify its height and width using `viewBox`, instead of explicit `height` and `width` tags.

Default options are `[color: "#000", shape: "square", background_color: "#FFF"]`.
"""
defdelegate svg(matrix, options \\ []), to: EQRCode.SVG

@doc """
```elixir
qr_code_content
|> EQRCode.encode()
|> EQRCode.png(color: <<255, 0, 255>>, width: 200)
```

You can specify the following attributes of the QR code:

* `color`: In binary format. The default is `<<0, 0, 0>>`
* `background_color`: In binary format or `:transparent`. The default is `<<255, 255, 255>>`
* `width`: The width of the QR code in pixel. (the actual size may vary, due to the number of modules in the code)

By default, QR code size will be dynamically generated based on the input string.
"""
defdelegate png(matrix, options \\ []), to: EQRCode.PNG

@doc """
```elixir
qr_code_content
|> EQRCode.encode()
|> EQRCode.render()
```
"""
defdelegate render(matrix), to: EQRCode.Render
end
27 changes: 19 additions & 8 deletions lib/eqrcode/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ defmodule EQRCode.Encode do
@doc """
Encode the binary.

Example:
## Examples

iex> EQRCode.Encode.encode("hello world!", :l)
{1, :l, [0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1,
0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0]}

"""
@spec encode(binary, SpecTable.error_correction_level()) :: {SpecTable.version(), SpecTable.error_correction_level(), [0 | 1]}
def encode(bin, error_correction_level) when error_correction_level in @error_correction_level do
@spec encode(binary, SpecTable.error_correction_level()) ::
{SpecTable.version(), SpecTable.error_correction_level(), [0 | 1]}
def encode(bin, error_correction_level)
when error_correction_level in @error_correction_level do
{:ok, version} = version(bin, error_correction_level)
cci_len = SpecTable.character_count_indicator_bits(version, error_correction_level)
mode = SpecTable.mode_indicator()
Expand All @@ -38,8 +42,10 @@ defmodule EQRCode.Encode do
end

# Encode the binary with custom pattern bits.
@spec encode(binary, SpecTable.error_correction_level(), bitstring) :: {SpecTable.version(), SpecTable.error_correction_level(), [0 | 1]}
def encode(bin, error_correction_level, bits) when error_correction_level in @error_correction_level do
@spec encode(binary, SpecTable.error_correction_level(), bitstring) ::
{SpecTable.version(), SpecTable.error_correction_level(), [0 | 1]}
def encode(bin, error_correction_level, bits)
when error_correction_level in @error_correction_level do
version = 5
n = byte_size(bin)
n1 = n + 2
Expand All @@ -66,11 +72,14 @@ defmodule EQRCode.Encode do
@doc """
Returns the lowest version for the given binary.

Example:
## Examples

iex> EQRCode.Encode.version("hello world!", :l)
{:ok, 1}

"""
@spec version(binary, SpecTable.error_correction_level()) :: {:error, :no_version_found} | {:ok, SpecTable.version()}
@spec version(binary, SpecTable.error_correction_level()) ::
{:error, :no_version_found} | {:ok, SpecTable.version()}
def version(bin, error_correction_level) do
byte_size(bin)
|> SpecTable.find_version(error_correction_level)
Expand All @@ -79,9 +88,11 @@ defmodule EQRCode.Encode do
@doc """
Returns bits for any binary data.

Example:
## Examples

iex> EQRCode.Encode.bits(<<123, 4>>)
[0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0]

"""
@spec bits(bitstring) :: [0 | 1]
def bits(bin) do
Expand Down
8 changes: 6 additions & 2 deletions lib/eqrcode/galois_field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ defmodule EQRCode.GaloisField do
@doc """
Given alpha exponent returns integer.

Example:
## Examples

iex> EQRCode.GaloisField.to_i(1)
2

"""
@spec to_i(integer) :: integer
def to_i(alpha)

@doc """
Given integer returns alpha exponent.

Example:
## Examples

iex> EQRCode.GaloisField.to_a(2)
1

"""
@spec to_a(integer) :: integer
def to_a(integer)
Expand Down
22 changes: 18 additions & 4 deletions lib/eqrcode/matrix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ defmodule EQRCode.Matrix do

@type coordinate :: {non_neg_integer(), non_neg_integer()}
@type matrix :: term
@type t :: %__MODULE__{version: SpecTable.version(), error_correction_level: SpecTable.error_correction_level(), modules: integer, matrix: matrix}
@type t :: %__MODULE__{
version: SpecTable.version(),
error_correction_level: SpecTable.error_correction_level(),
modules: integer,
matrix: matrix
}

@alignments %{
1 => [],
Expand Down Expand Up @@ -90,7 +95,12 @@ defmodule EQRCode.Matrix do
Tuple.duplicate(nil, modules)
|> Tuple.duplicate(modules)

%__MODULE__{version: version, error_correction_level: error_correction_level, modules: modules, matrix: matrix}
%__MODULE__{
version: version,
error_correction_level: error_correction_level,
modules: modules,
matrix: matrix
}
end

@doc """
Expand Down Expand Up @@ -286,7 +296,9 @@ defmodule EQRCode.Matrix do
Fill the reserved format information areas.
"""
@spec draw_format_areas(t) :: t
def draw_format_areas(%__MODULE__{matrix: matrix, modules: modules, mask: mask, error_correction_level: ecl} = m) do
def draw_format_areas(
%__MODULE__{matrix: matrix, modules: modules, mask: mask, error_correction_level: ecl} = m
) do
ecc_l = SpecTable.error_corretion_bits(ecl)
data = EQRCode.ReedSolomon.bch_encode(<<ecc_l::2, mask::3>>)

Expand Down Expand Up @@ -379,11 +391,13 @@ defmodule EQRCode.Matrix do
Given the starting point {x, y} and {width, height}
returns the coordinates of the shape.

Example:
## Examples

iex> EQRCode.Matrix.shape({0, 0}, {3, 3})
[{0, 0}, {0, 1}, {0, 2},
{1, 0}, {1, 1}, {1, 2},
{2, 0}, {2, 1}, {2, 2}]

"""
@spec shape(coordinate, {integer, integer}) :: [coordinate]
def shape({x, y}, {w, h}) do
Expand Down
39 changes: 23 additions & 16 deletions lib/eqrcode/png.ex
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
defmodule EQRCode.PNG do
@moduledoc """
Render the QR Code matrix in PNG format

```elixir
qr_code_content
|> EQRCode.encode()
|> EQRCode.png()
```

You can specify the following attributes of the QR code:

* `color`: In binary format. The default is `<<0, 0, 0>>`
* `background_color`: In binary format or `:transparent`. The default is `<<255, 255, 255>>`
* `width`: The width of the QR code in pixel. (the actual size may vary, due to the number of modules in the code)

By default, QR code size will be dynamically generated based on the input string.
Render the QR Code matrix in PNG format.
"""

alias EQRCode.Matrix
Expand All @@ -31,7 +17,28 @@ defmodule EQRCode.PNG do
@png_signature <<137, 80, 78, 71, 13, 10, 26, 10>>

@doc """
Return the PNG binary representation of the QR Code
Returns the PNG binary representation of the QR Code

## Options

You can specify the following attributes of the QR code:

* `:color` - In binary format. The default is `<<0, 0, 0>>`

* `:background_color` - In binary format or `:transparent`. The default is
`<<255, 255, 255>>`

* `:width` - The width of the QR code in pixel. (the actual size may vary, due
to the number of modules in the code)

By default, QR code size will be dynamically generated based on the input string.

## Examples

qr_code_content
|> EQRCode.encode()
|> EQRCode.png(color: <<255, 0, 255>>, width: 200)

"""
@spec png(Matrix.t(), map() | Keyword.t()) :: String.t()
def png(%Matrix{matrix: matrix} = m, options \\ []) do
Expand Down
Loading