barby 0.6.2 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/CHANGELOG +8 -0
  4. data/Gemfile +5 -0
  5. data/README.md +108 -0
  6. data/Rakefile +15 -0
  7. data/barby.gemspec +31 -0
  8. data/lib/barby/barcode.rb +3 -3
  9. data/lib/barby/barcode/bookland.rb +0 -1
  10. data/lib/barby/barcode/codabar.rb +82 -0
  11. data/lib/barby/barcode/code_128.rb +22 -20
  12. data/lib/barby/barcode/code_25.rb +6 -3
  13. data/lib/barby/barcode/code_25_interleaved.rb +2 -2
  14. data/lib/barby/barcode/code_39.rb +2 -1
  15. data/lib/barby/barcode/data_matrix.rb +1 -1
  16. data/lib/barby/barcode/ean_13.rb +1 -1
  17. data/lib/barby/barcode/gs1_128.rb +5 -1
  18. data/lib/barby/barcode/qr_code.rb +2 -1
  19. data/lib/barby/outputter.rb +1 -1
  20. data/lib/barby/outputter/cairo_outputter.rb +16 -3
  21. data/lib/barby/outputter/png_outputter.rb +43 -7
  22. data/lib/barby/outputter/prawn_outputter.rb +12 -4
  23. data/lib/barby/outputter/rmagick_outputter.rb +43 -11
  24. data/lib/barby/outputter/svg_outputter.rb +11 -13
  25. data/lib/barby/version.rb +2 -2
  26. data/test/barcodes.rb +20 -0
  27. data/test/bookland_test.rb +54 -0
  28. data/test/codabar_test.rb +58 -0
  29. data/test/code_128_test.rb +470 -0
  30. data/test/code_25_iata_test.rb +19 -0
  31. data/test/code_25_interleaved_test.rb +116 -0
  32. data/test/code_25_test.rb +110 -0
  33. data/test/code_39_test.rb +210 -0
  34. data/test/code_93_test.rb +144 -0
  35. data/test/data_matrix_test.rb +30 -0
  36. data/test/ean13_test.rb +169 -0
  37. data/test/ean8_test.rb +100 -0
  38. data/test/outputter/cairo_outputter_test.rb +129 -0
  39. data/test/outputter/html_outputter_test.rb +68 -0
  40. data/test/outputter/pdfwriter_outputter_test.rb +37 -0
  41. data/test/outputter/png_outputter_test.rb +49 -0
  42. data/test/outputter/prawn_outputter_test.rb +79 -0
  43. data/test/outputter/rmagick_outputter_test.rb +83 -0
  44. data/test/outputter/svg_outputter_test.rb +89 -0
  45. data/test/outputter_test.rb +134 -0
  46. data/test/pdf_417_test.rb +45 -0
  47. data/test/qr_code_test.rb +78 -0
  48. data/test/test_helper.rb +24 -0
  49. data/test/upc_supplemental_test.rb +109 -0
  50. metadata +160 -19
  51. data/README +0 -93
  52. data/lib/barby/outputter/point_matrix.rb +0 -211
@@ -129,7 +129,7 @@ module Barby
129
129
  def weighted_sum
130
130
  odds, evens = odd_and_even_numbers
131
131
  odds.map!{|n| n * 3 }
132
- sum = (odds+evens).inject(0){|s,n| s+n }
132
+ (odds+evens).inject(0){|s,n| s+n }
133
133
  end
134
134
 
135
135
  #Mod10
@@ -3,18 +3,22 @@ require 'barby/barcode/code_128'
3
3
  module Barby
4
4
 
5
5
 
6
+ #DEPRECATED - Use the Code128 class directly instead:
7
+ #
8
+ # Code128.new("#{Code128::FNC1}#{application_identifier}#{data}")
9
+ #
6
10
  #AKA EAN-128, UCC-128
7
11
  class GS1128 < Code128
8
12
 
9
13
  attr_accessor :application_identifier
