barby 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25c48f523056303c71f3477a056906f12613bde3
4
- data.tar.gz: 1860f349c37684539345aef410f09a570a50f319
3
+ metadata.gz: 8062a4f8a077022c7b52a7fc23d73886bbdf9e96
4
+ data.tar.gz: 80ab18bc484c11d6044b5c7aa683dc65f316b359
5
5
  SHA512:
6
- metadata.gz: a793a5c6a5a5fba522ec2300ec703a3340edd5c45d249b9ca5d2056c2460555e6f4944c0cc4d9ccb8863f6a068a1aa2083f5f5181820d0c70b7a79d2c9cf2799
7
- data.tar.gz: f7acba5a08dea02a79c1620a2b4743e1b77a1f53469d83bf4d58229c5f5e618dc3f89959d2e858b8dbf80c0fef7ef33cfe211f5bdf28d750d9e578178d4bda6d
6
+ metadata.gz: e99ce1411282a91273107da8b5ef1963baaa7b04c107132c33dedb463669d77172968dfc6d357c480ce633b7db79c24f62f49ff91de8bf9a9192d428c099c5b2
7
+ data.tar.gz: 0ff28cc5b9eac5a16bfe2eacde4b3dcd275d57840e28d3aeecdc3cf8d4a9189e97ed53efb5d2aa00676af355560016fdc9e211fab06db963a5f0af3868654c49
@@ -0,0 +1,107 @@
1
+ # Barby
2
+ Barby is a Ruby library that generates barcodes in a variety of symbologies.
3
+
4
+ Its functionality is split into _barcode_ and "_outputter_" objects:
5
+ * [`Barby::Barcode` objects] [symbologies] turn data into a binary representation for a given symbology.
6
+ * [`Barby::Outputter`] [outputters] then takes this representation and turns it into images, PDF, etc.
7
+
8
+ You can easily add a symbology without having to worry about graphical
9
+ representation. If it can be represented as the usual 1D or 2D matrix of
10
+ lines or squares, outputters will do that for you.
11
+
12
+ Likewise, you can easily add an outputter for a format that doesn't have one
13
+ yet, and it will work with all existing symbologies.
14
+
15
+ For more information, check out [the Barby wiki] [wiki].
16
+
17
+ ### New require policy
18
+
19
+ Barcode symbologies are no longer required automatically, so you'll have to
20
+ require the ones you need.
21
+
22
+ If you need EAN-13, `require 'barby/barcode/ean_13'`. Full list of symbologies and filenames below.
23
+
24
+ ## Example
25
+
26
+ ```ruby
27
+ require 'barby'
28
+ require 'barby/barcode/code_128'
29
+ require 'barby/outputter/ascii_outputter'
30
+
31
+ barcode = Barby::Code128B.new('BARBY')
32
+
33
+ puts barcode.to_ascii #Implicitly uses the AsciiOutputter
34
+
35
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
36
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
37
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
38
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
39
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
40
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
41
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
42
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
43
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
44
+ ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
45
+ B A R B Y
46
+ ```
47
+
48
+ ## Supported symbologies
49
+
50
+ ```ruby
51
+ require 'barby/barcode/<filename>'
52
+ ```
53
+
54
+ | Name | Filename | Dependencies |
55
+ | ----------------------------------- | --------------------- | ------------- |
56
+ | Code 25 | `code_25` | ─ |
57
+ | ├─ Interleaved | `code_25_interleaved` | ─ |
58
+ | └─ IATA | `code_25_iata` | ─ |
59
+ | Code 39 | `code_39` | ─ |
60
+ | └─ Extended | `code_39` | ─ |
61
+ | Code 93 | `code_93` | ─ |
62
+ | Code 128 (A, B, and C) | `code_128` | ─ |
63
+ | └─ GS1 128 | `gs1_128` | ─ |
64
+ | EAN-13 | `ean_13` | ─ |
65
+ | ├─ Bookland | `bookland` | ─ |
66
+ | └─ UPC-A | `ean_13` | ─ |
67
+ | EAN-8 | `ean_8` | ─ |
68
+ | UPC/EAN supplemental, 2 & 5 digits | `upc_supplemental` | ─ |
69
+ | QR Code | `qr_code` | `rqrcode` |
70
+ | DataMatrix (Semacode) | `data_matrix` | `semacode` |
71
+ | PDF417 | `pdf_417` | JRuby |
72
+
73
+
74
+ ## Outputters
75
+
76
+ ```ruby
77
+ require 'barby/outputter/<filename>_outputter'
78
+ ```
79
+
80
+ | filename | dependencies |
81
+ | ----------- | ------------- |
82
+ | `ascii` | ─ |
83
+ | `cairo` | cairo |
84
+ | `html` | ─ |
85
+ | `pdfwriter` | ─ |
86
+ | `png` | chunky_png |
87
+ | `prawn` | prawn |
88
+ | `rmagick` | rmagick |
89
+ | `svg` | ─ |
90
+
91
+ ### Formats supported by outputters
92
+
93
+ * Text (mostly for testing)
94
+ * PNG, JPEG, GIF
95
+ * PS, EPS
96
+ * SVG
97
+ * PDF
98
+ * HTML
99
+
100
+ ---
101
+
102
+ For more information, check out [the Barby wiki] [wiki].
103
+
104
+
105
+ [wiki]: https://github.com/toretore/barby/wiki
106
+ [symbologies]: https://github.com/toretore/barby/wiki/Symbologies
107
+ [outputters]: https://github.com/toretore/barby/wiki/Outputters
@@ -132,7 +132,6 @@ module Barby
132
132
  end
