Class Barby::Code128
In: lib/barby/barcode/code_128.rb
Parent: Barcode1D
EAN13 Bookland EAN8 Barcode1D Code128 Code25 Code93 Code39 Barcode Barcode2D QrCode Pdf417 Code128A Code128B Code128C GS1128 Outputter CairoOutputter PngOutputter ASCIIOutputter RmagickOutputter PDFWriterOutputter SvgOutputter PrawnOutputter Code25IATA Code25Interleaved lib/barby/barcode/gs1_128.rb lib/barby/outputter/png_outputter.rb lib/barby/outputter/svg_outputter.rb lib/barby/outputter/rmagick_outputter.rb lib/barby/barcode.rb lib/barby/outputter/ascii_outputter.rb lib/barby/outputter.rb lib/barby/barcode/code_128.rb lib/barby/barcode/code_39.rb lib/barby/outputter/pdfwriter_outputter.rb lib/barby/barcode/code_93.rb lib/barby/outputter/prawn_outputter.rb lib/barby/barcode/ean_8.rb lib/barby/barcode/pdf_417.rb lib/barby/barcode/code_25_iata.rb lib/barby/outputter/cairo_outputter.rb lib/barby/barcode/ean_13.rb lib/barby/barcode/code_25.rb lib/barby/barcode/code_25_interleaved.rb lib/barby/barcode/qr_code.rb lib/barby/barcode/bookland.rb VERSION Barby dot/m_23_0.png

Code 128 barcodes

Note that you must provide a type for each object, either by passing a string as a second parameter to Code128.new or by instantiating one of the child classes.

You can switch type by using the CODEA, CODEB and CODEC characters:

 "\305" => A
 "\306" => B
 "\307" => C

As an example, here‘s one that starts out as type A and then switches to B and then C:

  Code128A.new("ABC123\306def\3074567")

Methods

Constants

