arena_barby 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README +29 -0
  2. data/bin/barby +41 -0
  3. data/lib/barby.rb +17 -0
  4. data/lib/barby/barcode.rb +116 -0
  5. data/lib/barby/barcode/bookland.rb +37 -0
  6. data/lib/barby/barcode/code_128.rb +410 -0
  7. data/lib/barby/barcode/code_25.rb +193 -0
  8. data/lib/barby/barcode/code_25_iata.rb +23 -0
  9. data/lib/barby/barcode/code_25_interleaved.rb +73 -0
  10. data/lib/barby/barcode/code_39.rb +233 -0
  11. data/lib/barby/barcode/code_93.rb +230 -0
  12. data/lib/barby/barcode/ean_13.rb +178 -0
  13. data/lib/barby/barcode/ean_8.rb +32 -0
  14. data/lib/barby/barcode/gs1_128.rb +42 -0
  15. data/lib/barby/barcode/pdf_417.rb +76 -0
  16. data/lib/barby/barcode/qr_code.rb +101 -0
  17. data/lib/barby/outputter.rb +127 -0
  18. data/lib/barby/outputter/ascii_outputter.rb +41 -0
  19. data/lib/barby/outputter/cairo_outputter.rb +185 -0
  20. data/lib/barby/outputter/pdfwriter_outputter.rb +83 -0
  21. data/lib/barby/outputter/png_outputter.rb +97 -0
  22. data/lib/barby/outputter/prawn_outputter.rb +99 -0
  23. data/lib/barby/outputter/rmagick_outputter.rb +126 -0
  24. data/lib/barby/outputter/svg_outputter.rb +225 -0
  25. data/lib/barby/vendor.rb +3 -0
  26. data/lib/barby/version.rb +9 -0
  27. data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.jar +0 -0
  28. data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.java +1471 -0
  29. data/vendor/rqrcode/CHANGELOG +29 -0
  30. data/vendor/rqrcode/COPYING +19 -0
  31. data/vendor/rqrcode/README +98 -0
  32. data/vendor/rqrcode/Rakefile +65 -0
  33. data/vendor/rqrcode/lib/rqrcode.rb +13 -0
  34. data/vendor/rqrcode/lib/rqrcode/core_ext.rb +5 -0
  35. data/vendor/rqrcode/lib/rqrcode/core_ext/array.rb +5 -0
  36. data/vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb +9 -0
  37. data/vendor/rqrcode/lib/rqrcode/core_ext/integer.rb +5 -0
  38. data/vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb +11 -0
  39. data/vendor/rqrcode/lib/rqrcode/qrcode.rb +4 -0
  40. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb +37 -0
  41. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb +56 -0
  42. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb +421 -0
  43. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb +63 -0
  44. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb +78 -0
  45. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb +313 -0
  46. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb +254 -0
  47. data/vendor/rqrcode/test/runtest.rb +78 -0
  48. data/vendor/rqrcode/test/test_data.rb +21 -0
  49. metadata +114 -0
@@ -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 '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 = PNG::Canvas.new(full_width, full_height, PNG::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] = PNG::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
+ PNG.new(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
@@ -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('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
219
+ end
220
+
221
+
222
+ end
223
+
224
+
225
+ end