Class: Barby::Code39

Inherits:
Barcode1D show all
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

Instance Method Summary collapse

Methods inherited from Barcode

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

Constructor Details

#initialize(data, extended = false) {|_self| ... } ⇒ Code39

Do not surround “data” with the mandatory “*” as is this is done automically for you. So instead of passing “123456” as “data”, just pass “123456”.

Yields:

  • (_self)

Yield Parameters:

  • _self (Barby::Code39)

    the object that the method was called on

Raises:

  • (ArgumentError)


95
96
97
98
99
100
# File 'lib/barby/barcode/code_39.rb', line 95

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

#dataObject

Returns the value of attribute data.



90
91
92
# File 'lib/barby/barcode/code_39.rb', line 90

def data
  @data
end

#extendedObject

Returns the value of attribute extended.



90
91
92
# File 'lib/barby/barcode/code_39.rb', line 90

def extended
  @extended
end

#include_checksumObject

Returns the value of attribute include_checksum.



90
91
92
# File 'lib/barby/barcode/code_39.rb', line 90

def include_checksum
  @include_checksum
end

#narrow_widthObject



197
198
199
# File 'lib/barby/barcode/code_39.rb', line 197

def narrow_width
  @narrow_width ||= 1
end

#spacingObject

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



188
189
190
# File 'lib/barby/barcode/code_39.rb', line 188

def spacing
  @spacing ||= 1
end

#wide_widthObject



201
202
203
# File 'lib/barby/barcode/code_39.rb', line 201

def wide_width
  @wide_width ||= 2
end

Instance Method Details

#charactersObject

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



111
112
113
114
# File 'lib/barby/barcode/code_39.rb', line 111

def characters
  chars = raw_characters
  extended ? chars.map{|c| EXTENDED_ENCODINGS[c].split(//) }.flatten : chars
end

#characters_with_checksumObject



116
117
118
# File 'lib/barby/barcode/code_39.rb', line 116

def characters_with_checksum
  characters + [checksum_character]
end

#checksumObject

Checksum is optional



150
151
152
153
154
# File 'lib/barby/barcode/code_39.rb', line 150

def checksum
  characters.inject(0) do |sum,char|
    sum + CHECKSUM_VALUES[char]
  end % 43
end

#checksum_characterObject



156
157
158
# File 'lib/barby/barcode/code_39.rb', line 156

def checksum_character
  CHECKSUM_VALUES.invert[checksum]
end

#checksum_encodingObject



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

def checksum_encoding
  encoding_for(checksum_character)
end

#data_encodingObject

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



130
131
132
# File 'lib/barby/barcode/code_39.rb', line 130

def data_encoding
  encoded_characters.join(spacing_encoding)
end

#data_encoding_with_checksumObject



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

def data_encoding_with_checksum
  encoded_characters_with_checksum.join(spacing_encoding)
end

#encoded_charactersObject



120
121
122
# File 'lib/barby/barcode/code_39.rb', line 120

def encoded_characters
  characters.map{|c| encoding_for(c) }
end

#encoded_characters_with_checksumObject



124
125
126
# File 'lib/barby/barcode/code_39.rb', line 124

def encoded_characters_with_checksum
  encoded_characters + [checksum_encoding]
end

#encodingObject



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

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



181
182
183
# File 'lib/barby/barcode/code_39.rb', line 181

def encoding_for(character)
  encoding_for_bars(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



172
173
174
175
176
177
178
# File 'lib/barby/barcode/code_39.rb', line 172

def encoding_for_bars(*bars_and_spaces)
  bar = false
  bars_and_spaces.flatten.map do |width|
    bar = !bar
    (bar ? '1' : '0') * (width == WIDE ? wide_width : narrow_width)
  end.join
end

#encoding_with_checksumObject



144
145
146
# File 'lib/barby/barcode/code_39.rb', line 144

def encoding_with_checksum
  start_encoding+spacing_encoding+data_encoding_with_checksum+spacing_encoding+stop_encoding
end

#extended?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/barby/barcode/code_39.rb', line 206

def extended?
  extended
end

#include_checksum?Boolean

Set include_checksum to true to make encoding include the checksum

Returns:

  • (Boolean)


165
166
167
# File 'lib/barby/barcode/code_39.rb', line 165

def include_checksum?
  include_checksum
end

#raw_charactersObject

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



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

def raw_characters
  data.split(//)
end

#spacing_encodingObject



192
193
194
# File 'lib/barby/barcode/code_39.rb', line 192

def spacing_encoding
  '0' * spacing
end

#start_encodingObject



211
212
213
# File 'lib/barby/barcode/code_39.rb', line 211

def start_encoding
  encoding_for_bars(START_ENCODING)
end

#stop_encodingObject



215
216
217
# File 'lib/barby/barcode/code_39.rb', line 215

def stop_encoding
  encoding_for_bars(STOP_ENCODING)
end

#to_sObject



228
229
230
# File 'lib/barby/barcode/code_39.rb', line 228

def to_s
  data
end

#valid?Boolean

Returns:

  • (Boolean)


219
220
221
222
223
224
225
# File 'lib/barby/barcode/code_39.rb', line 219

def valid?
  if extended?
    raw_characters.all?{|c| EXTENDED_ENCODINGS.include?(c) }
  else
    raw_characters.all?{|c| ENCODINGS.include?(c) }
  end
end