133
133
 
134
134
  def inspect
135
- klass = (self.class.ancestors + [self.class.name]).join(':')
136
135
  "#<#{self.class}:0x#{'%014x' % object_id} #{formatted_isbn}>"
137
136
  end
138
137
 
@@ -18,6 +18,14 @@ module Barby
18
18
  #As an example, here's one that starts out as type A and then switches to B and then C:
19
19
  #
20
20
  # Code128A.new("ABC123\306def\3074567")
21
+ #
22
+ #
23
+ #GS1-128/EAN-128/UCC-128
24
+ #
25
+ #To make a GS1-128 code, prefix the data with FNC1 and the Application Identifier:
26
+ #
27
+ # #AI=00, data=12345
28
+ # Code128.new("#{Code128::FNC1}0012345")
21
29
  class Code128 < Barcode1D
22
30
 
23
31
  FNC1 = "\xc1"
@@ -175,7 +183,7 @@ module Barby
175
183
 
176
184
 
177
185
  def type=(type)
178
- type.upcase!
186
+ type = type.upcase
179
187
  raise ArgumentError, 'type must be A, B or C' unless type =~ /^[ABC]$/
180
188
  @type = type
181
189
  end
@@ -230,7 +238,8 @@ module Barby
230
238
  #there are no more extras, the barcode ends with that object.
231
239
  #Most barcodes probably don't change charsets and don't have extras.
232
240
  def extra
233
- @extra
241
+ return @extra if defined?(@extra)
242
+ @extra = nil
234
243
  end
235
244
 
236
245
  #Set the extra for this barcode. The argument is a string starting with the
@@ -28,11 +28,14 @@ module Barby
28
28
  9 => [N,W,N,W,N]
29
29
  }
30
30
 
31
- attr_accessor :data, :narrow_width, :wide_width, :space_width, :include_checksum
31
+ attr_accessor :include_checksum
32
+ attr_writer :narrow_width, :wide_width, :space_width
33
+ attr_reader :data
34
+
32
35
 
33
-
34
36
  def initialize(data)
35
37
  self.data = data
38
+ @narrow_width, @wide_width, @space_width = nil
36
39
  end
37
40
 
38
41
 
@@ -105,7 +108,7 @@ module Barby
105
108
  #Mod10
106
109
  def checksum
107
110
  evens, odds = even_and_odd_digits
108
- sum = odds.inject(0){|sum,d| sum + d } + evens.inject(0){|sum,d| sum + (d*3) }
111
+ sum = odds.inject(0){|s,d| s + d } + evens.inject(0){|s,d| s + (d*3) }
109
112
  sum %= 10
110
113
  sum.zero? ? 0 : 10-sum
111
114
  end
@@ -14,9 +14,9 @@ module Barby
14
14
 
15
15
 
16
16
  def digit_pairs(d=nil)
17
- (d || digits).inject [] do |ary,d|
17
+ (d || digits).inject [] do |ary,i|
18
18
  ary << [] if !ary.last || ary.last.size == 2
19
- ary.last << d
19
+ ary.last << i
20
20
  ary
21
21
  end
22
22
  end
@@ -87,7 +87,8 @@ module Barby
87
87
  START_ENCODING = [N,W,N,N,W,N,W,N,N] # *
88
88
  STOP_ENCODING = [N,W,N,N,W,N,W,N,N] # *
89
89
 
90
- attr_accessor :data, :spacing, :narrow_width, :wide_width, :extended, :include_checksum
90
+ attr_accessor :data, :extended, :include_checksum
91
+ attr_writer :spacing, :narrow_width, :wide_width
91
92
 
92
93
  # Do not surround "data" with the mandatory "*" as is this is done automically for you.
