barby 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/bin/barby +40 -0
  2. data/lib/barby.rb +3 -0
  3. data/lib/barby/barcode.rb +6 -0
  4. data/lib/barby/barcode/code_128.rb +16 -3
  5. data/lib/barby/barcode/code_25.rb +161 -0
  6. data/lib/barby/barcode/code_25_interleaved.rb +73 -0
  7. data/lib/barby/barcode/code_93.rb +225 -0
  8. data/lib/barby/barcode/qr_code.rb +93 -0
  9. data/lib/barby/outputter.rb +56 -2
  10. data/lib/barby/outputter/ascii_outputter.rb +20 -1
  11. data/lib/barby/outputter/cairo_outputter.rb +38 -7
  12. data/lib/barby/outputter/pdfwriter_outputter.rb +44 -35
  13. data/lib/barby/outputter/png_outputter.rb +48 -16
  14. data/lib/barby/outputter/prawn_outputter.rb +100 -0
  15. data/lib/barby/outputter/rmagick_outputter.rb +48 -20
  16. data/lib/barby/vendor.rb +3 -0
  17. data/vendor/rqrcode/CHANGELOG +21 -0
  18. data/vendor/rqrcode/COPYING +19 -0
  19. data/vendor/rqrcode/README +98 -0
  20. data/vendor/rqrcode/Rakefile +65 -0
  21. data/vendor/rqrcode/lib/rqrcode.rb +13 -0
  22. data/vendor/rqrcode/lib/rqrcode/core_ext.rb +5 -0
  23. data/vendor/rqrcode/lib/rqrcode/core_ext/array.rb +5 -0
  24. data/vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb +9 -0
  25. data/vendor/rqrcode/lib/rqrcode/core_ext/integer.rb +5 -0
  26. data/vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb +11 -0
  27. data/vendor/rqrcode/lib/rqrcode/qrcode.rb +4 -0
  28. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb +35 -0
  29. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb +56 -0
  30. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb +421 -0
  31. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb +63 -0
  32. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb +78 -0
  33. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb +134 -0
  34. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb +254 -0
  35. data/vendor/rqrcode/test/runtest.rb +78 -0
  36. data/vendor/rqrcode/test/test_data.rb +21 -0
  37. metadata +86 -44
