rqrcode_core 0.1.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +29 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +39 -0
- data/Gemfile.lock +34 -5
- data/README.md +37 -13
- data/Rakefile +17 -8
- data/lib/rqrcode_core.rb +0 -1
- data/lib/rqrcode_core/qrcode.rb +9 -9
- data/lib/rqrcode_core/qrcode/qr_8bit_byte.rb +4 -12
- data/lib/rqrcode_core/qrcode/qr_alphanumeric.rb +18 -25
- data/lib/rqrcode_core/qrcode/qr_bit_buffer.rb +15 -25
- data/lib/rqrcode_core/qrcode/qr_code.rb +258 -258
- data/lib/rqrcode_core/qrcode/qr_math.rb +9 -10
- data/lib/rqrcode_core/qrcode/qr_numeric.rb +9 -20
- data/lib/rqrcode_core/qrcode/qr_polynomial.rb +15 -15
- data/lib/rqrcode_core/qrcode/qr_rs_block.rb +8 -10
- data/lib/rqrcode_core/qrcode/qr_util.rb +51 -47
- data/lib/rqrcode_core/version.rb +1 -1
- data/rqrcode_core.gemspec +18 -18
- metadata +22 -11
- data/lib/rqrcode_core/core_ext.rb +0 -4
- data/lib/rqrcode_core/core_ext/array.rb +0 -7
- data/lib/rqrcode_core/core_ext/array/behavior.rb +0 -14
- data/lib/rqrcode_core/core_ext/integer.rb +0 -7
- data/lib/rqrcode_core/core_ext/integer/bitwise.rb +0 -15
@@ -2,141 +2,142 @@
|
|
2
2
|
|
3
3
|
module RQRCodeCore
|
4
4
|
QRMODE = {
|
5
|
-
:
|
6
|
-
:
|
7
|
-
:
|
8
|
-
}
|
5
|
+
mode_number: 1 << 0,
|
6
|
+
mode_alpha_numk: 1 << 1,
|
7
|
+
mode_8bit_byte: 1 << 2
|
8
|
+
}.freeze
|
9
9
|
|
10
10
|
QRMODE_NAME = {
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
}
|
11
|
+
number: :mode_number,
|
12
|
+
alphanumeric: :mode_alpha_numk,
|
13
|
+
byte_8bit: :mode_8bit_byte
|
14
|
+
}.freeze
|
15
15
|
|
16
16
|
QRERRORCORRECTLEVEL = {
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
20
|
-
:
|
21
|
-
}
|
17
|
+
l: 1,
|
18
|
+
m: 0,
|
19
|
+
q: 3,
|
20
|
+
h: 2
|
21
|
+
}.freeze
|
22
22
|
|
23
23
|
QRMASKPATTERN = {
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
}
|
24
|
+
pattern000: 0,
|
25
|
+
pattern001: 1,
|
26
|
+
pattern010: 2,
|
27
|
+
pattern011: 3,
|
28
|
+
pattern100: 4,
|
29
|
+
pattern101: 5,
|
30
|
+
pattern110: 6,
|
31
|
+
pattern111: 7
|
32
|
+
}.freeze
|
33
33
|
|
34
34
|
QRMASKCOMPUTATIONS = [
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
]
|
35
|
+
proc { |i, j| (i + j) % 2 == 0 },
|
36
|
+
proc { |i, j| i % 2 == 0 },
|
37
|
+
proc { |i, j| j % 3 == 0 },
|
38
|
+
proc { |i, j| (i + j) % 3 == 0 },
|
39
|
+
proc { |i, j| ((i / 2).floor + (j / 3).floor) % 2 == 0 },
|
40
|
+
proc { |i, j| (i * j) % 2 + (i * j) % 3 == 0 },
|
41
|
+
proc { |i, j| ((i * j) % 2 + (i * j) % 3) % 2 == 0 },
|
42
|
+
proc { |i, j| ((i * j) % 3 + (i + j) % 2) % 2 == 0 }
|
43
|
+
].freeze
|
44
44
|
|
45
45
|
QRPOSITIONPATTERNLENGTH = (7 + 1) * 2 + 1
|
46
46
|
QRFORMATINFOLENGTH = 15
|
47
47
|
|
48
|
-
#http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable1-e.html
|
49
|
-
#http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable2-e.html
|
50
|
-
#http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable3-e.html
|
51
|
-
#http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable4-e.html
|
48
|
+
# http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable1-e.html
|
49
|
+
# http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable2-e.html
|
50
|
+
# http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable3-e.html
|
51
|
+
# http://web.archive.org/web/20110710094955/http://www.denso-wave.com/qrcode/vertable4-e.html
|
52
52
|
# Each array contains levels max chars from level 1 to level 40
|
53
53
|
QRMAXDIGITS = {
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
}
|
54
|
+
l: {
|
55
|
+
mode_number: [
|
56
|
+
41, 77, 127, 187, 255, 322, 370, 461, 552, 652, 772,
|
57
|
+
883, 1022, 1101, 1250, 1408, 1548, 1725, 1903, 2061,
|
58
|
+
2232, 2409, 2620, 2812, 3057, 3283, 3514, 3669, 3909, 4158,
|
59
|
+
4417, 4686, 4965, 5253, 5529, 5836, 6153, 6479, 6743, 7089
|
60
|
+
],
|
61
|
+
mode_alpha_numk: [
|
62
|
+
25, 47, 77, 114, 154, 195, 224, 279, 335, 395,
|
63
|
+
468, 535, 619, 667, 758, 854, 938, 1046, 1153, 1249,
|
64
|
+
1352, 1460, 1588, 1704, 1853, 1990, 2132, 2223, 2369, 2520,
|
65
|
+
2677, 2840, 3009, 3183, 3351, 3537, 3729, 3927, 4087, 4296
|
66
|
+
],
|
67
|
+
mode_8bit_byte: [
|
68
|
+
17, 32, 53, 78, 106, 134, 154, 192, 230, 271,
|
69
|
+
321, 367, 425, 458, 520, 586, 644, 718, 792, 858,
|
70
|
+
929, 1003, 1091, 1171, 1273, 1367, 1465, 1528, 1628, 1732,
|
71
|
+
1840, 1952, 2068, 2188, 2303, 2431, 2563, 2699, 2809, 2953
|
72
|
+
]
|
73
|
+
},
|
74
|
+
m: {
|
75
|
+
mode_number: [
|
76
|
+
34, 63, 101, 149, 202, 255, 293, 365, 432, 513,
|
77
|
+
604, 691, 796, 871, 991, 1082, 1212, 1346, 1500, 1600,
|
78
|
+
1708, 1872, 2059, 2188, 2395, 2544, 2701, 2857, 3035, 3289,
|
79
|
+
3486, 3693, 3909, 4134, 4343, 4588, 4775, 5039, 5313, 5596
|
80
|
+
],
|
81
|
+
mode_alpha_numk: [
|
82
|
+
20, 38, 61, 90, 122, 154, 178, 221, 262, 311,
|
83
|
+
366, 419, 483, 528, 600, 656, 734, 816, 909, 970,
|
84
|
+
1035, 1134, 1248, 1326, 1451, 1542, 1637, 1732, 1839, 1994,
|
85
|
+
2113, 2238, 2369, 2506, 2632, 2780, 2894, 3054, 3220, 3391
|
86
|
+
],
|
87
|
+
mode_8bit_byte: [
|
88
|
+
14, 26, 42, 62, 84, 106, 122, 152, 180, 213,
|
89
|
+
251, 287, 331, 362, 412, 450, 504, 560, 624, 666,
|
90
|
+
711, 779, 857, 911, 997, 1059, 1125, 1190, 1264, 1370,
|
91
|
+
1452, 1538, 1628, 1722, 1809, 1911, 1989, 2099, 2213, 2331
|
92
|
+
]
|
93
|
+
},
|
94
|
+
q: {
|
95
|
+
mode_number: [
|
96
|
+
27, 48, 77, 111, 144, 178, 207, 259, 312, 364,
|
97
|
+
427, 489, 580, 621, 703, 775, 876, 948, 1063, 1159,
|
98
|
+
1224, 1358, 1468, 1588, 1718, 1804, 1933, 2085, 2181, 2358,
|
99
|
+
2473, 2670, 2805, 2949, 3081, 3244, 3417, 3599, 3791, 3993
|
100
|
+
],
|
101
|
+
mode_alpha_numk: [
|
102
|
+
16, 29, 47, 67, 87, 108, 125, 157, 189, 221,
|
103
|
+
259, 296, 352, 376, 426, 470, 531, 574, 644, 702,
|
104
|
+
742, 823, 890, 963, 1041, 1094, 1172, 1263, 1322, 1429,
|
105
|
+
1499, 1618, 1700, 1787, 1867, 1966, 2071, 2181, 2298, 2420
|
106
|
+
],
|
107
|
+
mode_8bit_byte: [
|
108
|
+
11, 20, 32, 46, 60, 74, 86, 108, 130, 151,
|
109
|
+
177, 203, 241, 258, 292, 22, 364, 394, 442, 482,
|
110
|
+
509, 565, 611, 661, 715, 751, 805, 868, 908, 982,
|
111
|
+
1030, 1112, 1168, 1228, 1283, 1351, 1423, 1499, 1579, 1663
|
112
|
+
]
|
113
|
+
},
|
114
|
+
h: {
|
115
|
+
mode_number: [
|
116
|
+
17, 34, 58, 82, 106, 139, 154, 202, 235, 288, 331, 374, 427, 468, 530, 602, 674,
|
117
|
+
331, 374, 427, 468, 530, 602, 674, 746, 813, 919,
|
118
|
+
969, 1056, 1108, 1228, 1286, 1425, 1501, 1581, 1677, 1782,
|
119
|
+
1897, 2022, 2157, 2301, 2361, 2524, 2625, 2735, 2927, 3057
|
120
|
+
|
121
|
+
],
|
122
|
+
mode_alpha_numk: [
|
123
|
+
10, 20, 35, 50, 64, 84, 93, 122, 143, 174, 200,
|
124
|
+
200, 227, 259, 283, 321, 365, 408, 452, 493, 557,
|
125
|
+
587, 640, 672, 744, 779, 864, 910, 958, 1016, 1080,
|
126
|
+
1150, 1226, 1307, 1394, 1431, 1530, 1591, 1658, 1774, 1852
|
127
|
+
],
|
128
|
+
mode_8bit_byte: [
|
129
|
+
7, 14, 24, 34, 44, 58, 64, 84, 98, 119,
|
130
|
+
137, 155, 177, 194, 220, 250, 280, 310, 338, 382,
|
131
|
+
403, 439, 461, 511, 535, 593, 625, 658, 698, 742,
|
132
|
+
790, 842, 898, 958, 983, 1051, 1093, 1139, 1219, 1273
|
133
|
+
]
|
134
|
+
}
|
135
|
+
}.freeze
|
136
136
|
|
137
137
|
# StandardErrors
|
138
138
|
|
139
139
|
class QRCodeArgumentError < ArgumentError; end
|
140
|
+
|
140
141
|
class QRCodeRunTimeError < RuntimeError; end
|
141
142
|
|
142
143
|
# == Creation
|
@@ -154,7 +155,7 @@ module RQRCodeCore
|
|
154
155
|
# Expects a string to be parsed in, other args are optional
|
155
156
|
#
|
156
157
|
# # string - the string you wish to encode
|
157
|
-
# # size - the size of the qrcode (
|
158
|
+
# # size - the size (Integer) of the qrcode (defaults to smallest size needed to encode the string)
|
158
159
|
# # level - the error correction level, can be:
|
159
160
|
# * Level :l 7% of code can be restored
|
160
161
|
# * Level :m 15% of code can be restored
|
@@ -169,54 +170,53 @@ module RQRCodeCore
|
|
169
170
|
# qr = RQRCodeCore::QRCode.new('hello world', size: 1, level: :m, mode: :alphanumeric)
|
170
171
|
#
|
171
172
|
|
172
|
-
def initialize(
|
173
|
+
def initialize(string, *args)
|
173
174
|
if !string.is_a? String
|
174
175
|
raise QRCodeArgumentError, "The passed data is #{string.class}, not String"
|
175
176
|
end
|
176
177
|
|
177
|
-
options
|
178
|
-
level
|
178
|
+
options = extract_options!(args)
|
179
|
+
level = (options[:level] || :h).to_sym
|
179
180
|
|
180
181
|
if !QRERRORCORRECTLEVEL.has_key?(level)
|
181
182
|
raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`"
|
182
183
|
end
|
183
184
|
|
184
|
-
@data
|
185
|
+
@data = string
|
185
186
|
|
186
|
-
mode
|
187
|
+
mode = QRMODE_NAME[(options[:mode] || "").to_sym]
|
187
188
|
# If mode is not explicitely given choose mode according to data type
|
188
|
-
mode ||=
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
QRMODE_NAME[:byte_8bit]
|
189
|
+
mode ||= if RQRCodeCore::QRNumeric.valid_data?(@data)
|
190
|
+
QRMODE_NAME[:number]
|
191
|
+
elsif QRAlphanumeric.valid_data?(@data)
|
192
|
+
QRMODE_NAME[:alphanumeric]
|
193
|
+
else
|
194
|
+
QRMODE_NAME[:byte_8bit]
|
195
195
|
end
|
196
196
|
|
197
|
-
max_size_array
|
198
|
-
size
|
197
|
+
max_size_array = QRMAXDIGITS[level][mode]
|
198
|
+
size = options[:size] || smallest_size_for(string, max_size_array)
|
199
199
|
|
200
200
|
if size > QRUtil.max_size
|
201
201
|
raise QRCodeArgumentError, "Given size greater than maximum possible size of #{QRUtil.max_size}"
|
202
202
|
end
|
203
203
|
|
204
|
-
@error_correct_level
|
205
|
-
@version
|
206
|
-
@module_count
|
207
|
-
@modules
|
208
|
-
@data_list
|
204
|
+
@error_correct_level = QRERRORCORRECTLEVEL[level]
|
205
|
+
@version = size
|
206
|
+
@module_count = @version * 4 + QRPOSITIONPATTERNLENGTH
|
207
|
+
@modules = Array.new(@module_count)
|
208
|
+
@data_list =
|
209
209
|
case mode
|
210
210
|
when :mode_number
|
211
|
-
QRNumeric.new(
|
211
|
+
QRNumeric.new(@data)
|
212
212
|
when :mode_alpha_numk
|
213
|
-
QRAlphanumeric.new(
|
213
|
+
QRAlphanumeric.new(@data)
|
214
214
|
else
|
215
|
-
QR8bitByte.new(
|
215
|
+
QR8bitByte.new(@data)
|
216
216
|
end
|
217
217
|
|
218
|
-
@data_cache
|
219
|
-
|
218
|
+
@data_cache = nil
|
219
|
+
make
|
220
220
|
end
|
221
221
|
|
222
222
|
# <tt>checked?</tt> is called with a +col+ and +row+ parameter. This will
|
@@ -227,18 +227,21 @@ module RQRCodeCore
|
|
227
227
|
# instance.checked?( 10, 10 ) => true
|
228
228
|
#
|
229
229
|
|
230
|
-
def checked?(
|
230
|
+
def checked?(row, col)
|
231
231
|
if !row.between?(0, @module_count - 1) || !col.between?(0, @module_count - 1)
|
232
232
|
raise QRCodeRunTimeError, "Invalid row/column pair: #{row}, #{col}"
|
233
233
|
end
|
234
234
|
@modules[row][col]
|
235
235
|
end
|
236
|
+
alias_method :dark?, :checked?
|
237
|
+
extend Gem::Deprecate
|
238
|
+
deprecate :dark?, :checked?, 2020, 1
|
236
239
|
|
237
240
|
# This is a public method that returns the QR Code you have
|
238
241
|
# generated as a string. It will not be able to be read
|
239
242
|
# in this format by a QR Code reader, but will give you an
|
240
243
|
# idea if the final outout. It takes two optional args
|
241
|
-
# +:
|
244
|
+
# +:dark+ and +:light+ which are there for you to choose
|
242
245
|
# how the output looks. Here's an example of it's use:
|
243
246
|
#
|
244
247
|
# instance.to_s =>
|
@@ -252,11 +255,11 @@ module RQRCodeCore
|
|
252
255
|
# EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE
|
253
256
|
#
|
254
257
|
|
255
|
-
def to_s(
|
256
|
-
options
|
257
|
-
dark
|
258
|
-
light
|
259
|
-
quiet_zone_size
|
258
|
+
def to_s(*args)
|
259
|
+
options = extract_options!(args)
|
260
|
+
dark = options[:dark] || "x"
|
261
|
+
light = options[:light] || " "
|
262
|
+
quiet_zone_size = options[:quiet_zone_size] || 0
|
260
263
|
|
261
264
|
rows = []
|
262
265
|
|
@@ -306,12 +309,12 @@ module RQRCodeCore
|
|
306
309
|
|
307
310
|
def make #:nodoc:
|
308
311
|
prepare_common_patterns
|
309
|
-
make_impl(
|
312
|
+
make_impl(false, get_best_mask_pattern)
|
310
313
|
end
|
311
314
|
|
312
315
|
private
|
313
316
|
|
314
|
-
def prepare_common_patterns
|
317
|
+
def prepare_common_patterns #:nodoc:
|
315
318
|
@modules.map! { |row| Array.new(@module_count) }
|
316
319
|
|
317
320
|
place_position_probe_pattern(0, 0)
|
@@ -323,7 +326,7 @@ module RQRCodeCore
|
|
323
326
|
@common_patterns = @modules.map(&:clone)
|
324
327
|
end
|
325
328
|
|
326
|
-
def make_impl(
|
329
|
+
def make_impl(test, mask_pattern) #:nodoc:
|
327
330
|
@modules = @common_patterns.map(&:clone)
|
328
331
|
|
329
332
|
place_format_info(test, mask_pattern)
|
@@ -335,20 +338,19 @@ module RQRCodeCore
|
|
335
338
|
)
|
336
339
|
end
|
337
340
|
|
338
|
-
map_data(
|
341
|
+
map_data(@data_cache, mask_pattern)
|
339
342
|
end
|
340
343
|
|
341
|
-
|
342
|
-
def place_position_probe_pattern( row, col ) #:nodoc:
|
344
|
+
def place_position_probe_pattern(row, col) #:nodoc:
|
343
345
|
(-1..7).each do |r|
|
344
|
-
next
|
346
|
+
next unless (row + r).between?(0, @module_count - 1)
|
345
347
|
|
346
348
|
(-1..7).each do |c|
|
347
|
-
next
|
349
|
+
next unless (col + c).between?(0, @module_count - 1)
|
348
350
|
|
349
351
|
is_vert_line = (r.between?(0, 6) && (c == 0 || c == 6))
|
350
352
|
is_horiz_line = (c.between?(0, 6) && (r == 0 || r == 6))
|
351
|
-
is_square = r.between?(2,4) && c.between?(2, 4)
|
353
|
+
is_square = r.between?(2, 4) && c.between?(2, 4)
|
352
354
|
|
353
355
|
is_part_of_probe = is_vert_line || is_horiz_line || is_square
|
354
356
|
@modules[row + r][col + c] = is_part_of_probe
|
@@ -356,14 +358,13 @@ module RQRCodeCore
|
|
356
358
|
end
|
357
359
|
end
|
358
360
|
|
359
|
-
|
360
361
|
def get_best_mask_pattern #:nodoc:
|
361
362
|
min_lost_point = 0
|
362
363
|
pattern = 0
|
363
364
|
|
364
|
-
(
|
365
|
-
make_impl(
|
366
|
-
lost_point = QRUtil.get_lost_points(
|
365
|
+
(0...8).each do |i|
|
366
|
+
make_impl(true, i)
|
367
|
+
lost_point = QRUtil.get_lost_points(modules)
|
367
368
|
|
368
369
|
if i == 0 || min_lost_point > lost_point
|
369
370
|
min_lost_point = lost_point
|
@@ -373,14 +374,12 @@ module RQRCodeCore
|
|
373
374
|
pattern
|
374
375
|
end
|
375
376
|
|
376
|
-
|
377
377
|
def place_timing_pattern #:nodoc:
|
378
|
-
(
|
378
|
+
(8...@module_count - 8).each do |i|
|
379
379
|
@modules[i][6] = @modules[6][i] = i % 2 == 0
|
380
380
|
end
|
381
381
|
end
|
382
382
|
|
383
|
-
|
384
383
|
def place_position_adjust_pattern #:nodoc:
|
385
384
|
positions = QRUtil.get_pattern_positions(@version)
|
386
385
|
|
@@ -388,9 +387,9 @@ module RQRCodeCore
|
|
388
387
|
positions.each do |col|
|
389
388
|
next unless @modules[row][col].nil?
|
390
389
|
|
391
|
-
(
|
392
|
-
(
|
393
|
-
is_part_of_pattern = (r.abs == 2 || c.abs == 2 || (
|
390
|
+
(-2..2).each do |r|
|
391
|
+
(-2..2).each do |c|
|
392
|
+
is_part_of_pattern = (r.abs == 2 || c.abs == 2 || (r == 0 && c == 0))
|
394
393
|
@modules[row + r][col + c] = is_part_of_pattern
|
395
394
|
end
|
396
395
|
end
|
@@ -398,69 +397,65 @@ module RQRCodeCore
|
|
398
397
|
end
|
399
398
|
end
|
400
399
|
|
401
|
-
|
402
400
|
def place_version_info(test) #:nodoc:
|
403
401
|
bits = QRUtil.get_bch_version(@version)
|
404
402
|
|
405
|
-
(
|
406
|
-
mod = (
|
407
|
-
@modules[
|
408
|
-
@modules[
|
403
|
+
(0...18).each do |i|
|
404
|
+
mod = (!test && ((bits >> i) & 1) == 1)
|
405
|
+
@modules[(i / 3).floor][ i % 3 + @module_count - 8 - 3 ] = mod
|
406
|
+
@modules[i % 3 + @module_count - 8 - 3][ (i / 3).floor ] = mod
|
409
407
|
end
|
410
408
|
end
|
411
409
|
|
412
|
-
|
413
410
|
def place_format_info(test, mask_pattern) #:nodoc:
|
414
411
|
data = (@error_correct_level << 3 | mask_pattern)
|
415
412
|
bits = QRUtil.get_bch_format_info(data)
|
416
413
|
|
417
414
|
QRFORMATINFOLENGTH.times do |i|
|
418
|
-
mod = (!test && (
|
415
|
+
mod = (!test && ((bits >> i) & 1) == 1)
|
419
416
|
|
420
417
|
# vertical
|
421
|
-
if i < 6
|
422
|
-
|
418
|
+
row = if i < 6
|
419
|
+
i
|
423
420
|
elsif i < 8
|
424
|
-
|
421
|
+
i + 1
|
425
422
|
else
|
426
|
-
|
423
|
+
@module_count - 15 + i
|
427
424
|
end
|
428
425
|
@modules[row][8] = mod
|
429
426
|
|
430
427
|
# horizontal
|
431
|
-
if i < 8
|
432
|
-
|
428
|
+
col = if i < 8
|
429
|
+
@module_count - i - 1
|
433
430
|
elsif i < 9
|
434
|
-
|
431
|
+
15 - i - 1 + 1
|
435
432
|
else
|
436
|
-
|
433
|
+
15 - i - 1
|
437
434
|
end
|
438
435
|
@modules[8][col] = mod
|
439
436
|
end
|
440
437
|
|
441
438
|
# fixed module
|
442
|
-
@modules[
|
439
|
+
@modules[@module_count - 8][8] = !test
|
443
440
|
end
|
444
441
|
|
445
|
-
|
446
|
-
def map_data( data, mask_pattern ) #:nodoc:
|
442
|
+
def map_data(data, mask_pattern) #:nodoc:
|
447
443
|
inc = -1
|
448
444
|
row = @module_count - 1
|
449
445
|
bit_index = 7
|
450
446
|
byte_index = 0
|
451
447
|
|
452
|
-
(
|
453
|
-
col
|
448
|
+
(@module_count - 1).step(1, -2) do |col|
|
449
|
+
col -= 1 if col <= 6
|
454
450
|
|
455
|
-
|
456
|
-
(
|
457
|
-
|
458
|
-
if @modules[row][ col - c ].nil?
|
451
|
+
loop do
|
452
|
+
(0...2).each do |c|
|
453
|
+
if @modules[row][col - c].nil?
|
459
454
|
dark = false
|
460
455
|
if byte_index < data.size && !data[byte_index].nil?
|
461
|
-
dark = ((
|
456
|
+
dark = ((QRUtil.rszf(data[byte_index], bit_index) & 1) == 1)
|
462
457
|
end
|
463
|
-
mask = QRUtil.get_mask(
|
458
|
+
mask = QRUtil.get_mask(mask_pattern, row, col - c)
|
464
459
|
dark = !dark if mask
|
465
460
|
@modules[row][ col - c ] = dark
|
466
461
|
bit_index -= 1
|
@@ -485,95 +480,100 @@ module RQRCodeCore
|
|
485
480
|
|
486
481
|
def smallest_size_for(string, max_size_array) #:nodoc:
|
487
482
|
l = string.bytesize
|
488
|
-
ver = max_size_array.index{|i| i >= l}
|
489
|
-
raise QRCodeRunTimeError,"code length overflow. (#{l} digits > any version capacity)" unless ver
|
483
|
+
ver = max_size_array.index { |i| i >= l }
|
484
|
+
raise QRCodeRunTimeError, "code length overflow. (#{l} digits > any version capacity)" unless ver
|
490
485
|
ver + 1
|
491
486
|
end
|
492
487
|
|
493
|
-
def
|
494
|
-
|
495
|
-
sum + rs_block.data_count
|
496
|
-
end
|
497
|
-
|
498
|
-
return max_data_bytes * 8
|
488
|
+
def extract_options!(arr) #:nodoc:
|
489
|
+
arr.last.is_a?(::Hash) ? arr.pop : {}
|
499
490
|
end
|
500
491
|
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
data_list.write(buffer)
|
507
|
-
buffer.end_of_message(max_data_bits)
|
492
|
+
class << self
|
493
|
+
def count_max_data_bits(rs_blocks) #:nodoc:
|
494
|
+
max_data_bytes = rs_blocks.reduce(0) do |sum, rs_block|
|
495
|
+
sum + rs_block.data_count
|
496
|
+
end
|
508
497
|
|
509
|
-
|
510
|
-
raise QRCodeRunTimeError, "code length overflow. (#{buffer.get_length_in_bits}>#{max_data_bits}). (Try a larger size!)"
|
498
|
+
max_data_bytes * 8
|
511
499
|
end
|
512
500
|
|
513
|
-
|
501
|
+
def create_data(version, error_correct_level, data_list) #:nodoc:
|
502
|
+
rs_blocks = QRRSBlock.get_rs_blocks(version, error_correct_level)
|
503
|
+
max_data_bits = QRCode.count_max_data_bits(rs_blocks)
|
504
|
+
buffer = QRBitBuffer.new(version)
|
514
505
|
|
515
|
-
|
516
|
-
|
506
|
+
data_list.write(buffer)
|
507
|
+
buffer.end_of_message(max_data_bits)
|
517
508
|
|
509
|
+
if buffer.get_length_in_bits > max_data_bits
|
510
|
+
raise QRCodeRunTimeError, "code length overflow. (#{buffer.get_length_in_bits}>#{max_data_bits}). (Try a larger size!)"
|
511
|
+
end
|
518
512
|
|
519
|
-
|
520
|
-
offset = 0
|
521
|
-
max_dc_count = 0
|
522
|
-
max_ec_count = 0
|
523
|
-
dcdata = Array.new( rs_blocks.size )
|
524
|
-
ecdata = Array.new( rs_blocks.size )
|
513
|
+
buffer.pad_until(max_data_bits)
|
525
514
|
|
526
|
-
|
527
|
-
|
528
|
-
ec_count = rs_block.total_count - dc_count
|
529
|
-
max_dc_count = [ max_dc_count, dc_count ].max
|
530
|
-
max_ec_count = [ max_ec_count, ec_count ].max
|
515
|
+
QRCode.create_bytes(buffer, rs_blocks)
|
516
|
+
end
|
531
517
|
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
dcdata
|
518
|
+
def create_bytes(buffer, rs_blocks) #:nodoc:
|
519
|
+
offset = 0
|
520
|
+
max_dc_count = 0
|
521
|
+
max_ec_count = 0
|
522
|
+
dcdata = Array.new(rs_blocks.size)
|
523
|
+
ecdata = Array.new(rs_blocks.size)
|
524
|
+
|
525
|
+
rs_blocks.each_with_index do |rs_block, r|
|
526
|
+
dc_count = rs_block.data_count
|
527
|
+
ec_count = rs_block.total_count - dc_count
|
528
|
+
max_dc_count = [max_dc_count, dc_count].max
|
529
|
+
max_ec_count = [max_ec_count, ec_count].max
|
530
|
+
|
531
|
+
dcdata_block = Array.new(dc_count)
|
532
|
+
dcdata_block.size.times do |i|
|
533
|
+
dcdata_block[i] = 0xff & buffer.buffer[i + offset]
|
534
|
+
end
|
535
|
+
dcdata[r] = dcdata_block
|
537
536
|
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
537
|
+
offset += dc_count
|
538
|
+
rs_poly = QRUtil.get_error_correct_polynomial(ec_count)
|
539
|
+
raw_poly = QRPolynomial.new(dcdata[r], rs_poly.get_length - 1)
|
540
|
+
mod_poly = raw_poly.mod(rs_poly)
|
542
541
|
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
542
|
+
ecdata_block = Array.new(rs_poly.get_length - 1)
|
543
|
+
ecdata_block.size.times do |i|
|
544
|
+
mod_index = i + mod_poly.get_length - ecdata_block.size
|
545
|
+
ecdata_block[i] = mod_index >= 0 ? mod_poly.get(mod_index) : 0
|
546
|
+
end
|
547
|
+
ecdata[r] = ecdata_block
|
547
548
|
end
|
548
|
-
ecdata[r] = ecdata_block
|
549
|
-
end
|
550
549
|
|
551
|
-
|
552
|
-
|
553
|
-
|
550
|
+
total_code_count = rs_blocks.reduce(0) do |sum, rs_block|
|
551
|
+
sum + rs_block.total_count
|
552
|
+
end
|
554
553
|
|
555
|
-
|
556
|
-
|
554
|
+
data = Array.new(total_code_count)
|
555
|
+
index = 0
|
557
556
|
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
557
|
+
max_dc_count.times do |i|
|
558
|
+
rs_blocks.size.times do |r|
|
559
|
+
if i < dcdata[r].size
|
560
|
+
data[index] = dcdata[r][i]
|
561
|
+
index += 1
|
562
|
+
end
|
563
563
|
end
|
564
564
|
end
|
565
|
-
end
|
566
565
|
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
566
|
+
max_ec_count.times do |i|
|
567
|
+
rs_blocks.size.times do |r|
|
568
|
+
if i < ecdata[r].size
|
569
|
+
data[index] = ecdata[r][i]
|
570
|
+
index += 1
|
571
|
+
end
|
572
572
|
end
|
573
573
|
end
|
574
|
-
end
|
575
574
|
|
576
|
-
|
575
|
+
data
|
576
|
+
end
|
577
577
|
end
|
578
578
|
end
|
579
579
|
end
|