lib/barby/outputter/rmagick_outputter.rb in barby-0.6.7 vs lib/barby/outputter/rmagick_outputter.rb in barby-0.6.8
- old
+ new
@@ -2,25 +2,29 @@
require 'rmagick'
module Barby
- #Renders images from barcodes using RMagick
+ # Renders images from barcodes using RMagick
#
- #Registers the to_png, to_gif, to_jpg and to_image methods
+ # Registers the to_png, to_gif, to_jpg and to_image methods
+ #
+ # Options:
+ #
+ # * xdim -
+ # * ydim - 2D only
+ # * height - 1D only
+ # * margin -
+ # * foreground - RMagick "colorspec"
+ # * background - ^
class RmagickOutputter < Outputter
register :to_png, :to_gif, :to_jpg, :to_image
- attr_writer :height, :xdim, :ydim, :margin
+ attr_writer :height, :xdim, :ydim, :margin, :foreground, :background
- def initialize(*)
- super
- @height, @xdim, @ydim, @margin = nil
- end
-
#Returns a string containing a PNG image
def to_png(*a)
to_blob('png', *a)
end
@@ -46,12 +50,14 @@
end
#Returns an instance of Magick::Image
def to_image(opts={})
with_options opts do
- canvas = Magick::Image.new(full_width, full_height)
+ b = background #Capture locally because Magick::Image.new block uses instance_eval
+ canvas = Magick::Image.new(full_width, full_height){ self.background_color = b }
bars = Magick::Draw.new
+ bars.fill = foreground
x1 = margin
y1 = margin
if barcode.two_dimensional?
@@ -141,9 +147,18 @@
#The height of the image. This is the height of the
#barcode + the top and bottom margin
def full_height
height + (margin * 2)
+ end
+
+
+ def foreground
+ @foreground || 'black'
+ end
+
+ def background
+ @background || 'white'
end
end