Class | Barby::Code39 |
In: |
lib/barby/barcode/code_39.rb
|
Parent: | Barcode1D |
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' |
data | [RW] | |
extended | [RW] | |
include_checksum | [RW] | |
spacing | [RW] |
# 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
Returns the encodable characters. If extended mode is enabled, each character will first be replaced by two characters from the encodable charset
# 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
# File lib/barby/barcode/code_39.rb, line 110 110: def characters_with_checksum 111: characters + [checksum_character] 112: end
Checksum is optional
# 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
# File lib/barby/barcode/code_39.rb, line 150 150: def checksum_character 151: CHECKSUM_VALUES.invert[checksum] 152: end
# 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)
# File lib/barby/barcode/code_39.rb, line 124 124: def data_encoding 125: encoded_characters.join(spacing_encoding) 126: end
# 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
# File lib/barby/barcode/code_39.rb, line 114 114: def encoded_characters 115: characters.map{|c| ENCODINGS[c] } 116: end
# File lib/barby/barcode/code_39.rb, line 118 118: def encoded_characters_with_checksum 119: encoded_characters + [checksum_encoding] 120: end
# 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
# 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
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
# 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
# File lib/barby/barcode/code_39.rb, line 166 166: def spacing 167: @spacing || 1 168: end