Class: Barby::PngOutputter

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

Attributes inherited from Outputter

#barcode

Instance Method Summary collapse

Methods inherited from Outputter

#initialize, register

Constructor Details

This class inherits a constructor from Barby::Outputter

Instance Attribute Details

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/barby/outputter/png_outputter.rb', line 13

def height
  @height
end

#marginObject

Returns the value of attribute margin.



13
14
15
# File 'lib/barby/outputter/png_outputter.rb', line 13

def margin
  @margin
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/barby/outputter/png_outputter.rb', line 13

def width
  @width
end

#xdimObject

Returns the value of attribute xdim.



13
14
15
# File 'lib/barby/outputter/png_outputter.rb', line 13

def xdim
  @xdim
end

#ydimObject

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_heightObject



74
75
76
# File 'lib/barby/outputter/png_outputter.rb', line 74

def full_height
  height + (margin * 2)
end

#full_widthObject



70
71
72
# File 'lib/barby/outputter/png_outputter.rb', line 70

def full_width
  width + (margin * 2)
end

#lengthObject



90
91
92
# File 'lib/barby/outputter/png_outputter.rb', line 90

def length
  barcode.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={})
  with_options opts do
    canvas = ChunkyPNG::Canvas.new(full_width, full_height, ChunkyPNG::Color::WHITE)

    if barcode.two_dimensional?
      x, y = margin, margin
      booleans.reverse_each do |line|
        line.each do |bar|
          if bar
            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 |bar|
        if bar
          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