Class: Barby::Code93

Inherits:
Barcode1D show all
Defined in:
lib/barby/barcode/code_93.rb

Constant Summary collapse

SHIFT_DOLLAR =

($)

"\301"
SHIFT_PERCENT =

(%)

"\302"
SHIFT_SLASH =

(/)

"\303"
SHIFT_PLUS =

(+)

"\304"
SHIFT_CHARACTERS =
[SHIFT_DOLLAR, SHIFT_PERCENT, SHIFT_SLASH, SHIFT_PLUS]
ENCODINGS =
{
  "0" => "100010100", "1" => "101001000",
  "2" => "101000100", "3" => "101000010",
  "4" => "100101000", "5" => "100100100",
  "6" => "100100010", "7" => "101010000",
  "8" => "100010010", "9" => "100001010",
  "A" => "110101000", "B" => "110100100",
  "C" => "110100010", "D" => "110010100",
  "E" => "110010010", "F" => "110001010",
  "G" => "101101000", "H" => "101100100",
  "I" => "101100010", "J" => "100110100",
  "K" => "100011010", "L" => "101011000",
  "M" => "101001100", "N" => "101000110",
  "O" => "100101100", "P" => "100010110",
  "Q" => "110110100", "R" => "110110010",
  "S" => "110101100", "T" => "110100110",
  "U" => "110010110", "V" => "110011010",
  "W" => "101101100", "X" => "101100110",
  "Y" => "100110110", "Z" => "100111010",
  "-" => "100101110", "." => "111010100",
  " " => "111010010", "$" => "111001010",
  "/" => "101101110", "+" => "101110110",
  "%" => "110101110",
  SHIFT_DOLLAR  => "100100110",
  SHIFT_PERCENT => "111011010",
  SHIFT_SLASH   => "111010110",
  SHIFT_PLUS    => "100110010"
}
EXTENDED_MAPPING =
{
  "\000" => "\302U",    " "    => " ",        "@"  => "\302V", "`"    =>    "\302W",
  "\001" => "\301A",    "!"    => "\303A",    "A"  => "A",     "a"    =>    "\304A",
  "\002" => "\301B",    '"'    => "\303B",    "B"  => "B",     "b"    =>    "\304B",
  "\003" => "\301C",    "#"    => "\303C",    "C"  => "C",     "c"    =>    "\304C",
  "\004" => "\301D",    "$"    => "\303D",    "D"  => "D",     "d"    =>    "\304D",
  "\005" => "\301E",    "%"    => "\303E",    "E"  => "E",     "e"    =>    "\304E",
  "\006" => "\301F",    "&"    => "\303F",    "F"  => "F",     "f"    =>    "\304F",
  "\007" => "\301G",    "'"    => "\303G",    "G"  => "G",     "g"    =>    "\304G",
  "\010" => "\301H",    "("    => "\303H",    "H"  => "H",     "h"    =>    "\304H",
  "\011" => "\301I",    ")"    => "\303I",    "I"  => "I",     "i"    =>    "\304I",
  "\012" => "\301J",    "*"    => "\303J",    "J"  => "J",     "j"    =>    "\304J",
  "\013" => "\301K",    "/"    => "\303K",    "K"  => "K",     "k"    =>    "\304K",
  "\014" => "\301L",    ","    => "\303L",    "L"  => "L",     "l"    =>    "\304L",
  "\015" => "\301M",    "-"    => "-",        "M"  => "M",     "m"    =>    "\304M",
  "\016" => "\301N",    "."    => ".",        "N"  => "N",     "n"    =>    "\304N",
  "\017" => "\301O",    "+"    => "\303O",    "O"  => "O",     "o"    =>    "\304O",
  "\020" => "\301P",    "0"    => "0",        "P"  => "P",     "p"    =>    "\304P",
  "\021" => "\301Q",    "1"    => "1",        "Q"  => "Q",     "q"    =>    "\304Q",
  "\022" => "\301R",    "2"    => "2",        "R"  => "R",     "r"    =>    "\304R",
  "\023" => "\301S",    "3"    => "3",        "S"  => "S",     "s"    =>    "\304S",
  "\024" => "\301T",    "4"    => "4",        "T"  => "T",     "t"    =>    "\304T",
  "\025" => "\301U",    "5"    => "5",        "U"  => "U",     "u"    =>    "\304U",
  "\026" => "\301V",    "6"    => "6",        "V"  => "V",     "v"    =>    "\304V",
  "\027" => "\301W",    "7"    => "7",        "W"  => "W",     "w"    =>    "\304W",
  "\030" => "\301X",    "8"    => "8",        "X"  => "X",     "x"    =>    "\304X",
  "\031" => "\301Y",    "9"    => "9",        "Y"  => "Y",     "y"    =>    "\304Y",
  "\032" => "\301Z",    ":"    => "\303Z",    "Z"  => "Z",     "z"    =>    "\304Z",
  "\033" => "\302A",    ";"    => "\302F",    "["  => "\302K", "{"    =>    "\302P",
  "\034" => "\302B",    "<"    => "\302G",    "\\" => "\302L", "|"    =>    "\302Q",
  "\035" => "\302C",    "="    => "\302H",    "]"  => "\302M", "}"    =>    "\302R",
  "\036" => "\302D",    ">"    => "\302I",    "^"  => "\302N", "~"    =>    "\302S",
  "\037" => "\302E",    "?"    => "\302J",    "_"  => "\302O", "\177" =>    "\302T"
}
EXTENDED_CHARACTERS =
EXTENDED_MAPPING.keys - ENCODINGS.keys
CHARACTERS =
{
  0  => "0",  1 => "1", 2  => "2", 3  => "3",
  4  => "4",  5 => "5", 6  => "6", 7  => "7",
  8  => "8",  9 => "9", 10 => "A", 11 => "B",
  12 => "C", 13 => "D", 14 => "E", 15 => "F",
  16 => "G", 17 => "H", 18 => "I", 19 => "J",
  20 => "K", 21 => "L", 22 => "M", 23 => "N",
  24 => "O", 25 => "P", 26 => "Q", 27 => "R",
  28 => "S", 29 => "T", 30 => "U", 31 => "V",
  32 => "W", 33 => "X", 34 => "Y", 35 => "Z",
  36 => "-", 37 => ".", 38 => " ", 39 => "$",
  40 => "/", 41 => "+", 42 => "%",
  43 => SHIFT_DOLLAR, 44 => SHIFT_PERCENT,
  45 => SHIFT_SLASH,  46 => SHIFT_PLUS
}
VALUES =
CHARACTERS.invert
START_ENCODING =