10
14
 
11
15
  def initialize(data, type, ai)
16
+ warn "DEPRECATED: The GS1128 class has been deprecated, use Code128 directly instead (called from #{caller[0]})"
12
17
  self.application_identifier = ai
13
18
  super(data, type)
14
19
  end
15
20
 
16
21
 
17
- #TODO: Not sure this is entirely right
18
22
  def data
19
23
  FNC1+application_identifier+super
20
24
  end
@@ -41,6 +41,7 @@ module Barby
41
41
 
42
42
  def initialize(data, options={})
43
43
  self.data = data
44
+ @level, @size = nil
44
45
  options.each{|k,v| send("#{k}=", v) }
45
46
  raise(ArgumentError, "data too large") unless size
46
47
  end
@@ -70,7 +71,7 @@ module Barby
70
71
  return @size if @size
71
72
 
72
73
  level_index = LEVELS[level]
73
- length = data.length
74
+ length = data.bytesize
74
75
  found_size = nil
75
76
  SIZES.each do |size,max_values|
76
77
  if max_values[level_index] >= length
@@ -28,7 +28,7 @@ module Barby
28
28
  self.barcode = barcode
29
29
  end
30
30
 
31
-
31
+
32
32
  #Register one or more handler methods with this outputter
33
33
  #Barcodes will then be able to use these methods to get the output
34
34
  #from the outputter. For example, if you have an ImageOutputter,
@@ -4,9 +4,17 @@ require 'stringio'
4
4
 
5
5
  module Barby
6
6
 
7
- #Uses Cairo to render a barcode to a number of formats: PNG, PS, EPS, PDF and SVG
7
+ # Uses Cairo to render a barcode to a number of formats: PNG, PS, EPS, PDF and SVG
8
8
  #
9
- #Registers methods render_to_cairo_context, to_png, to_ps, to_eps, to_pdf and to_svg
9
+ # Registers methods render_to_cairo_context, to_png, to_ps, to_eps, to_pdf and to_svg
10
+ #
11
+ # Options:
12
+ #
13
+ # * xdim
14
+ # * ydim - (2D only)
15
+ # * height - (1D only)
16
+ # * foreground - Cairo::Color::Base or Cairo::Color.parse(value)
17
+ # * background - ^
10
18
  class CairoOutputter < Outputter
11
19
 
12
20
  register :render_to_cairo_context
@@ -23,6 +31,11 @@ module Barby
23
31
  attr_writer :x, :y, :xdim, :height, :margin
24
32
 
25
33
 
34
+ def initialize(*)
35
+ super
36
+ @x, @y, @xdim, @height, @margin = nil
37
+ end
38
+
26
39
  #Render the barcode onto a Cairo context
27
40
  def render_to_cairo_context(context, options={})
28
41
  if context.respond_to?(:have_current_point?) and
@@ -37,7 +50,7 @@ module Barby
37
50
  _height = height(options)
38
51
  original_current_x = current_x
39
52
  context.save do
40
- context.set_source_color(:black)
53
+ context.set_source_color(options[:foreground] || :black)
41
54
  context.fill do
42
55
  if barcode.two_dimensional?
43
56
  boolean_groups.each do |groups|
@@ -3,20 +3,30 @@ require 'chunky_png'
3
3
 
4
4
  module Barby
5
5
 
6
- #Renders the barcode to a PNG image using chunky_png (gem install chunky_png)
6
+ # Renders the barcode to a PNG image using chunky_png (gem install chunky_png)
7
+ #
8
+ # Registers the to_png, to_datastream and to_canvas methods
9
+ #
10
+ # Options:
11
+ #
12
+ # * xdim: X dimension - bar width [1]
13
+ # * ydim: Y dimension - bar height (2D only) [1]
14
+ # * height: Height of bars (1D only) [100]
15
+ # * margin: Size of margin around barcode [0]
16
+ # * foreground: Foreground color (see ChunkyPNG::Color - can be HTML color name) [black]
17
+ # * background: Background color (see ChunkyPNG::Color - can be HTML color name) [white]
7
18
  #
