Class: Barby::PngOutputter
Overview
Renders the barcode to a PNG image using chunky_png (gem install chunky_png)
Registers the to_png, to_datastream and to_canvas methods
Options:
* xdim: X dimension - bar width [1]
* ydim: Y dimension - bar height (2D only) [1]
* height: Height of bars (1D only) [100]
* margin: Size of margin around barcode [0]
* foreground: Foreground color (see ChunkyPNG::Color - can be HTML color name) [black]
* background: Background color (see ChunkyPNG::Color - can be HTML color name) [white]
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?
Instance Attribute Details
#height ⇒ Object
86
87
88
|
# File 'lib/barby/outputter/png_outputter.rb', line 86
def height
barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100)
end
|
#margin ⇒ Object
106
107
108
|
# File 'lib/barby/outputter/png_outputter.rb', line 106
def margin
@margin || 10
end
|
#xdim ⇒ Object
98
99
100
|
# File 'lib/barby/outputter/png_outputter.rb', line 98
def xdim
@xdim || 1
end
|
#ydim ⇒ Object
102
103
104
|
# File 'lib/barby/outputter/png_outputter.rb', line 102
def ydim
@ydim || xdim
end
|
Instance Method Details
#background ⇒ Object
119
120
121
|
# File 'lib/barby/outputter/png_outputter.rb', line 119
def background
@background || ChunkyPNG::Color::WHITE
end
|
#background=(c) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/barby/outputter/png_outputter.rb', line 111
def background=(c)
@background = if c.is_a?(String) || c.is_a?(Symbol)
ChunkyPNG::Color.html_color(c.to_sym)
else
c
end
end
|
#foreground ⇒ Object
131
132
133
|
# File 'lib/barby/outputter/png_outputter.rb', line 131
def foreground
@foreground || ChunkyPNG::Color::BLACK
end
|
#foreground=(c) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/barby/outputter/png_outputter.rb', line 123
def foreground=(c)
@foreground = if c.is_a?(String) || c.is_a?(Symbol)
ChunkyPNG::Color.html_color(c.to_sym)
else
c
end
end
|
#full_height ⇒ Object
94
95
96
|
# File 'lib/barby/outputter/png_outputter.rb', line 94
def full_height
height + (margin * 2)
end
|
#full_width ⇒ Object
90
91
92
|
# File 'lib/barby/outputter/png_outputter.rb', line 90
def full_width
width + (margin * 2)
end
|
#length ⇒ Object
136
137
138
|
# File 'lib/barby/outputter/png_outputter.rb', line 136
def length
barcode.two_dimensional? ? encoding.first.length : encoding.length
end
|
#to_datastream(*a) ⇒ Object
Create a ChunkyPNG::Datastream containing the barcode image
:constraints - Value is passed on to ChunkyPNG::Image#to_datastream
E.g. to_datastream(:constraints => {:color_mode => ChunkyPNG::COLOR_GRAYSCALE})
70
71
72
73
|
# File 'lib/barby/outputter/png_outputter.rb', line 70
def to_datastream(*a)
constraints = a.first && a.first[:constraints] ? [a.first[:constraints]] : []
to_image(*a).to_datastream(*constraints)
end
|
#to_image(opts = {}) ⇒ Object
Creates a ChunkyPNG::Image object and renders the barcode on it
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/barby/outputter/png_outputter.rb', line 27
def to_image(opts={})
with_options opts do
canvas = ChunkyPNG::Image.new(full_width, full_height, background)
if barcode.two_dimensional?
x, y = margin, margin
booleans.each do |line|
line.each do |bar|
if bar
x.upto(x+(xdim-1)) do |xx|
y.upto y+(ydim-1) do |yy|
canvas[xx,yy] = foreground
end
end
end
x += xdim
end
y += ydim
x = margin
end
else
x, y = margin, margin
booleans.each do |bar|
if bar
x.upto(x+(xdim-1)) do |xx|
y.upto y+(height-1) do |yy|
canvas[xx,yy] = foreground
end
end
end
x += xdim
end
end
canvas
end
end
|
#to_png(*a) ⇒ Object
Renders the barcode to a PNG image
77
78
79
|
# File 'lib/barby/outputter/png_outputter.rb', line 77
def to_png(*a)
to_datastream(*a).to_s
end
|
#width ⇒ Object
82
83
84
|
# File 'lib/barby/outputter/png_outputter.rb', line 82
def width
length * xdim
end
|