Class: Barby::CairoOutputter
- 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
- #height(options = {}) ⇒ Object
- #margin(options = {}) ⇒ Object
- #x(options = {}) ⇒ Object
- #xdim(options = {}) ⇒ Object
- #y(options = {}) ⇒ Object
Attributes inherited from Outputter
Instance Method Summary collapse
- #full_height(options = {}) ⇒ Object
- #full_width(options = {}) ⇒ Object
-
#render_to_cairo_context(context, options = {}) ⇒ Object
Render the barcode onto a Cairo context.
-
#to_eps(options = {}) ⇒ Object
Render the barcode to an EPS document.
-
#to_pdf(options = {}) ⇒ Object
Render the barcode to a PDF document.
-
#to_png(options = {}) ⇒ Object
Render the barcode to a PNG image.
-
#to_ps(options = {}) ⇒ Object
Render the barcode to a PS document.
-
#to_svg(options = {}) ⇒ Object
Render the barcode to an SVG document.
- #width(options = {}) ⇒ Object
Methods inherited from Outputter
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(={}) if .two_dimensional? encoding.size * xdim() else @height || [:height] || 50 end end |
#margin(options = {}) ⇒ Object
158 159 160 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 158 def margin(={}) @margin || [:margin] || 10 end |
#x(options = {}) ⇒ Object
126 127 128 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 126 def x(={}) @x || [:x] end |
#xdim(options = {}) ⇒ Object
154 155 156 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 154 def xdim(={}) @xdim || [:xdim] || 1 end |
#y(options = {}) ⇒ Object
130 131 132 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 130 def y(={}) @y || [:y] end |
Instance Method Details
#full_height(options = {}) ⇒ Object
150 151 152 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 150 def full_height(={}) height() + (margin() * 2) end |
#full_width(options = {}) ⇒ Object
146 147 148 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 146 def full_width(={}) width() + (margin() * 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, ={}) if context.respond_to?(:have_current_point?) and context.have_current_point? current_x, current_y = context.current_point else current_x = x() || margin() current_y = y() || margin() end _xdim = xdim() _height = height() original_current_x = current_x context.save do context.set_source_color(:black) context.fill do if .two_dimensional? boolean_groups.each do |groups| groups.each do |,amount| current_width = _xdim * amount if 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 |,amount| current_width = _xdim * amount if 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(={}) to_ps(.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(={}) output_to_string_io do |io| Cairo::PDFSurface.new(io, full_width(), full_height()) do |surface| render(surface, ) 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(={}) output_to_string_io do |io| Cairo::ImageSurface.new([:format], full_width(), full_height()) do |surface| render(surface, ) 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(={}) output_to_string_io do |io| Cairo::PSSurface.new(io, full_width(), full_height()) do |surface| surface.eps = [:eps] if surface.respond_to?(:eps=) render(surface, ) 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(={}) output_to_string_io do |io| Cairo::SVGSurface.new(io, full_width(), full_height()) do |surface| render(surface, ) end end end |
#width(options = {}) ⇒ Object
134 135 136 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 134 def width(={}) (.two_dimensional? ? encoding.first.length : encoding.length) * xdim() end |