Class: Barby::RmagickOutputter

Inherits:
Outputter show all
Defined in:
lib/barby/outputter/rmagick_outputter.rb

Overview

Renders images from barcodes using RMagick

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    - ^

Instance Attribute Summary collapse

Attributes inherited from Outputter

#barcode

Instance Method Summary collapse

Methods inherited from Outputter

#boolean_groups, #booleans, #encoding, #initialize, register, #two_dimensional?

Constructor Details

This class inherits a constructor from Barby::Outputter

Instance Attribute Details

#backgroundObject



159
160
161
# File 'lib/barby/outputter/rmagick_outputter.rb', line 159

def background
  @background || 'white'
end

#foregroundObject



155
156
157
# File 'lib/barby/outputter/rmagick_outputter.rb', line 155

def foreground
  @foreground || 'black'
end

#heightObject

The height of the barcode in px For 2D barcodes this is the number of “lines” * ydim



113
114
115
# File 'lib/barby/outputter/rmagick_outputter.rb', line 113

def height
  barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100)
end

#marginObject

The margin of each edge surrounding the barcode in pixels



138
139
140
# File 'lib/barby/outputter/rmagick_outputter.rb', line 138

def margin
  @margin || 10
end

#xdimObject

X dimension. 1X == 1px



128
129
130
# File 'lib/barby/outputter/rmagick_outputter.rb', line 128

def xdim
  @xdim || 1
end

#ydimObject

Y dimension. Only for 2D codes



133
134
135
# File 'lib/barby/outputter/rmagick_outputter.rb', line 133

def ydim
  @ydim || xdim
end

Instance Method Details

#full_heightObject

The height of the image. This is the height of the barcode + the top and bottom margin



150
151
152
# File 'lib/barby/outputter/rmagick_outputter.rb', line 150

def full_height
  height + (margin * 2)
end

#full_widthObject

The full width of the image. This is the width of the barcode + the left and right margin



144
145
146
# File 'lib/barby/outputter/rmagick_outputter.rb', line 144

def full_width
  width + (margin * 2)
end

#lengthObject

Number of modules (xdims) on the x axis



123
124
125
# File 'lib/barby/outputter/rmagick_outputter.rb', line 123

def length
  barcode.two_dimensional? ? encoding.first.length : encoding.length
end

#to_blob(format, *a) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/barby/outputter/rmagick_outputter.rb', line 41

def to_blob(format, *a)
  img = to_image(*a)
  blob = img.to_blob{|i| i.format = format }

  #Release the memory used by RMagick explicitly. Ruby's GC
  #isn't aware of it and can't clean it up automatically
  img.destroy! if img.respond_to?(:destroy!)

  blob
end

#to_gif(*a) ⇒ Object

Returns a string containint a GIF image



32
33
34
# File 'lib/barby/outputter/rmagick_outputter.rb', line 32

def to_gif(*a)
  to_blob('gif', *a)
end

#to_image(opts = {}) ⇒ Object

Returns an instance of Magick::Image



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/barby/outputter/rmagick_outputter.rb', line 53

def to_image(opts={})
  with_options opts do
    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?
      encoding.each do |line|
        line.split(//).map{|c| c == '1' }.each do |bar|
          if bar
            x2 = x1+(xdim-1)
            y2 = y1+(ydim-1)
            # For single pixels use point
            if x1 == x2 && y1 == y2
              bars.point(x1,y1)
            else
              # For single pixel lines, use line
              if x1 == x2
                bars.line(x1, y1, x1, y2)
              elsif y1 == y2
                bars.line(x1, y1, x2, y1)
              else
                bars.rectangle(x1, y1, x2, y2)
              end
            end
          end
          x1 += xdim
        end
        x1 = margin
        y1 += ydim
      end
    else
      booleans.each do |bar|
        if bar
          x2 = x1+(xdim-1)
          y2 = y1+(height-1)
          # For single pixel width, use line
          if x1 == x2
            bars.line(x1, y1, x1, y2)
          else
            bars.rectangle(x1, y1, x2, y2)
          end
        end
        x1 += xdim
      end
    end

    bars.draw(canvas)

    canvas
  end
end

#to_jpg(*a) ⇒ Object

Returns a string containing a JPEG image



37
38
39
# File 'lib/barby/outputter/rmagick_outputter.rb', line 37

def to_jpg(*a)
  to_blob('jpg', *a)
end

#to_png(*a) ⇒ Object

Returns a string containing a PNG image



27
28
29
# File 'lib/barby/outputter/rmagick_outputter.rb', line 27

def to_png(*a)
  to_blob('png', *a)
end

#widthObject

The width of the barcode in px



118
119
120
# File 'lib/barby/outputter/rmagick_outputter.rb', line 118

def width
  length * xdim
end