barby 0.6.7 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9930672e6c6e3b86ec968b0185c698a37e00567d
4
- data.tar.gz: ec20ca147ea99a0d0543c3428d4cd4dbf4058ee0
2
+ SHA256:
3
+ metadata.gz: 64d74091ea1f3aa79457c34878fc3b0d6f4c805dbb985d12eb372f29a7837e36
4
+ data.tar.gz: a8a61c1af0a401a1b36bc675653d2063e569443d434a57b5a2d95c7abfc1d193
5
5
  SHA512:
6
- metadata.gz: c5ce490db8fc1212d5faffc15016c86fa49cc93248109d2aa1bcd4dc255283de6cfdabc94ac4fd5beb28ec2f45a0279105d0019161be07c0f4f8e24b8f193ed6
7
- data.tar.gz: 84f578fcfc64bafd2d6b224b8d8953ea96b69afbb9b9db1f5bcb69ec19ef34300d9ada7145be6521bd2b1618f6fb51c60bfd10c815863d8044584cc5ccbab6c4
6
+ metadata.gz: 99143c0bf41695fa1416956073bcb6c9d7257c677fa36337638f890681665bacd51c4b04cd3412008592c8f23e4130fec74363eb425c776f8c827bf360d07c5b
7
+ data.tar.gz: d1b1f6aa2a63484b00f79bd184bef605b75850152dd54600b7eb25434f114c45c6994ce725af7535edefb8a536fc93f3e42c7d4bd2547c6f88d8da90d0947f8f
@@ -0,0 +1,2 @@
1
+ *.swp
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,15 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs = ['lib','test']
7
+ t.test_files = Dir.glob("test/**/*_test.rb").sort
8
+ t.verbose = true
9
+ end
10
+
11
+ RDoc::Task.new do |rdoc|
12
+ rdoc.main = "README"
13
+ rdoc.rdoc_files.include("README", "lib")
14
+ rdoc.rdoc_dir = 'doc'
15
+ end
@@ -0,0 +1,31 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "barby/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "barby"
6
+ s.version = Barby::VERSION::STRING
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "The Ruby barcode generator"
9
+ s.email = "toredarell@gmail.com"
10
+ s.homepage = "http://toretore.github.com/barby"
11
+ s.description = "Barby creates barcodes."
12
+ s.authors = ['Tore Darell']
13
+
14
+ s.rubyforge_project = "barby"
15
+
16
+ s.extra_rdoc_files = ["README.md"]
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.test_files.delete("test/outputter/rmagick_outputter_test.rb")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_development_dependency "minitest", "~> 5.11"
25
+ s.add_development_dependency "bundler", "~> 1.16"
26
+ s.add_development_dependency "rake", "~> 10.0"
27
+ s.add_development_dependency "semacode-ruby19", "~> 0.7"
28
+ s.add_development_dependency "rqrcode", "~> 0.10"
29
+ s.add_development_dependency "prawn", "~> 2.2"
30
+ s.add_development_dependency "cairo", "~> 1.15"
31
+ end
@@ -17,7 +17,7 @@ module Barby
17
17
  # '101100111000111100001'
18
18
  # end
19
19
  # end
20
- #
20
+ #
21
21
  # require 'barby/outputter/ascii_outputter'
22
22
  # puts StaticBarcode.new.to_ascii(:height => 3)
23
23
  #
@@ -33,8 +33,8 @@ module Barby
33
33
  # end
34
34
  # end
35
35
  class Barcode
36
-
37
-
36
+
37
+
38
38
  #Every barcode must have an encoding method. This method returns
39
39
  #a string containing a series of 1 and 0, representing bars and
40
40
  #spaces. One digit is the width of one "module" or X dimension.
@@ -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
@@ -42,7 +50,7 @@ module Barby
42
50
  _height = height(options)
43
51
  original_current_x = current_x
44
52
  context.save do
45
- context.set_source_color(:black)
53
+ context.set_source_color(options[:foreground] || :black)
46
54
  context.fill do
47
55
  if barcode.two_dimensional?
48
56
  boolean_groups.each do |groups|
@@ -3,24 +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_writer :xdim, :ydim, :width, :height, :margin
23
+ attr_writer :xdim, :ydim, :height, :margin
14
24
 
15
- def initialize(*)
16
- super
17
- @xdim, @height, @margin = nil
18
- end
19
25
 
20
- #Creates a PNG::Canvas object and renders the barcode on it
26
+ #Creates a ChunkyPNG::Image object and renders the barcode on it
21
27
  def to_image(opts={})
22
28
  with_options opts do
23
- canvas = ChunkyPNG::Image.new(full_width, full_height, ChunkyPNG::Color::WHITE)
29
+ canvas = ChunkyPNG::Image.new(full_width, full_height, background)
24
30
 
25
31
  if barcode.two_dimensional?
26
32
  x, y = margin, margin
@@ -29,7 +35,7 @@ module Barby
29
35
  if bar
30
36
  x.upto(x+(xdim-1)) do |xx|
31
37
  y.upto y+(ydim-1) do |yy|
32
- canvas[xx,yy] = ChunkyPNG::Color::BLACK
38
+ canvas[xx,yy] = foreground
33
39
  end
34
40
  end
35
41
  end
@@ -44,7 +50,7 @@ module Barby
44
50
  if bar
45
51
  x.upto(x+(xdim-1)) do |xx|
46
52
  y.upto y+(height-1) do |yy|
47
- canvas[xx,yy] = ChunkyPNG::Color::BLACK
53
+ canvas[xx,yy] = foreground
48
54
  end
49
55
  end
50
56
  end
@@ -101,6 +107,32 @@ module Barby
101
107
  @margin || 10
102
108
  end
