barby 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require 'semacode'
2
+ require 'barby/barcode'
3
+
4
+ module Barby
5
+
6
+
7
+ #Uses the semacode library (gem install semacode) to encode DataMatrix barcodes
8
+ class DataMatrix < Barcode2D
9
+
10
+ attr_accessor :data
11
+
12
+
13
+ def initialize(data)
14
+ self.data = data
15
+ end
16
+
17
+
18
+ def data=(data)
19
+ @data = data
20
+ @encoder = nil
21
+ end
22
+
23
+ def encoder
24
+ @encoder ||= ::DataMatrix::Encoder.new(data)
25
+ end
26
+
27
+
28
+ def encoding
29
+ encoder.data.map{|a| a.map{|b| b ? '1' : '0' }.join }
30
+ end
31
+
32
+
33
+ def semacode?
34
+ #TODO: Not sure if this is right
35
+ data =~ /^http:\/\//
36
+ end
37
+
38
+
39
+ def to_s
40
+ data
41
+ end
42
+
43
+
44
+ end
45
+
46
+ end
@@ -60,13 +60,18 @@ module Barby
60
60
 
61
61
  private
62
62
 
63
+ def two_dimensional?
64
+ barcode.respond_to?(:two_dimensional?) && barcode.two_dimensional?
65
+ end
66
+
67
+
63
68
  #Converts the barcode's encoding (a string containing 1s and 0s)
64
69
  #to true and false values (1 == true == "black bar")
65
70
  #
66
71
  #If the barcode is 2D, each line will be converted to an array
67
72
  #in the same way
68
73
  def booleans(reload=false)#:doc:
