Class: RBarcode::Code39
Overview
This class takes a string and generates a Code 39-based barcode from that string.
Constant Summary collapse
- API_VERSION =
"0.2"
- @@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 Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Code39
constructor
Takes a string, uppercases it, and wraps it in asterisks.
-
#to_img(filename, width, height) ⇒ Object
The overly complicated image generation code.
-
#to_s ⇒ Object
Outputs a string that represents the encoding.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Code39
Takes a string, uppercases it, and wraps it in asterisks. If any character is unavailable in Code 39 encoding, throws a vicious error.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbarcode/code39.rb', line 73 def initialize( = {}) @required = [:text] super @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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rbarcode/code39.rb', line 103 def to_img(filename, width, height) = width - (width % ( @text.length * 14 ) ) letter_width = / @text.length = letter_width / 14 = ((7.0 / 3.0) * .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 - ) / 2 self.to_s.split(//).each do |char| if char.upcase == "B" gc.rectangle(cur_x, 0, cur_x + (char == "B" ? : ), height) end cur_x += char.match(/[BW]/) ? : end gc.draw(canvas) canvas.write(filename) end |
#to_s ⇒ Object
Outputs a string that represents the encoding. Not necessarily useful externally, but who knows?
89 90 91 92 93 94 95 |
# File 'lib/rbarcode/code39.rb', line 89 def to_s output = String.new @text.split(//).each do |char| output += @@encoding[char] + " " end return output end |