Class: Barby::PngOutputter
- Defined in:
- lib/barby/outputter/png_outputter.rb
Overview
Renders the barcode to a PNG image using the “png” gem (gem install png)
Registers the to_png and to_canvas methods
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#margin ⇒ Object
Returns the value of attribute margin.
-
#width ⇒ Object
Returns the value of attribute width.
-
#xdim ⇒ Object
Returns the value of attribute xdim.
-
#ydim ⇒ Object
Returns the value of attribute ydim.
Attributes inherited from Outputter
Instance Method Summary collapse
- #full_height ⇒ Object
- #full_width ⇒ Object
- #length ⇒ Object
-
#to_canvas(opts = {}) ⇒ Object
Creates a PNG::Canvas object and renders the barcode on it.
-
#to_png(*a) ⇒ Object
Renders the barcode to a PNG image.
Methods inherited from Outputter
Constructor Details
This class inherits a constructor from Barby::Outputter
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
13 14 15 |
# File 'lib/barby/outputter/png_outputter.rb', line 13 def height @height end |
#margin ⇒ Object
Returns the value of attribute margin.
13 14 15 |
# File 'lib/barby/outputter/png_outputter.rb', line 13 def margin @margin end |
#width ⇒ Object
Returns the value of attribute width.
13 14 15 |
# File 'lib/barby/outputter/png_outputter.rb', line 13 def width @width end |
#xdim ⇒ Object
Returns the value of attribute xdim.
13 14 15 |
# File 'lib/barby/outputter/png_outputter.rb', line 13 def xdim @xdim end |
#ydim ⇒ Object
Returns the value of attribute ydim.
13 14 15 |
# File 'lib/barby/outputter/png_outputter.rb', line 13 def ydim @ydim end |
Instance Method Details
#full_height ⇒ Object
74 75 76 |
# File 'lib/barby/outputter/png_outputter.rb', line 74 def full_height height + (margin * 2) end |
#full_width ⇒ Object
70 71 72 |
# File 'lib/barby/outputter/png_outputter.rb', line 70 def full_width width + (margin * 2) end |
#length ⇒ Object
90 91 92 |
# File 'lib/barby/outputter/png_outputter.rb', line 90 def length .two_dimensional? ? encoding.first.length : encoding.length end |
#to_canvas(opts = {}) ⇒ Object
Creates a PNG::Canvas object and renders the barcode on it
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/barby/outputter/png_outputter.rb', line 17 def to_canvas(opts={}) opts do canvas = ChunkyPNG::Canvas.new(full_width, full_height, ChunkyPNG::Color::WHITE) if .two_dimensional? x, y = margin, margin booleans.reverse_each do |line| line.each do || if x.upto(x+(xdim-1)) do |xx| y.upto y+(ydim-1) do |yy| canvas[xx,yy] = ChunkyPNG::Color::Black end end end x += xdim end y += ydim x = margin end else x, y = margin, margin booleans.each do || if x.upto(x+(xdim-1)) do |xx| y.upto y+(height-1) do |yy| canvas[xx,yy] = ChunkyPNG::Color::BLACK end end end x += xdim end end canvas end end |
#to_png(*a) ⇒ Object
Renders the barcode to a PNG image
57 58 59 |
# File 'lib/barby/outputter/png_outputter.rb', line 57 def to_png(*a) to_canvas(*a).to_blob end |