69
- if barcode.two_dimensional?
74
+ if two_dimensional?
70
75
  encoding(reload).map{|l| l.split(//).map{|c| c == '1' } }
71
76
  else
72
77
  encoding(reload).split(//).map{|c| c == '1' }
@@ -88,7 +93,7 @@ module Barby
88
93
  #
89
94
  #For example, "1100111000" becomes [[true,2],[false,2],[true,3],[false,3]]
90
95
  def boolean_groups(reload=false)
91
- if barcode.two_dimensional?
96
+ if two_dimensional?
92
97
  encoding(reload).map do |line|
93
98
  line.scan(/1+|0+/).map do |group|
94
99
  [group[0,1] == '1', group.size]
@@ -102,6 +107,9 @@ module Barby
102
107
  end
103
108
 
104
109
 
110
+ #Takes a hash and temporarily sets properties on self (the outputter object)
111
+ #corresponding with the keys to their values. When the block exits, the
112
+ #properties are reset to their original values. Returns whatever the block returns.
105
113
  def with_options(options={})
106
114
  original_options = options.inject({}) do |origs,pair|
107
115
  if respond_to?(pair.first) && respond_to?("#{pair.first}=")
@@ -7,90 +7,112 @@ 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
12
 
11
13
  def to_pdf(opts={})
12
- opts = options(opts)
13
- annotate_pdf(Prawn::Document.new(opts[:document]), opts).render
14
+ doc_opts = opts.delete(:document) || {}
15
+ doc_opts[:page_size] ||= 'A4'
16
+ annotate_pdf(Prawn::Document.new(doc_opts), opts).render
14
17
  end
15
18
 
16
19
 
17
20
  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|
21
+ with_options opts do
22
+ xpos, ypos = x, y
23
+ orig_xpos = xpos
24
+
25
+ if barcode.two_dimensional?
26
+ boolean_groups.reverse_each do |groups|
27
+ groups.each do |bar,amount|
28
+ if bar
29
+ pdf.move_to(xpos+unbleed, ypos+unbleed)
30
+ pdf.line_to(xpos+unbleed, ypos+ydim-unbleed)
31
+ pdf.line_to(xpos+(xdim*amount)-unbleed, ypos+ydim-unbleed)
32
+ pdf.line_to(xpos+(xdim*amount)-unbleed, ypos+unbleed)
33
+ pdf.line_to(xpos+unbleed, ypos+unbleed)
34
+ pdf.fill
35
+ end
36
+ xpos += (xdim*amount)
37
+ end
38
+ xpos = orig_xpos
39
+ ypos += ydim
40
+ end
41
+ else
42
+ boolean_groups.each do |bar,amount|
26
43
  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)
44
+ pdf.move_to(xpos+unbleed, ypos)
45
+ pdf.line_to(xpos+unbleed, ypos+height)
46
+ pdf.line_to(xpos+(xdim*amount)-unbleed, ypos+height)
47
+ pdf.line_to(xpos+(xdim*amount)-unbleed, ypos)
48
+ pdf.line_to(xpos+unbleed, ypos)
32
49
  pdf.fill
33
50
  end
34
51
  xpos += (xdim*amount)
35
52
  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
53
  end
54
+
51
55
  end
52
56
 
53
57
  pdf
54
58
  end
55
59
 
56
60
 
57
- private
61
+ def length
62
+ two_dimensional? ? encoding.first.length : encoding.length
63
+ end
58
64
 
59
- def default_options
60
- @default_options ||= {
61
- :margin => 5,
62
- :height => 100,
63
- :xdim => 1
64
- }
65
+ def width
66
+ length * xdim
65
67
  end
66
68
 
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
69
+ def height
70
+ two_dimensional? ? (ydim * encoding.length) : (@height || 50)
74
71
  end
75
72
 
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
73
+ def full_width
74
+ width + (margin * 2)
82
75
  end
83
76
 
84
- def page_size(xdim, height, margin)
85
- [width(xdim,margin), height(height,margin)]
77
+ def full_height
78
+ height + (margin * 2)
86
79
  end
87
80
 
88
- def width(xdim, margin)
89
- (xdim * encoding.length) + (margin * 2)
81
+ #Margin is used for x and y if not given explicitly, effectively placing the barcode
82
+ #<margin> points from the [left,bottom] of the page.
83
+ #If you define x and y, there will be no margin. And if you don't define margin, it's 0.
84
+ def margin
85
+ @margin || 0
90
86
  end
91
87
 
92
- def height(height, margin)
93
- height + (margin * 2)
88
+ def x
89
+ @x || margin
90
+ end
91
+
92
+ def y
93
+ @y || margin
94
+ end
95
+
96
+ def xdim
97
+ @xdim || 1
98
+ end
99
+
100
+ def ydim
101
+ @ydim || xdim
102
+ end
103
+
104
+ #Defines an amount to reduce black bars/squares by to account for "ink bleed"
105
+ #If xdim = 3, unbleed = 0.2, a single/width black bar will be 2.6 wide
106
+ #For 2D, both x and y dimensions are reduced.
107
+ def unbleed
108
+ @unbleed || 0
109
+ end
110
+
111
+
112
+ private
113
+
114
+ def page_size(xdim, height, margin)
115
+ [width(xdim,margin), height(height,margin)]
94
116
  end
95
117
 
96
118
 
@@ -1,8 +1,8 @@
1
1
  module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 3
5
- TINY = 2
4
+ MINOR = 4
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,19 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Tore Darell
8
- autorequire: barby
14
+ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-11-26 00:00:00 +01:00
18
+ date: 2010-08-18 00:00:00 +02:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
16
- description:
22
+ description: Barby creates barcodes.
17
23
  email: toredarell@gmail.com
18
24
  executables: []
19
25
 
@@ -22,53 +28,54 @@ extensions: []
22
28
  extra_rdoc_files:
23
29
  - README
24
30
  files:
25
- - lib/barby/vendor.rb
31
+ - lib/barby/barcode/bookland.rb
32
+ - lib/barby/barcode/code_128.rb
33
+ - lib/barby/barcode/code_25.rb
34
+ - lib/barby/barcode/code_25_iata.rb
35
+ - lib/barby/barcode/code_25_interleaved.rb
36
+ - lib/barby/barcode/code_39.rb
37
+ - lib/barby/barcode/code_93.rb
38
+ - lib/barby/barcode/data_matrix.rb
39
+ - lib/barby/barcode/ean_13.rb
40
+ - lib/barby/barcode/ean_8.rb
41
+ - lib/barby/barcode/gs1_128.rb
42
+ - lib/barby/barcode/pdf_417.rb
43
+ - lib/barby/barcode/qr_code.rb
26
44
  - lib/barby/barcode.rb
27
- - lib/barby/version.rb
28
- - lib/barby/outputter.rb
29
- - lib/barby/outputter/pdfwriter_outputter.rb
30
- - lib/barby/outputter/rmagick_outputter.rb
31
45
  - lib/barby/outputter/ascii_outputter.rb
46
+ - lib/barby/outputter/cairo_outputter.rb
47
+ - lib/barby/outputter/pdfwriter_outputter.rb
32
48
  - lib/barby/outputter/png_outputter.rb
33
49
  - lib/barby/outputter/prawn_outputter.rb
34
- - lib/barby/outputter/cairo_outputter.rb
50
+ - lib/barby/outputter/rmagick_outputter.rb
35
51
  - lib/barby/outputter/svg_outputter.rb
36
- - lib/barby/barcode/pdf_417.rb
37
- - lib/barby/barcode/code_25.rb
38
- - lib/barby/barcode/ean_13.rb
39
- - lib/barby/barcode/code_93.rb
40
- - lib/barby/barcode/ean_8.rb
41
- - lib/barby/barcode/qr_code.rb
42
- - lib/barby/barcode/code_25_interleaved.rb
43
- - lib/barby/barcode/code_39.rb
44
- - lib/barby/barcode/bookland.rb
45
- - lib/barby/barcode/code_128.rb
46
- - lib/barby/barcode/code_25_iata.rb
47
- - lib/barby/barcode/gs1_128.rb
52
+ - lib/barby/outputter.rb
53
+ - lib/barby/vendor.rb
54
+ - lib/barby/version.rb
48
55
  - lib/barby.rb
49
56
  - bin/barby
50
57
  - vendor/Pdf417lib-java-0.91/lib/Pdf417lib.jar
51
58
  - vendor/Pdf417lib-java-0.91/lib/Pdf417lib.java
52
- - vendor/rqrcode/README
53
- - vendor/rqrcode/Rakefile
59
+ - vendor/rqrcode/CHANGELOG
54
60
  - vendor/rqrcode/COPYING
55
- - vendor/rqrcode/lib/rqrcode/qrcode.rb
56
- - vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb
61
+ - vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb
62
+ - vendor/rqrcode/lib/rqrcode/core_ext/array.rb
63
+ - vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb
64
+ - vendor/rqrcode/lib/rqrcode/core_ext/integer.rb
65
+ - vendor/rqrcode/lib/rqrcode/core_ext.rb
66
+ - vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb
57
67
  - vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb
58
- - vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb
59
68
  - vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb
69
+ - vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb
60
70
  - vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb
71
+ - vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb
61
72
  - vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb
62
- - vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb
63
- - vendor/rqrcode/lib/rqrcode/core_ext.rb
64
- - vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb
65
- - vendor/rqrcode/lib/rqrcode/core_ext/integer.rb
66
- - vendor/rqrcode/lib/rqrcode/core_ext/array.rb
67
- - vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb
73
+ - vendor/rqrcode/lib/rqrcode/qrcode.rb
68
74
  - vendor/rqrcode/lib/rqrcode.rb
69
- - vendor/rqrcode/test/test_data.rb
75
+ - vendor/rqrcode/Rakefile
76
+ - vendor/rqrcode/README
70
77
  - vendor/rqrcode/test/runtest.rb
71
- - vendor/rqrcode/CHANGELOG
78
+ - vendor/rqrcode/test/test_data.rb
72
79
  - README
73
80
  has_rdoc: true
74
81
  homepage: http://toretore.github.com/barby
@@ -80,23 +87,29 @@ rdoc_options: []
80
87
  require_paths:
81
88
  - lib
82
89
  required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
83
91
  requirements:
84
92
  - - ">="
85
93
  - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
86
97
  version: "0"
87
- version:
88
98
  required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
89
100
  requirements:
90
101
  - - ">="
91
102
  - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
92
106
  version: "0"
93
- version:
94
107
  requirements: []
95
108
 
96
109
  rubyforge_project: barby
97
- rubygems_version: 1.3.5
110
+ rubygems_version: 1.3.7
98
111
  signing_key:
99
112
  specification_version: 3
100
- summary: A pure-Ruby barcode generator
113
+ summary: The Ruby barcode generator
101
114
  test_files: []
102
115