barcodes 0.0.1 → 0.0.2

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.
Files changed (63) hide show
  1. data/Gemfile +6 -0
  2. data/README.md +24 -24
  3. data/Rakefile +6 -0
  4. data/barcodes.gemspec +6 -0
  5. data/lib/barcodes.rb +52 -0
  6. data/lib/barcodes/exec.rb +52 -31
  7. data/lib/barcodes/renderer.rb +6 -0
  8. data/lib/barcodes/renderer/ascii.rb +7 -0
  9. data/lib/barcodes/renderer/pdf.rb +6 -0
  10. data/lib/barcodes/symbology.rb +6 -0
  11. data/lib/barcodes/symbology/base.rb +55 -15
  12. data/lib/barcodes/symbology/codabar.rb +23 -0
  13. data/lib/barcodes/symbology/code11.rb +18 -0
  14. data/lib/barcodes/symbology/code128.rb +44 -5
  15. data/lib/barcodes/symbology/code39.rb +21 -0
  16. data/lib/barcodes/symbology/code39extended.rb +12 -0
  17. data/lib/barcodes/symbology/code39extendedmod43.rb +14 -0
  18. data/lib/barcodes/symbology/code39mod43.rb +14 -0
  19. data/lib/barcodes/symbology/code93.rb +23 -0
  20. data/lib/barcodes/symbology/code93extended.rb +17 -1
  21. data/lib/barcodes/symbology/ean.rb +28 -1
  22. data/lib/barcodes/symbology/ean13.rb +17 -0
  23. data/lib/barcodes/symbology/ean8.rb +18 -0
  24. data/lib/barcodes/symbology/interleaved2of5.rb +18 -0
  25. data/lib/barcodes/symbology/interleaved2of5mod10.rb +14 -0
  26. data/lib/barcodes/symbology/msi.rb +16 -0
  27. data/lib/barcodes/symbology/msimod10.rb +14 -0
  28. data/lib/barcodes/symbology/msimod11.rb +14 -0
  29. data/lib/barcodes/symbology/planet.rb +24 -1
  30. data/lib/barcodes/symbology/postnet.rb +24 -1
  31. data/lib/barcodes/symbology/standard2of5.rb +16 -0
  32. data/lib/barcodes/symbology/standard2of5mod10.rb +14 -0
  33. data/lib/barcodes/symbology/upca.rb +16 -0
  34. data/lib/barcodes/version.rb +9 -1
  35. data/spec/barcodes/exec_spec.rb +7 -1
  36. data/spec/barcodes/renderer/ascii_spec.rb +6 -0
  37. data/spec/barcodes/renderer/pdf_spec.rb +6 -0
  38. data/spec/barcodes/symbology/base_spec.rb +6 -0
  39. data/spec/barcodes/symbology/codabar_spec.rb +6 -0
  40. data/spec/barcodes/symbology/code11_spec.rb +6 -0
  41. data/spec/barcodes/symbology/code128_spec.rb +6 -0
  42. data/spec/barcodes/symbology/code39_spec.rb +6 -0
  43. data/spec/barcodes/symbology/code39extended_spec.rb +6 -0
  44. data/spec/barcodes/symbology/code39extendedmod43_spec.rb +6 -0
  45. data/spec/barcodes/symbology/code39mod43_spec.rb +6 -0
  46. data/spec/barcodes/symbology/code93_spec.rb +6 -0
  47. data/spec/barcodes/symbology/code93extended_spec.rb +6 -0
  48. data/spec/barcodes/symbology/ean13_spec.rb +6 -0
  49. data/spec/barcodes/symbology/ean8_spec.rb +6 -0
  50. data/spec/barcodes/symbology/ean_spec.rb +6 -0
  51. data/spec/barcodes/symbology/interleaved2of5_spec.rb +6 -0
  52. data/spec/barcodes/symbology/interleaved2of5mod10_spec.rb +6 -0
  53. data/spec/barcodes/symbology/msi_spec.rb +6 -0
  54. data/spec/barcodes/symbology/msimod10_spec.rb +6 -0
  55. data/spec/barcodes/symbology/msimod11_spec.rb +6 -0
  56. data/spec/barcodes/symbology/planet_spec.rb +6 -0
  57. data/spec/barcodes/symbology/postnet_spec.rb +6 -0
  58. data/spec/barcodes/symbology/standard2of5_spec.rb +6 -0
  59. data/spec/barcodes/symbology/standard2of5mod10_spec.rb +6 -0
  60. data/spec/barcodes/symbology/upca_spec.rb +6 -0
  61. data/spec/barcodes_spec.rb +6 -0
  62. data/spec/spec_helper.rb +6 -0
  63. metadata +8 -8