93
94
  # So instead of passing "*123456*" as "data", just pass "123456".
@@ -7,7 +7,7 @@ module Barby
7
7
  #Uses the semacode library (gem install semacode) to encode DataMatrix barcodes
8
8
  class DataMatrix < Barcode2D
9
9
 
10
- attr_accessor :data
10
+ attr_reader :data
11
11
 
12
12
 
13
13
  def initialize(data)
@@ -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
@@ -23,6 +23,11 @@ module Barby
23
23
  attr_writer :x, :y, :xdim, :height, :margin
24
24
 
25
25
 
26
+ def initialize(*)
27
+ super
28
+ @x, @y, @xdim, @height, @margin = nil
29
+ end
30
+
26
31
  #Render the barcode onto a Cairo context
27
32
  def render_to_cairo_context(context, options={})
28
33
  if context.respond_to?(:have_current_point?) and
@@ -10,8 +10,12 @@ module Barby
10
10
 
11
11
  register :to_png, :to_image, :to_datastream
12
12
 
13
- attr_accessor :xdim, :ydim, :width, :height, :margin
13
+ attr_writer :xdim, :ydim, :width, :height, :margin
14
14
 
15
+ def initialize(*)
16
+ super
17
+ @xdim, @height, @margin = nil
18
+ end
15
19
 
16
20
  #Creates a PNG::Canvas object and renders the barcode on it
17
21
  def to_image(opts={})
@@ -7,9 +7,14 @@ module Barby
7
7
 
8
8
  register :to_pdf, :annotate_pdf
9
9
 
10
- attr_accessor :xdim, :ydim, :x, :y, :height, :margin, :unbleed
10
+ attr_writer :xdim, :ydim, :x, :y, :height, :margin, :unbleed
11
11
 
12
12
 
13
+ def initialize(*)
14
+ super
15
+ @xdim, @ydim, @x, @y, @height, @margin, @unbleed = nil
16
+ end
17
+
13
18
  def to_pdf(opts={})
14
19
  doc_opts = opts.delete(:document) || {}
15
20
  doc_opts[:page_size] ||= 'A4'
@@ -1,5 +1,5 @@
1
1
  require 'barby/outputter'
2
- require 'RMagick'
2
+ require 'rmagick'
3
3
 
4
4
  module Barby
5
5
 
@@ -8,11 +8,16 @@ module Barby
8
8
  #
9
9
  #Registers the to_png, to_gif, to_jpg and to_image methods
10
10
  class RmagickOutputter < Outputter
11
-
11
+
12
12
  register :to_png, :to_gif, :to_jpg, :to_image
13
13
 
14
- attr_accessor :height, :xdim, :ydim, :margin
14
+ attr_writer :height, :xdim, :ydim, :margin
15
+
15
16
 
17
+ def initialize(*)
18
+ super
19
+ @height, @xdim, @ydim, @margin = nil
20
+ end
16
21
 
17
22
  #Returns a string containing a PNG image
18
23
  def to_png(*a)
@@ -28,15 +33,15 @@ module Barby
28
33
  def to_jpg(*a)
29
34
  to_blob('jpg', *a)
30
35
  end
31
-
36
+
32
37
  def to_blob(format, *a)
33
38
  img = to_image(*a)
34
39
  blob = img.to_blob{|i| i.format = format }
35
-
40
+
36
41
  #Release the memory used by RMagick explicitly. Ruby's GC
37
42
  #isn't aware of it and can't clean it up automatically
38
43
  img.destroy! if img.respond_to?(:destroy!)
39
-
44
+
40
45
  blob
41
46
  end
42
47
 
@@ -22,6 +22,11 @@ module Barby
22
22
  attr_writer :title, :xdim, :ydim, :height, :rmargin, :lmargin, :tmargin, :bmargin, :xmargin, :ymargin, :margin
23
23
 
24
24
 
25
+ def initialize(*)
26
+ super
27
+ @title, @xdim, @ydim, @height, @rmargin, @lmargin, @tmargin, @bmargin, @xmargin, @ymargin, @margin = nil
28
+ end
29
+
25
30
  def to_svg(opts={})
26
31
  with_options opts do
27
32
  case opts[:use]
@@ -2,7 +2,7 @@ module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,25 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tore Darell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-15 00:00:00.000000000 Z
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Barby creates barcodes.
14
14
  email: toredarell@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files:
18
- - README
18
+ - README.md
19
19
  files:
20
20
  - CHANGELOG
21
21
  - LICENSE
22
- - README
22
+ - README.md
23
23
  - bin/barby
24
24
  - lib/barby.rb
