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

Options:

* xdim
* ydim             - (2D only)
* height           - (1D only)
* foreground       - Cairo::Color::Base or Cairo::Color.parse(value)
* background       - ^

Instance Attribute Summary collapse

Attributes inherited from Outputter

#barcode

Instance Method Summary collapse

Methods inherited from Outputter

#boolean_groups, #booleans, #encoding, register, #two_dimensional?

Constructor Details

#initializeCairoOutputter

Returns a new instance of CairoOutputter.



34
35
36
37
# File 'lib/barby/outputter/cairo_outputter.rb', line 34

def initialize(*)
  super
  @x, @y, @xdim, @height, @margin = nil
end

Instance Attribute Details

#height(options = {}) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/barby/outputter/cairo_outputter.rb', line 151

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

#margin(options = {}) ⇒ Object



171
172
173
# File 'lib/barby/outputter/cairo_outputter.rb', line 171

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

#x(options = {}) ⇒ Object



139
140
141
# File 'lib/barby/outputter/cairo_outputter.rb', line 139

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

#xdim(options = {}) ⇒ Object



167
168
169
# File 'lib/barby/outputter/cairo_outputter.rb', line 167

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

#y(options = {}) ⇒ Object



143
144
145
# File 'lib/barby/outputter/cairo_outputter.rb', line 143

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

Instance Method Details

#full_height(options = {}) ⇒ Object



163
164
165
# File 'lib/barby/outputter/cairo_outputter.rb', line 163

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

#full_width(options = {}) ⇒ Object



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

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

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

Render the barcode onto a Cairo context



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
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/barby/outputter/cairo_outputter.rb', line 40

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(options[:foreground] || :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



110
111
112
# File 'lib/barby/outputter/cairo_outputter.rb', line 110

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

#to_pdf(options = {}) ⇒ Object

Render the barcode to a PDF document



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

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



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

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



97
98
99
100
101
102
103
104
105
106
# File 'lib/barby/outputter/cairo_outputter.rb', line 97

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



128
129
130
131
132
133
134
135
136
# File 'lib/barby/outputter/cairo_outputter.rb', line 128

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



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

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