barby 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ * 0.5.1
2
+
3
+ * Fix some encoding issues (ruby 2.0 & 1.9)
4
+ * Rewrite HtmlOutputter. Not entirely backwards compatible with the previous version.
5
+
6
+ * 0.5.0
7
+
8
+ * Require requirement of barcode symbologies in the same way outputters must be required before being used
9
+
1
10
  * 0.4.4
2
11
 
3
12
  * Use Gemfile for dependency management [Ken Collins]
data/README CHANGED
@@ -6,6 +6,8 @@ Full list of symbologies and filenames below.
6
6
 
7
7
  ***
8
8
 
9
+ For more information, check out the Barby wiki at https://github.com/toretore/barby/wiki
10
+
9
11
  Barby is a Ruby library that generates barcodes in a variety of symbologies.
10
12
  Its functionality is split into barcode and "outputter" objects. Barcode
11
13
  objects turn data into a binary representation for a given symbology.
@@ -1,3 +1,4 @@
1
+ #encoding: ASCII
1
2
  require 'barby/barcode/ean_13'
2
3
 
3
4
  module Barby
@@ -1,3 +1,4 @@
1
+ #encoding: ASCII
1
2
  require 'barby/barcode'
2
3
 
3
4
  module Barby
@@ -242,6 +243,8 @@ module Barby
242
243
  count = 0
243
244
  while count < chars.size
244
245
  if chars[count] =~ /^\d$/
246
+ #If encountering a digit, next char/byte *must* be second digit in pair. I.e. if chars[count] is 5,
247
+ #chars[count+1] must be /[0-9]/, otherwise it's not valid
245
248
  result << "#{chars[count]}#{chars[count+1]}"
246
249
  count += 2
247
250
  else
@@ -1,3 +1,4 @@
1
+ #encoding: ASCII
1
2
  require 'barby/barcode'
2
3
 
3
4
  module Barby
@@ -1,3 +1,4 @@
1
+ #encoding: ASCII
1
2
  require 'barby/barcode'
2
3
 
3
4
  module Barby
@@ -1,4 +1,4 @@
1
- require 'semacode'
1
+ require 'semacode' #Ruby 1.8: gem install semacode - Ruby 1.9: gem install semacode-ruby19
2
2
  require 'barby/barcode'
3
3
 
4
4
  module Barby
@@ -58,8 +58,6 @@ module Barby
58
58
  end
59
59
 
60
60
 
61
- private
62
-
63
61
  def two_dimensional?
64
62
  barcode.respond_to?(:two_dimensional?) && barcode.two_dimensional?
65
63
  end
@@ -107,6 +105,8 @@ module Barby
107
105
  end
108
106
 
109
107
 
108
+ private
109
+
110
110
  #Takes a hash and temporarily sets properties on self (the outputter object)
111
111
  #corresponding with the keys to their values. When the block exits, the
112
112
  #properties are reset to their original values. Returns whatever the block returns.
@@ -2,87 +2,92 @@ require 'barby/outputter'
2
2
 
3
3
  module Barby
4
4
 
5
- # Outputs an HTML representation of the barcode.
6
- #
7
- # Registers to_html
8
- #
9
- # Allowed options include.
10
- # :width - Applied to parent element's style attribute. Default 100.
11
- # :height - Applied to parent element's style attribute. Default 100.
12
- # :css - Include Barby::HtmlOutputter.css in output's style tag. If you pass false
13
- # you can include the output of Barby::HtmlOutputter.css in single place like
14
- # your own stylesheet on once on the page. Default true.
15
- # :parent_style - Include inline style for things like width and height on parent element.
16
- # Useful if you want to style these attributes elsewhere globally. Default true.
5
+ # Outputs an HTML <table> containing cells for each module in the barcode.
6
+ #
7
+ # This does NOT include any styling, you're expected to add the relevant
8
+ # CSS yourself. The markup is simple: One <table> with class 'barby-barcode',
9
+ # one or more <tr class="barby-row"> inside a <tbody> each containing
10
+ # <td class="barby-cell"> for each module with the additional class "on" or "off".
11
+ #
12
+ # Example, let's say the barcode.encoding == ['101', '010'] :
13
+ #
14
+ # <table class="barby-barcode">
15
+ # <tbody>
16
+ # <tr class="barby-row">
17
+ # <td class="barby-cell on"></td>
18
+ # <td class="barby-cell off"></td>
19
+ # <td class="barby-cell on"></td>
20
+ # </tr>
21
+ # <tr class="barby-row">
22
+ # <td class="barby-cell off"></td>
23
+ # <td class="barby-cell on"></td>
24
+ # <td class="barby-cell off"></td>
25
+ # </tr>
26
+ # </tbody>
27
+ # </table>
28
+ #
29
+ # You could then style this with:
30
+ #
31
+ # table.barby-barcode { border-spacing: 0; }
32
+ # tr.barby-row {}
33
+ # td.barby-cell { width: 3px; height: 3px; }
34
+ # td.barby-cell.on { background: #000; }
35
+ #
36
+ # Options:
37
+ #
38
+ # :class_name - A class name that will be added to the <table> in addition to barby-barcode
17
39
  class HtmlOutputter < Outputter