8
- #Registers the to_png, to_datastream and to_canvas methods
9
19
  class PngOutputter < Outputter
10
20
 
11
21
  register :to_png, :to_image, :to_datastream
12
22
 
13
- attr_accessor :xdim, :ydim, :width, :height, :margin
23
+ attr_writer :xdim, :ydim, :height, :margin
14
24
 
15
25
 
16
- #Creates a PNG::Canvas object and renders the barcode on it
26
+ #Creates a ChunkyPNG::Image object and renders the barcode on it
17
27
  def to_image(opts={})
18
28
  with_options opts do
19
- canvas = ChunkyPNG::Image.new(full_width, full_height, ChunkyPNG::Color::WHITE)
29
+ canvas = ChunkyPNG::Image.new(full_width, full_height, background)
20
30
 
21
31
  if barcode.two_dimensional?
22
32
  x, y = margin, margin
@@ -25,7 +35,7 @@ module Barby
25
35
  if bar
26
36
  x.upto(x+(xdim-1)) do |xx|
27
37
  y.upto y+(ydim-1) do |yy|
28
- canvas[xx,yy] = ChunkyPNG::Color::BLACK
38
+ canvas[xx,yy] = foreground
29
39
  end
30
40
  end
31
41
  end
@@ -40,7 +50,7 @@ module Barby
40
50
  if bar
41
51
  x.upto(x+(xdim-1)) do |xx|
42
52
  y.upto y+(height-1) do |yy|
43
- canvas[xx,yy] = ChunkyPNG::Color::BLACK
53
+ canvas[xx,yy] = foreground
44
54
  end
45
55
  end
46
56
  end
@@ -97,6 +107,32 @@ module Barby
97
107
  @margin || 10
98
108
  end
99
109
 
110
+
111
+ def background=(c)
112
+ @background = if c.is_a?(String) || c.is_a?(Symbol)
113
+ ChunkyPNG::Color.html_color(c.to_sym)
114
+ else
115
+ c
116
+ end
117
+ end
118
+
119
+ def background
120
+ @background || ChunkyPNG::Color::WHITE
121
+ end
122
+
123
+ def foreground=(c)
124
+ @foreground = if c.is_a?(String) || c.is_a?(Symbol)
125
+ ChunkyPNG::Color.html_color(c.to_sym)
126
+ else
127
+ c
128
+ end
129
+ end
130
+
131
+ def foreground
132
+ @foreground || ChunkyPNG::Color::BLACK
133
+ end
134
+
135
+
100
136
  def length
101
137
  barcode.two_dimensional? ? encoding.first.length : encoding.length
102
138
  end
@@ -7,8 +7,7 @@ module Barby
7
7
 
8
8
  register :to_pdf, :annotate_pdf
9
9
 
10
- attr_accessor :xdim, :ydim, :x, :y, :height, :margin, :unbleed
11
-
10
+ attr_writer :xdim, :ydim, :x, :y, :height, :margin, :unbleed, :color
12
11
 
13
12
  def to_pdf(opts={})
14
13
  doc_opts = opts.delete(:document) || {}
@@ -21,6 +20,9 @@ module Barby
21
20
  with_options opts do
22
21
  xpos, ypos = x, y
23
22
  orig_xpos = xpos
23
+ orig_color = pdf.fill_color
24
+
25
+ pdf.fill_color = color if color
24
26
 
25
27
  if barcode.two_dimensional?
26
28
  boolean_groups.reverse_each do |groups|
@@ -50,9 +52,11 @@ module Barby
50
52
  end
51
53
  xpos += (xdim*amount)
52
54
  end
53
- end
55
+ end#if
56
+
57
+ pdf.fill_color = orig_color
54
58
 
55
- end
59
+ end#with_options
56
60
 
57
61
  pdf
58
62
  end
