arena_barby 0.3.2

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 (49) hide show
  1. data/README +29 -0
  2. data/bin/barby +41 -0
  3. data/lib/barby.rb +17 -0
  4. data/lib/barby/barcode.rb +116 -0
  5. data/lib/barby/barcode/bookland.rb +37 -0
  6. data/lib/barby/barcode/code_128.rb +410 -0
  7. data/lib/barby/barcode/code_25.rb +193 -0
  8. data/lib/barby/barcode/code_25_iata.rb +23 -0
  9. data/lib/barby/barcode/code_25_interleaved.rb +73 -0
  10. data/lib/barby/barcode/code_39.rb +233 -0
  11. data/lib/barby/barcode/code_93.rb +230 -0
  12. data/lib/barby/barcode/ean_13.rb +178 -0
  13. data/lib/barby/barcode/ean_8.rb +32 -0
  14. data/lib/barby/barcode/gs1_128.rb +42 -0
  15. data/lib/barby/barcode/pdf_417.rb +76 -0
  16. data/lib/barby/barcode/qr_code.rb +101 -0
  17. data/lib/barby/outputter.rb +127 -0
  18. data/lib/barby/outputter/ascii_outputter.rb +41 -0
  19. data/lib/barby/outputter/cairo_outputter.rb +185 -0
  20. data/lib/barby/outputter/pdfwriter_outputter.rb +83 -0
  21. data/lib/barby/outputter/png_outputter.rb +97 -0
  22. data/lib/barby/outputter/prawn_outputter.rb +99 -0
  23. data/lib/barby/outputter/rmagick_outputter.rb +126 -0
  24. data/lib/barby/outputter/svg_outputter.rb +225 -0
  25. data/lib/barby/vendor.rb +3 -0
  26. data/lib/barby/version.rb +9 -0
  27. data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.jar +0 -0
  28. data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.java +1471 -0
  29. data/vendor/rqrcode/CHANGELOG +29 -0
  30. data/vendor/rqrcode/COPYING +19 -0
  31. data/vendor/rqrcode/README +98 -0
  32. data/vendor/rqrcode/Rakefile +65 -0
  33. data/vendor/rqrcode/lib/rqrcode.rb +13 -0
  34. data/vendor/rqrcode/lib/rqrcode/core_ext.rb +5 -0
  35. data/vendor/rqrcode/lib/rqrcode/core_ext/array.rb +5 -0
  36. data/vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb +9 -0
  37. data/vendor/rqrcode/lib/rqrcode/core_ext/integer.rb +5 -0
  38. data/vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb +11 -0
  39. data/vendor/rqrcode/lib/rqrcode/qrcode.rb +4 -0
  40. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb +37 -0
  41. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb +56 -0
  42. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb +421 -0
  43. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb +63 -0
  44. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb +78 -0
  45. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb +313 -0
  46. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb +254 -0
  47. data/vendor/rqrcode/test/runtest.rb +78 -0
  48. data/vendor/rqrcode/test/test_data.rb +21 -0
  49. metadata +114 -0
