Class: Code39

Inherits:
Object
  • Object
show all
Defined in:
lib/barcode/code39.rb

Overview

This class takes a string and generates a Code 39-based barcode from that string.

Constant Summary collapse

@@encoding =

quick reference as to what’s up with @@encoding:

  • B => large solid bar

  • b => small solid bar

  • W => large space

  • w => small space

{
   '0' => "bwbWBwBwb",
   '1' => "BwbWbwbwB",
   '2' => "bwBWbwbwB",
   '3' => "BwBWbwbwb",
   '4' => "bwbWBwbwB",
   '5' => "BwbWBwbwb",
   '6' => "bwBWBwbwb",
   '7' => "bwbWbwBwB",
   '8' => "BwbWbwBwb",
   '9' => "bwBWbwBwb",
   'A' => "BwbwbWbwB",
   'B' => "bwBwbWbwB",
   'C' => "BwBwbWbwb",
   'D' => "bwbwBWbwB",
   'E' => "BwbwBWbwb",
   'F' => "bwBwBWbwb",
   'G' => "bwbwbWBwB",
   'H' => "BwbwbWBwb",
   'I' => "bwBwbWBwb",
   'J' => "bwbwBWBwb",
   'K' => "BwbwbwbWB",
   'L' => "bwBwbwbWB",
   'M' => "BwBwbwbWb",
   'N' => "bwbwBwbWB",
   'O' => "BwbwBwbWb",
   'P' => "bwBwBwbWb",
   'Q' => "bwbwbwBWB",
   'R' => "BwbwbwBWb",
   'S' => "bwBwbwBWb",
   'T' => "bwbwBwBWb",
   'U' => "BWbwbwbwB",
   'V' => "bWBwbwbwB",
   'W' => "BWBwbwbwb",
   'X' => "bWbwBwbwB",
   'Y' => "BWbwBwbwb",
   'Z' => "bWBwBwbwb",
   '-' => "bWbwbwBwB",
   '.' => "BWbwbwBwb",
   ' ' => "bWBwbwBwb",
   '$' => "bWbWbWbwb",
   '/' => "bWbWbwbWb",
   '+' => "bWbwbWbWb",
   '%' => "bwbWbWbWb",
   '*' => "bWbwBwBwb"
}

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Code39

Takes a string, uppercases it, and wraps it in asterisks. If any character is unavailable in Code 39 encoding, throws a vicious error.



70
71
72
73
74
75
76
77
78
79
# File 'lib/barcode/code39.rb', line 70

def initialize(text)
   text = text.upcase
   text.split(//).each do |char|
      if not @@encoding.has_key?(char)
         raise "Unencodable string."
      end
   end
   
   @text = "*#{text}*"
end

Instance Method Details

#to_img(filename, width, height) ⇒ Object

The overly complicated image generation code. Your barcode image width and height should be in pixels. The actual barcode will not be that width, instead it will be a multiple of your text string (plus two asterisks - @text) length * 14. White space will center the rest.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/barcode/code39.rb', line 99

def to_img(filename, width, height)
   bar_code_width = width - (width % ( @text.length * 14 ) )
   letter_width = bar_code_width / @text.length
   small_bar_width = letter_width / 14
   large_bar_width = ((7.0 / 3.0) * small_bar_width.to_f).to_i
   
   canvas = Image.new(width, height)
   gc = Draw.new
   gc.fill('black')
   gc.stroke('black')
   gc.stroke_width(-1)
   # Necessary to prevent massive blurry image nonsense.
   gc.stroke_antialias(false)
   
   cur_x = (width - bar_code_width) / 2
   
   self.to_s.split(//).each do |char|
      if char.upcase == "B"
         gc.rectangle(cur_x, 0, cur_x + (char == "B" ? large_bar_width : small_bar_width), height)
      end
      cur_x += char.match(/[BW]/) ? large_bar_width : small_bar_width
   end
   gc.draw(canvas)
   canvas.write(filename)
end

#to_sObject

Outputs a string that represents the encoding. Not necessarily useful externally, but who knows?



84
85
86
87
88
89
90
# File 'lib/barcode/code39.rb', line 84

def to_s
   output = String.new()
   @text.split(//).each do |char|
      output += @@encoding[char] + " "
   end
   return output
end