Class: RBarcode::Postnet
Overview
This class takes a string and generates a Code 39-based barcode from that string.
Direct Known Subclasses
Constant Summary collapse
- API_VERSION =
"0.1"
- BAR_SHORT_HEIGHT =
7
- BAR_TALL_HEIGHT =
15
- BAR_WIDTH =
2
- @@encoding =
@@encoding key
-
t => tall bar
-
s => short bar
-
{ '0' => "ttsss", '1' => "ssstt", '2' => "sstst", '3' => "sstts", '4' => "stsst", '5' => "ststs", '6' => "sttss", '7' => "tssst", '8' => "tssts", '9' => "tstss" }
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Postnet
constructor
Pass it a hash with :text => number.
-
#to_img(filename) ⇒ Object
RMagic image generation code Barcode image width and height should be in pixels.
-
#to_s ⇒ Object
Passes back encoding.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Postnet
Pass it a hash with :text => number
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rbarcode/postnet.rb', line 33 def initialize( = {}) @required = [:text] super @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) ⇒ Object
RMagic image generation code Barcode image width and height should be in pixels.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rbarcode/postnet.rb', line 55 def to_img(filename) = @text.length * 2 * 5 * BAR_WIDTH + BAR_WIDTH * 3; #canvas = Image.new(width, height) canvas = Image.new(, BAR_TALL_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 = 0 # draw the start framing bar gc.rectangle(cur_x, 0, cur_x + BAR_WIDTH, BAR_TALL_HEIGHT) cur_x += BAR_WIDTH * 2 #stsst ttsss ssstt sstst sstts stsst ststs sstst sstts ststs sttss sstts sttss sttss self.to_s.split(//).each do |char| if char == 't' gc.rectangle(cur_x, 0, cur_x + BAR_WIDTH, BAR_TALL_HEIGHT) else gc.rectangle(cur_x, (BAR_TALL_HEIGHT - BAR_SHORT_HEIGHT), cur_x + BAR_WIDTH, BAR_TALL_HEIGHT) end cur_x += BAR_WIDTH * 2 end # draw the end framing bar gc.rectangle(cur_x, 0, cur_x + BAR_WIDTH - 1, BAR_TALL_HEIGHT) cur_x += BAR_WIDTH * 2 gc.draw(canvas) canvas.write(filename) end |
#to_s ⇒ Object
Passes back encoding
45 46 47 48 49 50 51 |
# File 'lib/rbarcode/postnet.rb', line 45 def to_s output = '' @text.split(//).each do |char| output += @@encoding[char] end return output end |