*

'101011110'
STOP_ENCODING =
'101011110'
TERMINATE_ENCODING =
'1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Barcode

#method_missing, #outputter_class_for, #outputter_for, outputters, register_outputter, #two_dimensional?

Constructor Details

#initialize(data) ⇒ Code93

Returns a new instance of Code93.



105
106
107
# File 'lib/barby/barcode/code_93.rb', line 105

def initialize(data)
  self.data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Barby::Barcode

Instance Attribute Details

#dataObject

Returns the value of attribute data.



102
103
104
# File 'lib/barby/barcode/code_93.rb', line 102

def data
  @data
end

Instance Method Details

#c_checksumObject

Calculates the C checksum based on checksum_values



183
184
185
186
187
188
189
# File 'lib/barby/barcode/code_93.rb', line 183

def c_checksum
  sum = 0
  checksum_values.each_with_index do |value, index|
    sum += ((index % 20) + 1) * value
  end
  sum % 47
end

#c_checksum_characterObject



191
192
193
# File 'lib/barby/barcode/code_93.rb', line 191

def c_checksum_character
  CHARACTERS[c_checksum]
end

#c_checksum_encodingObject



195
196
197
# File 'lib/barby/barcode/code_93.rb', line 195

def c_checksum_encoding
  ENCODINGS[c_checksum_character]