@@ -0,0 +1,193 @@
1
+ require 'barby/barcode'
2
+
3
+ module Barby
4
+
5
+
6
+ #Standard/Industrial 2 of 5, non-interleaved
7
+ #
8
+ #Checksum not included by default, to include it,
9
+ #set include_checksum = true
10
+ class Code25 < Barcode1D
11
+
12
+ WIDE = W = true
13
+ NARROW = N = false
14
+
15
+ START_ENCODING = [W,W,N]
16
+ STOP_ENCODING = [W,N,W]
17
+
18
+ ENCODINGS = {
19
+ 0 => [N,N,W,W,N],
20
+ 1 => [W,N,N,N,W],
21
+ 2 => [N,W,N,N,W],
22
+ 3 => [W,W,N,N,N],
23
+ 4 => [N,N,W,N,W],
24
+ 5 => [W,N,W,N,N],
25
+ 6 => [N,W,W,N,N],
26
+ 7 => [N,N,N,W,W],
27
+ 8 => [W,N,N,W,N],
28
+ 9 => [N,W,N,W,N]
29
+ }
30
+
31
+ attr_accessor :data, :narrow_width, :wide_width, :space_width, :include_checksum
32
+
33
+
34
+ def initialize(data)
35
+ self.data = data
36
+ end
37
+
38
+
39
+ def data_encoding
40
+ digit_encodings.join
41
+ end
42
+
43
+ def data_encoding_with_checksum
44
+ digit_encodings_with_checksum.join
45
+ end
46
+
47
+ def encoding
48
+ start_encoding+(include_checksum? ? data_encoding_with_checksum : data_encoding)+stop_encoding
49
+ end
50
+
51
+
52
+ def characters
53
+ data.split(//)
54
+ end
55
+
56
+ def characters_with_checksum
57
+ characters.push(checksum.to_s)
58
+ end
59
+
60
+ def digits
61
+ characters.map{|c| c.to_i }
62
+ end
63
+
64
+ def digits_with_checksum
65
+ digits.push(checksum)
66
+ end
67
+
68
+ def even_and_odd_digits
69
+ alternater = false
70
+ digits.reverse.partition{ alternater = !alternater }
71
+ end
72
+
73
+
74
+ def digit_encodings
75
+ raise_invalid unless valid?
76
+ digits.map{|d| encoding_for(d) }
77
+ end
78
+ alias character_encodings digit_encodings
79
+
80
+ def digit_encodings_with_checksum
81
+ raise_invalid unless valid?
82
+ digits_with_checksum.map{|d| encoding_for(d) }
83
+ end
84
+ alias character_encodings_with_checksum digit_encodings_with_checksum
85
+
86
+
87
+ #Returns the encoding for a single digit
88
+ def encoding_for(digit)
89
+ encoding_for_bars(ENCODINGS[digit])
90
+ end
91
+
92
+ #Generate encoding for an array of W,N
93
+ def encoding_for_bars(*bars)
94
+ wide, narrow, space = wide_encoding, narrow_encoding, space_encoding
95
+ bars.flatten.inject '' do |enc,bar|
96
+ enc + (bar == WIDE ? wide : narrow) + space
97
+ end
98
+ end
99
+
100
+ def encoding_for_bars_without_end_space(*a)
101
+ encoding_for_bars(*a).gsub(/0+$/, '')
102
+ end
103
+
104
+
105
+ #Mod10
106
+ def checksum
107
+ evens, odds = even_and_odd_digits
108
+ sum = odds.inject(0){|sum,d| sum + d } + evens.inject(0){|sum,d| sum + (d*3) }
109
+ sum %= 10
110
+ sum.zero? ? 0 : 10-sum
111
+ end
112
+
113
+ def checksum_encoding
114
+ encoding_for(checksum)
115
+ end
116
+
117
+
118
+ #The width of a narrow bar in xdims
119
+ def narrow_width
120
+ @narrow_width || 1
121
+ end
122
+
123
+ #The width of a wide bar in xdims
124
+ #By default three times as wide as a narrow bar
125
+ def wide_width
126
+ @wide_width || narrow_width*3
127
+ end
128
+
129
+ #The width of the space between the bars in xdims
130
+ #By default the same width as a narrow bar
131
+ #
132
+ #A space serves only as a separator for the bars,
133
+ #there is no encoded meaning in them
134
+ def space_width
135
+ @space_width || narrow_width
136
+ end
137
+
138
+
139
+ #2 of 5 doesn't require a checksum, but you can include a Mod10 checksum
140
+ #by setting +include_checksum+ to true
141
+ def include_checksum?
142
+ include_checksum
143
+ end
144
+
145
+
146
+ def data=(data)
147
+ @data = "#{data}"
148
+ end
149
+
150
+
151
+ def start_encoding
152
+ encoding_for_bars(START_ENCODING)
153
+ end
154
+
155
+ def stop_encoding
156
+ encoding_for_bars_without_end_space(STOP_ENCODING)
157
+ end
158
+
159
+
160
+ def narrow_encoding
161
+ '1' * narrow_width
162
+ end
163
+
164
+ def wide_encoding
165
+ '1' * wide_width
166
+ end
167
+
168
+ def space_encoding
169
+ '0' * space_width
170
+ end
171
+
172
+
173
+ def valid?
174
+ data =~ /^[0-9]*$/
175
+ end
176
+
177
+
178
+ def to_s
179
+ (include_checksum? ? characters_with_checksum : characters).join
180
+ end
181
+
182
+
183
+ private
184
+
185
+ def raise_invalid
186
+ raise ArgumentError, "data not valid"
187
+ end
188
+
189
+
190
+ end
191
+
192
+
193
+ end
@@ -0,0 +1,23 @@
1
+ require 'barby/barcode/code_25'
2
+
3
+ module Barby
4
+
5
+ #The IATA version of 2 of 5 is identical to its parent except for different
6
+ #start and stop codes. This is the one used on the tags they put on your
7
+ #luggage when you check it in at the airport.
8
+ class Code25IATA < Code25
9
+
10
+ START_ENCODING = [N,N]
11
+ STOP_ENCODING = [W,N]
12
+
13
+ def start_encoding
14
+ encoding_for_bars(START_ENCODING)
15
+ end
16
+
17
+ def stop_encoding
18
+ encoding_for_bars_without_end_space(STOP_ENCODING)
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,73 @@
1
+ require 'barby/barcode/code_25'
2
+
3
+ module Barby
4
+
5
+ #Code 2 of 5 interleaved. Same as standard 2 of 5, but spaces are used
6
+ #for encoding as well as the bars. Each pair of numbers get interleaved,
7
+ #that is, the first is encoded in the bars and the second is encoded
8
+ #in the spaced. This means an interleaved 2/5 barcode must have an even
9
+ #number of digits.
10
+ class Code25Interleaved < Code25
11
+
12
+ START_ENCODING = [N,N,N,N]
13
+ STOP_ENCODING = [W,N,N]
14
+
15
+
16
+ def digit_pairs(d=nil)
17
+ (d || digits).inject [] do |ary,d|
18
+ ary << [] if !ary.last || ary.last.size == 2
19
+ ary.last << d
20
+ ary
21
+ end
22
+ end
23
+
24
+ def digit_pairs_with_checksum
25
+ digit_pairs(digits_with_checksum)
26
+ end
27
+
28
+
29
+ def digit_encodings
30
+ raise_invalid unless valid?
31
+ digit_pairs.map{|p| encoding_for_pair(p) }
32
+ end
33
+
34
+ def digit_encodings_with_checksum
35
+ digit_pairs_with_checksum.map{|p| encoding_for_pair(p) }
36
+ end
37
+
38
+
39
+ def encoding_for_pair(pair)
40
+ bars, spaces = ENCODINGS[pair.first], ENCODINGS[pair.last]
41
+ encoding_for_interleaved(bars.zip(spaces))
42
+ end
43
+
44
+
45
+ #Encodes an array of interleaved W or N bars and spaces
46
+ #ex: [W,N,W,W,N,N] => "111011100010"
47
+ def encoding_for_interleaved(*bars_and_spaces)
48
+ bar = false#starts with bar
49
+ bars_and_spaces.flatten.inject '' do |enc,bar_or_space|
50
+ bar = !bar
51
+ enc << (bar ? '1' : '0') * (bar_or_space == WIDE ? wide_width : narrow_width)
52
+ end
53
+ end
54
+
55
+
56
+ def start_encoding
57
+ encoding_for_interleaved(START_ENCODING)
58
+ end
59
+
60
+ def stop_encoding
61
+ encoding_for_interleaved(STOP_ENCODING)
62
+ end
63
+
64
+
65
+ def valid?
66
+ # When checksum is included, it's included when determining "evenness"
67
+ super && digits.size % 2 == (include_checksum? ? 1 : 0)
68
+ end
69
+
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,233 @@
1
+ require 'barby/barcode'
2
+
3
+ module Barby
4
+
5
+
6
+ class Code39 < Barcode1D
7
+
8
+ WIDE = W = true
9
+ NARROW = N = false
10
+
11
+ ENCODINGS = {
12
+ ' ' => [N,W,W,N,N,N,W,N,N], '$' => [N,W,N,W,N,W,N,N,N],
13
+ '%' => [N,N,N,W,N,W,N,W,N], '+' => [N,W,N,N,N,W,N,W,N],
14
+ '-' => [N,W,N,N,N,N,W,N,W], '.' => [W,W,N,N,N,N,W,N,N],
15
+ '/' => [N,W,N,W,N,N,N,W,N], '0' => [N,N,N,W,W,N,W,N,N],
16
+ '1' => [W,N,N,W,N,N,N,N,W], '2' => [N,N,W,W,N,N,N,N,W],
17
+ '3' => [W,N,W,W,N,N,N,N,N], '4' => [N,N,N,W,W,N,N,N,W],
18
+ '5' => [W,N,N,W,W,N,N,N,N], '6' => [N,N,W,W,W,N,N,N,N],
19
+ '7' => [N,N,N,W,N,N,W,N,W], '8' => [W,N,N,W,N,N,W,N,N],
20
+ '9' => [N,N,W,W,N,N,W,N,N], 'A' => [W,N,N,N,N,W,N,N,W],
21
+ 'B' => [N,N,W,N,N,W,N,N,W], 'C' => [W,N,W,N,N,W,N,N,N],
22
+ 'D' => [N,N,N,N,W,W,N,N,W], 'E' => [W,N,N,N,W,W,N,N,N],
23
+ 'F' => [N,N,W,N,W,W,N,N,N], 'G' => [N,N,N,N,N,W,W,N,W],
24
+ 'H' => [W,N,N,N,N,W,W,N,N], 'I' => [N,N,W,N,N,W,W,N,N],
25
+ 'J' => [N,N,N,N,W,W,W,N,N], 'K' => [W,N,N,N,N,N,N,W,W],
26
+ 'L' => [N,N,W,N,N,N,N,W,W], 'M' => [W,N,W,N,N,N,N,W,N],
27
+ 'N' => [N,N,N,N,W,N,N,W,W], 'O' => [W,N,N,N,W,N,N,W,N],
28
+ 'P' => [N,N,W,N,W,N,N,W,N], 'Q' => [N,N,N,N,N,N,W,W,W],
29
+ 'R' => [W,N,N,N,N,N,W,W,N], 'S' => [N,N,W,N,N,N,W,W,N],
30
+ 'T' => [N,N,N,N,W,N,W,W,N], 'U' => [W,W,N,N,N,N,N,N,W],
31
+ 'V' => [N,W,W,N,N,N,N,N,W], 'W' => [W,W,W,N,N,N,N,N,N],
32
+ 'X' => [N,W,N,N,W,N,N,N,W], 'Y' => [W,W,N,N,W,N,N,N,N],
33
+ 'Z' => [N,W,W,N,W,N,N,N,N]
34
+ }
35
+
36
+ #In extended mode, each character is replaced with two characters from the "normal" encoding
37
+ EXTENDED_ENCODINGS = {
38
+ "\000" => '%U', " " => " ", "@" => "%V", "`" => "%W",
39
+ "\001" => '$A', "!" => "/A", "A" => "A", "a" => "+A",
40
+ "\002" => '$B', '"' => "/B", "B" => "B", "b" => "+B",
41
+ "\003" => '$C', "#" => "/C", "C" => "C", "c" => "+C",
42
+ "\004" => '$D', "$" => "/D", "D" => "D", "d" => "+D",
43
+ "\005" => '$E', "%" => "/E", "E" => "E", "e" => "+E",
44
+ "\006" => '$F', "&" => "/F", "F" => "F", "f" => "+F",
45
+ "\007" => '$G', "'" => "/G", "G" => "G", "g" => "+G",
46
+ "\010" => '$H', "(" => "/H", "H" => "H", "h" => "+H",
47
+ "\011" => '$I', ")" => "/I", "I" => "I", "i" => "+I",
48
+ "\012" => '$J', "*" => "/J", "J" => "J", "j" => "+J",
49
+ "\013" => '$K', "+" => "/K", "K" => "K", "k" => "+K",
50
+ "\014" => '$L', "," => "/L", "L" => "L", "l" => "+L",
51
+ "\015" => '$M', "-" => "-", "M" => "M", "m" => "+M",
52
+ "\016" => '$N', "." => ".", "N" => "N", "n" => "+N",
53
+ "\017" => '$O', "/" => "/O", "O" => "O", "o" => "+O",
54
+ "\020" => '$P', "0" => "0", "P" => "P", "p" => "+P",
55
+ "\021" => '$Q', "1" => "1", "Q" => "Q", "q" => "+Q",
56
+ "\022" => '$R', "2" => "2", "R" => "R", "r" => "+R",
57
+ "\023" => '$S', "3" => "3", "S" => "S", "s" => "+S",
58
+ "\024" => '$T', "4" => "4", "T" => "T", "t" => "+T",
59
+ "\025" => '$U', "5" => "5", "U" => "U", "u" => "+U",
60
+ "\026" => '$V', "6" => "6", "V" => "V", "v" => "+V",
61
+ "\027" => '$W', "7" => "7", "W" => "W", "w" => "+W",
62
+ "\030" => '$X', "8" => "8", "X" => "X", "x" => "+X",
63
+ "\031" => '$Y', "9" => "9", "Y" => "Y", "y" => "+Y",
64
+ "\032" => '$Z', ":" => "/Z", "Z" => "Z", "z" => "+Z",
65
+ "\033" => '%A', ";" => "%F", "[" => "%K", "{" => "%P",
66
+ "\034" => '%B', "<" => "%G", "\\" => "%L", "|" => "%Q",
67
+ "\035" => '%C', "=" => "%H", "]" => "%M", "}" => "%R",
68
+ "\036" => '%D', ">" => "%I", "^" => "%N", "~" => "%S",
69
+ "\037" => '%E', "?" => "%J", "_" => "%O", "\177" => "%T"
70
+ }
71
+
72
+ CHECKSUM_VALUES = {
73
+ '0' => 0, '1' => 1, '2' => 2, '3' => 3,
74
+ '4' => 4, '5' => 5, '6' => 6, '7' => 7,
75
+ '8' => 8, '9' => 9, 'A' => 10, 'B' => 11,
76
+ 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15,
77
+ 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19,
78
+ 'K' => 20, 'L' => 21, 'N' => 23, 'M' => 22,
79
+ 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27,
80
+ 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31,
81
+ 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35,
82
+ '-' => 36, '.' => 37, ' ' => 38, '$' => 39,
83
+ '/' => 40, '+' => 41, '%' => 42
84
+ }
85
+
86
+ START_ENCODING = [N,W,N,N,W,N,W,N,N] # *
87
+ STOP_ENCODING = [N,W,N,N,W,N,W,N,N] # *
88
+
89
+ attr_accessor :data, :spacing, :narrow_width, :wide_width, :extended, :include_checksum
90
+
91
+
92
+ def initialize(data, extended=false)
93
+ self.data = data
94
+ self.extended = extended
95
+ raise(ArgumentError, "data is not valid (extended=#{extended?})") unless valid?
96
+ yield self if block_given?
97
+ end
98
+
99
+
100
+ #Returns the characters that were passed in, no matter it they're part of
101
+ #the extended charset or if they're already encodable, "normal" characters
102
+ def raw_characters
103
+ data.split(//)
104
+ end
105
+
106
+ #Returns the encodable characters. If extended mode is enabled, each character will
107
+ #first be replaced by two characters from the encodable charset
108
+ def characters
109
+ chars = raw_characters
110
+ extended ? chars.map{|c| EXTENDED_ENCODINGS[c].split(//) }.flatten : chars
111
+ end
112
+
113
+ def characters_with_checksum
114
+ characters + [checksum_character]
115
+ end
116
+
117
+ def encoded_characters
118
+ characters.map{|c| encoding_for(c) }
119
+ end
120
+
121
+ def encoded_characters_with_checksum
122
+ encoded_characters + [checksum_encoding]
123
+ end
124
+
125
+
126
+ #The data part of the encoding (no start+stop characters)
127
+ def data_encoding
128
+ encoded_characters.join(spacing_encoding)
129
+ end
130
+
131
+ def data_encoding_with_checksum
132
+ encoded_characters_with_checksum.join(spacing_encoding)
133
+ end
134
+
135
+
136
+ def encoding
137
+ return encoding_with_checksum if include_checksum?
138
+ start_encoding+spacing_encoding+data_encoding+spacing_encoding+stop_encoding
139
+ end
140
+
141
+ def encoding_with_checksum
142
+ start_encoding+spacing_encoding+data_encoding_with_checksum+spacing_encoding+stop_encoding
143
+ end
144
+
145
+
146
+ #Checksum is optional
147
+ def checksum
148
+ characters.inject(0) do |sum,char|
149
+ sum + CHECKSUM_VALUES[char]
150
+ end % 43
151
+ end
152
+
153
+ def checksum_character
154
+ CHECKSUM_VALUES.invert[checksum]
155
+ end
156
+
157
+ def checksum_encoding
158
+ encoding_for(checksum_character)
159
+ end
160
+
161
+ #Set include_checksum to true to make +encoding+ include the checksum
162
+ def include_checksum?
163
+ include_checksum
164
+ end
165
+
166
+
167
+ #Takes an array of WIDE/NARROW values and returns the string representation for
168
+ #those bars and spaces, using wide_width and narrow_width
169
+ def encoding_for_bars(*bars_and_spaces)
170
+ bar = false
171
+ bars_and_spaces.flatten.map do |width|
172
+ bar = !bar
173
+ (bar ? '1' : '0') * (width == WIDE ? wide_width : narrow_width)
174
+ end.join
175
+ end
176
+
177
+ #Returns the string representation for a single character
178
+ def encoding_for(character)
179
+ encoding_for_bars(ENCODINGS[character])
180
+ end
181
+
182
+
183
+ #Spacing between the characters in xdims. Spacing will be inserted
184
+ #between each character in the encoding
185
+ def spacing
186
+ @spacing ||= 1
187
+ end
188
+
189
+ def spacing_encoding
190
+ '0' * spacing
191
+ end
192
+
193
+
194
+ def narrow_width
195
+ @narrow_width ||= 1
196
+ end
197
+
198
+ def wide_width
199
+ @wide_width ||= 2
200
+ end
201
+
202
+
203
+ def extended?
204
+ extended
205
+ end
206
+
207
+
208
+ def start_encoding
209
+ encoding_for_bars(START_ENCODING)
210
+ end
211
+
212
+ def stop_encoding
213
+ encoding_for_bars(STOP_ENCODING)
214
+ end
215
+
216
+ def valid?
217
+ if extended?
218
+ raw_characters.all?{|c| EXTENDED_ENCODINGS.include?(c) }
219
+ else
220
+ raw_characters.all?{|c| ENCODINGS.include?(c) }
221
+ end
222
+ end
223
+
224
+
225
+ def to_s
226
+ data
227
+ end
228
+
229
+
230
+ end
231
+
232
+
233
+ end