18
40
 
19
41
  register :to_html
20
-
21
- def self.css
22
- <<-CSS
23
- table.barby_code {
24
- border: 0 none transparent !important;
25
- border-collapse: collapse !important;
26
- }
27
- table.barby_code tr.barby_row {
28
- border: 0 none transparent !important;
29
- border-collapse: collapse !important;
30
- margin: 0 !important;
31
- padding: 0 !important;
32
- }
33
- table.barby_code tr.barby_row td { border: 0 none transparent !important; }
34
- table.barby_code tr.barby_row td.barby_black { background-color: black !important; }
35
- table.barby_code tr.barby_row td.barby_white { background-color: white !important; }
36
- CSS
42
+
43
+ attr_accessor :class_name
44
+
45
+
46
+ def to_html(options = {})
47
+ with_options options do
48
+ start + rows.join + stop
49
+ end
37
50
  end
38
-
39
- def to_html(options={})
40
- default_options = {:width => 100, :height => 100, :css => true, :parent_style => :true}
41
- options = default_options.merge(options)
42
- elements = if barcode.two_dimensional?
43
- booleans.map do |bools|
44
- line_to_elements_row(bools, options)
45
- end.join("\n")
46
- else
47
- line_to_elements_row(booleans, options)
48
- end
49
- html = %|<#{parent_element} class="barby_code" #{parent_style_attribute(options)}>\n#{elements}\n</#{parent_element}>|
50
- options[:css] ? "<style>#{self.class.css}</style>\n#{html}" : html
51
+
52
+
53
+ def rows
54
+ if barcode.two_dimensional?
55
+ rows_for(booleans)
56
+ else
57
+ rows_for([booleans])
58
+ end
51
59
  end
52
60
 
53
61
 
54
- private
62
+ def rows_for(boolean_groups)
63
+ boolean_groups.map{|g| row_for(cells_for(g)) }
64
+ end
55
65
 
