barby-chunky_png 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +29 -0
- data/bin/barby +41 -0
- data/lib/barby/barcode/bookland.rb +37 -0
- data/lib/barby/barcode/code_128.rb +410 -0
- data/lib/barby/barcode/code_25.rb +193 -0
- data/lib/barby/barcode/code_25_iata.rb +23 -0
- data/lib/barby/barcode/code_25_interleaved.rb +73 -0
- data/lib/barby/barcode/code_39.rb +233 -0
- data/lib/barby/barcode/code_93.rb +230 -0
- data/lib/barby/barcode/data_matrix.rb +46 -0
- data/lib/barby/barcode/ean_13.rb +178 -0
- data/lib/barby/barcode/ean_8.rb +32 -0
- data/lib/barby/barcode/gs1_128.rb +42 -0
- data/lib/barby/barcode/pdf_417.rb +76 -0
- data/lib/barby/barcode/qr_code.rb +101 -0
- data/lib/barby/barcode.rb +116 -0
- data/lib/barby/outputter/ascii_outputter.rb +41 -0
- data/lib/barby/outputter/cairo_outputter.rb +185 -0
- data/lib/barby/outputter/pdfwriter_outputter.rb +83 -0
- data/lib/barby/outputter/png_outputter.rb +97 -0
- data/lib/barby/outputter/prawn_outputter.rb +99 -0
- data/lib/barby/outputter/rmagick_outputter.rb +126 -0
- data/lib/barby/outputter/svg_outputter.rb +225 -0
- data/lib/barby/outputter.rb +132 -0
- data/lib/barby/vendor.rb +3 -0
- data/lib/barby/version.rb +9 -0
- data/lib/barby.rb +17 -0
- data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.jar +0 -0
- data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.java +1471 -0
- data/vendor/rqrcode/CHANGELOG +29 -0
- data/vendor/rqrcode/COPYING +19 -0
- data/vendor/rqrcode/README +98 -0
- data/vendor/rqrcode/Rakefile +65 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb +9 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/array.rb +5 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb +11 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/integer.rb +5 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext.rb +5 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb +37 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb +56 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb +421 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb +63 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb +78 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb +313 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb +254 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode.rb +4 -0
- data/vendor/rqrcode/lib/rqrcode.rb +13 -0
- data/vendor/rqrcode/test/runtest.rb +78 -0
- data/vendor/rqrcode/test/test_data.rb +21 -0
- metadata +129 -0
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
require 'RMagick'
|
3
|
+
|
4
|
+
module Barby
|
5
|
+
|
6
|
+
|
7
|
+
#Renders images from barcodes using RMagick
|
8
|
+
#
|
9
|
+
#Registers the to_png, to_gif, to_jpg and to_image methods
|
10
|
+
class RmagickOutputter < Outputter
|
11
|
+
|
12
|
+
register :to_png, :to_gif, :to_jpg, :to_image
|
13
|
+
|
14
|
+
attr_accessor :height, :xdim, :ydim, :margin
|
15
|
+
|
16
|
+
|
17
|
+
#Returns a string containing a PNG image
|
18
|
+
def to_png(*a)
|
19
|
+
to_blob('png', *a)
|
20
|
+
end
|
21
|
+
|
22
|
+
#Returns a string containint a GIF image
|
23
|
+
def to_gif(*a)
|
24
|
+
to_blob('gif', *a)
|
25
|
+
end
|
26
|
+
|
27
|
+
#Returns a string containing a JPEG image
|
28
|
+
def to_jpg(*a)
|
29
|
+
to_blob('jpg', *a)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_blob(format, *a)
|
33
|
+
img = to_image(*a)
|
34
|
+
blob = img.to_blob{|i| i.format = format }
|
35
|
+
|
36
|
+
#Release the memory used by RMagick explicitly. Ruby's GC
|
37
|
+
#isn't aware of it and can't clean it up automatically
|
38
|
+
img.destroy! if img.respond_to?(:destroy!)
|
39
|
+
|
40
|
+
blob
|
41
|
+
end
|
42
|
+
|
43
|
+
#Returns an instance of Magick::Image
|
44
|
+
def to_image(opts={})
|
45
|
+
with_options opts do
|
46
|
+
canvas = Magick::Image.new(full_width, full_height)
|
47
|
+
bars = Magick::Draw.new
|
48
|
+
|
49
|
+
x = margin
|
50
|
+
y = margin
|
51
|
+
|
52
|
+
if barcode.two_dimensional?
|
53
|
+
encoding.each do |line|
|
54
|
+
line.split(//).map{|c| c == '1' }.each do |bar|
|
55
|
+
if bar
|
56
|
+
bars.rectangle(x, y, x+(xdim-1), y+(ydim-1))
|
57
|
+
end
|
58
|
+
x += xdim
|
59
|
+
end
|
60
|
+
x = margin
|
61
|
+
y += ydim
|
62
|
+
end
|
63
|
+
else
|
64
|
+
booleans.each do |bar|
|
65
|
+
if bar
|
66
|
+
bars.rectangle(x, y, x+(xdim-1), y+(height-1))
|
67
|
+
end
|
68
|
+
x += xdim
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
bars.draw(canvas)
|
73
|
+
|
74
|
+
canvas
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
#The height of the barcode in px
|
80
|
+
#For 2D barcodes this is the number of "lines" * ydim
|
81
|
+
def height
|
82
|
+
barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100)
|
83
|
+
end
|
84
|
+
|
85
|
+
#The width of the barcode in px
|
86
|
+
def width
|
87
|
+
length * xdim
|
88
|
+
end
|
89
|
+
|
90
|
+
#Number of modules (xdims) on the x axis
|
91
|
+
def length
|
92
|
+
barcode.two_dimensional? ? encoding.first.length : encoding.length
|
93
|
+
end
|
94
|
+
|
95
|
+
#X dimension. 1X == 1px
|
96
|
+
def xdim
|
97
|
+
@xdim || 1
|
98
|
+
end
|
99
|
+
|
100
|
+
#Y dimension. Only for 2D codes
|
101
|
+
def ydim
|
102
|
+
@ydim || xdim
|
103
|
+
end
|
104
|
+
|
105
|
+
#The margin of each edge surrounding the barcode in pixels
|
106
|
+
def margin
|
107
|
+
@margin || 10
|
108
|
+
end
|
109
|
+
|
110
|
+
#The full width of the image. This is the width of the
|
111
|
+
#barcode + the left and right margin
|
112
|
+
def full_width
|
113
|
+
width + (margin * 2)
|
114
|
+
end
|
115
|
+
|
116
|
+
#The height of the image. This is the height of the
|
117
|
+
#barcode + the top and bottom margin
|
118
|
+
def full_height
|
119
|
+
height + (margin * 2)
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,225 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
|
3
|
+
module Barby
|
4
|
+
|
5
|
+
#Renders the barcode to a simple SVG image using pure ruby
|
6
|
+
#
|
7
|
+
#Registers the to_svg, bars_to_path, and bars_to_rects method
|
8
|
+
#
|
9
|
+
#Bars can be rendered as a stroked path or as filled rectangles. Path
|
10
|
+
#generally yields smaller files, but this doesn't render cleanly in Firefox
|
11
|
+
#3 for odd xdims. My guess is that the renderer tries to put half a pixel
|
12
|
+
#on one side of the path and half on the other, leading to fuzzy dithering
|
13
|
+
#instead of sharp, clean b&w.
|
14
|
+
#
|
15
|
+
#Therefore, default behavior is to use a path for even xdims, and
|
16
|
+
#rectangles for odd. This can be overridden by calling with explicit
|
17
|
+
#:use => 'rects' or :use => 'path' options.
|
18
|
+
class SvgOutputter < Outputter
|
19
|
+
|
20
|
+
register :to_svg, :bars_to_rects, :bars_to_path
|
21
|
+
|
22
|
+
attr_writer :title, :xdim, :ydim, :height, :rmargin, :lmargin, :tmargin, :bmargin, :xmargin, :ymargin, :margin
|
23
|
+
|
24
|
+
|
25
|
+
def to_svg(opts={})
|
26
|
+
with_options opts do
|
27
|
+
case opts[:use]
|
28
|
+
when 'rects' then bars = bars_to_rects
|
29
|
+
when 'path' then bars = bars_to_path
|
30
|
+
else
|
31
|
+
xdim_odd = (xdim % 2 == 1)
|
32
|
+
bars = xdim_odd ? bars_to_rects : bars_to_path
|
33
|
+
end
|
34
|
+
|
35
|
+
<<-"EOT"
|
36
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
37
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="#{svg_width(opts)}px" height="#{svg_height(opts)}px" viewBox="0 0 #{svg_width(opts)} #{svg_height(opts)}" version="1.1">
|
38
|
+
<title>#{escape title}</title>
|
39
|
+
<g id="canvas" #{transform(opts)}>
|
40
|
+
<rect x="0" y="0" width="#{full_width}px" height="#{full_height}px" fill="white" />
|
41
|
+
<g id="barcode" fill="black">
|
42
|
+
#{bars}
|
43
|
+
</g></g>
|
44
|
+
</svg>
|
45
|
+
EOT
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def bars_to_rects(opts={})
|
51
|
+
rects = ''
|
52
|
+
with_options opts do
|
53
|
+
x, y = lmargin, tmargin
|
54
|
+
|
55
|
+
if barcode.two_dimensional?
|
56
|
+
boolean_groups.each do |line|
|
57
|
+
line.each do |bar, amount|
|
58
|
+
bar_width = xdim * amount
|
59
|
+
if bar
|
60
|
+
rects << %Q|<rect x="#{x}" y="#{y}" width="#{bar_width}px" height="#{ydim}px" />\n|
|
61
|
+
end
|
62
|
+
x += bar_width
|
63
|
+
end
|
64
|
+
y += ydim
|
65
|
+
x = lmargin
|
66
|
+
end
|
67
|
+
|
68
|
+
else
|
69
|
+
boolean_groups.each do |bar, amount|
|
70
|
+
bar_width = xdim * amount
|
71
|
+
if bar
|
72
|
+
rects << %Q|<rect x="#{x}" y="#{y}" width="#{bar_width}px" height="#{height}px" />\n|
|
73
|
+
end
|
74
|
+
x += bar_width
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end # with_options
|
79
|
+
|
80
|
+
rects
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def bars_to_path(opts={})
|
85
|
+
with_options opts do
|
86
|
+
%Q|<path stroke="black" stroke-width="#{xdim}" d="#{bars_to_path_data(opts)}" />|
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def bars_to_path_data(opts={})
|
92
|
+
path_data = ''
|
93
|
+
with_options opts do
|
94
|
+
x, y = lmargin+(xdim/2), tmargin
|
95
|
+
|
96
|
+
if barcode.two_dimensional?
|
97
|
+
booleans.each do |line|
|
98
|
+
line.each do |bar|
|
99
|
+
if bar
|
100
|
+
path_data << "M#{x} #{y}V #{y+ydim}"
|
101
|
+
end
|
102
|
+
x += xdim
|
103
|
+
end
|
104
|
+
y += ydim
|
105
|
+
x = lmargin+(xdim/2)
|
106
|
+
end
|
107
|
+
|
108
|
+
else
|
109
|
+
booleans.each do |bar|
|
110
|
+
if bar
|
111
|
+
path_data << "M#{x} #{y}V#{y+height}"
|
112
|
+
end
|
113
|
+
x += xdim
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end # with_options
|
118
|
+
|
119
|
+
path_data
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
def title
|
124
|
+
@title || barcode.to_s
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
def width
|
129
|
+
length * xdim
|
130
|
+
end
|
131
|
+
|
132
|
+
def height
|
133
|
+
barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100)
|
134
|
+
end
|
135
|
+
|
136
|
+
def full_width
|
137
|
+
width + lmargin + rmargin
|
138
|
+
end
|
139
|
+
|
140
|
+
def full_height
|
141
|
+
height + tmargin + bmargin
|
142
|
+
end
|
143
|
+
|
144
|
+
def xdim
|
145
|
+
@xdim || 1
|
146
|
+
end
|
147
|
+
|
148
|
+
def ydim
|
149
|
+
@ydim || xdim
|
150
|
+
end
|
151
|
+
|
152
|
+
def lmargin
|
153
|
+
@lmargin || _xmargin
|
154
|
+
end
|
155
|
+
|
156
|
+
def rmargin
|
157
|
+
@rmargin || _xmargin
|
158
|
+
end
|
159
|
+
|
160
|
+
def tmargin
|
161
|
+
@tmargin || _ymargin
|
162
|
+
end
|
163
|
+
|
164
|
+
def bmargin
|
165
|
+
@bmargin || _ymargin
|
166
|
+
end
|
167
|
+
|
168
|
+
def xmargin
|
169
|
+
return nil if @lmargin || @rmargin
|
170
|
+
_margin
|
171
|
+
end
|
172
|
+
|
173
|
+
def ymargin
|
174
|
+
return nil if @tmargin || @bmargin
|
175
|
+
_margin
|
176
|
+
end
|
177
|
+
|
178
|
+
def margin
|
179
|
+
return nil if @ymargin || @xmargin || @tmargin || @bmargin || @lmargin || @rmargin
|
180
|
+
_margin
|
181
|
+
end
|
182
|
+
|
183
|
+
def length
|
184
|
+
barcode.two_dimensional? ? encoding.first.length : encoding.length
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
def svg_width(opts={})
|
189
|
+
opts[:rot] ? full_height : full_width
|
190
|
+
end
|
191
|
+
|
192
|
+
def svg_height(opts={})
|
193
|
+
opts[:rot] ? full_width : full_height
|
194
|
+
end
|
195
|
+
|
196
|
+
|
197
|
+
def transform(opts={})
|
198
|
+
opts[:rot] ? %Q|transform="rotate(-90) translate(-#{full_width}, 0)"| : nil
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
private
|
203
|
+
|
204
|
+
def _xmargin
|
205
|
+
@xmargin || _margin
|
206
|
+
end
|
207
|
+
|
208
|
+
def _ymargin
|
209
|
+
@ymargin || _margin
|
210
|
+
end
|
211
|
+
|
212
|
+
def _margin
|
213
|
+
@margin || 10
|
214
|
+
end
|
215
|
+
|
216
|
+
#Escape XML special characters <, & and >
|
217
|
+
def escape(str)
|
218
|
+
str.gsub('&', '&').gsub('<', '<').gsub('>', '>')
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'barby/barcode'
|
2
|
+
|
3
|
+
module Barby
|
4
|
+
|
5
|
+
|
6
|
+
#An Outputter creates something from a barcode. That something can be
|
7
|
+
#anything, but is most likely a graphical representation of the barcode.
|
8
|
+
#Outputters can register methods on barcodes that will be associated with
|
9
|
+
#them.
|
10
|
+
#
|
11
|
+
#The basic structure of an outputter class:
|
12
|
+
#
|
13
|
+
# class FooOutputter < Barby::Outputter
|
14
|
+
# register :to_foo
|
15
|
+
# def to_too
|
16
|
+
# do_something_with(barcode.encoding)
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
#Barcode#to_foo will now be available to all barcodes
|
21
|
+
class Outputter
|
22
|
+
|
23
|
+
attr_accessor :barcode
|
24
|
+
|
25
|
+
|
26
|
+
#An outputter instance will have access to a Barcode
|
27
|
+
def initialize(barcode)
|
28
|
+
self.barcode = barcode
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
#Register one or more handler methods with this outputter
|
33
|
+
#Barcodes will then be able to use these methods to get the output
|
34
|
+
#from the outputter. For example, if you have an ImageOutputter,
|
35
|
+
#you could do:
|
36
|
+
#
|
37
|
+
#register :to_png, :to_gif
|
38
|
+
#
|
39
|
+
#You could then do aBarcode.to_png and get the result of that method.
|
40
|
+
#The class which registers the method will receive the barcode as the only
|
41
|
+
#argument, and the default implementation of initialize puts that into
|
42
|
+
#the +barcode+ accessor.
|
43
|
+
#
|
44
|
+
#You can also have different method names on the barcode and the outputter
|
45
|
+
#by providing a hash:
|
46
|
+
#
|
47
|
+
#register :to_png => :create_png, :to_gif => :create_gif
|
48
|
+
def self.register(*method_names)
|
49
|
+
if method_names.first.is_a? Hash
|
50
|
+
method_names.first.each do |name, method_name|
|
51
|
+
Barcode.register_outputter(name, self, method_name)
|
52
|
+
end
|
53
|
+
else
|
54
|
+
method_names.each do |name|
|
55
|
+
Barcode.register_outputter(name, self, name)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def two_dimensional?
|
64
|
+
barcode.respond_to?(:two_dimensional?) && barcode.two_dimensional?
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
#Converts the barcode's encoding (a string containing 1s and 0s)
|
69
|
+
#to true and false values (1 == true == "black bar")
|
70
|
+
#
|
71
|
+
#If the barcode is 2D, each line will be converted to an array
|
72
|
+
#in the same way
|
73
|
+
def booleans(reload=false)#:doc:
|
74
|
+
if two_dimensional?
|
75
|
+
encoding(reload).map{|l| l.split(//).map{|c| c == '1' } }
|
76
|
+
else
|
77
|
+
encoding(reload).split(//).map{|c| c == '1' }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
#Returns the barcode's encoding. The encoding
|
83
|
+
#is cached and can be reloaded by passing true
|
84
|
+
def encoding(reload=false)#:doc:
|
85
|
+
@encoding = barcode.encoding if reload
|
86
|
+
@encoding ||= barcode.encoding
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
#Collects continuous groups of bars and spaces (1 and 0)
|
91
|
+
#into arrays where the first item is true or false (1 or 0)
|
92
|
+
#and the second is the size of the group
|
93
|
+
#
|
94
|
+
#For example, "1100111000" becomes [[true,2],[false,2],[true,3],[false,3]]
|
95
|
+
def boolean_groups(reload=false)
|
96
|
+
if two_dimensional?
|
97
|
+
encoding(reload).map do |line|
|
98
|
+
line.scan(/1+|0+/).map do |group|
|
99
|
+
[group[0,1] == '1', group.size]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
else
|
103
|
+
encoding(reload).scan(/1+|0+/).map do |group|
|
104
|
+
[group[0,1] == '1', group.size]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def with_options(options={})
|
111
|
+
original_options = options.inject({}) do |origs,pair|
|
112
|
+
if respond_to?(pair.first) && respond_to?("#{pair.first}=")
|
113
|
+
origs[pair.first] = send(pair.first)
|
114
|
+
send("#{pair.first}=", pair.last)
|
115
|
+
end
|
116
|
+
origs
|
117
|
+
end
|
118
|
+
|
119
|
+
rv = yield
|
120
|
+
|
121
|
+
original_options.each do |attribute,value|
|
122
|
+
send("#{attribute}=", value)
|
123
|
+
end
|
124
|
+
|
125
|
+
rv
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
end
|
data/lib/barby/vendor.rb
ADDED
data/lib/barby.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'barby/vendor'
|
2
|
+
require 'barby/version'
|
3
|
+
|
4
|
+
require 'barby/barcode'
|
5
|
+
require 'barby/barcode/code_128'
|
6
|
+
require 'barby/barcode/gs1_128'
|
7
|
+
require 'barby/barcode/code_39'
|
8
|
+
require 'barby/barcode/code_93'
|
9
|
+
require 'barby/barcode/ean_13'
|
10
|
+
require 'barby/barcode/ean_8'
|
11
|
+
require 'barby/barcode/bookland'
|
12
|
+
require 'barby/barcode/qr_code'
|
13
|
+
require 'barby/barcode/code_25'
|
14
|
+
require 'barby/barcode/code_25_interleaved'
|
15
|
+
require 'barby/barcode/code_25_iata'
|
16
|
+
|
17
|
+
require 'barby/outputter'
|
Binary file
|