Class Barby::Code39
In: lib/barby/barcode/code_39.rb
Parent: Barcode1D
Barcode1D EAN13 Code128 Code25 Code93 Code39 Bookland EAN8 Barcode Barcode2D QrCode 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/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_22_0.png

Methods

Constants

ENCODINGS = { '0' => '101001101101', 'M' => '110110101001', '1' => '110100101011', 'N' => '101011010011', '2' => '101100101011', 'O' => '110101101001', '3' => '110110010101', 'P' => '101101101001', '4' => '101001101011', 'Q' => '101010110011', '5' => '110100110101', 'R' => '110101011001', '6' => '101100110101', 'S' => '101101011001', '7' => '101001011011', 'T' => '101011011001', '8' => '110100101101', 'U' => '110010101011', '9' => '101100101101', 'V' => '100110101011', 'A' => '110101001011', 'W' => '110011010101', 'B' => '101101001011', 'X' => '100101101011', 'C' => '110110100101', 'Y' => '110010110101', 'D' => '101011001011', 'Z' => '100110110101', 'E' => '110101100101', '-' => '100101011011', 'F' => '101101100101', '.' => '110010101101', 'G' => '101010011011', ' ' => '100110101101', 'H' => '110101001101', '$' => '100100100101', 'I' => '101101001101', '/' => '100100101001', 'J' => '101011001101', '+' => '100101001001', 'K' => '110101010011', '%' => '101001001001', 'L' => '101101010011'
EXTENDED_ENCODINGS = { "\000" => '%U', " " => " ", "@" => "%V", "`" => "%W", "\001" => '$A', "!" => "/A", "A" => "A", "a" => "+A", "\002" => '$B', '"' => "/B", "B" => "B", "b" => "+B", "\003" => '$C', "#" => "/C", "C" => "C", "c" => "+C", "\004" => '$D', "$" => "/D", "D" => "D", "d" => "+D", "\005" => '$E', "%" => "/E", "E" => "E", "e" => "+E", "\006" => '$F', "&" => "/F", "F" => "F", "f" => "+F", "\007" => '$G', "'" => "/G", "G" => "G", "g" => "+G", "\010" => '$H', "(" => "/H", "H" => "H", "h" => "+H", "\011" => '$I', ")" => "/I", "I" => "I", "i" => "+I", "\012" => '$J', "*" => "/J", "J" => "J", "j" => "+J", "\013" => '$K', "+" => "/K", "K" => "K", "k" => "+K", "\014" => '$L', "," => "/L", "L" => "L", "l" => "+L", "\015" => '$M', "-" => "-", "M" => "M", "m" => "+M", "\016" => '$N', "." => ".", "N" => "N", "n" => "+N", "\017" => '$O', "/" => "/O", "O" => "O", "o" => "+O", "\020" => '$P', "0" => "0", "P" => "P", "p" => "+P", "\021" => '$Q', "1" => "1", "Q" => "Q", "q" => "+Q", "\022" => '$R', "2" => "2", "R" => "R", "r" => "+R", "\023" => '$S', "3" => "3", "S" => "S", "s" => "+S", "\024" => '$T', "4" => "4", "T" => "T", "t" => "+T", "\025" => '$U', "5" => "5", "U" => "U", "u" => "+U", "\026" => '$V', "6" => "6", "V" => "V", "v" => "+V", "\027" => '$W', "7" => "7", "W" => "W", "w" => "+W", "\030" => '$X', "8" => "8", "X" => "X", "x" => "+X", "\031" => '$Y', "9" => "9", "Y" => "Y", "y" => "+Y", "\032" => '$Z', ":" => "/Z", "Z" => "Z", "z" => "+Z", "\033" => '%A', ";" => "%F", "[" => "%K", "{" => "%P", "\034" => '%B', "<" => "%G", "\\" => "%L", "|" => "%Q", "\035" => '%C', "=" => "%H", "]" => "%M", "}" => "%R", "\036" => '%D', ">" => "%I", "^" => "%N", "~" => "%S", "\037" => '%E', "?" => "%J", "_" => "%O", "\177" => "%T"   In extended mode, each character is replaced with two characters from the "normal" encoding
CHECKSUM_VALUES = { '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'N' => 23, 'M' => 22, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, '-' => 36, '.' => 37, ' ' => 38, '$' => 39, '/' => 40, '+' => 41, '%' => 42
START_ENCODING = '100101101101'
STOP_ENCODING = '100101101101'

Attributes

data  [RW] 
extended  [RW] 
include_checksum  [RW] 
spacing  [RW] 

Public Class methods

[Source]

    # File lib/barby/barcode/code_39.rb, line 89
89:     def initialize(data, extended=false)
90:       self.data = data
91:       self.extended = extended
92:       raise(ArgumentError, "data is not valid (extended=#{extended?})") unless valid?
93:       yield self if block_given?
94:     end

Public Instance methods

Returns the encodable characters. If extended mode is enabled, each character will first be replaced by two characters from the encodable charset

[Source]

     # File lib/barby/barcode/code_39.rb, line 105
105:     def characters
106:       chars = raw_characters
107:       extended ? chars.map{|c| EXTENDED_ENCODINGS[c].split(//) }.flatten : chars
108:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 110
110:     def characters_with_checksum
111:       characters + [checksum_character]
112:     end

Checksum is optional

[Source]

     # File lib/barby/barcode/code_39.rb, line 144
144:     def checksum
145:       characters.inject(0) do |sum,char|
146:         sum + CHECKSUM_VALUES[char]
147:       end % 43
148:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 150
150:     def checksum_character
151:       CHECKSUM_VALUES.invert[checksum]
152:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 154
154:     def checksum_encoding
155:       ENCODINGS[checksum_character]
156:     end

The data part of the encoding (no start+stop characters)

[Source]

     # File lib/barby/barcode/code_39.rb, line 124
124:     def data_encoding
125:       encoded_characters.join(spacing_encoding)
126:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 128
128:     def data_encoding_with_checksum
129:       encoded_characters_with_checksum.join(spacing_encoding)
130:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 114
114:     def encoded_characters
115:       characters.map{|c| ENCODINGS[c] }
116:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 118
118:     def encoded_characters_with_checksum
119:       encoded_characters + [checksum_encoding]
120:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 133
133:     def encoding
134:       return encoding_with_checksum if include_checksum?
135:       start_encoding+spacing_encoding+data_encoding+spacing_encoding+stop_encoding
136:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 138
138:     def encoding_with_checksum
139:       start_encoding+spacing_encoding+data_encoding_with_checksum+spacing_encoding+stop_encoding
140:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 175
175:     def extended?
176:       extended
177:     end

Set include_checksum to true to make encoding include the checksum

[Source]

     # File lib/barby/barcode/code_39.rb, line 159
159:     def include_checksum?
160:       include_checksum
161:     end

Returns the characters that were passed in, no matter it they‘re part of the extended charset or if they‘re already encodable, "normal" characters

[Source]

     # File lib/barby/barcode/code_39.rb, line 99
 99:     def raw_characters
100:       data.split(//)
101:     end

Spacing between the characters in xdims. Spacing will be inserted between each character in the encoding

[Source]

     # File lib/barby/barcode/code_39.rb, line 166
166:     def spacing
167:       @spacing || 1
168:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 170
170:     def spacing_encoding
171:       '0' * spacing
172:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 180
180:     def start_encoding
181:       START_ENCODING
182:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 184
184:     def stop_encoding
185:       STOP_ENCODING
186:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 197
197:     def to_s
198:       data
199:     end

[Source]

     # File lib/barby/barcode/code_39.rb, line 188
188:     def valid?
189:       if extended?
190:         raw_characters.all?{|c| EXTENDED_ENCODINGS.include?(c) }
191:       else
192:         raw_characters.all?{|c| ENCODINGS.include?(c) }
193:       end
194:     end

[Validate]