lib/barby/outputter/cairo_outputter.rb in barby-0.1.2 vs lib/barby/outputter/cairo_outputter.rb in barby-0.2.0
- old
+ new
@@ -1,9 +1,14 @@
+require 'barby/outputter'
require 'cairo'
require 'stringio'
module Barby
+
+ #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
class CairoOutputter < Outputter
register :render_to_cairo_context
register :to_png
@@ -16,10 +21,11 @@
register :to_svg if Cairo.const_defined?(:SVGSurface)
attr_writer :x, :y, :xdim, :height, :margin
+ #Render the barcode onto a Cairo context
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
@@ -27,27 +33,43 @@
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
- barcode.encoding.scan(/(?:0+|1+)/).each do |codes|
- current_width = _xdim * codes.size
- if codes[0] == ?1
- context.rectangle(current_x, current_y, current_width, _height)
+ 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
- current_x += current_width
+ 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
+ #Render the barcode to a PNG image
def to_png(options={})
output_to_string_io do |io|
Cairo::ImageSurface.new(options[:format],
full_width(options),
full_height(options)) do |surface|
@@ -56,10 +78,11 @@
end
end
end
+ #Render the barcode to a PS document
def to_ps(options={})
output_to_string_io do |io|
Cairo::PSSurface.new(io,
full_width(options),
full_height(options)) do |surface|
@@ -68,15 +91,17 @@
end
end
end
+ #Render the barcode to an EPS document
def to_eps(options={})
to_ps(options.merge(:eps => true))
end
+ #Render the barcode to a PDF document
def to_pdf(options={})
output_to_string_io do |io|
Cairo::PDFSurface.new(io,
full_width(options),
full_height(options)) do |surface|
@@ -84,10 +109,11 @@
end
end
end
+ #Render the barcode to an SVG document
def to_svg(options={})
output_to_string_io do |io|
Cairo::SVGSurface.new(io,
full_width(options),
full_height(options)) do |surface|
@@ -104,15 +130,19 @@
def y(options={})
@y || options[:y]
end
def width(options={})
- barcode.encoding.length * xdim(options)
+ (barcode.two_dimensional? ? encoding.first.length : encoding.length) * xdim(options)
end
def height(options={})
- @height || options[:height] || 50
+ if barcode.two_dimensional?
+ encoding.size * xdim(options)
+ else
+ @height || options[:height] || 50
+ end
end
def full_width(options={})
width(options) + (margin(options) * 2)
end
@@ -149,6 +179,7 @@
context
end
end
+
end