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
Options:
* xdim
* ydim - (2D only)
* height - (1D only)
* foreground - Cairo::Color::Base or Cairo::Color.parse(value)
* background - ^
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
-
#initialize ⇒ CairoOutputter
constructor
A new instance of CairoOutputter.
-
#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
#boolean_groups, #booleans, #encoding, register, #two_dimensional?
Constructor Details
#initialize ⇒ CairoOutputter
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(={}) if .two_dimensional? encoding.size * xdim() else @height || [:height] || 50 end end |
#margin(options = {}) ⇒ Object
171 172 173 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 171 def margin(={}) @margin || [:margin] || 10 end |
#x(options = {}) ⇒ Object
139 140 141 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 139 def x(={}) @x || [:x] end |
#xdim(options = {}) ⇒ Object
167 168 169 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 167 def xdim(={}) @xdim || [:xdim] || 1 end |
#y(options = {}) ⇒ Object
143 144 145 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 143 def y(={}) @y || [:y] end |
Instance Method Details
#full_height(options = {}) ⇒ Object
163 164 165 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 163 def full_height(={}) height() + (margin() * 2) end |
#full_width(options = {}) ⇒ Object
159 160 161 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 159 def full_width(={}) width() + (margin() * 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, ={}) 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([:foreground] || :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
110 111 112 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 110 def to_eps(={}) to_ps(.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(={}) 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
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 84 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
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 97 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
128 129 130 131 132 133 134 135 136 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 128 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
147 148 149 |
# File 'lib/barby/outputter/cairo_outputter.rb', line 147 def width(={}) (.two_dimensional? ? encoding.first.length : encoding.length) * xdim() end |