lib/barby/outputter/pdfwriter_outputter.rb in barby-0.1.2 vs lib/barby/outputter/pdfwriter_outputter.rb in barby-0.2.0
- old
+ new
@@ -1,36 +1,64 @@
require 'barby/outputter'
module Barby
+ #Annotates a PDFWriter document with the barcode
+ #
+ #Registers the annotate_pdf method
class PDFWriterOutputter < Outputter
register :annotate_pdf
attr_accessor :x, :y, :height, :xdim
+ #Annotate a PDFWriter document with the barcode
+ #
+ #Valid options are:
+ #
+ #x, y - The point in the document to start rendering from
+ #height - The height of the bars in PDF units
+ #xdim - The X dimension in PDF units
def annotate_pdf(pdf, options={})
- previous_options = options.map{|k,v| [k, send(k)] }
- options.each{|k,v| send("#{k}=", v) if respond_to?("#{k}=") }
+ with_options options do
- xpos, ypos = x, y
+ xpos, ypos = x, y
+ orig_xpos = xpos
- widths.each do |array|
- if array.first
- pdf.move_to(xpos, ypos).
- line_to(xpos, ypos+height).
- line_to(xpos+(xdim*array.size), ypos+height).
- line_to(xpos+(xdim*array.size), ypos).
- line_to(xpos, ypos).
- fill
+ if barcode.two_dimensional?
+ boolean_groups.reverse_each do |groups|
+ groups.each do |bar,amount|
+ if bar
+ pdf.move_to(xpos, ypos).
+ line_to(xpos, ypos+xdim).
+ line_to(xpos+(xdim*amount), ypos+xdim).
+ line_to(xpos+(xdim*amount), ypos).
+ line_to(xpos, ypos).
+ fill
+ end
+ xpos += (xdim*amount)
+ end
+ xpos = orig_xpos
+ ypos += xdim
+ end
+ else
+ boolean_groups.each do |bar,amount|
+ if bar
+ pdf.move_to(xpos, ypos).
+ line_to(xpos, ypos+height).
+ line_to(xpos+(xdim*amount), ypos+height).
+ line_to(xpos+(xdim*amount), ypos).
+ line_to(xpos, ypos).
+ fill
+ end
+ xpos += (xdim*amount)
+ end
end
- xpos += (xdim*array.size)
+
end
- previous_options.each{|k,v| send("#{k}=", v) }
-
pdf
end
def x
@@ -45,28 +73,9 @@
@height || 50
end
def xdim
@xdim || 1
- end
-
- def widths
- widths = []
- count = nil
-
- booleans.inject nil do |previous,current|
- if current != previous
- widths << count if count
- count = [current]
- else
- count << current
- end
- current
- end
-
- widths << count
-
- widths
end
end