25
25
  - lib/barby/barcode.rb
@@ -43,7 +43,6 @@ files:
43
43
  - lib/barby/outputter/html_outputter.rb
44
44
  - lib/barby/outputter/pdfwriter_outputter.rb
45
45
  - lib/barby/outputter/png_outputter.rb
46
- - lib/barby/outputter/point_matrix.rb
47
46
  - lib/barby/outputter/prawn_outputter.rb
48
47
  - lib/barby/outputter/rmagick_outputter.rb
49
48
  - lib/barby/outputter/svg_outputter.rb
@@ -54,15 +53,7 @@ files:
54
53
  homepage: http://toretore.github.com/barby
55
54
  licenses: []
56
55
  metadata: {}
57
- post_install_message: |2+
58
-
59
- *** NEW REQUIRE POLICY ***"
60
- Barby no longer require all barcode symbologies by default. You'll have
61
- to require the ones you need. For example, if you need EAN-13,
62
- require 'barby/barcode/ean_13'; For a full list of symbologies and their
63
- filenames, see README.
64
- ***
65
-
56
+ post_install_message:
66
57
  rdoc_options: []
67
58
  require_paths:
68
59
  - lib
@@ -78,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
69
  version: '0'
79
70
  requirements: []
80
71
  rubyforge_project: barby
81
- rubygems_version: 2.2.2
72
+ rubygems_version: 2.5.1
82
73
  signing_key:
83
74
  specification_version: 4
84
75
  summary: The Ruby barcode generator
data/README DELETED
@@ -1,93 +0,0 @@
1
- *** NEW REQUIRE POLICY ***
2
-
3
- Barcode symbologies are no longer requires automatically, so you'll have to
4
- require the ones you need. If you need EAN-13, require 'barby/barcode/ean_13'.
5
- Full list of symbologies and filenames below.
6
-
7
- ***
8
-
9
- For more information, check out the Barby wiki at https://github.com/toretore/barby/wiki
10
-
11
- Barby is a Ruby library that generates barcodes in a variety of symbologies.
12
- Its functionality is split into barcode and "outputter" objects. Barcode
13
- objects turn data into a binary representation for a given symbology.
14
- Outputters then take this representation and turns it into images, PDF, etc.
15
-
16
- You can easily add a symbology without having to worry about graphical
17
- representation. If it can be represented as the usual 1D or 2D matrix of
18
- lines or squares, outputters will do that for you.
19
-
20
- Likewise, you can easily add an outputter for a format that doesn't have one
21
- yet, and it will work with all existing symbologies.
22
-
23
- See Barby::Barcode and Barby::Outputter for more information.
24
-
25
- require 'barby'
26
- require 'barby/barcode/code_128'
27
- require 'barby/outputter/ascii_outputter'
28
-
29
- barcode = Barby::Code128B.new('BARBY')
30
-
31
- puts barcode.to_ascii #Implicitly uses the AsciiOutputter
32
-
33
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
34
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
35
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
36
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
37
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
38
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
39
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
40
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
41
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
42
- ## # # # # ## # # ## ## # ### # # ## ### ## # ## ### ### ## ### # ##
43
- B A R B Y
44
-
45
-
46
- Supported symbologies:
47
-
48
- Name - filename - dependencies
49
-
50
- require 'barby/barcode/<filename>'
51
-
52
- * Code 25 - code_25
53
- * Interleaved - code_25_interleaved
54
- * IATA - code_25_iata
55
- * Code 39 - code_39
56
- * Code 93 - code_93
57
- * Code 128 - code_128
58
- * GS1 128 - gs1_128
59
- * EAN-13 - ean_13
60
- * Bookland - bookland
61
- * UPC-A - ean_13
62
- * EAN-8 - ean_8
63
- * UPC/EAN supplemental, 2 & 5 digits - upc_supplemental
64
-
65
- * QR Code - qr_code - rqrcode
66
- * DataMatrix (Semacode) - data_matrix - semacode
67
- * PDF417 - pdf_417 - java (JRuby)
68
-
69
-
70
- Formats supported by outputters:
71
-
72
- * Text (mostly for testing)
73
- * PNG, JPEG, GIF
74
- * PS, EPS
75
- * SVG
76
- * PDF
77
- * HTML
78
-
79
-
80
- Outputters:
81
-
82
- filename (dependencies)
83
-
84
- require 'barby/outputter/<filename>_outputter'
85
-
86
- * ascii
87
- * cairo (cairo)
88
- * html
89
- * pdfwriter
90
- * png (chunky_png)
91
- * prawn (prawn)
92
- * rmagick (RMagick)
93
- * svg
@@ -1,211 +0,0 @@
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