Browse files

Get rid of the options BS. They're objects, they can have state.

  • Loading branch information...
1 parent befa5c0 commit fc7c8c3d5399bcce4d8e3d626db9c2cd56634759 @toretore toretore committed Aug 18, 2010
Showing with 116 additions and 52 deletions.
  1. +67 −52 lib/barby/outputter/prawn_outputter.rb
  2. +49 −0 spec/prawn_outputter_spec.rb
View
119 lib/barby/outputter/prawn_outputter.rb
@@ -7,90 +7,105 @@ class PrawnOutputter < Outputter
register :to_pdf, :annotate_pdf
+ attr_accessor :xdim, :ydim, :x, :y, :height, :margin
+
def to_pdf(opts={})
- opts = options(opts)
- annotate_pdf(Prawn::Document.new(opts[:document]), opts).render
+ doc_opts = opts.delete(:document) || {}
+ doc_opts[:page_size] ||= 'A4'
+ annotate_pdf(Prawn::Document.new(doc_opts), opts).render
end
def annotate_pdf(pdf, opts={})
- opts = options(opts)
- xpos, ypos, height, xdim = opts[:x], opts[:y], opts[:height], opts[:xdim]
- ydim = opts[:ydim] || xdim
- orig_xpos = xpos
-
- if barcode.two_dimensional?
- boolean_groups.reverse_each do |groups|
- groups.each do |bar,amount|
+ with_options opts do
+ xpos, ypos = x, y
+ orig_xpos = xpos
+
+ if barcode.two_dimensional?
+ boolean_groups.reverse_each do |groups|
+ groups.each do |bar,amount|
+ if bar
+ pdf.move_to(xpos, ypos)
+ pdf.line_to(xpos, ypos+ydim)
+ pdf.line_to(xpos+(xdim*amount), ypos+ydim)
+ pdf.line_to(xpos+(xdim*amount), ypos)
+ pdf.line_to(xpos, ypos)
+ pdf.fill
+ end
+ xpos += (xdim*amount)
+ end
+ xpos = orig_xpos
+ ypos += ydim
+ end
+ else
+ boolean_groups.each do |bar,amount|
if bar
pdf.move_to(xpos, ypos)
- pdf.line_to(xpos, ypos+ydim)
- pdf.line_to(xpos+(xdim*amount), ypos+ydim)
+ pdf.line_to(xpos, ypos+height)
+ pdf.line_to(xpos+(xdim*amount), ypos+height)
pdf.line_to(xpos+(xdim*amount), ypos)
pdf.line_to(xpos, ypos)
pdf.fill
end
xpos += (xdim*amount)
end
- xpos = orig_xpos
- ypos += ydim
- end
- else
- boolean_groups.each do |bar,amount|
- if bar
- pdf.move_to(xpos, ypos)
- pdf.line_to(xpos, ypos+height)
- pdf.line_to(xpos+(xdim*amount), ypos+height)
- pdf.line_to(xpos+(xdim*amount), ypos)
- pdf.line_to(xpos, ypos)
- pdf.fill
- end
- xpos += (xdim*amount)
end
+
end
pdf
end
- private
+ def length
+ two_dimensional? ? encoding.first.length : encoding.length
+ end
- def default_options
- @default_options ||= {
- :margin => 5,
- :height => 100,
- :xdim => 1
- }
+ def width
+ length * xdim
end
- def options(opts={})
- doc_opts = opts.delete(:document) || {}
- opts = default_options.merge(opts)
- opts[:x] ||= opts[:margin]
- opts[:y] ||= opts[:margin]
- opts[:document] = document_options(opts, doc_opts)
- opts
+ def height
+ two_dimensional? ? (ydim * encoding.length) : (@height || 50)
end
- def document_options(opts, doc_opts)
- o = doc_opts.dup
- #o[:page_size] ||= page_size(opts[:xdim], opts[:height], opts[:margin])
- #%w(left right top bottom).each{|s| o[:"#{s}_margin"] ||= opts[:margin] }
- o[:page_size] ||= 'A4' #Prawn doesn't currently support custom page sizes
- o
+ def full_width
+ width + (margin * 2)
end
- def page_size(xdim, height, margin)
- [width(xdim,margin), height(height,margin)]
+ def full_height
+ height + (margin * 2)
end
- def width(xdim, margin)
- (xdim * encoding.length) + (margin * 2)
+ #Margin is used for x and y if not given explicitly, effectively placing the barcode
+ #<margin> points from the [left,bottom] of the page.
+ #If you define x and y, there will be no margin. And if you don't define margin, it's 0.
+ def margin
+ @margin || 0
end
- def height(height, margin)
- height + (margin * 2)
+ def x
+ @x || margin
+ end
+
+ def y
+ @y || margin
+ end
+
+ def xdim
+ @xdim || 1
+ end
+
+ def ydim
+ @ydim || xdim
+ end
+
+
+ private
+
+ def page_size(xdim, height, margin)
+ [width(xdim,margin), height(height,margin)]
end
View
49 spec/prawn_outputter_spec.rb
@@ -28,4 +28,53 @@ def @barcode.encoding; '10110011100011110000'; end
@barcode.annotate_pdf(doc).object_id.should == doc.object_id
end
+ it "should default x and y to margin value" do
+ @outputter.margin = 123
+ @outputter.x.should == 123
+ @outputter.y.should == 123
+ end
+
+ it "should default ydim to xdim value" do
+ @outputter.xdim = 321
+ @outputter.ydim.should == 321
+ end
+
+ it "should be able to calculate width required" do
+ @outputter.width.should == @barcode.encoding.length
+ @outputter.xdim = 2
+ @outputter.width.should == @barcode.encoding.length * 2
+ @outputter.full_width.should == @barcode.encoding.length * 2
+ @outputter.margin = 5
+ @outputter.full_width.should == (@barcode.encoding.length * 2) + 10
+
+ #2D
+ barcode = Barcode2D.new
+ def barcode.encoding; ['111', '000', '111'] end
+ outputter = PrawnOutputter.new(barcode)
+ outputter.width.should == 3
+ outputter.xdim = 2
+ outputter.margin = 5
+ outputter.width.should == 6
+ outputter.full_width.should == 16
+ end
+
+ it "should be able to calculate height required" do
+ @outputter.full_height.should == @outputter.height
+ @outputter.margin = 5
+ @outputter.full_height.should == @outputter.height + 10
+
+ #2D
+ barcode = Barcode2D.new
+ def barcode.encoding; ['111', '000', '111'] end
+ outputter = PrawnOutputter.new(barcode)
+ outputter.height.should == 3
+ outputter.xdim = 2 #ydim defaults to xdim
+ outputter.margin = 5
+ outputter.height.should == 6
+ outputter.full_height.should == 16
+ outputter.ydim = 3 #ydim overrides xdim when set
+ outputter.height.should == 9
+ outputter.full_height.should == 19
+ end
+
end

0 comments on commit fc7c8c3

Please sign in to comment.