@@ -1,12 +1,26 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/base'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This is the base class for all EAN type (EAN8, EAN13, UPC-A) barcode symbologies.
13
+ # EAN type barcodes can encode only the numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/European_Article_Number
5
16
  class Ean < Base
17
+
18
+ # EAN character set
6
19
  def self.charset
7
20
  ['0','1','2','3','4','5','6','7','8','9','S','C'].collect {|c| c.bytes.to_a[0] }
8
21
  end
9
22
 
23
+ # EAN parity values table
10
24
  def self.parity
11
25
  [
12
26
  "000000", "001011", "001101", "001110",
@@ -15,6 +29,7 @@ module Barcodes
15
29
  ]
16
30
  end
17
31
 
32
+ # EAN left hand even values table
18
33
  def self.left_hand_even
19
34
  [
20
35
  "0001101", "0011001", "0010011", "0111101",
@@ -23,6 +38,7 @@ module Barcodes
23
38
  ]
24
39
  end
25
40
 
41
+ # EAN left hand odd values table
26
42
  def self.left_hand_odd
27
43
  [
28
44
  "0100111", "0110011", "0011011", "0100001",
@@ -31,6 +47,7 @@ module Barcodes
31
47
  ]
32
48
  end
33
49
 
50
+ # EAN right hand values table
34
51
  def self.right_hand
35
52
  [
36
53
  "1110010", "1100110", "1101100", "1000010",
@@ -39,10 +56,12 @@ module Barcodes
39
56
  ]
40
57
  end
41
58
 
59
+ # EAN uses special value sets so this returns an empty array
42
60
  def self.valueset
43
61
  []
44
62
  end
45
63
 
64
+ # Creates a new EAN instance
46
65
  def initialize(args={})
47
66
  super(args)
48
67
 
@@ -51,10 +70,12 @@ module Barcodes
51
70
  @center_character = 'C'
52
71
  end
53
72
 
73
+ # Returns data + checksum
54
74
  def caption_data
55
75
  @data + self.checksum
56
76
  end
57
77
 
78
+ # Encodes data into 1's and 0's
58
79
  def encoded_data
59
80
  if self.valid?
60
81
  formatted_data = self.formatted_data
@@ -83,10 +104,12 @@ module Barcodes
83
104
  end
84
105
  end
85
106
 
107
+ # EAN uses quiet zone that is 9 times the bar width in mils
86
108
  def quiet_zone_width
87
109
  self.bar_width * 9
88
110
  end
89
111
 
112
+ # Calculates the checksum
90
113
  def checksum
91
114
  if self.valid?
92
115
  sum = 0
@@ -108,6 +131,7 @@ module Barcodes
108
131
  end
109
132
  end
110
133
 
134
+ # Validates barcode using provided data
111
135
  def valid?
112
136
  self.data.each_byte do |char|
113
137
  if char.chr == 'S' || char.chr == 'C'
@@ -121,6 +145,7 @@ module Barcodes
121
145
 
122
146
  protected
123
147
 
148
+ # Encodes a single given character (as ASCII integer) using the given parity (1,0)
124
149
  def _encode_character_with_parity(character, parity)
125
150
  if self.class.charset.include? character
126
151
  if parity.to_i.even?
@@ -133,6 +158,7 @@ module Barcodes
133
158
  end
134
159
  end
135
160
 
161
+ # Encodes a single given character (as ASCII integer) using right hand values table
136
162
  def _encode_character_with_right_hand(character)
137
163
  if self.class.charset.include? character
138
164
  self.class.right_hand.at(self.class.charset.index(character))
@@ -140,7 +166,8 @@ module Barcodes
140
166
  return nil
141
167
  end
142
168
  end
143
-
169
+
170
+ # Returns nil
144
171
  def _encode_character(character)
145
172
  nil
146
173
  end
@@ -1,8 +1,21 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/ean'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the EAN13 symbology
13
+ # EAN13 can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/International_Article_Number_(EAN)
5
16
  class Ean13 < Ean
17
+
18
+ # Creates a new Ean13 instance
6
19
  def initialize(args={})
7
20
  unless args.has_key? :data