end

#charactersObject



119
120
121
# File 'lib/barby/barcode/code_93.rb', line 119

def characters
  raw_characters.map{|c| EXTENDED_MAPPING[c].split(//) }.flatten
end

#checksum_charactersObject



164
165
166
# File 'lib/barby/barcode/code_93.rb', line 164

def checksum_characters
  checksums.map{|s| CHARACTERS[s] }
end

#checksum_encodingObject



155
156
157
# File 'lib/barby/barcode/code_93.rb', line 155

def checksum_encoding
  checksum_encodings.join
end

#checksum_encodingsObject



151
152
153
# File 'lib/barby/barcode/code_93.rb', line 151

def checksum_encodings
  checksum_characters.map{|c| ENCODINGS[c] }
end

#checksum_valuesObject

Returns the values used for computing the C checksum in the “right” order (i.e. reversed)



171
172
173
# File 'lib/barby/barcode/code_93.rb', line 171

def checksum_values
  characters.map{|c| VALUES[c] }.reverse
end

#checksum_values_with_c_checksumObject

Returns the normal checksum plus the c_checksum This is used for calculating the k_checksum



177
178
179
# File 'lib/barby/barcode/code_93.rb', line 177

def checksum_values_with_c_checksum
  [c_checksum] + checksum_values
end

#checksumsObject



160
161
162
# File 'lib/barby/barcode/code_93.rb', line 160

def checksums
  [c_checksum, k_checksum]
end

#data_encodingObject



143
144
145
# File 'lib/barby/barcode/code_93.rb', line 143

def data_encoding
  character_encodings.join
end

#data_encoding_with_checksumsObject



147
148
149
# File 'lib/barby/barcode/code_93.rb', line 147

def data_encoding_with_checksums
  (character_encodings + checksum_encodings).join
end

#encoded_charactersObject Also known as: character_encodings



123
124
125
# File 'lib/barby/barcode/code_93.rb', line 123

def encoded_characters
  characters.map{|c| ENCODINGS[c] }
end

#encodingObject



139
140
141
# File 'lib/barby/barcode/code_93.rb', line 139

def encoding
  start_encoding+data_encoding_with_checksums+stop_encoding
end

#extended?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/barby/barcode/code_93.rb', line 110

def extended?
  raw_characters.any?{|c| EXTENDED_CHARACTERS.include?(c) }
end

#k_checksumObject

Calculates the K checksum based on checksum_values_with_c_checksum



201
202
203
204
205
206
207
# File 'lib/barby/barcode/code_93.rb', line 201

def k_checksum
  sum = 0
  checksum_values_with_c_checksum.each_with_index do |value, index|
    sum += ((index % 15) + 1) * value
  end
  sum % 47
end

#k_checksum_characterObject



209
210
211
# File 'lib/barby/barcode/code_93.rb', line 209

def k_checksum_character
  CHARACTERS[k_checksum]
end

#k_checksum_encodingObject



213
214
215
# File 'lib/barby/barcode/code_93.rb', line 213

def k_checksum_encoding
  ENCODINGS[k_checksum_character]
end

#raw_charactersObject



115
116
117
# File 'lib/barby/barcode/code_93.rb', line 115

def raw_characters
  data.split(//)
end

#start_encodingObject



129
130
131
# File 'lib/barby/barcode/code_93.rb', line 129

def start_encoding
  START_ENCODING
end

#stop_encodingObject

The stop encoding includes the termination bar



134
135
136
# File 'lib/barby/barcode/code_93.rb', line 134

def stop_encoding
  STOP_ENCODING+TERMINATE_ENCODING
end

#to_sObject



223
224
225
# File 'lib/barby/barcode/code_93.rb', line 223

def to_s
  data
end

#valid?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/barby/barcode/code_93.rb', line 218

def valid?
  characters.all?{|c| ENCODINGS.include?(c) }
end