ENCODINGS = { 0 => "11011001100", 1 => "11001101100", 2 => "11001100110", 3 => "10010011000", 4 => "10010001100", 5 => "10001001100", 6 => "10011001000", 7 => "10011000100", 8 => "10001100100", 9 => "11001001000", 10 => "11001000100", 11 => "11000100100", 12 => "10110011100", 13 => "10011011100", 14 => "10011001110", 15 => "10111001100", 16 => "10011101100", 17 => "10011100110", 18 => "11001110010", 19 => "11001011100", 20 => "11001001110", 21 => "11011100100", 22 => "11001110100", 23 => "11101101110", 24 => "11101001100", 25 => "11100101100", 26 => "11100100110", 27 => "11101100100", 28 => "11100110100", 29 => "11100110010", 30 => "11011011000", 31 => "11011000110", 32 => "11000110110", 33 => "10100011000", 34 => "10001011000", 35 => "10001000110", 36 => "10110001000", 37 => "10001101000", 38 => "10001100010", 39 => "11010001000", 40 => "11000101000", 41 => "11000100010", 42 => "10110111000", 43 => "10110001110", 44 => "10001101110", 45 => "10111011000", 46 => "10111000110", 47 => "10001110110", 48 => "11101110110", 49 => "11010001110", 50 => "11000101110", 51 => "11011101000", 52 => "11011100010", 53 => "11011101110", 54 => "11101011000", 55 => "11101000110", 56 => "11100010110", 57 => "11101101000", 58 => "11101100010", 59 => "11100011010", 60 => "11101111010", 61 => "11001000010", 62 => "11110001010", 63 => "10100110000", 64 => "10100001100", 65 => "10010110000", 66 => "10010000110", 67 => "10000101100", 68 => "10000100110", 69 => "10110010000", 70 => "10110000100", 71 => "10011010000", 72 => "10011000010", 73 => "10000110100", 74 => "10000110010", 75 => "11000010010", 76 => "11001010000", 77 => "11110111010", 78 => "11000010100", 79 => "10001111010", 80 => "10100111100", 81 => "10010111100", 82 => "10010011110", 83 => "10111100100", 84 => "10011110100", 85 => "10011110010", 86 => "11110100100", 87 => "11110010100", 88 => "11110010010", 89 => "11011011110", 90 => "11011110110", 91 => "11110110110", 92 => "10101111000", 93 => "10100011110", 94 => "10001011110", 95 => "10111101000", 96 => "10111100010", 97 => "11110101000", 98 => "11110100010", 99 => "10111011110", 100 => "10111101110", 101 => "11101011110", 102 => "11110101110", 103 => "11010000100", 104 => "11010010000", 105 => "11010011100"
VALUES = { 'A' => { 0 => " ", 1 => "!", 2 => "\"", 3 => "#", 4 => "$", 5 => "%", 6 => "&", 7 => "'", 8 => "(", 9 => ")", 10 => "*", 11 => "+", 12 => ",", 13 => "-", 14 => ".", 15 => "/", 16 => "0", 17 => "1", 18 => "2", 19 => "3", 20 => "4", 21 => "5", 22 => "6", 23 => "7", 24 => "8", 25 => "9", 26 => ":", 27 => ";", 28 => "<", 29 => "=", 30 => ">", 31 => "?", 32 => "@", 33 => "A", 34 => "B", 35 => "C", 36 => "D", 37 => "E", 38 => "F", 39 => "G", 40 => "H", 41 => "I", 42 => "J", 43 => "K", 44 => "L", 45 => "M", 46 => "N", 47 => "O", 48 => "P", 49 => "Q", 50 => "R", 51 => "S", 52 => "T", 53 => "U", 54 => "V", 55 => "W", 56 => "X", 57 => "Y", 58 => "Z", 59 => "[", 60 => "\\", 61 => "]", 62 => "^", 63 => "_", 64 => "\000", 65 => "\001", 66 => "\002", 67 => "\003", 68 => "\004", 69 => "\005", 70 => "\006", 71 => "\a", 72 => "\b", 73 => "\t", 74 => "\n", 75 => "\v", 76 => "\f", 77 => "\r", 78 => "\016", 79 => "\017", 80 => "\020", 81 => "\021", 82 => "\022", 83 => "\023", 84 => "\024", 85 => "\025", 86 => "\026", 87 => "\027", 88 => "\030", 89 => "\031", 90 => "\032", 91 => "\e", 92 => "\034", 93 => "\035", 94 => "\036", 95 => "\037", 96 => "\303", 97 => "\302", 98 => "SHIFT", 99 => "\307", 100 => "\306", 101 => "\304", 102 => "\301", 103 => "STARTA", 104 => "STARTB", 105 => "STARTC"
FNC1 = "\xc1"
FNC2 = "\xc2"
FNC3 = "\xc3"
FNC4 = "\xc4"
CODEA = "\xc5"
CODEB = "\xc6"
CODEC = "\xc7"
STOP = '11000111010'
TERMINATE = '11'

Attributes

type  [R] 

Public Class methods

[Source]

     # File lib/barby/barcode/code_128.rb, line 159
159:     def initialize(data, type)
160:       self.type = type
161:       self.data = "#{data}"
162:       raise ArgumentError, 'Data not valid' unless valid?
163:     end

Public Instance methods

Find the encoding to change to the character set in barcode

[Source]

     # File lib/barby/barcode/code_128.rb, line 345
345:     def change_code_encoding_for(barcode)
346:       encodings[change_code_number_for(barcode)]
347:     end

Find the character that changes the character set to the one represented in barcode

[Source]

     # File lib/barby/barcode/code_128.rb, line 334
334:     def change_code_for(barcode)
335:       change_code_for_class(barcode.class)
336:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 328
328:     def change_code_for_class(klass)
329:       {Code128A => CODEA, Code128B => CODEB, Code128C => CODEC}[klass]
330:     end

Find the numeric value for the character that changes the character set to the one represented in barcode

[Source]

     # File lib/barby/barcode/code_128.rb, line 340
340:     def change_code_number_for(barcode)
341:       values[change_code_for(barcode)]
342:     end

Get an array of the individual characters for this barcode. Special characters like FNC1 will be present. Characters from extras are not present.

[Source]

     # File lib/barby/barcode/code_128.rb, line 237
237:     def characters
238:       chars = data.split(//n)
239: 
240:       if type == 'C'
241:         result = []
242:         count = 0
243:         while count < chars.size
244:           if chars[count] =~ /^\d$/
245:             result << "#{chars[count]}#{chars[count+1]}"
246:             count += 2
247:           else
248:             result << chars[count]
249:             count += 1
250:           end
251:         end
252:         result
253:       else
254:         chars
255:       end
256:     end

Calculate the checksum for the data in this barcode. The data includes data from extras.

[Source]

     # File lib/barby/barcode/code_128.rb, line 285
285:     def checksum
286:       pos = 0
287:       (numbers+extra_numbers).inject(start_num) do |sum,number|
288:         pos += 1
289:         sum + (number * pos)
290:       end % 103
291:     end

Get the encoding for the checksum

[Source]

     # File lib/barby/barcode/code_128.rb, line 294
294:     def checksum_encoding
295:       encodings[checksum]
296:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 349
349:     def class_for(character)
350:       case character
351:       when 'A' then Code128A
352:       when 'B' then Code128B
353:       when 'C' then Code128C
354:       when CODEA then Code128A
355:       when CODEB then Code128B
356:       when CODEC then Code128C
357:       end
358:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 178
178:     def data
179:       @data
180:     end

Set the data for this barcode. If the barcode changes character set, an extra will be created.

[Source]

     # File lib/barby/barcode/code_128.rb, line 209
209:     def data=(data)
210:       data, *extra = data.split(/([#{CODEA+CODEB+CODEC}])/n)
211:       @data = data || ''
212:       self.extra = extra.join unless extra.empty?
213:     end

Returns the encoding for the data part of this barcode, without any extras

[Source]

     # File lib/barby/barcode/code_128.rb, line 264
264:     def data_encoding
265:       characters.map do |char|
266:         encoding_for char
267:       end.join
268:     end

Returns the data encoding of this barcode and extras.

[Source]

     # File lib/barby/barcode/code_128.rb, line 271
271:     def data_encoding_with_extra_encoding
272:       data_encoding+extra_encoding
273:     end

Return the encoding of this barcode as a string of 1 and 0

[Source]

     # File lib/barby/barcode/code_128.rb, line 259
259:     def encoding
260:       start_encoding+data_encoding+extra_encoding+checksum_encoding+stop_encoding
261:     end

Find the encoding for the specified character for this barcode

[Source]

     # File lib/barby/barcode/code_128.rb, line 324
324:     def encoding_for(char)
325:       encodings[values[char]]
326:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 314
314:     def encodings
315:       ENCODINGS
316:     end

An "extra" is present if the barcode changes character set. If a 128A barcode changes to C, the extra will be an instance of Code128C. Extras can themselves have an extra if the barcode changes character set again. It‘s like a linked list, and when there are no more extras, the barcode ends with that object. Most barcodes probably don‘t change charsets and don‘t have extras.

[Source]

     # File lib/barby/barcode/code_128.rb, line 221
221:     def extra
222:       @extra
223:     end

Set the extra for this barcode. The argument is a string starting with the "change character set" symbol. The string may contain several character sets, in which case the extra will itself have an extra.

[Source]

     # File lib/barby/barcode/code_128.rb, line 228
228:     def extra=(extra)
229:       raise ArgumentError, "Extra must begin with \\305, \\306 or \\307" unless extra =~ /^[#{CODEA+CODEB+CODEC}]/n
230:       type, data = extra[0,1], extra[1..-1]
231:       @extra = class_for(type).new(data)
232:     end

Returns the data encoding of this barcode‘s extra and its extra until the barcode ends.

[Source]

     # File lib/barby/barcode/code_128.rb, line 277
277:     def extra_encoding
278:       return '' unless extra
279:       change_code_encoding_for(extra) + extra.data_encoding + extra.extra_encoding
280:     end

Returns the numeric values for extras

[Source]

     # File lib/barby/barcode/code_128.rb, line 309
309:     def extra_numbers
310:       return [] unless extra
311:       [change_code_number_for(extra)] + extra.numbers + extra.extra_numbers
312:     end

Returns the data for this barcode plus that for the entire extra chain, excluding all change codes

[Source]

     # File lib/barby/barcode/code_128.rb, line 184
184:     def full_data
185:       data + full_extra_data
186:     end

Returns the data for this barcode plus that for the entire extra chain, including all change codes prefixing each extra

[Source]

     # File lib/barby/barcode/code_128.rb, line 190
190:     def full_data_with_change_codes
191:       data + full_extra_data_with_change_code
192:     end

Returns the full_data for the extra or an empty string if there is no extra

[Source]

     # File lib/barby/barcode/code_128.rb, line 195
195:     def full_extra_data
196:       return '' unless extra
197:       extra.full_data
198:     end

Returns the full_data for the extra with the change code for the extra prepended. If there is no extra, an empty string is returned

[Source]

     # File lib/barby/barcode/code_128.rb, line 202
202:     def full_extra_data_with_change_code
203:       return '' unless extra
204:       change_code_for(extra) + extra.full_data_with_change_codes
205:     end

Returns the numeric values for the characters in the barcode in an array

[Source]

     # File lib/barby/barcode/code_128.rb, line 302
302:     def numbers
303:       characters.map do |char|
304:         values[char]
305:       end
306:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 375
375:     def start_encoding
376:       encodings[start_num]
377:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 371
371:     def start_num
372:       values["START#{type}"]
373:     end

The start encoding starts the barcode

[Source]

     # File lib/barby/barcode/code_128.rb, line 319
319:     def stop_encoding
320:       STOP+TERMINATE
321:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 173
173:     def to_s
174:       full_data
175:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 166
166:     def type=(type)
167:       type.upcase!
168:       raise ArgumentError, 'type must be A, B or C' unless type =~ /^[ABC]$/
169:       @type = type
170:     end

Is the data in this barcode valid? Does a lookup of every character and checks if it exists in the character set. An empty data string will also be reported as invalid.

[Source]

     # File lib/barby/barcode/code_128.rb, line 363
363:     def valid?
364:       characters.any? && characters.all?{|c| values.include?(c) }
365:     end

[Source]

     # File lib/barby/barcode/code_128.rb, line 367
367:     def values
368:       VALUES[type]
369:     end

[Validate]