Class: Barby::CairoOutputter

Inherits:
Outputter show all
Defined in:
lib/barby/outputter/cairo_outputter.rb

Overview

Uses Cairo to render a barcode to a number of formats: PNG, PS, EPS, PDF and SVG

Registers methods render_to_cairo_context, to_png, to_ps, to_eps, to_pdf and to_svg

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

#height(options = {}) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/barby/outputter/cairo_outputter.rb', line 138

def height(options={})
  if barcode.two_dimensional?
    encoding.size * xdim(options)
  else
    @height || options[:height] || 50
  end
end

#margin(options = {}) ⇒ Object



158
159
160
# File 'lib/barby/outputter/cairo_outputter.rb', line 158

def margin(options={})
  @margin || options[:margin] || 10
end

#x(options = {}) ⇒ Object



126
127
128
# File 'lib/barby/outputter/cairo_outputter.rb', line 126

def x(options={})
  @x || options[:x]
end

#xdim(options = {}) ⇒ Object



154
155
156
# File 'lib/barby/outputter/cairo_outputter.rb', line 154

def xdim(options={})
  @xdim || options[:xdim] || 1
end

#y(options = {}) ⇒ Object



130
131
132
# File 'lib/barby/outputter/cairo_outputter.rb', line 130

def y(options={})
  @y || options[:y]
end

Instance Method Details

#full_height(options = {}) ⇒ Object



150
151
152
# File 'lib/barby/outputter/cairo_outputter.rb', line 150

def full_height(options={})
  height(options) + (margin(options) * 2)
end

#full_width(options = {}) ⇒ Object



146
147
148
# File 'lib/barby/outputter/cairo_outputter.rb', line 146

def full_width(options={})
  width(options) + (margin(options) * 2)
end

#render_to_cairo_context(context, options = {}) ⇒ Object

Render the barcode onto a Cairo context



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/barby/outputter/cairo_outputter.rb', line 27

def render_to_cairo_context(context, options={})
  if context.respond_to?(:have_current_point?) and
      context.have_current_point?
    current_x, current_y = context.current_point
  else
    current_x = x(options) || margin(options)
    current_y = y(options) || margin(options)
  end

  _xdim = xdim(options)
  _height = height(options)
  original_current_x = current_x
  context.save do
    context.set_source_color(:black)
    context.fill do
      if barcode.two_dimensional?
        boolean_groups.each do |groups|
          groups.each do |bar,amount|
            current_width = _xdim * amount
            if bar
              context.rectangle(current_x, current_y, current_width, _xdim)
            end
            current_x += current_width
          end
          current_x = original_current_x
          current_y += _xdim
        end
      else
        boolean_groups.each do |bar,amount|
          current_width = _xdim * amount
          if bar
            context.rectangle(current_x, current_y, current_width, _height)
          end
          current_x += current_width
        end
      end
    end
  end

  context
end

#to_eps(options = {}) ⇒ Object

Render the barcode to an EPS document



97
98
99
# File 'lib/barby/outputter/cairo_outputter.rb', line 97

def to_eps(options={})
  to_ps(options.merge(:eps => true))
end

#to_pdf(options = {}) ⇒ Object

Render the barcode to a PDF document



103
104
105
106
107
108
109
110
111
# File 'lib/barby/outputter/cairo_outputter.rb', line 103

def to_pdf(options={})
  output_to_string_io do |io|
    Cairo::PDFSurface.new(io,
                          full_width(options),
                          full_height(options)) do |surface|
      render(surface, options)
    end
  end
end

#to_png(options = {}) ⇒ Object

Render the barcode to a PNG image



71
72
73
74
75
76
77
78
79
80
# File 'lib/barby/outputter/cairo_outputter.rb', line 71

def to_png(options={})
  output_to_string_io do |io|
    Cairo::ImageSurface.new(options[:format],
                            full_width(options),
                            full_height(options)) do |surface|
      render(surface, options)
      surface.write_to_png(io)
    end
  end
end

#to_ps(options = {}) ⇒ Object

Render the barcode to a PS document



84
85
86
87
88
89
90
91
92
93
# File 'lib/barby/outputter/cairo_outputter.rb', line 84

def to_ps(options={})
  output_to_string_io do |io|
    Cairo::PSSurface.new(io,
                         full_width(options),
                         full_height(options)) do |surface|
      surface.eps = options[:eps] if surface.respond_to?(:eps=)
      render(surface, options)
    end
  end
end

#to_svg(options = {}) ⇒ Object

Render the barcode to an SVG document



115
116
117
118
119
120
121
122
123
# File 'lib/barby/outputter/cairo_outputter.rb', line 115

def to_svg(options={})
  output_to_string_io do |io|
    Cairo::SVGSurface.new(io,
                          full_width(options),
                          full_height(options)) do |surface|
      render(surface, options)
    end
  end
end

#width(options = {}) ⇒ Object



134
135
136
# File 'lib/barby/outputter/cairo_outputter.rb', line 134

def width(options={})
  (barcode.two_dimensional? ? encoding.first.length : encoding.length) * xdim(options)
end