barby 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +0,0 @@
1
- require 'rqrcode/core_ext/integer/bitwise'
2
-
3
- class Integer #:nodoc:
4
- include CoreExtensions::Integer::Bitwise
5
- end
@@ -1,11 +0,0 @@
1
- module CoreExtensions #:nodoc:
2
- module Integer #:nodoc:
3
- module Bitwise
4
- def rszf(count)
5
- # zero fill right shift
6
- (self >> count) & ((2 ** ((self.size * 8) - count))-1)
7
- end
8
- end
9
- end
10
- end
11
-
@@ -1,4 +0,0 @@
1
- Dir[File.dirname(__FILE__) + "/qrcode/*.rb"].sort.each do |path|
2
- filename = File.basename(path)
3
- require "rqrcode/qrcode/#{filename}"
4
- end
@@ -1,37 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #--
4
- # Copyright 2004 by Duncan Robertson (duncan@whomwah.com).
5
- # All rights reserved.
6
-
7
- # Permission is granted for use, copying, modification, distribution,
8
- # and distribution of modified versions of this work as long as the
9
- # above copyright notice is included.
10
- #++
11
-
12
- module RQRCode
13
-
14
- class QR8bitByte
15
- attr_reader :mode
16
-
17
- def initialize( data )
18
- @mode = QRMODE[:mode_8bit_byte]
19
- @data = data;
20
- end
21
-
22
-
23
- def get_length
24
- @data.size
25
- end
26
-
27
-
28
- def write( buffer )
29
- ( 0...@data.size ).each do |i|
30
- c = @data[i]
31
- c = c.ord if c.respond_to?(:ord)#String#[] returns single-char string in 1.9, .ord gets ASCII pos
32
- buffer.put( c, 8 )
33
- end
34
- end
35
- end
36
-
37
- end
@@ -1,56 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #--
4
- # Copyright 2004 by Duncan Robertson (duncan@whomwah.com).
5
- # All rights reserved.
6
-
7
- # Permission is granted for use, copying, modification, distribution,
8
- # and distribution of modified versions of this work as long as the
9
- # above copyright notice is included.
10
- #++
11
-
12
- module RQRCode
13
-
14
- class QRBitBuffer
15
- attr_reader :buffer
16
-
17
- def initialize
18
- @buffer = []
19
- @length = 0
20
- end
21
-
22
-
23
- def get( index )
24
- buf_index = (index / 8).floor
25
- (( (@buffer[buf_index]).rszf(7 - index % 8)) & 1) == 1
26
- end
27
-
28
-
29
- def put( num, length )
30
- ( 0...length ).each do |i|
31
- put_bit((((num).rszf(length - i - 1)) & 1) == 1)
32
- end
33
- end
34
-
35
-
36
- def get_length_in_bits
37
- @length
38
- end
39
-
40
-
41
- def put_bit( bit )
42
- buf_index = ( @length / 8 ).floor
43
- if @buffer.size <= buf_index
44
- @buffer << 0
45
- end
46
-
47
- if bit
48
- @buffer[buf_index] |= ((0x80).rszf(@length % 8))
49
- end
50
-
51
- @length += 1
52
- end
53
-
54
- end
55
-
56
- end
@@ -1,423 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #--
4
- # Copyright 2008 by Duncan Robertson (duncan@whomwah.com).
5
- # All rights reserved.
6
-
7
- # Permission is granted for use, copying, modification, distribution,
8
- # and distribution of modified versions of this work as long as the
9
- # above copyright notice is included.
10
- #++
11
-
12
- module RQRCode #:nodoc:
13
-
14
- QRMODE = {
15
- :mode_number => 1 << 0,
16
- :mode_alpha_numk => 1 << 1,
17
- :mode_8bit_byte => 1 << 2,
18
- :mode_kanji => 1 << 3
19
- }
20
-
21
- QRERRORCORRECTLEVEL = {
22
- :l => 1,
23
- :m => 0,
24
- :q => 3,
25
- :h => 2
26
- }
27
-
28
- QRMASKPATTERN = {
29
- :pattern000 => 0,
30
- :pattern001 => 1,
31
- :pattern010 => 2,
32
- :pattern011 => 3,
33
- :pattern100 => 4,
34
- :pattern101 => 5,
35
- :pattern110 => 6,
36
- :pattern111 => 7
37
- }
38
-
39
- # StandardErrors
40
-
41
- class QRCodeArgumentError < ArgumentError; end
42
- class QRCodeRunTimeError < RuntimeError; end
43
-
44
- # == Creation
45
- #
46
- # QRCode objects expect only one required constructor parameter
47
- # and an optional hash of any other. Here's a few examples:
48
- #
49
- # qr = RQRCode::QRCode.new('hello world')
50
- # qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )
51
- #
52
-
53
- class QRCode
54
- attr_reader :modules, :module_count
55
-
56
- PAD0 = 0xEC
57
- PAD1 = 0x11
58
-
59
- # Expects a string to be parsed in, other args are optional
60
- #
61
- # # string - the string you wish to encode
62
- # # size - the size of the qrcode (default 4)
63
- # # level - the error correction level, can be:
64
- # * Level :l 7% of code can be restored
65
- # * Level :m 15% of code can be restored
66
- # * Level :q 25% of code can be restored
67
- # * Level :h 30% of code can be restored (default :h)
68
- #
69
- # qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )
70
- #
71
-
72
- def initialize( *args )
73
- raise QRCodeArgumentError unless args.first.kind_of?( String )
74
-
75
- @data = args.shift
76
- options = args.extract_options!
77
- level = options[:level] || :h
78
-
79
- raise QRCodeArgumentError unless %w(l m q h).include?(level.to_s)
80
-
81
- @error_correct_level = QRERRORCORRECTLEVEL[ level.to_sym ]
82
- @type_number = options[:size] || 4
83
- @module_count = @type_number * 4 + 17
84
- @modules = Array.new( @module_count )
85
- @data_list = QR8bitByte.new( @data )
86
- @data_cache = nil
87
-
88
- self.make
89
- end
90
-
91
- # <tt>is_dark</tt> is called with a +col+ and +row+ parameter. This will
92
- # return true or false based on whether that coordinate exists in the
93
- # matrix returned. It would normally be called while iterating through
94
- # <tt>modules</tt>. A simple example would be:
95
- #
96
- # instance.is_dark( 10, 10 ) => true
97
- #
98
-
99
- def is_dark( row, col )
100
- if row < 0 || @module_count <= row || col < 0 || @module_count <= col
101
- raise QRCodeRunTimeError, "#{row},#{col}"
102
- end
103
- @modules[row][col]
104
- end
105
-
106
- # This is a public method that returns the QR Code you have
107
- # generated as a string. It will not be able to be read
108
- # in this format by a QR Code reader, but will give you an
109
- # idea if the final outout. It takes two optional args
110
- # +:true+ and +:false+ which are there for you to choose
111
- # how the output looks. Here's an example of it's use:
112
- #
113
- # instance.to_s =>
114
- # xxxxxxx x x x x x xx xxxxxxx
115
- # x x xxx xxxxxx xxx x x
116
- # x xxx x xxxxx x xx x xxx x
117
- #
118
- # instance._to_s( :true => 'E', :false => 'Q') =>
119
- # EEEEEEEQEQQEQEQQQEQEQQEEQQEEEEEEE
120
- # EQQQQQEQQEEEQQEEEEEEQEEEQQEQQQQQE
121
- # EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE
122
- #
123
-
124
- def to_s( *args )
125
- options = args.extract_options!
126
- row = options[:true] || 'x'
127
- col = options[:false] || ' '
128
-
129
- res = []
130
-
131
- @modules.each_index do |c|
132
- tmp = []
133
- @modules.each_index do |r|
134
- if is_dark(c,r)
135
- tmp << row
136
- else
137
- tmp << col
138
- end
139
- end
140
- res << tmp.join
141
- end
142
- res.join("\n")
143
- end
144
-
145
- protected
146
-
147
- def make #:nodoc:
148
- make_impl( false, get_best_mask_pattern )
149
- end
150
-
151
- private
152
-
153
-
154
- def make_impl( test, mask_pattern ) #:nodoc:
155
-
156
- ( 0...@module_count ).each do |row|
157
- @modules[row] = Array.new( @module_count )
158
- end
159
-
160
- setup_position_probe_pattern( 0, 0 )
161
- setup_position_probe_pattern( @module_count - 7, 0 )
162
- setup_position_probe_pattern( 0, @module_count - 7 )
163
- setup_position_adjust_pattern
164
- setup_timing_pattern
165
- setup_type_info( test, mask_pattern )
166
- setup_type_number( test ) if @type_number >= 7
167
-
168
- if @data_cache.nil?
169
- @data_cache = QRCode.create_data(
170
- @type_number, @error_correct_level, @data_list
171
- )
172
- end
173
-
174
- map_data( @data_cache, mask_pattern )
175
- end
176
-
177
-
178
- def setup_position_probe_pattern( row, col ) #:nodoc:
179
- ( -1..7 ).each do |r|
180
- next if ( row + r ) <= -1 || @module_count <= ( row + r )
181
- ( -1..7 ).each do |c|
182
- next if ( col + c ) <= -1 || @module_count <= ( col + c )
183
- if 0 <= r && r <= 6 && ( c == 0 || c == 6 ) || 0 <= c && c <= 6 && ( r == 0 || r == 6 ) || 2 <= r && r <= 4 && 2 <= c && c <= 4
184
- @modules[row + r][col + c] = true;
185
- else
186
- @modules[row + r][col + c] = false;
187
- end
188
- end
189
- end
190
- end
191
-
192
-
193
- def get_best_mask_pattern #:nodoc:
194
- min_lost_point = 0
195
- pattern = 0
196
-
197
- ( 0...8 ).each do |i|
198
- make_impl( true, i )
199
- lost_point = QRUtil.get_lost_point( self )
200
-
201
- if i == 0 || min_lost_point > lost_point
202
- min_lost_point = lost_point
203
- pattern = i
204
- end
205
- end
206
- pattern
207
- end
208
-
209
-
210
- def setup_timing_pattern #:nodoc:
211
- ( 8...@module_count - 8 ).each do |i|
212
- @modules[i][6] = @modules[6][i] = i % 2 == 0
213
- end
214
- end
215
-
216
-
217
- def setup_position_adjust_pattern #:nodoc:
218
- pos = QRUtil.get_pattern_position(@type_number)
219
-
220
- ( 0...pos.size ).each do |i|
221
- ( 0...pos.size ).each do |j|
222
- row = pos[i]
223
- col = pos[j]
224
-
225
- next unless @modules[row][col].nil?
226
-
227
- ( -2..2 ).each do |r|
228
- ( -2..2 ).each do |c|
229
- if r == -2 || r == 2 || c == -2 || c == 2 || ( r == 0 && c == 0 )
230
- @modules[row + r][col + c] = true
231
- else
232
- @modules[row + r][col + c] = false
233
- end
234
- end
235
- end
236
- end
237
- end
238
- end
239
-
240
-
241
- def setup_type_number( test ) #:nodoc:
242
- bits = QRUtil.get_bch_type_number( @type_number )
243
-
244
- ( 0...18 ).each do |i|
245
- mod = ( !test && ( (bits >> i) & 1) == 1 )
246
- @modules[ (i / 3).floor ][ i % 3 + @module_count - 8 - 3 ] = mod
247
- @modules[ i % 3 + @module_count - 8 - 3 ][ (i / 3).floor ] = mod
248
- end
249
- end
250
-
251
-
252
- def setup_type_info( test, mask_pattern ) #:nodoc:
253
- data = (@error_correct_level << 3 | mask_pattern)
254
- bits = QRUtil.get_bch_type_info( data )
255
-
256
- ( 0...15 ).each do |i|
257
- mod = (!test && ( (bits >> i) & 1) == 1)
258
-
259
- # vertical
260
- if i < 6
261
- @modules[i][8] = mod
262
- elsif i < 8
263
- @modules[ i + 1 ][8] = mod
264
- else
265
- @modules[ @module_count - 15 + i ][8] = mod
266
- end
267
-
268
- # horizontal
269
- if i < 8
270
- @modules[8][ @module_count - i - 1 ] = mod
271
- elsif i < 9
272
- @modules[8][ 15 - i - 1 + 1 ] = mod
273
- else
274
- @modules[8][ 15 - i - 1 ] = mod
275
- end
276
- end
277
-
278
- # fixed module
279
- @modules[ @module_count - 8 ][8] = !test
280
- end
281
-
282
-
283
- def map_data( data, mask_pattern ) #:nodoc:
284
- inc = -1
285
- row = @module_count - 1
286
- bit_index = 7
287
- byte_index = 0
288
-
289
- ( @module_count - 1 ).step( 1, -2 ) do |col|
290
- col = col - 1 if col <= 6
291
-
292
- while true do
293
- ( 0...2 ).each do |c|
294
-
295
- if @modules[row][ col - c ].nil?
296
- dark = false
297
- if byte_index < data.size && !data[byte_index].nil?
298
- dark = (( (data[byte_index]).rszf( bit_index ) & 1) == 1 )
299
- end
300
- mask = QRUtil.get_mask( mask_pattern, row, col - c )
301
- dark = !dark if mask
302
- @modules[row][ col - c ] = dark
303
- bit_index -= 1
304
-
305
- if bit_index == -1
306
- byte_index += 1
307
- bit_index = 7
308
- end
309
- end
310
- end
311
-
312
- row += inc
313
-
314
- if row < 0 || @module_count <= row
315
- row -= inc
316
- inc = -inc
317
- break
318
- end
319
- end
320
- end
321
- end
322
-
323
- def QRCode.create_data( type_number, error_correct_level, data_list ) #:nodoc:
324
- rs_blocks = QRRSBlock.get_rs_blocks( type_number, error_correct_level )
325
- buffer = QRBitBuffer.new
326
-
327
- data = data_list
328
- buffer.put( data.mode, 4 )
329
- buffer.put(
330
- data.get_length, QRUtil.get_length_in_bits( data.mode, type_number )
331
- )
332
- data.write( buffer )
333
-
334
- total_data_count = 0
335
- ( 0...rs_blocks.size ).each do |i|
336
- total_data_count = total_data_count + rs_blocks[i].data_count
337
- end
338
-
339
- if buffer.get_length_in_bits > total_data_count * 8
340
- raise QRCodeRunTimeError,
341
- "code length overflow. (#{buffer.get_length_in_bits}>#{total_data_count})"
342
- end
343
-
344
- if buffer.get_length_in_bits + 4 <= total_data_count * 8
345
- buffer.put( 0, 4 )
346
- end
347
-
348
- while buffer.get_length_in_bits % 8 != 0
349
- buffer.put_bit( false )
350
- end
351
-
352
- while true
353
- break if buffer.get_length_in_bits >= total_data_count * 8
354
- buffer.put( QRCode::PAD0, 8 )
355
- break if buffer.get_length_in_bits >= total_data_count * 8
356
- buffer.put( QRCode::PAD1, 8 )
357
- end
358
-
359
- QRCode.create_bytes( buffer, rs_blocks )
360
- end
361
-
362
-
363
- def QRCode.create_bytes( buffer, rs_blocks ) #:nodoc:
364
- offset = 0
365
- max_dc_count = 0
366
- max_ec_count = 0
367
- dcdata = Array.new( rs_blocks.size )
368
- ecdata = Array.new( rs_blocks.size )
369
-
370
- ( 0...rs_blocks.size ).each do |r|
371
- dc_count = rs_blocks[r].data_count
372
- ec_count = rs_blocks[r].total_count - dc_count
373
- max_dc_count = [ max_dc_count, dc_count ].max
374
- max_ec_count = [ max_ec_count, ec_count ].max
375
- dcdata[r] = Array.new( dc_count )
376
-
377
- ( 0...dcdata[r].size ).each do |i|
378
- dcdata[r][i] = 0xff & buffer.buffer[ i + offset ]
379
- end
380
-
381
- offset = offset + dc_count
382
- rs_poly = QRUtil.get_error_correct_polynomial( ec_count )
383
- raw_poly = QRPolynomial.new( dcdata[r], rs_poly.get_length - 1 )
384
- mod_poly = raw_poly.mod( rs_poly )
385
- ecdata[r] = Array.new( rs_poly.get_length - 1 )
386
- ( 0...ecdata[r].size ).each do |i|
387
- mod_index = i + mod_poly.get_length - ecdata[r].size
388
- ecdata[r][i] = mod_index >= 0 ? mod_poly.get( mod_index ) : 0
389
- end
390
- end
391
-
392
- total_code_count = 0
393
- ( 0...rs_blocks.size ).each do |i|
394
- total_code_count = total_code_count + rs_blocks[i].total_count
395
- end
396
-
397
- data = Array.new( total_code_count )
398
- index = 0
399
-
400
- ( 0...max_dc_count ).each do |i|
401
- ( 0...rs_blocks.size ).each do |r|
402
- if i < dcdata[r].size
403
- index += 1
404
- data[index-1] = dcdata[r][i]
405
- end
406
- end
407
- end
408
-
409
- ( 0...max_ec_count ).each do |i|
410
- ( 0...rs_blocks.size ).each do |r|
411
- if i < ecdata[r].size
412
- index += 1
413
- data[index-1] = ecdata[r][i]
414
- end
415
- end
416
- end
417
-
418
- data
419
- end
420
-
421
- end
422
-
423
- end