@@ -0,0 +1,9 @@
1
+ module CoreExtensions #:nodoc:
2
+ module Array #:nodoc:
3
+ module Behavior
4
+ def extract_options!
5
+ last.is_a?(::Hash) ? pop : {}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'rqrcode/core_ext/integer/bitwise'
2
+
3
+ class Integer #:nodoc:
4
+ include CoreExtensions::Integer::Bitwise
5
+ end
@@ -0,0 +1,11 @@
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
+
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + "/qrcode/*.rb"].sort.each do |path|
2
+ filename = File.basename(path)
3
+ require "rqrcode/qrcode/#{filename}"
4
+ end
@@ -0,0 +1,35 @@
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
+ buffer.put( @data[i], 8 )
31
+ end
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,56 @@
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
@@ -0,0 +1,421 @@
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
+ @error_correct_level = QRERRORCORRECTLEVEL[ level.to_sym ]
79
+ @type_number = options[:size] || 4
80
+ @module_count = @type_number * 4 + 17
81
+ @modules = nil
82
+ @data_cache = nil
83
+ @data_list = QR8bitByte.new( @data )
84
+
85
+ self.make
86
+ end
87
+
88
+ # <tt>is_dark</tt> is called with a +col+ and +row+ parameter. This will
89
+ # return true or false based on whether that coordinate exists in the
90
+ # matrix returned. It would normally be called while iterating through
91
+ # <tt>modules</tt>. A simple example would be:
92
+ #
93
+ # instance.is_dark( 10, 10 ) => true
94
+ #
95
+
96
+ def is_dark( row, col )
97
+ if row < 0 || @module_count <= row || col < 0 || @module_count <= col
98
+ raise QRCodeRunTimeError, "#{row},#{col}"
99
+ end
100
+ @modules[row][col]
101
+ end
102
+
103
+ # This is a public method that returns the QR Code you have
104
+ # generated as a string. It will not be able to be read
105
+ # in this format by a QR Code reader, but will give you an
106
+ # idea if the final outout. It takes two optional args
107
+ # +:true+ and +:false+ which are there for you to choose
108
+ # how the output looks. Here's an example of it's use:
109
+ #
110
+ # instance.to_s =>
111
+ # xxxxxxx x x x x x xx xxxxxxx
112
+ # x x xxx xxxxxx xxx x x
113
+ # x xxx x xxxxx x xx x xxx x
114
+ #
115
+ # instance._to_s( :true => 'E', :false => 'Q') =>
116
+ # EEEEEEEQEQQEQEQQQEQEQQEEQQEEEEEEE
117
+ # EQQQQQEQQEEEQQEEEEEEQEEEQQEQQQQQE
118
+ # EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE
119
+ #
120
+
121
+ def to_s( *args )
122
+ options = args.extract_options!
123
+ row = options[:true] || 'x'
124
+ col = options[:false] || ' '
125
+
126
+ res = []
127
+
128
+ @modules.each_index do |c|
129
+ tmp = []
130
+ @modules.each_index do |r|
131
+ if is_dark(c,r)
132
+ tmp << row
133
+ else
134
+ tmp << col
135
+ end
136
+ end
137
+ res << tmp.join
138
+ end
139
+ res.join("\n")
140
+ end
141
+
142
+ protected
143
+
144
+ def make #:nodoc:
145
+ make_impl( false, get_best_mask_pattern )
146
+ end
147
+
148
+ private
149
+
150
+
151
+ def make_impl( test, mask_pattern ) #:nodoc:
152
+ @modules = Array.new( @module_count )
153
+
154
+ ( 0...@module_count ).each do |row|
155
+ @modules[row] = Array.new( @module_count )
156
+ end
157
+
158
+ setup_position_probe_pattern( 0, 0 )
159
+ setup_position_probe_pattern( @module_count - 7, 0 )
160
+ setup_position_probe_pattern( 0, @module_count - 7 )
161
+ setup_position_adjust_pattern
162
+ setup_timing_pattern
163
+ setup_type_info( test, mask_pattern )
164
+ setup_type_number( test ) if @type_number >= 7
165
+
166
+ if @data_cache.nil?
167
+ @data_cache = QRCode.create_data(
168
+ @type_number, @error_correct_level, @data_list
169
+ )
170
+ end
171
+
172
+ map_data( @data_cache, mask_pattern )
173
+ end
174
+
175
+
176
+ def setup_position_probe_pattern( row, col ) #:nodoc:
177
+ ( -1..7 ).each do |r|
178
+ next if ( row + r ) <= -1 || @module_count <= ( row + r )
179
+ ( -1..7 ).each do |c|
180
+ next if ( col + c ) <= -1 || @module_count <= ( col + c )
181
+ 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
182
+ @modules[row + r][col + c] = true;
183
+ else
184
+ @modules[row + r][col + c] = false;
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+
191
+ def get_best_mask_pattern #:nodoc:
192
+ min_lost_point = 0
193
+ pattern = 0
194
+
195
+ ( 0...8 ).each do |i|
196
+ make_impl( true, i )
197
+ lost_point = QRUtil.get_lost_point( self )
198
+
199
+ if i == 0 || min_lost_point > lost_point
200
+ min_lost_point = lost_point
201
+ pattern = i
202
+ end
203
+ end
204
+ pattern
205
+ end
206
+
207
+
208
+ def setup_timing_pattern #:nodoc:
209
+ ( 8...@module_count - 8 ).each do |i|
210
+ @modules[i][6] = @modules[6][i] = i % 2 == 0
211
+ end
212
+ end
213
+
214
+
215
+ def setup_position_adjust_pattern #:nodoc:
216
+ pos = QRUtil.get_pattern_position(@type_number)
217
+
218
+ ( 0...pos.size ).each do |i|
219
+ ( 0...pos.size ).each do |j|
220
+ row = pos[i]
221
+ col = pos[j]
222
+
223
+ next unless @modules[row][col].nil?
224
+
225
+ ( -2..2 ).each do |r|
226
+ ( -2..2 ).each do |c|
227
+ if r == -2 || r == 2 || c == -2 || c == 2 || ( r == 0 && c == 0 )
228
+ @modules[row + r][col + c] = true
229
+ else
230
+ @modules[row + r][col + c] = false
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
236
+ end
237
+
238
+
239
+ def setup_type_number( test ) #:nodoc:
240
+ bits = QRUtil.get_bch_type_number( @type_number )
241
+
242
+ ( 0...18 ).each do |i|
243
+ mod = ( !test && ( (bits >> i) & 1) == 1 )
244
+ @modules[ (i / 3).floor ][ i % 3 + @module_count - 8 - 3 ] = mod
245
+ @modules[ i % 3 + @module_count - 8 - 3 ][ (i / 3).floor ] = mod
246
+ end
247
+ end
248
+
249
+
250
+ def setup_type_info( test, mask_pattern ) #:nodoc:
251
+ data = (@error_correct_level << 3 | mask_pattern)
252
+ bits = QRUtil.get_bch_type_info( data )
253
+
254
+ ( 0...15 ).each do |i|
255
+ mod = (!test && ( (bits >> i) & 1) == 1)
256
+
257
+ # vertical
258
+ if i < 6
259
+ @modules[i][8] = mod
260
+ elsif i < 8
261
+ @modules[ i + 1 ][8] = mod
262
+ else
263
+ @modules[ @module_count - 15 + i ][8] = mod
264
+ end
265
+
266
+ # horizontal
267
+ if i < 8
268
+ @modules[8][ @module_count - i - 1 ] = mod
269
+ elsif i < 9
270
+ @modules[8][ 15 - i - 1 + 1 ] = mod
271
+ else
272
+ @modules[8][ 15 - i - 1 ] = mod
273
+ end
274
+ end
275
+
276
+ # fixed module
277
+ @modules[ @module_count - 8 ][8] = !test
278
+ end
279
+
280
+
281
+ def map_data( data, mask_pattern ) #:nodoc:
282
+ inc = -1
283
+ row = @module_count - 1
284
+ bit_index = 7
285
+ byte_index = 0
286
+
287
+ ( @module_count - 1 ).step( 1, -2 ) do |col|
288
+ col = col - 1 if col <= 6
289
+
290
+ while true do
291
+ ( 0...2 ).each do |c|
292
+
293
+ if @modules[row][ col - c ].nil?
294
+ dark = false
295
+ if byte_index < data.size
296
+ dark = (( (data[byte_index]).rszf( bit_index ) & 1) == 1 )
297
+ end
298
+ mask = QRUtil.get_mask( mask_pattern, row, col - c )
299
+ dark = !dark if mask
300
+ @modules[row][ col - c ] = dark
301
+ bit_index -= 1
302
+
303
+ if bit_index == -1
304
+ byte_index += 1
305
+ bit_index = 7
306
+ end
307
+ end
308
+ end
309
+
310
+ row += inc
311
+
312
+ if row < 0 || @module_count <= row
313
+ row -= inc
314
+ inc = -inc
315
+ break
316
+ end
317
+ end
318
+ end
319
+ end
320
+
321
+ def QRCode.create_data( type_number, error_correct_level, data_list ) #:nodoc:
322
+ rs_blocks = QRRSBlock.get_rs_blocks( type_number, error_correct_level )
323
+ buffer = QRBitBuffer.new
324
+
325
+ data = data_list
326
+ buffer.put( data.mode, 4 )
327
+ buffer.put(
328
+ data.get_length, QRUtil.get_length_in_bits( data.mode, type_number )
329
+ )
330
+ data.write( buffer )
331
+
332
+ total_data_count = 0
333
+ ( 0...rs_blocks.size ).each do |i|
334
+ total_data_count = total_data_count + rs_blocks[i].data_count
335
+ end
336
+
337
+ if buffer.get_length_in_bits > total_data_count * 8
338
+ raise QRCodeRunTimeError,
339
+ "code length overflow. (#{buffer.get_length_in_bits}>#{total_data_count})"
340
+ end
341
+
342
+ if buffer.get_length_in_bits + 4 <= total_data_count * 8
343
+ buffer.put( 0, 4 )
344
+ end
345
+
346
+ while buffer.get_length_in_bits % 8 != 0
347
+ buffer.put_bit( false )
348
+ end
349
+
350
+ while true
351
+ break if buffer.get_length_in_bits >= total_data_count * 8
352
+ buffer.put( QRCode::PAD0, 8 )
353
+ break if buffer.get_length_in_bits >= total_data_count * 8
354
+ buffer.put( QRCode::PAD1, 8 )
355
+ end
356
+
357
+ QRCode.create_bytes( buffer, rs_blocks )
358
+ end
359
+
360
+
361
+ def QRCode.create_bytes( buffer, rs_blocks ) #:nodoc:
362
+ offset = 0
363
+ max_dc_count = 0
364
+ max_ec_count = 0
365
+ dcdata = Array.new( rs_blocks.size )
366
+ ecdata = Array.new( rs_blocks.size )
367
+
368
+ ( 0...rs_blocks.size ).each do |r|
369
+ dc_count = rs_blocks[r].data_count
370
+ ec_count = rs_blocks[r].total_count - dc_count
371
+ max_dc_count = [ max_dc_count, dc_count ].max
372
+ max_ec_count = [ max_ec_count, ec_count ].max
373
+ dcdata[r] = Array.new( dc_count )
374
+
375
+ ( 0...dcdata[r].size ).each do |i|
376
+ dcdata[r][i] = 0xff & buffer.buffer[ i + offset ]
377
+ end
378
+
379
+ offset = offset + dc_count
380
+ rs_poly = QRUtil.get_error_correct_polynomial( ec_count )
381
+ raw_poly = QRPolynomial.new( dcdata[r], rs_poly.get_length - 1 )
382
+ mod_poly = raw_poly.mod( rs_poly )
383
+ ecdata[r] = Array.new( rs_poly.get_length - 1 )
384
+ ( 0...ecdata[r].size ).each do |i|
385
+ mod_index = i + mod_poly.get_length - ecdata[r].size
386
+ ecdata[r][i] = mod_index >= 0 ? mod_poly.get( mod_index ) : 0
387
+ end
388
+ end
389
+
390
+ total_code_count = 0
391
+ ( 0...rs_blocks.size ).each do |i|
392
+ total_code_count = total_code_count + rs_blocks[i].total_count
393
+ end
394
+
395
+ data = Array.new( total_code_count )
396
+ index = 0
397
+
398
+ ( 0...max_dc_count ).each do |i|
399
+ ( 0...rs_blocks.size ).each do |r|
400
+ if i < dcdata[r].size
401
+ index += 1
402
+ data[index-1] = dcdata[r][i]
403
+ end
404
+ end
405
+ end
406
+
407
+ ( 0...max_ec_count ).each do |i|
408
+ ( 0...rs_blocks.size ).each do |r|
409
+ if i < ecdata[r].size
410
+ index += 1
411
+ data[index-1] = ecdata[r][i]
412
+ end
413
+ end
414
+ end
415
+
416
+ data
417
+ end
418
+
419
+ end
420
+
421
+ end