@@ -108,6 +112,10 @@ module Barby
108
112
  @unbleed || 0
109
113
  end
110
114
 
115
+ def color
116
+ @color
117
+ end
118
+
111
119
 
112
120
  private
113
121
 
@@ -1,17 +1,26 @@
1
1
  require 'barby/outputter'
2
- require 'RMagick'
2
+ require 'rmagick'
3
3
 
4
4
  module Barby
5
5
 
6
6
 
7
- #Renders images from barcodes using RMagick
7
+ # Renders images from barcodes using RMagick
8
8
  #
9
- #Registers the to_png, to_gif, to_jpg and to_image methods
9
+ # Registers the to_png, to_gif, to_jpg and to_image methods
10
+ #
11
+ # Options:
12
+ #
13
+ # * xdim -
14
+ # * ydim - 2D only
15
+ # * height - 1D only
16
+ # * margin -
17
+ # * foreground - RMagick "colorspec"
18
+ # * background - ^
10
19
  class RmagickOutputter < Outputter
11
-
20
+
12
21
  register :to_png, :to_gif, :to_jpg, :to_image
13
22
 
14
- attr_accessor :height, :xdim, :ydim, :margin
23
+ attr_writer :height, :xdim, :ydim, :margin, :foreground, :background
15
24
 
16
25
 
17
26
  #Returns a string containing a PNG image
@@ -28,23 +37,25 @@ module Barby
28
37
  def to_jpg(*a)
29
38
  to_blob('jpg', *a)
30
39
  end
31
-
40
+
32
41
  def to_blob(format, *a)
33
42
  img = to_image(*a)
34
43
  blob = img.to_blob{|i| i.format = format }
35
-
44
+
36
45
  #Release the memory used by RMagick explicitly. Ruby's GC
37
46
  #isn't aware of it and can't clean it up automatically
38
47
  img.destroy! if img.respond_to?(:destroy!)
39
-
48
+
40
49
  blob
41
50
  end
42
51
 
43
52
  #Returns an instance of Magick::Image
44
53
  def to_image(opts={})
45
54
  with_options opts do
46
- canvas = Magick::Image.new(full_width, full_height)
55
+ b = background #Capture locally because Magick::Image.new block uses instance_eval
56
+ canvas = Magick::Image.new(full_width, full_height){ self.background_color = b }
47
57
  bars = Magick::Draw.new
58
+ bars.fill = foreground
48
59
 
49
60
  x1 = margin
50
61
  y1 = margin
@@ -59,7 +70,14 @@ module Barby
59
70
  if x1 == x2 && y1 == y2
60
71
  bars.point(x1,y1)
61
72
  else
62
- bars.rectangle(x1, y1, x2, y2)
73
+ # For single pixel lines, use line
74
+ if x1 == x2
75
+ bars.line(x1, y1, x1, y2)
76
+ elsif y1 == y2
77
+ bars.line(x1, y1, x2, y1)
78
+ else
79
+ bars.rectangle(x1, y1, x2, y2)
80
+ end
63
81
  end
64
82
  end
65
83
  x1 += xdim
@@ -72,7 +90,12 @@ module Barby
72
90
  if bar
73
91
  x2 = x1+(xdim-1)
74
92
  y2 = y1+(height-1)
75
- bars.rectangle(x1, y1, x2, y2)
93
+ # For single pixel width, use line
94
+ if x1 == x2
95
+ bars.line(x1, y1, x1, y2)
96
+ else
97
+ bars.rectangle(x1, y1, x2, y2)
98
+ end
76
99
  end
77
100
  x1 += xdim
78
101
  end
@@ -129,6 +152,15 @@ module Barby
129
152
  end
130
153
 
131
154
 
155
+ def foreground
156
+ @foreground || 'black'
157
+ end
158
+
159
+ def background
160
+ @background || 'white'
161
+ end
162
+
163
+
132
164
  end
133
165
 
134
166
 