56
- def line_to_elements_row(bools, options)
57
- elements = bools.map{ |b| b ? black_tag : white_tag }.join
58
- Array(%|<#{row_element} class="barby_row">#{elements}</#{row_element}>|)
66
+ def cells_for(booleans)
67
+ booleans.map{|b| b ? on_cell : off_cell }
59
68
  end
60
-
61
- def black_tag
62
- '<td class="barby_black"></td>'
69
+
70
+ def row_for(cells)
71
+ "<tr class=\"barby-row\">#{cells.join}</tr>"
63
72
  end
64
-
65
- def white_tag
66
- '<td class="barby_white"></td>'
73
+
74
+ def on_cell
75
+ '<td class="barby-cell on"></td>'
67
76
  end
68
-
69
- def row_element
70
- 'tr'
77
+
78
+ def off_cell
79
+ '<td class="barby-cell off"></td>'
71
80
  end
72
-
73
- def parent_element
74
- 'table'
81
+
82
+ def start
83
+ '<table class="barby-barcode'+(class_name ? " #{class_name}" : '')+'"><tbody>'
75
84
  end
76
-
77
- def parent_style_attribute(options)
78
- return unless options[:parent_style]
79
- s = ''
80
- s << "width: #{options[:width]}px; " if options[:width]
81
- s << "height: #{options[:height]}px; " if options[:height]
82
- s.strip!
83
- s.empty? ? nil : %|style="#{s}"|
85
+
86
+ def stop
87
+ '</tbody></table>'
84
88
  end
85
89
 
90
+
86
91
  end
87
92
 
88
93
  end
@@ -0,0 +1,211 @@
1
+ require 'barby/outputter'
2
+
3
+ module Barby
4
+
5
+ class Outputter
6
+
7
+
8
+ #A collection of rows - an array of arrays
9
+ class PointMatrix < Array
10
+
11
+ DEFAULT_XDIM = 1.0
12
+ DEFAULT_YDIM = 1.0
13
+
14
+ attr_writer :xdim, :ydim
15
+
16
+
17
+ def initialize(*a, &b)
18
+ # new([[t,f,t,f], [f,t,f,t], ...])
19
+ if a[0].is_a?(Array) && a[0][0].is_a?(Array) && [true,false].include?(a[0][0][0])
20
+ a[0] = a[0].map{|r| PointRow.new(self, r) }
21
+ # new(['1010', '0101', ...])
22
+ elsif a[0].is_a?(Array) && a[0][0].is_a?(String)
23
+ a[0] = a[0].map{|s| PointRow.new(self, s) }
24
+ end
25
+
26
+ super(*a, &b)
27
+ end
28
+
29
+
30
+ def xdim
31
+ @xdim || DEFAULT_XDIM
32
+ end
33
+
34
+ def ydim
35
+ @ydim || DEFAULT_YDIM
36
+ end
37
+
38
+
39
+ def width
40
+ inject(0){|m,r| r.width > m ? r.width : m }
41
+ end
42
+
43
+ def height
44
+ inject(0){|s,r| s + r.height }
45
+ end
46
+
47
+
48
+ def to_s
49
+ map{|r| r.to_s }
50
+ end
51
+
52
+ def inspect
53
+ to_s
54
+ end
55
+
56
+
57
+ def dup
58
+ self.class.new(map{|r| r.dup })
59
+ end
60
+
61
+
62
+ end
63
+
64
+
65
+ class PointRow < Array
66
+
67
+ attr_reader :matrix
68
+ attr_accessor :xdim, :ydim
69
+
70
+
71
+ def initialize(*a, &b)
72
+ raise ArgumentError, 'PointRow must belong to a PointMatrix' unless a[0].is_a?(PointMatrix)
73
+ @matrix = a.shift
74
+
75
+ #Assume called with [t,f,t,f,..]
76
+ if a[0].is_a?(Array) && [true, false].include?(a[0][0])
77
+ a[0] = a[0].map{|b| Point.new(self, b) }
78
+ #Assume called with '1010..'
79
+ elsif a[0].is_a?(String)
80
+ a[0] = a[0].split(//).map{|s| Point.new(self, s == '1') }
81
+ end
82
+
83
+ super(*a, &b)
84
+ end
85
+
86
+
87
+ def booleans
88
+ map{|p| p.active? }
89
+ end
90
+
91
+
92
+ def xdim
93
+ @xdim || matrix.xdim
94
+ end
95
+
96
+ def ydim
97
+ @ydim || matrix.ydim
98
+ end
99
+
100
+
101
+ def width
102
+ inject(0){|s,p| s + p.width }
103
+ end
104
+
105
+ def height
106
+ inject(0){|m,p| p.height > m ? p.height : m }
107
+ end
108
+
109
+
110
+ def to_s
111
+ inject(''){|s,p| s << (p.active ? '1' : '0') }
112
+ end
113
+
114
+ def inspect
115
+ to_s
116
+ end
117
+
118
+ def ==(other)
119
+ super #Just to say explicitly that equality should behave like in an array
120
+ end
121
+
122
+
123
+ def dup
124
+ self.class.new(matrix, map{|p| p.dup })
125
+ end
126
+
127
+ end
128
+
129
+
130
+ class Point
131
+
132
+ attr_reader :row, :active
133
+ attr_writer :width, :height, :color, :active
134
+ alias active? active
135
+
136
+
137
+ def initialize(row, active, opts={})
138
+ @row = row
139
+ self.active = active
140
+ end
141
+
142
+
143
+ def color
144
+ @color || (active ? Color.new(0,0,0) : nil)
145
+ end
146
+
147
+ def width
148
+ @width || row.xdim
149
+ end
150
+
151
+ def height
152
+ @height || row.ydim
153
+ end
154
+
155
+
156
+ def ==(other)
157
+ active == other.active &&
158
+ width == other.width &&
159
+ height == other.height &&
160
+ color == other.color
161
+ end
162
+
163
+
164
+ end
165
+
166
+
167
+ #TODO Add alpha
168
+ class Color
169
+
170
+ MIN_VALUE = 0
171
+ MAX_VALUE = 255
172
+
173
+ attr_reader :red, :blue, :green
174
+
175
+ def initialize(r, b, g)
176
+ self.red, self.blue, self.green = r, b, g
177
+ end
178
+
179
+
180
+ def red=(r)
181
+ set_color(:red, r)
182
+ end
183
+
184
+ def blue=(b)
185
+ set_color(:blue, b)
186
+ end
187
+
188
+ def green=(g)
189
+ set_color(:green, g)
190
+ end
191
+
192
+
193
+ def ==(other)
194
+ other.red == red && other.blue == blue && other.green == green
195
+ end
196
+
197
+
198
+ private
199
+
200
+ def set_color(name, value)
201
+ raise ArgumentError, "Value must be between 0-255" if value < MIN_VALUE || value > MAX_VALUE
202
+ instance_variable_set("@#{name}", value)
203
+ end
204
+
205
+
206
+ end
207
+
208
+
209
+ end
210
+
211
+ end
data/lib/barby/version.rb CHANGED
@@ -2,7 +2,7 @@ module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 5
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,27 +1,23 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: barby
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
4
5
  prerelease:
5
- version: 0.5.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Tore Darell
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-08-18 00:00:00 Z
12
+ date: 2013-03-12 00:00:00.000000000 Z
14
13
  dependencies: []
15
-
16
14
  description: Barby creates barcodes.
17
15
  email: toredarell@gmail.com
18
16
  executables: []
19
-
20
17
  extensions: []
21
-
22
- extra_rdoc_files:
18
+ extra_rdoc_files:
23
19
  - README
24
- files:
20
+ files:
25
21
  - CHANGELOG
26
22
  - README
27
23
  - LICENSE
@@ -45,6 +41,7 @@ files:
45
41
  - lib/barby/outputter/html_outputter.rb
46
42
  - lib/barby/outputter/pdfwriter_outputter.rb
47
43
  - lib/barby/outputter/png_outputter.rb
44
+ - lib/barby/outputter/point_matrix.rb
48
45
  - lib/barby/outputter/prawn_outputter.rb
49
46
  - lib/barby/outputter/rmagick_outputter.rb
50
47
  - lib/barby/outputter/svg_outputter.rb
@@ -57,36 +54,41 @@ files:
57
54
  - bin/barby
58
55
  homepage: http://toretore.github.com/barby
59
56
  licenses: []
57
+ post_install_message: ! '
60
58
 
61
- post_install_message: "\n\
62
- *** NEW REQUIRE POLICY ***\"\n\
63
- Barby no longer require all barcode symbologies by default. You'll have\n\
64
- to require the ones you need. For example, if you need EAN-13,\n\
65
- require 'barby/barcode/ean_13'; For a full list of symbologies and their\n\
66
- filenames, see README.\n\
67
- ***\n\n"
68
- rdoc_options: []
59
+ *** NEW REQUIRE POLICY ***"
60
+
61
+ Barby no longer require all barcode symbologies by default. You''ll have
62
+
63
+ to require the ones you need. For example, if you need EAN-13,
69
64
 
70
- require_paths:
65
+ require ''barby/barcode/ean_13''; For a full list of symbologies and their
66
+
67
+ filenames, see README.
68
+
69
+ ***
70
+
71
+
72
+ '
73
+ rdoc_options: []
74
+ require_paths:
71
75
  - lib
72
- required_ruby_version: !ruby/object:Gem::Requirement
76
+ required_ruby_version: !ruby/object:Gem::Requirement
73
77
  none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: "0"
78
- required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
83
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: "0"
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
84
88
  requirements: []
85
-
86
89
  rubyforge_project: barby
87
- rubygems_version: 1.8.5
90
+ rubygems_version: 1.8.23
88
91
  signing_key:
89
92
  specification_version: 3
90
93
  summary: The Ruby barcode generator
91
94
  test_files: []
92
-