Class: Barby::Code39
- Defined in:
- lib/barby/barcode/code_39.rb
Constant Summary collapse
- 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 =
In extended mode, each character is replaced with two characters from the “normal” encoding
{ "\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" }
- 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]
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#extended ⇒ Object
Returns the value of attribute extended.
-
#include_checksum ⇒ Object
Returns the value of attribute include_checksum.
-
#narrow_width ⇒ Object
Returns the value of attribute narrow_width.
-
#spacing ⇒ Object
Spacing between the characters in xdims.
-
#wide_width ⇒ Object
Returns the value of attribute wide_width.
Instance Method Summary collapse
-
#characters ⇒ Object
Returns the encodable characters.
- #characters_with_checksum ⇒ Object
-
#checksum ⇒ Object
Checksum is optional.
- #checksum_character ⇒ Object
- #checksum_encoding ⇒ Object
-
#data_encoding ⇒ Object
The data part of the encoding (no start+stop characters).
- #data_encoding_with_checksum ⇒ Object
- #encoded_characters ⇒ Object
- #encoded_characters_with_checksum ⇒ Object
- #encoding ⇒ Object
-
#encoding_for(character) ⇒ Object
Returns the string representation for a single character.
-
#encoding_for_bars(*bars_and_spaces) ⇒ Object
Takes an array of WIDE/NARROW values and returns the string representation for those bars and spaces, using wide_width and narrow_width.
- #encoding_with_checksum ⇒ Object
- #extended? ⇒ Boolean
-
#include_checksum? ⇒ Boolean
Set include_checksum to true to make
encoding
include the checksum. -
#initialize(data, extended = false) {|_self| ... } ⇒ Code39
constructor
A new instance of Code39.
-
#raw_characters ⇒ Object
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.
- #spacing_encoding ⇒ Object
- #start_encoding ⇒ Object
- #stop_encoding ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from Barcode
#method_missing, #outputter_class_for, #outputter_for, outputters, register_outputter, #two_dimensional?
Constructor Details
#initialize(data, extended = false) {|_self| ... } ⇒ Code39
Returns a new instance of Code39.
92 93 94 95 96 97 |
# File 'lib/barby/barcode/code_39.rb', line 92 def initialize(data, extended=false) self.data = data self.extended = extended raise(ArgumentError, "data is not valid (extended=#{extended?})") unless valid? yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Barby::Barcode
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
89 90 91 |
# File 'lib/barby/barcode/code_39.rb', line 89 def data @data end |
#extended ⇒ Object
Returns the value of attribute extended.
89 90 91 |
# File 'lib/barby/barcode/code_39.rb', line 89 def extended @extended end |
#include_checksum ⇒ Object
Returns the value of attribute include_checksum.
89 90 91 |
# File 'lib/barby/barcode/code_39.rb', line 89 def include_checksum @include_checksum end |
#narrow_width ⇒ Object
Returns the value of attribute narrow_width.
89 90 91 |
# File 'lib/barby/barcode/code_39.rb', line 89 def narrow_width @narrow_width end |
#spacing ⇒ Object
Spacing between the characters in xdims. Spacing will be inserted between each character in the encoding
185 186 187 |
# File 'lib/barby/barcode/code_39.rb', line 185 def spacing @spacing end |
#wide_width ⇒ Object
Returns the value of attribute wide_width.
89 90 91 |
# File 'lib/barby/barcode/code_39.rb', line 89 def wide_width @wide_width end |
Instance Method Details
#characters ⇒ Object
Returns the encodable characters. If extended mode is enabled, each character will first be replaced by two characters from the encodable charset
108 109 110 111 |
# File 'lib/barby/barcode/code_39.rb', line 108 def characters chars = raw_characters extended ? chars.map{|c| EXTENDED_ENCODINGS[c].split(//) }.flatten : chars end |
#characters_with_checksum ⇒ Object
113 114 115 |
# File 'lib/barby/barcode/code_39.rb', line 113 def characters_with_checksum characters + [checksum_character] end |
#checksum ⇒ Object
Checksum is optional
147 148 149 150 151 |
# File 'lib/barby/barcode/code_39.rb', line 147 def checksum characters.inject(0) do |sum,char| sum + CHECKSUM_VALUES[char] end % 43 end |
#checksum_character ⇒ Object
153 154 155 |
# File 'lib/barby/barcode/code_39.rb', line 153 def checksum_character CHECKSUM_VALUES.invert[checksum] end |
#checksum_encoding ⇒ Object
157 158 159 |
# File 'lib/barby/barcode/code_39.rb', line 157 def checksum_encoding encoding_for(checksum_character) end |
#data_encoding ⇒ Object
The data part of the encoding (no start+stop characters)
127 128 129 |
# File 'lib/barby/barcode/code_39.rb', line 127 def data_encoding encoded_characters.join(spacing_encoding) end |
#data_encoding_with_checksum ⇒ Object
131 132 133 |
# File 'lib/barby/barcode/code_39.rb', line 131 def data_encoding_with_checksum encoded_characters_with_checksum.join(spacing_encoding) end |
#encoded_characters ⇒ Object
117 118 119 |
# File 'lib/barby/barcode/code_39.rb', line 117 def encoded_characters characters.map{|c| encoding_for(c) } end |
#encoded_characters_with_checksum ⇒ Object
121 122 123 |
# File 'lib/barby/barcode/code_39.rb', line 121 def encoded_characters_with_checksum encoded_characters + [checksum_encoding] end |
#encoding ⇒ Object
136 137 138 139 |
# File 'lib/barby/barcode/code_39.rb', line 136 def encoding return encoding_with_checksum if include_checksum? start_encoding+spacing_encoding+data_encoding+spacing_encoding+stop_encoding end |
#encoding_for(character) ⇒ Object
Returns the string representation for a single character
178 179 180 |
# File 'lib/barby/barcode/code_39.rb', line 178 def encoding_for(character) (ENCODINGS[character]) end |
#encoding_for_bars(*bars_and_spaces) ⇒ Object
Takes an array of WIDE/NARROW values and returns the string representation for those bars and spaces, using wide_width and narrow_width
169 170 171 172 173 174 175 |
# File 'lib/barby/barcode/code_39.rb', line 169 def (*) = false .flatten.map do |width| = ! ( ? '1' : '0') * (width == WIDE ? wide_width : narrow_width) end.join end |
#encoding_with_checksum ⇒ Object
141 142 143 |
# File 'lib/barby/barcode/code_39.rb', line 141 def encoding_with_checksum start_encoding+spacing_encoding+data_encoding_with_checksum+spacing_encoding+stop_encoding end |
#extended? ⇒ Boolean
203 204 205 |
# File 'lib/barby/barcode/code_39.rb', line 203 def extended? extended end |
#include_checksum? ⇒ Boolean
Set include_checksum to true to make encoding
include the checksum
162 163 164 |
# File 'lib/barby/barcode/code_39.rb', line 162 def include_checksum? include_checksum end |
#raw_characters ⇒ Object
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
102 103 104 |
# File 'lib/barby/barcode/code_39.rb', line 102 def raw_characters data.split(//) end |
#spacing_encoding ⇒ Object
189 190 191 |
# File 'lib/barby/barcode/code_39.rb', line 189 def spacing_encoding '0' * spacing end |
#start_encoding ⇒ Object
208 209 210 |
# File 'lib/barby/barcode/code_39.rb', line 208 def start_encoding (START_ENCODING) end |
#stop_encoding ⇒ Object
212 213 214 |
# File 'lib/barby/barcode/code_39.rb', line 212 def stop_encoding (STOP_ENCODING) end |
#to_s ⇒ Object
225 226 227 |
# File 'lib/barby/barcode/code_39.rb', line 225 def to_s data end |
#valid? ⇒ Boolean
216 217 218 219 220 221 222 |
# File 'lib/barby/barcode/code_39.rb', line 216 def valid? if extended? raw_characters.all?{|c| EXTENDED_ENCODINGS.include?(c) } else raw_characters.all?{|c| ENCODINGS.include?(c) } end end |