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,101 @@
|
|
1
|
+
require 'rqrcode'
|
2
|
+
require 'barby/barcode'
|
3
|
+
|
4
|
+
module Barby
|
5
|
+
|
6
|
+
|
7
|
+
#QrCode is a thin wrapper around the RQRCode library
|
8
|
+
class QrCode < Barcode2D
|
9
|
+
|
10
|
+
#Maximum sizes for each correction level for binary data
|
11
|
+
#It's an array
|
12
|
+
SIZES = {
|
13
|
+
#L M Q H
|
14
|
+
1 => [17, 14, 11, 7], 2 => [32, 26, 20, 14],
|
15
|
+
3 => [53, 42, 32, 24], 4 => [78, 62, 46, 34],
|
16
|
+
5 => [106, 84, 60, 44], 6 => [134, 106, 74, 58],
|
17
|
+
7 => [154, 122, 86, 64], 8 => [192, 152, 108, 84],
|
18
|
+
9 => [230, 180, 130, 98], 10 => [271, 213, 151, 119],
|
19
|
+
11 => [321, 251, 177, 137], 12 => [367, 287, 203, 155],
|
20
|
+
13 => [425, 331, 241, 177], 14 => [458, 362, 258, 194],
|
21
|
+
15 => [520, 412, 292, 220], 16 => [586, 450, 322, 250],
|
22
|
+
17 => [644, 504, 364, 280], 18 => [718, 560, 394, 310],
|
23
|
+
19 => [792, 624, 442, 338], 20 => [858, 666, 482, 382],
|
24
|
+
21 => [929, 711, 509, 403], 22 => [1003, 779, 565, 439],
|
25
|
+
23 => [1091, 857, 611, 461], 24 => [1171, 911, 661, 511],
|
26
|
+
25 => [1273, 997, 715, 535], 26 => [1367, 1059, 751, 593],
|
27
|
+
27 => [1465, 1125, 805, 625], 28 => [1528, 1190, 868, 658],
|
28
|
+
29 => [1628, 1264, 908, 698], 30 => [1732, 1370, 982, 742],
|
29
|
+
31 => [1840, 1452, 1030, 790], 32 => [1952, 1538, 1112, 842],
|
30
|
+
33 => [2068, 1628, 1168, 898], 34 => [2188, 1722, 1228, 958],
|
31
|
+
35 => [2303, 1809, 1283, 983], 36 => [2431, 1911, 1351, 1051],
|
32
|
+
37 => [2563, 1989, 1423, 1093], 38 => [2699, 2099, 1499, 1139],
|
33
|
+
39 => [2809, 2213, 1579, 1219], 40 => [2953, 2331, 1663, 1273]
|
34
|
+
}.sort
|
35
|
+
|
36
|
+
LEVELS = { :l => 0, :m => 1, :q => 2, :h => 3 }
|
37
|
+
|
38
|
+
attr_reader :data
|
39
|
+
attr_writer :level, :size
|
40
|
+
|
41
|
+
|
42
|
+
def initialize(data, options={})
|
43
|
+
self.data = data
|
44
|
+
options.each{|k,v| send("#{k}=", v) }
|
45
|
+
raise(ArgumentError, "data too large") unless size
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def data=(data)
|
50
|
+
@data = data
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def encoding
|
55
|
+
rqrcode.modules.map{|r| r.inject(''){|s,m| s << (m ? '1' : '0') } }
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
#Error correction level
|
60
|
+
#Can be one of [:l, :m, :q, :h] (7%, 15%, 25%, 30%)
|
61
|
+
def level
|
62
|
+
@level || :l
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def size
|
67
|
+
#@size is only used for manual override, if it's not set
|
68
|
+
#manually the size is always dynamic, calculated from the
|
69
|
+
#length of the data
|
70
|
+
return @size if @size
|
71
|
+
|
72
|
+
level_index = LEVELS[level]
|
73
|
+
length = data.length
|
74
|
+
found_size = nil
|
75
|
+
SIZES.each do |size,max_values|
|
76
|
+
if max_values[level_index] >= length
|
77
|
+
found_size = size
|
78
|
+
break
|
79
|
+
end
|
80
|
+
end
|
81
|
+
found_size
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
def to_s
|
86
|
+
data[0,20]
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
#Generate an RQRCode object with the available values
|
93
|
+
def rqrcode
|
94
|
+
RQRCode::QRCode.new(data, :level => level, :size => size)
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module Barby
|
2
|
+
|
3
|
+
|
4
|
+
#The base class for all barcodes. It includes some method_missing magic
|
5
|
+
#that is used to find registered outputters.
|
6
|
+
#
|
7
|
+
#The only interface requirement of a barcode class is that is has an encoding
|
8
|
+
#method that returns a string consisting of 1s and 0s representing the barcode's
|
9
|
+
#"black" and "white" parts. One digit is the width of the "X dimension"; that is,
|
10
|
+
#"101100" represents a single-width bar followed by a single-width space, then
|
11
|
+
#a bar and a space twice that width.
|
12
|
+
#
|
13
|
+
#Example implementation:
|
14
|
+
#
|
15
|
+
# class StaticBarcode < Barby::Barcode1D
|
16
|
+
# def encoding
|
17
|
+
# '101100111000111100001'
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# require 'barby/outputter/ascii_outputter'
|
22
|
+
# puts StaticBarcode.new.to_ascii(:height => 3)
|
23
|
+
#
|
24
|
+
# # ## ### #### #
|
25
|
+
# # ## ### #### #
|
26
|
+
# # ## ### #### #
|
27
|
+
#
|
28
|
+
#2D implementation:
|
29
|
+
#
|
30
|
+
# class Static2DBarcode < Barby::Barcode2D
|
31
|
+
# def encoding
|
32
|
+
# ['1010101', '010101110', '0001010100']
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
class Barcode
|
36
|
+
|
37
|
+
|
38
|
+
#Every barcode must have an encoding method. This method returns
|
39
|
+
#a string containing a series of 1 and 0, representing bars and
|
40
|
+
#spaces. One digit is the width of one "module" or X dimension.
|
41
|
+
#
|
42
|
+
#If the barcode is 2D, it returns an array of strings representing
|
43
|
+
#each line in the barcode
|
44
|
+
def encoding
|
45
|
+
raise NotImplementedError, 'Every barcode should implement this method'
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
#Is this barcode valid?
|
50
|
+
def valid?
|
51
|
+
false
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def to_s
|
56
|
+
self.class.name.split('::').last
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
#Is this barcode 2D?
|
61
|
+
def two_dimensional?
|
62
|
+
is_a? Barcode2D
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def method_missing(name, *args, &b)#:nodoc:
|
67
|
+
#See if an outputter has registered this method
|
68
|
+
if self.class.outputters.include?(name)
|
69
|
+
klass, method_name = self.class.outputters[name]
|
70
|
+
klass.new(self).send(method_name, *args, &b)
|
71
|
+
else
|
72
|
+
super
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
#Returns an instantiated outputter for +name+ if any outputter
|
78
|
+
#has registered that name
|
79
|
+
def outputter_for(name, *a, &b)
|
80
|
+
outputter_class_for(name).new(self, *a, &b)
|
81
|
+
end
|
82
|
+
|
83
|
+
#Get the outputter class object for +name+
|
84
|
+
def outputter_class_for(name)
|
85
|
+
self.class.outputters[name].first
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
class << self
|
90
|
+
|
91
|
+
def outputters
|
92
|
+
@@outputters ||= {}
|
93
|
+
end
|
94
|
+
|
95
|
+
#Registers an outputter with +name+ so that a call to
|
96
|
+
#+name+ on a Barcode instance will be delegated to this outputter
|
97
|
+
def register_outputter(name, klass, method_name)
|
98
|
+
outputters[name] = [klass, method_name]
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
#Most barcodes are one-dimensional. They have bars.
|
107
|
+
class Barcode1D < Barcode
|
108
|
+
end
|
109
|
+
|
110
|
+
#2D barcodes are 1D barcodes stacked on top of each other.
|
111
|
+
#Their encoding method must return an array of strings
|
112
|
+
class Barcode2D < Barcode
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
|
3
|
+
module Barby
|
4
|
+
|
5
|
+
#Outputs an ASCII representation of the barcode. This is mostly useful for printing
|
6
|
+
#the barcode directly to the terminal for testing.
|
7
|
+
#
|
8
|
+
#Registers to_ascii
|
9
|
+
class ASCIIOutputter < Outputter
|
10
|
+
|
11
|
+
register :to_ascii
|
12
|
+
|
13
|
+
|
14
|
+
def to_ascii(opts={})
|
15
|
+
default_opts = {:height => 10, :xdim => 1, :bar => '#', :space => ' '}
|
16
|
+
default_opts.update(:height => 1, :bar => ' X ', :space => ' ') if barcode.two_dimensional?
|
17
|
+
opts = default_opts.merge(opts)
|
18
|
+
|
19
|
+
if barcode.two_dimensional?
|
20
|
+
booleans.map do |bools|
|
21
|
+
line_to_ascii(bools, opts)
|
22
|
+
end.join("\n")
|
23
|
+
else
|
24
|
+
line_to_ascii(booleans, opts)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def line_to_ascii(booleans, opts)
|
32
|
+
Array.new(
|
33
|
+
opts[:height],
|
34
|
+
booleans.map{|b| (b ? opts[:bar] : opts[:space]) * opts[:xdim] }.join
|
35
|
+
).join("\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
require 'cairo'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module Barby
|
6
|
+
|
7
|
+
#Uses Cairo to render a barcode to a number of formats: PNG, PS, EPS, PDF and SVG
|
8
|
+
#
|
9
|
+
#Registers methods render_to_cairo_context, to_png, to_ps, to_eps, to_pdf and to_svg
|
10
|
+
class CairoOutputter < Outputter
|
11
|
+
|
12
|
+
register :render_to_cairo_context
|
13
|
+
register :to_png
|
14
|
+
|
15
|
+
if Cairo.const_defined?(:PSSurface)
|
16
|
+
register :to_ps
|
17
|
+
register :to_eps if Cairo::PSSurface.method_defined?(:eps=)
|
18
|
+
end
|
19
|
+
|
20
|
+
register :to_pdf if Cairo.const_defined?(:PDFSurface)
|
21
|
+
register :to_svg if Cairo.const_defined?(:SVGSurface)
|
22
|
+
|
23
|
+
attr_writer :x, :y, :xdim, :height, :margin
|
24
|
+
|
25
|
+
|
26
|
+
#Render the barcode onto a Cairo context
|
27
|
+
def render_to_cairo_context(context, options={})
|
28
|
+
if context.respond_to?(:have_current_point?) and
|
29
|
+
context.have_current_point?
|
30
|
+
current_x, current_y = context.current_point
|
31
|
+
else
|
32
|
+
current_x = x(options) || margin(options)
|
33
|
+
current_y = y(options) || margin(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
_xdim = xdim(options)
|
37
|
+
_height = height(options)
|
38
|
+
original_current_x = current_x
|
39
|
+
context.save do
|
40
|
+
context.set_source_color(:black)
|
41
|
+
context.fill do
|
42
|
+
if barcode.two_dimensional?
|
43
|
+
boolean_groups.each do |groups|
|
44
|
+
groups.each do |bar,amount|
|
45
|
+
current_width = _xdim * amount
|
46
|
+
if bar
|
47
|
+
context.rectangle(current_x, current_y, current_width, _xdim)
|
48
|
+
end
|
49
|
+
current_x += current_width
|
50
|
+
end
|
51
|
+
current_x = original_current_x
|
52
|
+
current_y += _xdim
|
53
|
+
end
|
54
|
+
else
|
55
|
+
boolean_groups.each do |bar,amount|
|
56
|
+
current_width = _xdim * amount
|
57
|
+
if bar
|
58
|
+
context.rectangle(current_x, current_y, current_width, _height)
|
59
|
+
end
|
60
|
+
current_x += current_width
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
#Render the barcode to a PNG image
|
71
|
+
def to_png(options={})
|
72
|
+
output_to_string_io do |io|
|
73
|
+
Cairo::ImageSurface.new(options[:format],
|
74
|
+
full_width(options),
|
75
|
+
full_height(options)) do |surface|
|
76
|
+
render(surface, options)
|
77
|
+
surface.write_to_png(io)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
#Render the barcode to a PS document
|
84
|
+
def to_ps(options={})
|
85
|
+
output_to_string_io do |io|
|
86
|
+
Cairo::PSSurface.new(io,
|
87
|
+
full_width(options),
|
88
|
+
full_height(options)) do |surface|
|
89
|
+
surface.eps = options[:eps] if surface.respond_to?(:eps=)
|
90
|
+
render(surface, options)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
#Render the barcode to an EPS document
|
97
|
+
def to_eps(options={})
|
98
|
+
to_ps(options.merge(:eps => true))
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
#Render the barcode to a PDF document
|
103
|
+
def to_pdf(options={})
|
104
|
+
output_to_string_io do |io|
|
105
|
+
Cairo::PDFSurface.new(io,
|
106
|
+
full_width(options),
|
107
|
+
full_height(options)) do |surface|
|
108
|
+
render(surface, options)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
#Render the barcode to an SVG document
|
115
|
+
def to_svg(options={})
|
116
|
+
output_to_string_io do |io|
|
117
|
+
Cairo::SVGSurface.new(io,
|
118
|
+
full_width(options),
|
119
|
+
full_height(options)) do |surface|
|
120
|
+
render(surface, options)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
def x(options={})
|
127
|
+
@x || options[:x]
|
128
|
+
end
|
129
|
+
|
130
|
+
def y(options={})
|
131
|
+
@y || options[:y]
|
132
|
+
end
|
133
|
+
|
134
|
+
def width(options={})
|
135
|
+
(barcode.two_dimensional? ? encoding.first.length : encoding.length) * xdim(options)
|
136
|
+
end
|
137
|
+
|
138
|
+
def height(options={})
|
139
|
+
if barcode.two_dimensional?
|
140
|
+
encoding.size * xdim(options)
|
141
|
+
else
|
142
|
+
@height || options[:height] || 50
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def full_width(options={})
|
147
|
+
width(options) + (margin(options) * 2)
|
148
|
+
end
|
149
|
+
|
150
|
+
def full_height(options={})
|
151
|
+
height(options) + (margin(options) * 2)
|
152
|
+
end
|
153
|
+
|
154
|
+
def xdim(options={})
|
155
|
+
@xdim || options[:xdim] || 1
|
156
|
+
end
|
157
|
+
|
158
|
+
def margin(options={})
|
159
|
+
@margin || options[:margin] || 10
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def output_to_string_io
|
166
|
+
io = StringIO.new
|
167
|
+
yield(io)
|
168
|
+
io.rewind
|
169
|
+
io.read
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
def render(surface, options)
|
174
|
+
context = Cairo::Context.new(surface)
|
175
|
+
yield(context) if block_given?
|
176
|
+
context.set_source_color(options[:background] || :white)
|
177
|
+
context.paint
|
178
|
+
render_to_cairo_context(context, options)
|
179
|
+
context
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
|
3
|
+
module Barby
|
4
|
+
|
5
|
+
#Annotates a PDFWriter document with the barcode
|
6
|
+
#
|
7
|
+
#Registers the annotate_pdf method
|
8
|
+
class PDFWriterOutputter < Outputter
|
9
|
+
|
10
|
+
register :annotate_pdf
|
11
|
+
|
12
|
+
attr_accessor :x, :y, :height, :xdim
|
13
|
+
|
14
|
+
|
15
|
+
#Annotate a PDFWriter document with the barcode
|
16
|
+
#
|
17
|
+
#Valid options are:
|
18
|
+
#
|
19
|
+
#x, y - The point in the document to start rendering from
|
20
|
+
#height - The height of the bars in PDF units
|
21
|
+
#xdim - The X dimension in PDF units
|
22
|
+
def annotate_pdf(pdf, options={})
|
23
|
+
with_options options do
|
24
|
+
|
25
|
+
xpos, ypos = x, y
|
26
|
+
orig_xpos = xpos
|
27
|
+
|
28
|
+
if barcode.two_dimensional?
|
29
|
+
boolean_groups.reverse_each do |groups|
|
30
|
+
groups.each do |bar,amount|
|
31
|
+
if bar
|
32
|
+
pdf.move_to(xpos, ypos).
|
33
|
+
line_to(xpos, ypos+xdim).
|
34
|
+
line_to(xpos+(xdim*amount), ypos+xdim).
|
35
|
+
line_to(xpos+(xdim*amount), ypos).
|
36
|
+
line_to(xpos, ypos).
|
37
|
+
fill
|
38
|
+
end
|
39
|
+
xpos += (xdim*amount)
|
40
|
+
end
|
41
|
+
xpos = orig_xpos
|
42
|
+
ypos += xdim
|
43
|
+
end
|
44
|
+
else
|
45
|
+
boolean_groups.each do |bar,amount|
|
46
|
+
if bar
|
47
|
+
pdf.move_to(xpos, ypos).
|
48
|
+
line_to(xpos, ypos+height).
|
49
|
+
line_to(xpos+(xdim*amount), ypos+height).
|
50
|
+
line_to(xpos+(xdim*amount), ypos).
|
51
|
+
line_to(xpos, ypos).
|
52
|
+
fill
|
53
|
+
end
|
54
|
+
xpos += (xdim*amount)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
pdf
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def x
|
65
|
+
@x || 10
|
66
|
+
end
|
67
|
+
|
68
|
+
def y
|
69
|
+
@y || 10
|
70
|
+
end
|
71
|
+
|
72
|
+
def height
|
73
|
+
@height || 50
|
74
|
+
end
|
75
|
+
|
76
|
+
def xdim
|
77
|
+
@xdim || 1
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
require 'chunky_png'
|
3
|
+
|
4
|
+
module Barby
|
5
|
+
|
6
|
+
#Renders the barcode to a PNG image using the "png" gem (gem install png)
|
7
|
+
#
|
8
|
+
#Registers the to_png and to_canvas methods
|
9
|
+
class PngOutputter < Outputter
|
10
|
+
|
11
|
+
register :to_png, :to_canvas
|
12
|
+
|
13
|
+
attr_accessor :xdim, :ydim, :width, :height, :margin
|
14
|
+
|
15
|
+
|
16
|
+
#Creates a PNG::Canvas object and renders the barcode on it
|
17
|
+
def to_canvas(opts={})
|
18
|
+
with_options opts do
|
19
|
+
canvas = ChunkyPNG::Canvas.new(full_width, full_height, ChunkyPNG::Color::WHITE)
|
20
|
+
|
21
|
+
if barcode.two_dimensional?
|
22
|
+
x, y = margin, margin
|
23
|
+
booleans.reverse_each do |line|
|
24
|
+
line.each do |bar|
|
25
|
+
if bar
|
26
|
+
x.upto(x+(xdim-1)) do |xx|
|
27
|
+
y.upto y+(ydim-1) do |yy|
|
28
|
+
canvas[xx,yy] = PNG::Color::Black
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
x += xdim
|
33
|
+
end
|
34
|
+
y += ydim
|
35
|
+
x = margin
|
36
|
+
end
|
37
|
+
else
|
38
|
+
x, y = margin, margin
|
39
|
+
booleans.each do |bar|
|
40
|
+
if bar
|
41
|
+
x.upto(x+(xdim-1)) do |xx|
|
42
|
+
y.upto y+(height-1) do |yy|
|
43
|
+
canvas[xx,yy] = ChunkyPNG::Color::BLACK
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
x += xdim
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
canvas
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
#Renders the barcode to a PNG image
|
57
|
+
def to_png(*a)
|
58
|
+
to_canvas(*a).to_blob
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
def width
|
63
|
+
length * xdim
|
64
|
+
end
|
65
|
+
|
66
|
+
def height
|
67
|
+
barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100)
|
68
|
+
end
|
69
|
+
|
70
|
+
def full_width
|
71
|
+
width + (margin * 2)
|
72
|
+
end
|
73
|
+
|
74
|
+
def full_height
|
75
|
+
height + (margin * 2)
|
76
|
+
end
|
77
|
+
|
78
|
+
def xdim
|
79
|
+
@xdim || 1
|
80
|
+
end
|
81
|
+
|
82
|
+
def ydim
|
83
|
+
@ydim || xdim
|
84
|
+
end
|
85
|
+
|
86
|
+
def margin
|
87
|
+
@margin || 10
|
88
|
+
end
|
89
|
+
|
90
|
+
def length
|
91
|
+
barcode.two_dimensional? ? encoding.first.length : encoding.length
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'barby/outputter'
|
2
|
+
require 'prawn'
|
3
|
+
|
4
|
+
module Barby
|
5
|
+
|
6
|
+
class PrawnOutputter < Outputter
|
7
|
+
|
8
|
+
register :to_pdf, :annotate_pdf
|
9
|
+
|
10
|
+
|
11
|
+
def to_pdf(opts={})
|
12
|
+
opts = options(opts)
|
13
|
+
annotate_pdf(Prawn::Document.new(opts[:document]), opts).render
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def annotate_pdf(pdf, opts={})
|
18
|
+
opts = options(opts)
|
19
|
+
xpos, ypos, height, xdim = opts[:x], opts[:y], opts[:height], opts[:xdim]
|
20
|
+
ydim = opts[:ydim] || xdim
|
21
|
+
orig_xpos = xpos
|
22
|
+
|
23
|
+
if barcode.two_dimensional?
|
24
|
+
boolean_groups.reverse_each do |groups|
|
25
|
+
groups.each do |bar,amount|
|
26
|
+
if bar
|
27
|
+
pdf.move_to(xpos, ypos)
|
28
|
+
pdf.line_to(xpos, ypos+ydim)
|
29
|
+
pdf.line_to(xpos+(xdim*amount), ypos+ydim)
|
30
|
+
pdf.line_to(xpos+(xdim*amount), ypos)
|
31
|
+
pdf.line_to(xpos, ypos)
|
32
|
+
pdf.fill
|
33
|
+
end
|
34
|
+
xpos += (xdim*amount)
|
35
|
+
end
|
36
|
+
xpos = orig_xpos
|
37
|
+
ypos += ydim
|
38
|
+
end
|
39
|
+
else
|
40
|
+
boolean_groups.each do |bar,amount|
|
41
|
+
if bar
|
42
|
+
pdf.move_to(xpos, ypos)
|
43
|
+
pdf.line_to(xpos, ypos+height)
|
44
|
+
pdf.line_to(xpos+(xdim*amount), ypos+height)
|
45
|
+
pdf.line_to(xpos+(xdim*amount), ypos)
|
46
|
+
pdf.line_to(xpos, ypos)
|
47
|
+
pdf.fill
|
48
|
+
end
|
49
|
+
xpos += (xdim*amount)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
pdf
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def default_options
|
60
|
+
@default_options ||= {
|
61
|
+
:margin => 5,
|
62
|
+
:height => 100,
|
63
|
+
:xdim => 1
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
def options(opts={})
|
68
|
+
doc_opts = opts.delete(:document) || {}
|
69
|
+
opts = default_options.merge(opts)
|
70
|
+
opts[:x] ||= opts[:margin]
|
71
|
+
opts[:y] ||= opts[:margin]
|
72
|
+
opts[:document] = document_options(opts, doc_opts)
|
73
|
+
opts
|
74
|
+
end
|
75
|
+
|
76
|
+
def document_options(opts, doc_opts)
|
77
|
+
o = doc_opts.dup
|
78
|
+
#o[:page_size] ||= page_size(opts[:xdim], opts[:height], opts[:margin])
|
79
|
+
#%w(left right top bottom).each{|s| o[:"#{s}_margin"] ||= opts[:margin] }
|
80
|
+
o[:page_size] ||= 'A4' #Prawn doesn't currently support custom page sizes
|
81
|
+
o
|
82
|
+
end
|
83
|
+
|
84
|
+
def page_size(xdim, height, margin)
|
85
|
+
[width(xdim,margin), height(height,margin)]
|
86
|
+
end
|
87
|
+
|
88
|
+
def width(xdim, margin)
|
89
|
+
(xdim * encoding.length) + (margin * 2)
|
90
|
+
end
|
91
|
+
|
92
|
+
def height(height, margin)
|
93
|
+
height + (margin * 2)
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|