Class | Barby::Code39 |
In: |
lib/barby/barcode/code_39.rb
|
Parent: | Barcode1D |
WIDE | = | W = true | ||
NARROW | = | N = false | ||
ENCODINGS | = | { ' ' => [N,W,W,N,N,N,W,N,N], '$' => [N,W,N,W,N,W,N,N,N], '%' => [N,N,N,W,N,W,N,W,N], '+' => [N,W,N,N,N,W,N,W,N], '-' => [N,W,N,N,N,N,W,N,W], '.' => [W,W,N,N,N,N,W,N,N], '/' => [N,W,N,W,N,N,N,W,N], '0' => [N,N,N,W,W,N,W,N,N], '1' => [W,N,N,W,N,N,N,N,W], '2' => [N,N,W,W,N,N,N,N,W], '3' => [W,N,W,W,N,N,N,N,N], '4' => [N,N,N,W,W,N,N,N,W], '5' => [W,N,N,W,W,N,N,N,N], '6' => [N,N,W,W,W,N,N,N,N], '7' => [N,N,N,W,N,N,W,N,W], '8' => [W,N,N,W,N,N,W,N,N], '9' => [N,N,W,W,N,N,W,N,N], 'A' => [W,N,N,N,N,W,N,N,W], 'B' => [N,N,W,N,N,W,N,N,W], 'C' => [W,N,W,N,N,W,N,N,N], 'D' => [N,N,N,N,W,W,N,N,W], 'E' => [W,N,N,N,W,W,N,N,N], 'F' => [N,N,W,N,W,W,N,N,N], 'G' => [N,N,N,N,N,W,W,N,W], 'H' => [W,N,N,N,N,W,W,N,N], 'I' => [N,N,W,N,N,W,W,N,N], 'J' => [N,N,N,N,W,W,W,N,N], 'K' => [W,N,N,N,N,N,N,W,W], 'L' => [N,N,W,N,N,N,N,W,W], 'M' => [W,N,W,N,N,N,N,W,N], 'N' => [N,N,N,N,W,N,N,W,W], 'O' => [W,N,N,N,W,N,N,W,N], 'P' => [N,N,W,N,W,N,N,W,N], 'Q' => [N,N,N,N,N,N,W,W,W], 'R' => [W,N,N,N,N,N,W,W,N], 'S' => [N,N,W,N,N,N,W,W,N], 'T' => [N,N,N,N,W,N,W,W,N], 'U' => [W,W,N,N,N,N,N,N,W], 'V' => [N,W,W,N,N,N,N,N,W], 'W' => [W,W,W,N,N,N,N,N,N], 'X' => [N,W,N,N,W,N,N,N,W], 'Y' => [W,W,N,N,W,N,N,N,N], 'Z' => [N,W,W,N,W,N,N,N,N] | ||
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 | = | [N,W,N,N,W,N,W,N,N] | ||
STOP_ENCODING | = | [N,W,N,N,W,N,W,N,N] |
data | [RW] | |
extended | [RW] | |
include_checksum | [RW] | |
narrow_width | [RW] | |
spacing | [RW] | |
wide_width | [RW] |
# File lib/barby/barcode/code_39.rb, line 92 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
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 108 108: def characters 109: chars = raw_characters 110: extended ? chars.map{|c| EXTENDED_ENCODINGS[c].split(//) }.flatten : chars 111: end
# File lib/barby/barcode/code_39.rb, line 113 113: def characters_with_checksum 114: characters + [checksum_character] 115: end
Checksum is optional
# File lib/barby/barcode/code_39.rb, line 147 147: def checksum 148: characters.inject(0) do |sum,char| 149: sum + CHECKSUM_VALUES[char] 150: end % 43 151: end
# File lib/barby/barcode/code_39.rb, line 153 153: def checksum_character 154: CHECKSUM_VALUES.invert[checksum] 155: end
# File lib/barby/barcode/code_39.rb, line 157 157: def checksum_encoding 158: encoding_for(checksum_character) 159: end
The data part of the encoding (no start+stop characters)
# File lib/barby/barcode/code_39.rb, line 127 127: def data_encoding 128: encoded_characters.join(spacing_encoding) 129: end
# File lib/barby/barcode/code_39.rb, line 131 131: def data_encoding_with_checksum 132: encoded_characters_with_checksum.join(spacing_encoding) 133: end
# File lib/barby/barcode/code_39.rb, line 117 117: def encoded_characters 118: characters.map{|c| encoding_for(c) } 119: end
# File lib/barby/barcode/code_39.rb, line 121 121: def encoded_characters_with_checksum 122: encoded_characters + [checksum_encoding] 123: end
# File lib/barby/barcode/code_39.rb, line 136 136: def encoding 137: return encoding_with_checksum if include_checksum? 138: start_encoding+spacing_encoding+data_encoding+spacing_encoding+stop_encoding 139: end
Returns the string representation for a single character
# File lib/barby/barcode/code_39.rb, line 178 178: def encoding_for(character) 179: encoding_for_bars(ENCODINGS[character]) 180: end
Takes an array of WIDE/NARROW values and returns the string representation for those bars and spaces, using wide_width and narrow_width
# File lib/barby/barcode/code_39.rb, line 169 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
# File lib/barby/barcode/code_39.rb, line 141 141: def encoding_with_checksum 142: start_encoding+spacing_encoding+data_encoding_with_checksum+spacing_encoding+stop_encoding 143: end
# File lib/barby/barcode/code_39.rb, line 194 194: def narrow_width 195: @narrow_width ||= 1 196: 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 102 102: def raw_characters 103: data.split(//) 104: 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 185 185: def spacing 186: @spacing ||= 1 187: end
# File lib/barby/barcode/code_39.rb, line 208 208: def start_encoding 209: encoding_for_bars(START_ENCODING) 210: end
# File lib/barby/barcode/code_39.rb, line 212 212: def stop_encoding 213: encoding_for_bars(STOP_ENCODING) 214: end
# File lib/barby/barcode/code_39.rb, line 216 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