Class | Barby::CairoOutputter |
In: |
lib/barby/outputter/cairo_outputter.rb
|
Parent: | Outputter |
height | [W] | |
margin | [W] | |
x | [W] | |
xdim | [W] | |
y | [W] |
# File lib/barby/outputter/cairo_outputter.rb, line 150 150: def full_height(options={}) 151: height(options) + (margin(options) * 2) 152: end
# File lib/barby/outputter/cairo_outputter.rb, line 146 146: def full_width(options={}) 147: width(options) + (margin(options) * 2) 148: end
# File lib/barby/outputter/cairo_outputter.rb, line 138 138: def height(options={}) 139: if barcode.two_dimensional? 140: encoding.size * xdim(options) 141: else 142: @height || options[:height] || 50 143: end 144: end
# File lib/barby/outputter/cairo_outputter.rb, line 158 158: def margin(options={}) 159: @margin || options[:margin] || 10 160: end
Render the barcode onto a Cairo context
# File lib/barby/outputter/cairo_outputter.rb, line 27 27: def render_to_cairo_context(context, options={}) 28: if context.respond_to?(:have_current_point?) and 29: context.have_current_point? 30: current_x, current_y = context.current_point 31: else 32: current_x = x(options) || margin(options) 33: current_y = y(options) || margin(options) 34: end 35: 36: _xdim = xdim(options) 37: _height = height(options) 38: original_current_x = current_x 39: context.save do 40: context.set_source_color(:black) 41: context.fill do 42: if barcode.two_dimensional? 43: boolean_groups.each do |groups| 44: groups.each do |bar,amount| 45: current_width = _xdim * amount 46: if bar 47: context.rectangle(current_x, current_y, current_width, _xdim) 48: end 49: current_x += current_width 50: end 51: current_x = original_current_x 52: current_y += _xdim 53: end 54: else 55: boolean_groups.each do |bar,amount| 56: current_width = _xdim * amount 57: if bar 58: context.rectangle(current_x, current_y, current_width, _height) 59: end 60: current_x += current_width 61: end 62: end 63: end 64: end 65: 66: context 67: end
Render the barcode to an EPS document
# File lib/barby/outputter/cairo_outputter.rb, line 97 97: def to_eps(options={}) 98: to_ps(options.merge(:eps => true)) 99: end
Render the barcode to a PDF document
# File lib/barby/outputter/cairo_outputter.rb, line 103 103: def to_pdf(options={}) 104: output_to_string_io do |io| 105: Cairo::PDFSurface.new(io, 106: full_width(options), 107: full_height(options)) do |surface| 108: render(surface, options) 109: end 110: end 111: end
Render the barcode to a PNG image
# File lib/barby/outputter/cairo_outputter.rb, line 71 71: def to_png(options={}) 72: output_to_string_io do |io| 73: Cairo::ImageSurface.new(options[:format], 74: full_width(options), 75: full_height(options)) do |surface| 76: render(surface, options) 77: surface.write_to_png(io) 78: end 79: end 80: end
Render the barcode to a PS document
# File lib/barby/outputter/cairo_outputter.rb, line 84 84: def to_ps(options={}) 85: output_to_string_io do |io| 86: Cairo::PSSurface.new(io, 87: full_width(options), 88: full_height(options)) do |surface| 89: surface.eps = options[:eps] if surface.respond_to?(:eps=) 90: render(surface, options) 91: end 92: end 93: end
Render the barcode to an SVG document
# File lib/barby/outputter/cairo_outputter.rb, line 115 115: def to_svg(options={}) 116: output_to_string_io do |io| 117: Cairo::SVGSurface.new(io, 118: full_width(options), 119: full_height(options)) do |surface| 120: render(surface, options) 121: end 122: end 123: end
# File lib/barby/outputter/cairo_outputter.rb, line 134 134: def width(options={}) 135: (barcode.two_dimensional? ? encoding.first.length : encoding.length) * xdim(options) 136: end
# File lib/barby/outputter/cairo_outputter.rb, line 126 126: def x(options={}) 127: @x || options[:x] 128: end
# File lib/barby/outputter/cairo_outputter.rb, line 154 154: def xdim(options={}) 155: @xdim || options[:xdim] || 1 156: end
# File lib/barby/outputter/cairo_outputter.rb, line 130 130: def y(options={}) 131: @y || options[:y] 132: end
# File lib/barby/outputter/cairo_outputter.rb, line 165 165: def output_to_string_io 166: io = StringIO.new 167: yield(io) 168: io.rewind 169: io.read 170: end
# File lib/barby/outputter/cairo_outputter.rb, line 173 173: def render(surface, options) 174: context = Cairo::Context.new(surface) 175: yield(context) if block_given? 176: context.set_source_color(options[:background] || :white) 177: context.paint 178: render_to_cairo_context(context, options) 179: context 180: end