rqrcode 0.10.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +79 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +223 -135
- data/Rakefile +10 -0
- data/_config.yml +1 -0
- data/bin/console +14 -0
- data/images/ansi-screen-shot.png +0 -0
- data/images/github-qrcode.png +0 -0
- data/images/github-qrcode.svg +32 -0
- data/lib/rqrcode/export/ansi.rb +22 -25
- data/lib/rqrcode/export/html.rb +8 -10
- data/lib/rqrcode/export/png.rb +62 -46
- data/lib/rqrcode/export/svg.rb +180 -24
- data/lib/rqrcode/export.rb +6 -0
- data/lib/rqrcode/qrcode/qrcode.rb +17 -0
- data/lib/rqrcode/qrcode.rb +3 -4
- data/lib/rqrcode/version.rb +3 -1
- data/lib/rqrcode.rb +8 -19
- data/rqrcode.gemspec +39 -0
- metadata +90 -60
- data/CHANGELOG +0 -97
- data/LICENSE +0 -19
- data/lib/rqrcode/core_ext/array/behavior.rb +0 -12
- data/lib/rqrcode/core_ext/array.rb +0 -5
- data/lib/rqrcode/core_ext/integer/bitwise.rb +0 -13
- data/lib/rqrcode/core_ext/integer.rb +0 -5
- data/lib/rqrcode/core_ext.rb +0 -5
- data/lib/rqrcode/qrcode/qr_8bit_byte.rb +0 -36
- data/lib/rqrcode/qrcode/qr_alphanumeric.rb +0 -47
- data/lib/rqrcode/qrcode/qr_bit_buffer.rb +0 -99
- data/lib/rqrcode/qrcode/qr_code.rb +0 -585
- data/lib/rqrcode/qrcode/qr_math.rb +0 -63
- data/lib/rqrcode/qrcode/qr_numeric.rb +0 -57
- data/lib/rqrcode/qrcode/qr_polynomial.rb +0 -78
- data/lib/rqrcode/qrcode/qr_rs_block.rb +0 -314
- data/lib/rqrcode/qrcode/qr_util.rb +0 -272
- data/test/data.rb +0 -25
- data/test/test_helper.rb +0 -5
- data/test/test_regresions.rb +0 -10
- data/test/test_rqrcode.rb +0 -155
- data/test/test_rqrcode_export.rb +0 -27
data/lib/rqrcode/export/ansi.rb
CHANGED
@@ -1,54 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RQRCode
|
2
4
|
module Export
|
3
5
|
module ANSI
|
4
|
-
|
6
|
+
#
|
5
7
|
# Returns a string of the QR code as
|
6
8
|
# characters writen with ANSI background set.
|
7
|
-
#
|
8
|
-
# Options:
|
9
|
+
#
|
10
|
+
# Options:
|
9
11
|
# light: Foreground ("\033[47m")
|
10
|
-
# dark: Background ANSI code. ("\033[
|
12
|
+
# dark: Background ANSI code. ("\033[40m")
|
11
13
|
# fill_character: The written character. (' ')
|
12
14
|
# quiet_zone_size: (4)
|
13
15
|
#
|
14
|
-
def as_ansi(options={})
|
16
|
+
def as_ansi(options = {})
|
15
17
|
options = {
|
16
18
|
light: "\033[47m",
|
17
19
|
dark: "\033[40m",
|
18
|
-
fill_character:
|
20
|
+
fill_character: " ",
|
19
21
|
quiet_zone_size: 4
|
20
22
|
}.merge(options)
|
21
23
|
|
22
|
-
normal = "\033[m"
|
24
|
+
normal = "\033[m\n"
|
23
25
|
light = options.fetch(:light)
|
24
26
|
dark = options.fetch(:dark)
|
25
27
|
fill_character = options.fetch(:fill_character)
|
26
28
|
quiet_zone_size = options.fetch(:quiet_zone_size)
|
29
|
+
output = []
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
@modules.each_index do |c|
|
31
|
-
|
31
|
+
@qrcode.modules.each_index do |c|
|
32
32
|
# start row with quiet zone
|
33
33
|
row = light + fill_character * quiet_zone_size
|
34
34
|
previous_dark = false
|
35
35
|
|
36
|
-
@modules.each_index do |r|
|
37
|
-
if
|
38
|
-
# dark
|
36
|
+
@qrcode.modules.each_index do |r|
|
37
|
+
if @qrcode.checked?(c, r)
|
39
38
|
if previous_dark != true
|
40
39
|
row << dark
|
41
40
|
previous_dark = true
|
42
41
|
end
|
43
|
-
|
44
|
-
else
|
42
|
+
elsif previous_dark != false
|
45
43
|
# light
|
46
|
-
|
47
|
-
|
48
|
-
previous_dark = false
|
49
|
-
end
|
50
|
-
row << fill_character
|
44
|
+
row << light
|
45
|
+
previous_dark = false
|
51
46
|
end
|
47
|
+
|
48
|
+
row << fill_character
|
52
49
|
end
|
53
50
|
|
54
51
|
# add quiet zone
|
@@ -58,18 +55,18 @@ module RQRCode
|
|
58
55
|
row << fill_character * quiet_zone_size
|
59
56
|
|
60
57
|
# always end with reset and newline
|
61
|
-
row << normal
|
58
|
+
row << normal
|
62
59
|
|
63
60
|
output << row
|
64
61
|
end
|
65
62
|
|
66
63
|
# count the row width so we can add quiet zone rows
|
67
|
-
width = output.
|
64
|
+
width = output.first.scan(fill_character).length
|
68
65
|
|
69
|
-
quiet_row = light + fill_character * width + normal
|
66
|
+
quiet_row = light + fill_character * width + normal
|
70
67
|
quiet_rows = quiet_row * quiet_zone_size
|
71
68
|
|
72
|
-
|
69
|
+
quiet_rows + output.join + quiet_rows
|
73
70
|
end
|
74
71
|
end
|
75
72
|
end
|
data/lib/rqrcode/export/html.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RQRCode
|
3
4
|
module Export
|
4
5
|
module HTML
|
5
|
-
|
6
|
+
#
|
7
|
+
# Use this module to HTML-ify the QR code if you just want the default HTML
|
6
8
|
def as_html
|
7
|
-
[
|
9
|
+
["<table>", rows.as_html, "</table>"].join
|
8
10
|
end
|
9
11
|
|
10
12
|
private
|
11
13
|
|
12
14
|
def rows
|
13
|
-
Rows.new(
|
15
|
+
Rows.new(@qrcode)
|
14
16
|
end
|
15
17
|
|
16
18
|
class Rows < Struct.new(:qr)
|
@@ -25,7 +27,7 @@ module RQRCode
|
|
25
27
|
|
26
28
|
class Row < Struct.new(:qr, :qr_module, :row_index)
|
27
29
|
def as_html
|
28
|
-
[
|
30
|
+
["<tr>", cells.map(&:as_html).join, "</tr>"].join
|
29
31
|
end
|
30
32
|
|
31
33
|
def cells
|
@@ -39,11 +41,7 @@ module RQRCode
|
|
39
41
|
end
|
40
42
|
|
41
43
|
def html_class
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
def dark?
|
46
|
-
qr.dark?(row_index, col_index)
|
44
|
+
qr.checked?(row_index, col_index) ? "black" : "white"
|
47
45
|
end
|
48
46
|
end
|
49
47
|
end
|
data/lib/rqrcode/export/png.rb
CHANGED
@@ -1,69 +1,80 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "chunky_png"
|
2
4
|
|
3
5
|
# This class creates PNG files.
|
4
|
-
# Code from: https://github.com/DCarper/rqrcode
|
5
6
|
module RQRCode
|
6
7
|
module Export
|
7
8
|
module PNG
|
8
|
-
|
9
|
-
#
|
9
|
+
# Render the PNG from the QR Code.
|
10
|
+
#
|
11
|
+
# Options:
|
12
|
+
# fill - Background ChunkyPNG::Color, defaults to 'white'.
|
13
|
+
# color - Foreground ChunkyPNG::Color, defaults to 'black'.
|
14
|
+
#
|
15
|
+
# When option :file is supplied you can use the following ChunkyPNG constraints
|
16
|
+
# color_mode - The color mode to use. Use one of the ChunkyPNG::COLOR_* constants.
|
17
|
+
# (defaults to 'ChunkyPNG::COLOR_GRAYSCALE')
|
18
|
+
# bit_depth - The bit depth to use. This option is only used for indexed images.
|
19
|
+
# (defaults to 1 bit)
|
20
|
+
# interlace - Whether to use interlacing (true or false).
|
21
|
+
# (defaults to ChunkyPNG default)
|
22
|
+
# compression - The compression level for Zlib. This can be a value between 0 and 9, or a
|
23
|
+
# Zlib constant like Zlib::BEST_COMPRESSION
|
24
|
+
# (defaults to ChunkyPNG default)
|
10
25
|
#
|
11
|
-
# There are two sizing
|
26
|
+
# There are two sizing algorithms.
|
12
27
|
#
|
13
28
|
# - Original that can result in blurry and hard to scan images
|
14
29
|
# - Google's Chart API inspired sizing that resizes the module size to fit within the given image size.
|
15
|
-
#
|
16
|
-
# The Googleis one will be used when no options are given or when the new size option is used.
|
17
30
|
#
|
18
|
-
#
|
19
|
-
# fill - Background ChunkyPNG::Color, defaults to 'white'
|
20
|
-
# color - Foreground ChunkyPNG::Color, defaults to 'black'
|
31
|
+
# The Googleis one will be used when no options are given or when the new size option is used.
|
21
32
|
#
|
22
|
-
# *
|
23
|
-
# size - Total size of PNG in pixels. The module size is calculated so it fits.
|
24
|
-
#
|
33
|
+
# *Google*
|
34
|
+
# size - Total size of PNG in pixels. The module size is calculated so it fits.
|
35
|
+
# (defaults to 120)
|
36
|
+
# border_modules - Width of white border around in modules.
|
37
|
+
# (defaults to 4).
|
25
38
|
#
|
26
39
|
# -- DONT USE border_modules OPTION UNLESS YOU KNOW ABOUT THE QUIET ZONE NEEDS OF QR CODES --
|
27
40
|
#
|
28
41
|
# *Original*
|
29
42
|
# module_px_size - Image size, in pixels.
|
30
|
-
# border
|
43
|
+
# border - Border thickness, in pixels
|
31
44
|
#
|
32
45
|
# It first creates an image where 1px = 1 module, then resizes.
|
33
|
-
# Defaults to
|
46
|
+
# Defaults to 120x120 pixels, customizable by option.
|
34
47
|
#
|
35
48
|
def as_png(options = {})
|
36
|
-
|
37
49
|
default_img_options = {
|
38
|
-
:
|
39
|
-
:
|
40
|
-
:
|
41
|
-
:
|
42
|
-
:
|
43
|
-
:
|
44
|
-
:
|
45
|
-
:
|
50
|
+
bit_depth: 1,
|
51
|
+
border_modules: 4,
|
52
|
+
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
|
53
|
+
color: "black",
|
54
|
+
file: false,
|
55
|
+
fill: "white",
|
56
|
+
module_px_size: 6,
|
57
|
+
resize_exactly_to: false,
|
58
|
+
resize_gte_to: false,
|
59
|
+
size: 120
|
46
60
|
}
|
47
|
-
|
48
|
-
googleis = options.length == 0 || (options[:size] != nil)
|
49
|
-
|
50
|
-
options = default_img_options.merge(options) # reverse_merge
|
51
61
|
|
52
|
-
|
53
|
-
|
62
|
+
googleis = options.length == 0 || !options[:size].nil?
|
63
|
+
options = default_img_options.merge(options) # reverse_merge
|
64
|
+
fill = ChunkyPNG::Color(*(options[:fill].is_a?(Array) ? options[:fill] : [options[:fill]]))
|
65
|
+
color = ChunkyPNG::Color(*(options[:color].is_a?(Array) ? options[:color] : [options[:color]]))
|
54
66
|
output_file = options[:file]
|
55
|
-
|
56
67
|
module_px_size = nil
|
57
68
|
border_px = nil
|
58
69
|
png = nil
|
59
|
-
|
70
|
+
|
60
71
|
if googleis
|
61
72
|
total_image_size = options[:size]
|
62
73
|
border_modules = options[:border_modules]
|
63
74
|
|
64
|
-
module_px_size = (total_image_size.to_f / (
|
75
|
+
module_px_size = (total_image_size.to_f / (@qrcode.module_count + 2 * border_modules).to_f).floor.to_i
|
65
76
|
|
66
|
-
img_size = module_px_size *
|
77
|
+
img_size = module_px_size * @qrcode.module_count
|
67
78
|
|
68
79
|
remaining = total_image_size - img_size
|
69
80
|
border_px = (remaining / 2.0).floor.to_i
|
@@ -73,43 +84,48 @@ module RQRCode
|
|
73
84
|
border = options[:border_modules]
|
74
85
|
total_border = border * 2
|
75
86
|
module_px_size = if options[:resize_gte_to]
|
76
|
-
(options[:resize_gte_to].to_f / (
|
87
|
+
(options[:resize_gte_to].to_f / (@qrcode.module_count + total_border).to_f).ceil.to_i
|
77
88
|
else
|
78
89
|
options[:module_px_size]
|
79
90
|
end
|
80
|
-
border_px = border *
|
91
|
+
border_px = border * module_px_size
|
81
92
|
total_border_px = border_px * 2
|
82
93
|
resize_to = options[:resize_exactly_to]
|
83
94
|
|
84
|
-
img_size = module_px_size *
|
95
|
+
img_size = module_px_size * @qrcode.module_count
|
85
96
|
total_img_size = img_size + total_border_px
|
86
97
|
|
87
98
|
png = ChunkyPNG::Image.new(total_img_size, total_img_size, fill)
|
88
99
|
end
|
89
100
|
|
90
|
-
|
91
|
-
|
92
|
-
if
|
101
|
+
@qrcode.modules.each_index do |x|
|
102
|
+
@qrcode.modules.each_index do |y|
|
103
|
+
if @qrcode.checked?(x, y)
|
93
104
|
(0...module_px_size).each do |i|
|
94
105
|
(0...module_px_size).each do |j|
|
95
|
-
png[(y * module_px_size) + border_px + j
|
106
|
+
png[(y * module_px_size) + border_px + j, (x * module_px_size) + border_px + i] = color
|
96
107
|
end
|
97
108
|
end
|
98
109
|
end
|
99
110
|
end
|
100
111
|
end
|
101
|
-
|
112
|
+
|
102
113
|
if !googleis && resize_to
|
103
|
-
png = png.resize(resize_to, resize_to)
|
114
|
+
png = png.resize(resize_to, resize_to)
|
104
115
|
end
|
105
116
|
|
106
|
-
|
107
117
|
if output_file
|
108
|
-
|
118
|
+
constraints = {
|
119
|
+
color_mode: options[:color_mode],
|
120
|
+
bit_depth: options[:bit_depth]
|
121
|
+
}
|
122
|
+
constraints[:interlace] = options[:interlace] if options.has_key?(:interlace)
|
123
|
+
constraints[:compression] = options[:compression] if options.has_key?(:compression)
|
124
|
+
png.save(output_file, constraints)
|
109
125
|
end
|
126
|
+
|
110
127
|
png
|
111
128
|
end
|
112
|
-
|
113
129
|
end
|
114
130
|
end
|
115
131
|
end
|
data/lib/rqrcode/export/svg.rb
CHANGED
@@ -1,50 +1,206 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# This class creates a SVG files.
|
2
|
-
#
|
4
|
+
# Initial code from: https://github.com/samvincent/rqrcode-rails3
|
3
5
|
module RQRCode
|
4
6
|
module Export
|
5
|
-
|
6
7
|
module SVG
|
8
|
+
class BaseOutputSVG
|
9
|
+
attr_reader :result
|
10
|
+
|
11
|
+
def initialize(qrcode)
|
12
|
+
@qrcode = qrcode
|
13
|
+
@result = []
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Path < BaseOutputSVG
|
18
|
+
def build(module_size, offset, color)
|
19
|
+
modules_array = @qrcode.modules
|
20
|
+
matrix_width = matrix_height = modules_array.length + 1
|
21
|
+
empty_row = [Array.new(matrix_width - 1, false)]
|
22
|
+
edge_matrix = Array.new(matrix_height) { Array.new(matrix_width) }
|
23
|
+
|
24
|
+
(empty_row + modules_array + empty_row).each_cons(2).with_index do |row_pair, row_index|
|
25
|
+
first_row, second_row = row_pair
|
26
|
+
|
27
|
+
# horizontal edges
|
28
|
+
first_row.zip(second_row).each_with_index do |cell_pair, column_index|
|
29
|
+
edge = case cell_pair
|
30
|
+
when [true, false] then Edge.new column_index + 1, row_index, :left
|
31
|
+
when [false, true] then Edge.new column_index, row_index, :right
|
32
|
+
end
|
33
|
+
|
34
|
+
(edge_matrix[edge.start_y][edge.start_x] ||= []) << edge if edge
|
35
|
+
end
|
36
|
+
|
37
|
+
# vertical edges
|
38
|
+
([false] + second_row + [false]).each_cons(2).each_with_index do |cell_pair, column_index|
|
39
|
+
edge = case cell_pair
|
40
|
+
when [true, false] then Edge.new column_index, row_index, :down
|
41
|
+
when [false, true] then Edge.new column_index, row_index + 1, :up
|
42
|
+
end
|
43
|
+
|
44
|
+
(edge_matrix[edge.start_y][edge.start_x] ||= []) << edge if edge
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
edge_count = edge_matrix.flatten.compact.count
|
49
|
+
path = []
|
50
|
+
|
51
|
+
while edge_count > 0
|
52
|
+
edge_loop = []
|
53
|
+
next_matrix_cell = edge_matrix.find(&:any?).find { |cell| cell&.any? }
|
54
|
+
edge = next_matrix_cell.first
|
55
|
+
|
56
|
+
while edge
|
57
|
+
edge_loop << edge
|
58
|
+
matrix_cell = edge_matrix[edge.start_y][edge.start_x]
|
59
|
+
matrix_cell.delete edge
|
60
|
+
edge_matrix[edge.start_y][edge.start_x] = nil if matrix_cell.empty?
|
61
|
+
edge_count -= 1
|
62
|
+
|
63
|
+
# try to find an edge continuing the current edge
|
64
|
+
edge = edge_matrix[edge.end_y][edge.end_x]&.first
|
65
|
+
end
|
66
|
+
|
67
|
+
first_edge = edge_loop.first
|
68
|
+
edge_loop_string = SVG_PATH_COMMANDS[:move]
|
69
|
+
edge_loop_string += "#{first_edge.start_x} #{first_edge.start_y}"
|
7
70
|
|
71
|
+
edge_loop.chunk(&:direction).to_a[0...-1].each do |direction, edges|
|
72
|
+
edge_loop_string << "#{SVG_PATH_COMMANDS[direction]}#{edges.length}"
|
73
|
+
end
|
74
|
+
edge_loop_string << SVG_PATH_COMMANDS[:close]
|
75
|
+
|
76
|
+
path << edge_loop_string
|
77
|
+
end
|
78
|
+
|
79
|
+
# Prefix hexadecimal colors unless using a named color (symbol)
|
80
|
+
color = "##{color}" unless color.is_a?(Symbol)
|
81
|
+
|
82
|
+
@result << %{<path d="#{path.join}" fill="#{color}" transform="translate(#{offset},#{offset}) scale(#{module_size})"/>}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Rect < BaseOutputSVG
|
87
|
+
def build(module_size, offset, color)
|
88
|
+
# Prefix hexadecimal colors unless using a named color (symbol)
|
89
|
+
color = "##{color}" unless color.is_a?(Symbol)
|
90
|
+
|
91
|
+
@qrcode.modules.each_index do |c|
|
92
|
+
tmp = []
|
93
|
+
@qrcode.modules.each_index do |r|
|
94
|
+
y = c * module_size + offset
|
95
|
+
x = r * module_size + offset
|
96
|
+
|
97
|
+
next unless @qrcode.checked?(c, r)
|
98
|
+
tmp << %(<rect width="#{module_size}" height="#{module_size}" x="#{x}" y="#{y}" fill="#{color}"/>)
|
99
|
+
end
|
100
|
+
|
101
|
+
@result << tmp.join
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Edge < Struct.new(:start_x, :start_y, :direction)
|
107
|
+
def end_x
|
108
|
+
case direction
|
109
|
+
when :right then start_x + 1
|
110
|
+
when :left then start_x - 1
|
111
|
+
else start_x
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def end_y
|
116
|
+
case direction
|
117
|
+
when :down then start_y + 1
|
118
|
+
when :up then start_y - 1
|
119
|
+
else start_y
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
DEFAULT_SVG_ATTRIBUTES = [
|
125
|
+
%(version="1.1"),
|
126
|
+
%(xmlns="http://www.w3.org/2000/svg"),
|
127
|
+
%(xmlns:xlink="http://www.w3.org/1999/xlink"),
|
128
|
+
%(xmlns:ev="http://www.w3.org/2001/xml-events")
|
129
|
+
]
|
130
|
+
|
131
|
+
SVG_PATH_COMMANDS = {
|
132
|
+
move: "M",
|
133
|
+
up: "v-",
|
134
|
+
down: "v",
|
135
|
+
left: "h-",
|
136
|
+
right: "h",
|
137
|
+
close: "z"
|
138
|
+
}
|
139
|
+
|
140
|
+
#
|
8
141
|
# Render the SVG from the Qrcode.
|
9
142
|
#
|
10
143
|
# Options:
|
11
|
-
# offset
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
144
|
+
# offset - Padding around the QR Code in pixels
|
145
|
+
# (default 0)
|
146
|
+
# fill - Background color e.g "ffffff"
|
147
|
+
# (default none)
|
148
|
+
# color - Foreground color e.g "000"
|
149
|
+
# (default "000")
|
150
|
+
# module_size - The Pixel size of each module
|
151
|
+
# (defaults 11)
|
152
|
+
# shape_rendering - SVG Attribute: auto | optimizeSpeed | crispEdges | geometricPrecision
|
153
|
+
# (defaults crispEdges)
|
154
|
+
# standalone - Whether to make this a full SVG file, or only an svg to embed in other svg
|
155
|
+
# (default true)
|
156
|
+
# use_path - Use <path> to render SVG rather than <rect> to significantly reduce size
|
157
|
+
# and quality. This will become the default in future versions.
|
158
|
+
# (default false)
|
159
|
+
# viewbox - replace `width` and `height` in <svg> with a viewBox, allows CSS scaling
|
160
|
+
# (default false)
|
161
|
+
# svg_attributes - A optional hash of custom <svg> attributes. Existing attributes will remain.
|
162
|
+
# (default {})
|
16
163
|
#
|
17
|
-
def as_svg(options={})
|
164
|
+
def as_svg(options = {})
|
165
|
+
fill = options[:fill]
|
166
|
+
use_path = options[:use_path]
|
18
167
|
offset = options[:offset].to_i || 0
|
19
168
|
color = options[:color] || "000"
|
20
169
|
shape_rendering = options[:shape_rendering] || "crispEdges"
|
21
170
|
module_size = options[:module_size] || 11
|
171
|
+
standalone = options[:standalone].nil? ? true : options[:standalone]
|
172
|
+
viewbox = options[:viewbox].nil? ? false : options[:viewbox]
|
173
|
+
svg_attributes = options[:svg_attributes] || {}
|
22
174
|
|
23
175
|
# height and width dependent on offset and QR complexity
|
24
|
-
dimension = (
|
176
|
+
dimension = (@qrcode.module_count * module_size) + (2 * offset)
|
177
|
+
# use dimensions differently if we are using a viewBox
|
178
|
+
dimensions_attr = viewbox ? %(viewBox="0 0 #{dimension} #{dimension}") : %(width="#{dimension}" height="#{dimension}")
|
25
179
|
|
26
|
-
|
27
|
-
|
180
|
+
svg_tag_attributes = (DEFAULT_SVG_ATTRIBUTES + [
|
181
|
+
dimensions_attr,
|
182
|
+
%(shape-rendering="#{shape_rendering}")
|
183
|
+
] + svg_attributes.map { |k, v| %(#{k}="#{v}") }).join(" ")
|
184
|
+
|
185
|
+
xml_tag = %(<?xml version="1.0" standalone="yes"?>)
|
186
|
+
open_tag = %(<svg #{svg_tag_attributes}>)
|
28
187
|
close_tag = "</svg>"
|
29
188
|
|
30
|
-
|
31
|
-
|
32
|
-
tmp = []
|
33
|
-
self.modules.each_index do |r|
|
34
|
-
y = c*module_size + offset
|
35
|
-
x = r*module_size + offset
|
189
|
+
output_tag = (use_path ? Path : Rect).new(@qrcode)
|
190
|
+
output_tag.build(module_size, offset, color)
|
36
191
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
result
|
192
|
+
if fill
|
193
|
+
# Prefix hexadecimal colors unless using a named color (symbol)
|
194
|
+
fill = "##{fill}" unless fill.is_a?(Symbol)
|
195
|
+
output_tag.result.unshift %(<rect width="#{dimension}" height="#{dimension}" x="0" y="0" fill="#{fill}"/>)
|
41
196
|
end
|
42
197
|
|
43
|
-
if
|
44
|
-
result.unshift
|
198
|
+
if standalone
|
199
|
+
output_tag.result.unshift(xml_tag, open_tag)
|
200
|
+
output_tag.result << close_tag
|
45
201
|
end
|
46
202
|
|
47
|
-
|
203
|
+
output_tag.result.join
|
48
204
|
end
|
49
205
|
end
|
50
206
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
module RQRCode # :nodoc:
|
6
|
+
class QRCode
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators :@qrcode, :to_s
|
9
|
+
def_delegators :@qrcode, :modules # deprecated
|
10
|
+
|
11
|
+
attr_reader :qrcode
|
12
|
+
|
13
|
+
def initialize(string, *args)
|
14
|
+
@qrcode = RQRCodeCore::QRCode.new(string, *args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rqrcode/qrcode.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rqrcode/qrcode/qrcode"
|
data/lib/rqrcode/version.rb
CHANGED
data/lib/rqrcode.rb
CHANGED
@@ -1,19 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
# above copyright notice is included.
|
10
|
-
#++
|
11
|
-
|
12
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
-
|
14
|
-
require "rqrcode/core_ext"
|
15
|
-
require "rqrcode/qrcode"
|
16
|
-
require 'rqrcode/export/png'
|
17
|
-
require 'rqrcode/export/svg'
|
18
|
-
require 'rqrcode/export/html'
|
19
|
-
require 'rqrcode/export/ansi'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RQRCode
|
4
|
+
require "rqrcode_core"
|
5
|
+
require "rqrcode/qrcode"
|
6
|
+
require "rqrcode/export"
|
7
|
+
require "rqrcode/version"
|
8
|
+
end
|
data/rqrcode.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "rqrcode/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rqrcode"
|
7
|
+
spec.version = RQRCode::VERSION
|
8
|
+
spec.platform = Gem::Platform::RUBY
|
9
|
+
spec.authors = ["Duncan Robertson"]
|
10
|
+
spec.email = ["duncan@whomwah.com"]
|
11
|
+
|
12
|
+
spec.summary = "A library to encode QR Codes"
|
13
|
+
spec.description = <<~EOF
|
14
|
+
rqrcode is a library for encoding QR Codes. The simple
|
15
|
+
interface allows you to create QR Code data structures
|
16
|
+
and then render them in the way you choose.
|
17
|
+
EOF
|
18
|
+
spec.homepage = "https://github.com/whomwah/rqrcode"
|
19
|
+
spec.license = "MIT"
|
20
|
+
spec.metadata = {
|
21
|
+
"bug_tracker_uri" => "https://github.com/whomwah/rqrcode/issues",
|
22
|
+
"changelog_uri" => "https://github.com/whomwah/rqrcode/blob/master/CHANGELOG.md"
|
23
|
+
}
|
24
|
+
|
25
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.required_ruby_version = ">= 2.6"
|
33
|
+
spec.add_dependency "rqrcode_core", "~> 1.0"
|
34
|
+
spec.add_dependency "chunky_png", "~> 1.0"
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
36
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
38
|
+
spec.add_development_dependency "standardrb", "~> 1.0"
|
39
|
+
end
|