8
21
  args[:data] = '012345678999'
@@ -11,10 +24,14 @@ module Barcodes
11
24
  super(args)
12
25
  end
13
26
 
27
+ # Returns start character + data + center character + data + checksum + stop character
14
28
  def formatted_data
15
29
  @start_character + @data[0..6] + @center_character + @data[7..12] + self.checksum + @stop_character
16
30
  end
17
31
 
32
+ # Determines whether or not the barcode data to be encoded
33
+ # is valid.
34
+ # Data length must be exactly 12 digits
18
35
  def valid?
19
36
  unless super
20
37
  return false
@@ -1,8 +1,21 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/ean'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the EAN8 symbology
13
+ # EAN8 can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/International_Article_Number_(EAN)
5
16
  class Ean8 < Ean
17
+
18
+ # Creates a new Ean13 instance
6
19
  def initialize(args={})
7
20
  unless args.has_key? :data
8
21
  args[:data] = '0123456'
@@ -11,10 +24,12 @@ module Barcodes
11
24
  super(args)
12
25
  end
13
26
 
27
+ # Returns start character + data + center character + data + checksum + stop character
14
28
  def formatted_data
15
29
  @start_character + @data[0..3] + @center_character + @data[4..6] + self.checksum + @stop_character
16
30
  end
17
31
 
32
+ # Encodes data into 1's and 0's
18
33
  def encoded_data
19
34
  if self.valid?
20
35
  formatted_data = self.formatted_data
@@ -38,6 +53,9 @@ module Barcodes
38
53
  end
39
54
  end
40
55
 
56
+ # Determines whether or not the barcode data to be encoded
57
+ # is valid.
58
+ # Data length must be exactly 7 digits
41
59
  def valid?
42
60
  unless super
43
61
  return false
@@ -1,12 +1,26 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/base'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the Interleaved 2 of 5 symbology
13
+ # Interleaved 2 of 5 can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/Interleaved_2_of_5
5
16
  class Interleaved2Of5 < Base
17
+
18
+ # Interleaved 2 of 5 character set
6
19
  def self.charset
7
20
  ['0','1','2','3','4','5','6','7','8','9','S','E'].collect {|c| c.bytes.to_a[0] }
8
21
  end
9
22
 
23
+ # Interleaved 2 of 5 values set
10
24
  def self.valueset
11
25
  [
12
26
  '101011011010','110101010110','101101010110','110110101010',
@@ -15,6 +29,9 @@ module Barcodes
15
29
  ]
16
30
  end
17
31
 
32
+ # Creates a new Interleaved2Of5 instance
33
+ # Interleaved 2 of 5 must have the start and stop characters
34
+ # 'S' and 'E' so they are overridden here.
18
35
  def initialize(args={})
19
36
  super(args)
20
37
 
@@ -22,6 +39,7 @@ module Barcodes
22
39
  @stop_character = 'E'
23
40
  end
24
41
 
42
+ # Returns start character + data + stop character
25
43
  def formatted_data
26
44
  @start_character + @data + @stop_character
27
45
  end
@@ -1,8 +1,21 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/interleaved2of5'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the Interleaved 2 of 5 Mod 10 symbology
13
+ # Interleaved 2 of 5 Mod 10 can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/Interleaved_2_of_5
5
16
  class Interleaved2Of5Mod10 < Interleaved2Of5
17
+
18
+ # Returns start character + data + checksum + stop character
6
19
  def formatted_data
7
20
  checksum = self.checksum
8
21
  unless checksum.nil?
@@ -10,6 +23,7 @@ module Barcodes
10
23
  end
11
24
  end
12
25
 
26
+ # Calculates the checksum using provided data
13
27
  def checksum
14
28
  even_sum = 0
15
29
  odd_sum = 0
@@ -1,12 +1,26 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/base'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the MSI symbology
13
+ # MSI can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/MSI_Barcode
5
16
  class Msi < Base
17
+
18
+ # MSI character set
6
19
  def self.charset
7
20
  ['0','1','2','3','4','5','6','7','8','9','S','E'].collect {|c| c.bytes.to_a[0] }
8
21
  end
9
22
 
23
+ # MSI values set
10
24
  def self.valueset
11
25
  [
12
26
  '100100100100','100100100110','100100110100','100100110110',
@@ -15,6 +29,7 @@ module Barcodes
15
29
  ]
16
30
  end
17
31
 
32
+ # Creates a new Msi instance
18
33
  def initialize(args={})