103
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
+
104
136
  def length
105
137
  barcode.two_dimensional? ? encoding.first.length : encoding.length
106
138
  end
@@ -7,13 +7,7 @@ module Barby
7
7
 
8
8
  register :to_pdf, :annotate_pdf
9
9
 
10
- attr_writer :xdim, :ydim, :x, :y, :height, :margin, :unbleed
11
-
12
-
13
- def initialize(*)
14
- super
15
- @xdim, @ydim, @x, @y, @height, @margin, @unbleed = nil
16
- end
10
+ attr_writer :xdim, :ydim, :x, :y, :height, :margin, :unbleed, :color
17
11
 
18
12
  def to_pdf(opts={})
19
13
  doc_opts = opts.delete(:document) || {}
@@ -26,6 +20,9 @@ module Barby
26
20
  with_options opts do
27
21
  xpos, ypos = x, y
28
22
  orig_xpos = xpos
23
+ orig_color = pdf.fill_color
24
+
25
+ pdf.fill_color = color if color
29
26
 
30
27
  if barcode.two_dimensional?
31
28
  boolean_groups.reverse_each do |groups|
@@ -55,9 +52,11 @@ module Barby
55
52
  end
56
53
  xpos += (xdim*amount)
57
54
  end
58
- end
55
+ end#if
59
56
 
60
- end
57
+ pdf.fill_color = orig_color
58
+
59
+ end#with_options
61
60
 
62
61
  pdf
63
62
  end
@@ -113,6 +112,10 @@ module Barby
113
112
  @unbleed || 0
114
113
  end
115
114
 
115
+ def color
116
+ @color
117
+ end
118
+
116
119
 
117
120
  private
118
121
 
@@ -4,21 +4,25 @@ require 'rmagick'
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_writer :height, :xdim, :ydim, :margin
23
+ attr_writer :height, :xdim, :ydim, :margin, :foreground, :background
15
24
 
16
25
 
17
- def initialize(*)
18
- super
19
- @height, @xdim, @ydim, @margin = nil
20
- end
21
-
22
26
  #Returns a string containing a PNG image
23
27
  def to_png(*a)
24
28
  to_blob('png', *a)
@@ -48,8 +52,10 @@ module Barby
48
52
  #Returns an instance of Magick::Image
49
53
  def to_image(opts={})
50
54
  with_options opts do
51
- 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 }
52
57
  bars = Magick::Draw.new
58
+ bars.fill = foreground
53
59
 
54
60
  x1 = margin
55
61
  y1 = margin
@@ -146,6 +152,15 @@ module Barby
146
152
  end
147
153
 
148
154
 
155
+ def foreground
156
+ @foreground || 'black'
157
+ end
158
+
159
+ def background
160
+ @background || 'white'
161
+ end
162
+
163
+
149
164
  end
150
165
 
151
166
 
@@ -19,12 +19,8 @@ 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
- def initialize(*)
25
- super
26
- @title, @xdim, @ydim, @height, @rmargin, @lmargin, @tmargin, @bmargin, @xmargin, @ymargin, @margin = nil
27
- end
28
24
 
29
25
  def to_svg(opts={})
30
26
  with_options opts do
@@ -41,8 +37,8 @@ module Barby
41
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" >
42
38
  <title>#{escape title}</title>
43
39
  <g id="canvas" #{transform(opts)}>
44
- <rect x="0" y="0" width="#{full_width}px" height="#{full_height}px" fill="white" />
45
- <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}">
46
42
  #{bars}
47
43
  </g></g>
48
44
  </svg>
@@ -183,6 +179,14 @@ EOT
183
179
  barcode.two_dimensional? ? encoding.first.length : encoding.length
184
180
  end
185
181
 
182
+ def foreground
183
+ @foreground || '#000'
184
+ end
185
+
186
+ def background
187
+ @background || '#fff'
188
+ end
189
+
186
190
  def svg_width(opts={})
187
191
  opts[:rot] ? full_height : full_width
188
192
  end
@@ -2,8 +2,8 @@ module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 7
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'
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/bookland'
3
+
4
+ class BooklandTest < Barby::TestCase
5
+
6
+ before do
7
+ # Gr Publ Tit Checksum
8
+ @isbn = '96-8261-240-3'
9
+ @code = Bookland.new(@isbn)
10
+ end
11
+
12
+ it "should have the expected data" do
13
+ @code.data.must_equal '978968261240'
14
+ end
15
+
16
+ it "should have the expected numbers" do
17
+ @code.numbers.must_equal [9,7,8,9,6,8,2,6,1,2,4,0]
18
+ end
19
+
20
+ it "should have the expected checksum" do
21
+ @code.checksum.must_equal 4
22
+ end
23
+
24
+ it "should raise an error when data not valid" do
25
+ lambda{ Bookland.new('1234') }.must_raise ArgumentError
26
+ end
27
+
28
+ describe 'ISBN conversion' do
29
+
30
+ it "should accept ISBN with number system and check digit" do
31
+ code = Bookland.new('978-82-92526-14-9')
32
+ assert code.valid?
33
+ code.data.must_equal '978829252614'
34
+ code = Bookland.new('979-82-92526-14-9')
35
+ assert code.valid?
36
+ code.data.must_equal '979829252614'
37
+ end
38
+
39
+ it "should accept ISBN without number system but with check digit" do
40
+ code = Bookland.new('82-92526-14-9')
41
+ assert code.valid?
42
+ code.data.must_equal '978829252614' #978 is the default prefix
43
+ end
44
+
45
+ it "should accept ISBN without number system or check digit" do
46
+ code = Bookland.new('82-92526-14')
47
+ assert code.valid?
48
+ code.data.must_equal '978829252614'
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+