@@ -19,7 +19,7 @@ module Barby
19
19
 
20
20
  register :to_svg, :bars_to_rects, :bars_to_path
21
21
 
22
- attr_writer :title, :xdim, :ydim, :height, :rmargin, :lmargin, :tmargin, :bmargin, :xmargin, :ymargin, :margin
22
+ attr_writer :title, :xdim, :ydim, :height, :rmargin, :lmargin, :tmargin, :bmargin, :xmargin, :ymargin, :margin, :foreground, :background
23
23
 
24
24
 
25
25
  def to_svg(opts={})
@@ -34,11 +34,11 @@ module Barby
34
34
 
35
35
  <<-"EOT"
36
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">
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" preserveAspectRatio="none" >
38
38
  <title>#{escape title}</title>
39
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">
40
+ <rect x="0" y="0" width="#{full_width}px" height="#{full_height}px" fill="#{background}" />
41
+ <g id="barcode" fill="#{foreground}">
42
42
  #{bars}
43
43
  </g></g>
44
44
  </svg>
@@ -46,7 +46,6 @@ EOT
46
46
  end
47
47
  end
48
48
 
49
-
50
49
  def bars_to_rects(opts={})
51
50
  rects = ''
52
51
  with_options opts do
@@ -80,14 +79,12 @@ EOT
80
79
  rects
81
80
  end
82
81
 
83
-
84
82
  def bars_to_path(opts={})
85
83
  with_options opts do
86
84
  %Q|<path stroke="black" stroke-width="#{xdim}" d="#{bars_to_path_data(opts)}" />|
87
85
  end
88
86
  end
89
87
 
90
-
91
88
  def bars_to_path_data(opts={})
92
89
  path_data = ''
93
90
  with_options opts do
@@ -119,12 +116,10 @@ EOT
119
116
  path_data
120
117
  end
121
118
 
122
-
123
119
  def title
124
120
  @title || barcode.to_s
125
121
  end
126
122
 
127
-
128
123
  def width
129
124
  length * xdim
130
125
  end
@@ -184,6 +179,13 @@ EOT
184
179
  barcode.two_dimensional? ? encoding.first.length : encoding.length
185
180
  end
186
181
 
182
+ def foreground
183
+ @foreground || '#000'
184
+ end
185
+
186
+ def background
187
+ @background || '#fff'
188
+ end
187
189
 
188
190
  def svg_width(opts={})
189
191
  opts[:rot] ? full_height : full_width
@@ -193,12 +195,10 @@ EOT
193
195
  opts[:rot] ? full_width : full_height
194
196
  end
195
197
 
196
-
197
198
  def transform(opts={})
198
199
  opts[:rot] ? %Q|transform="rotate(-90) translate(-#{full_width}, 0)"| : nil
199
200
  end
200
201
 
201
-
202
202
  private
203
203
 
204
204
  def _xmargin
@@ -218,8 +218,6 @@ EOT
218
218
  str.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
219
219
  end
220
220
 
221
-
222
221
  end
223
222
 
224
-
225
223
  end
@@ -2,8 +2,8 @@ module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 2
5
+ TINY = 8
6
6
 
7
- STRING = [MAJOR, MINOR, TINY].join('.')
7
+ STRING = [MAJOR, MINOR, TINY].join('.').freeze
8
8
  end
9
9
  end
@@ -0,0 +1,20 @@
1
+ require 'code_128_test'
2
+
3
+ require 'code_25_test'
4
+ require 'code_25_interleaved_test'
5
+ require 'code_25_iata_test'
6
+
7
+ require 'code_39_test'
8
+ require 'code_93_test'
9
+
10
+ require 'codabar_test'
11
+
12
+ require 'ean13_test'
13
+ require 'ean8_test'
14
+ require 'bookland_test'
15
+ require 'upc_supplemental_test'
16
+
17
+ require 'data_matrix_test'
18
+ require 'qr_code_test'
19
+
20
+ #require 'pdf_417_test'