19
34
  super(args)
20
35
 
@@ -22,6 +37,7 @@ module Barcodes
22
37
  @stop_character = 'E'
23
38
  end
24
39
 
40
+ # Returns start character + data + stop character
25
41
  def formatted_data
26
42
  @start_character + @data + @stop_character
27
43
  end
@@ -1,8 +1,21 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/msi'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the MSI Mod 10 symbology
13
+ # MSI Mod 10 can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/MSI_Barcode
5
16
  class MsiMod10 < Msi
17
+
18
+ # Returns start character + data + checksum + stop character
6
19
  def formatted_data
7
20
  checksum = self.checksum
8
21
  unless checksum.nil?
@@ -10,6 +23,7 @@ module Barcodes
10
23
  end
11
24
  end
12
25
 
26
+ # Calculates the checksum using the provided data
13
27
  def checksum
14
28
  if self.valid?
15
29
  used_nums = []
@@ -1,8 +1,21 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/msi'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the MSI Mod 11 symbology
13
+ # MSI Mod 11 can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/MSI_Barcode
5
16
  class MsiMod11 < Msi
17
+
18
+ # Returns start character + data + checksum + stop character
6
19
  def formatted_data
7
20
  checksum = self.checksum
8
21
  unless checksum.nil?
@@ -10,6 +23,7 @@ module Barcodes
10
23
  end
11
24
  end
12
25
 
26
+ # Calculates the checksum using the provided data
13
27
  def checksum
14
28
  if self.valid?
15
29
  sum = 0
@@ -1,12 +1,26 @@
1
+ # Barcodes is a RubyGem for creating and rendering common barcode symbologies.
2
+ #
3
+ # Author:: Aaron Wright (mailto:acwrightdesign@gmail.com)
4
+ # Copyright:: Copyright (c) 2012 Infinite Token LLC
5
+ # License:: MIT License
6
+
1
7
  require 'barcodes/symbology/base'
2
8
 
3
9
  module Barcodes
4
10
  module Symbology
11
+
12
+ # This class represents the PLANET symbology
13
+ # PLANET can encode only numbers 0-9
14
+ #
15
+ # More info: http://en.wikipedia.org/wiki/Postal_Alpha_Numeric_Encoding_Technique
5
16
  class Planet < Base
17
+
18
+ # PLANET character set
6
19
  def self.charset
7
20
  ['0','1','2','3','4','5','6','7','8','9'].collect {|c| c.bytes.to_a[0] }
8
21
  end
9
22
 
23
+ # PLANET values set
10
24
  def self.valueset
11
25
  [
12
26
  '00111','11100','11010','11001',
@@ -15,6 +29,7 @@ module Barcodes
15
29
  ]
16
30
  end
17
31
 
32
+ # Creates a new Planet instance
18
33
  def initialize(args={})
19
34
  unless args.has_key? :data
20
35
  args[:data] = '012345678999'
@@ -23,6 +38,7 @@ module Barcodes
23
38
  super(args)
24
39
  end
25
40
 
41
+ # Returns data + checksum
26
42
  def formatted_data
27
43
  checksum = self.checksum
28
44
  unless checksum.nil?
@@ -30,6 +46,7 @@ module Barcodes
30
46
  end
31
47
  end
32
48
 
49
+ # Returns the barcode data encoded as 1's and 0's.
33
50
  def encoded_data
34
51
  if self.valid?
35
52
  encoded_data = ''
@@ -40,6 +57,7 @@ module Barcodes
40
57
  end
41
58
  end
42
59
 
60
+ # Calculates the checksum using the provided data
43
61
  def checksum
44
62
  if self.valid?
45
63
  sum = 0
@@ -58,17 +76,22 @@ module Barcodes
58
76
  end
59
77
  end
60
78
 
79
+ # Returns the overall width of the barcode in mils.
61
80
  def width
62
81
  if self.valid?
63
82
  return (((self.encoded_data.length * 2) - 1) * 20)
64
83
  end
65
84
  return 0
66
85
  end
67
-
86
+
87
+ # Returns the overall height of the barcode in mils.
68
88
  def height
69
89
  125
70
90
  end
71
91
 
92
+ # Determines whether or not the barcode data to be encoded
93
+ # is valid.
94
+ # Data length must be 12 or 14 digits
72
95
  def valid?
73
96
  @data.each_byte do |char|
74
97
  if self._encode_character(char).nil?