barcodes 0.0.1

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 (68) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +22 -0
  6. data/README.md +107 -0
  7. data/Rakefile +11 -0
  8. data/barcodes.gemspec +24 -0
  9. data/bin/barcodes +6 -0
  10. data/lib/barcodes.rb +63 -0
  11. data/lib/barcodes/exec.rb +74 -0
  12. data/lib/barcodes/renderer.rb +9 -0
  13. data/lib/barcodes/renderer/ascii.rb +199 -0
  14. data/lib/barcodes/renderer/pdf.rb +257 -0
  15. data/lib/barcodes/symbology.rb +47 -0
  16. data/lib/barcodes/symbology/base.rb +103 -0
  17. data/lib/barcodes/symbology/codabar.rb +36 -0
  18. data/lib/barcodes/symbology/code11.rb +74 -0
  19. data/lib/barcodes/symbology/code128.rb +317 -0
  20. data/lib/barcodes/symbology/code39.rb +48 -0
  21. data/lib/barcodes/symbology/code39extended.rb +93 -0
  22. data/lib/barcodes/symbology/code39extendedmod43.rb +73 -0
  23. data/lib/barcodes/symbology/code39mod43.rb +73 -0
  24. data/lib/barcodes/symbology/code93.rb +142 -0
  25. data/lib/barcodes/symbology/code93extended.rb +116 -0
  26. data/lib/barcodes/symbology/ean.rb +149 -0
  27. data/lib/barcodes/symbology/ean13.rb +27 -0
  28. data/lib/barcodes/symbology/ean8.rb +50 -0
  29. data/lib/barcodes/symbology/interleaved2of5.rb +30 -0
  30. data/lib/barcodes/symbology/interleaved2of5mod10.rb +40 -0
  31. data/lib/barcodes/symbology/msi.rb +30 -0
  32. data/lib/barcodes/symbology/msimod10.rb +50 -0
  33. data/lib/barcodes/symbology/msimod11.rb +37 -0
  34. data/lib/barcodes/symbology/planet.rb +83 -0
  35. data/lib/barcodes/symbology/postnet.rb +83 -0
  36. data/lib/barcodes/symbology/standard2of5.rb +30 -0
  37. data/lib/barcodes/symbology/standard2of5mod10.rb +40 -0
  38. data/lib/barcodes/symbology/upca.rb +27 -0
  39. data/lib/barcodes/version.rb +3 -0
  40. data/spec/barcodes/exec_spec.rb +14 -0
  41. data/spec/barcodes/renderer/ascii_spec.rb +12 -0
  42. data/spec/barcodes/renderer/pdf_spec.rb +12 -0
  43. data/spec/barcodes/symbology/base_spec.rb +8 -0
  44. data/spec/barcodes/symbology/codabar_spec.rb +8 -0
  45. data/spec/barcodes/symbology/code11_spec.rb +8 -0
  46. data/spec/barcodes/symbology/code128_spec.rb +8 -0
  47. data/spec/barcodes/symbology/code39_spec.rb +8 -0
  48. data/spec/barcodes/symbology/code39extended_spec.rb +8 -0
  49. data/spec/barcodes/symbology/code39extendedmod43_spec.rb +8 -0
  50. data/spec/barcodes/symbology/code39mod43_spec.rb +8 -0
  51. data/spec/barcodes/symbology/code93_spec.rb +8 -0
  52. data/spec/barcodes/symbology/code93extended_spec.rb +8 -0
  53. data/spec/barcodes/symbology/ean13_spec.rb +8 -0
  54. data/spec/barcodes/symbology/ean8_spec.rb +8 -0
  55. data/spec/barcodes/symbology/ean_spec.rb +8 -0
  56. data/spec/barcodes/symbology/interleaved2of5_spec.rb +8 -0
  57. data/spec/barcodes/symbology/interleaved2of5mod10_spec.rb +8 -0
  58. data/spec/barcodes/symbology/msi_spec.rb +8 -0
  59. data/spec/barcodes/symbology/msimod10_spec.rb +8 -0
  60. data/spec/barcodes/symbology/msimod11_spec.rb +8 -0
  61. data/spec/barcodes/symbology/planet_spec.rb +8 -0
  62. data/spec/barcodes/symbology/postnet_spec.rb +8 -0
  63. data/spec/barcodes/symbology/standard2of5_spec.rb +8 -0
  64. data/spec/barcodes/symbology/standard2of5mod10_spec.rb +8 -0
  65. data/spec/barcodes/symbology/upca_spec.rb +8 -0
  66. data/spec/barcodes_spec.rb +27 -0
  67. data/spec/spec_helper.rb +17 -0
  68. metadata +174 -0
@@ -0,0 +1,27 @@
1
+ require 'barcodes/symbology/ean'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Ean13 < Ean
6
+ def initialize(args={})
7
+ unless args.has_key? :data
8
+ args[:data] = '012345678999'
9
+ end
10
+
11
+ super(args)
12
+ end
13
+
14
+ def formatted_data
15
+ @start_character + @data[0..6] + @center_character + @data[7..12] + self.checksum + @stop_character
16
+ end
17
+
18
+ def valid?
19
+ unless super
20
+ return false
21
+ end
22
+
23
+ return self.data.length == 12 ? true : false
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,50 @@
1
+ require 'barcodes/symbology/ean'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Ean8 < Ean
6
+ def initialize(args={})
7
+ unless args.has_key? :data
8
+ args[:data] = '0123456'
9
+ end
10
+
11
+ super(args)
12
+ end
13
+
14
+ def formatted_data
15
+ @start_character + @data[0..3] + @center_character + @data[4..6] + self.checksum + @stop_character
16
+ end
17
+
18
+ def encoded_data
19
+ if self.valid?
20
+ formatted_data = self.formatted_data
21
+ encoded_data = ""
22
+ index = 0
23
+ formatted_data.each_byte do |char|
24
+ if char.chr == 'S'
25
+ encoded_data += "101"
26
+ elsif char.chr == 'C'
27
+ encoded_data += "01010"
28
+ else
29
+ if index < 6
30
+ encoded_data += self._encode_character_with_parity(char, '1')
31
+ else
32
+ encoded_data += self._encode_character_with_right_hand(char)
33
+ end
34
+ end
35
+ index += 1
36
+ end
37
+ encoded_data
38
+ end
39
+ end
40
+
41
+ def valid?
42
+ unless super
43
+ return false
44
+ end
45
+
46
+ return self.data.length == 7 ? true : false
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ require 'barcodes/symbology/base'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Interleaved2Of5 < Base
6
+ def self.charset
7
+ ['0','1','2','3','4','5','6','7','8','9','S','E'].collect {|c| c.bytes.to_a[0] }
8
+ end
9
+
10
+ def self.valueset
11
+ [
12
+ '101011011010','110101010110','101101010110','110110101010',
13
+ '101011010110','110101101010','101101101010','101010110110',
14
+ '110101011010','101101011010','1010','1101'
15
+ ]
16
+ end
17
+
18
+ def initialize(args={})
19
+ super(args)
20
+
21
+ @start_character = 'S'
22
+ @stop_character = 'E'
23
+ end
24
+
25
+ def formatted_data
26
+ @start_character + @data + @stop_character
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ require 'barcodes/symbology/interleaved2of5'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Interleaved2Of5Mod10 < Interleaved2Of5
6
+ def formatted_data
7
+ checksum = self.checksum
8
+ unless checksum.nil?
9
+ @start_character + @data + checksum + @stop_character
10
+ end
11
+ end
12
+
13
+ def checksum
14
+ even_sum = 0
15
+ odd_sum = 0
16
+ index = 0
17
+ @data.reverse.each_char do |char|
18
+ if ('0'..'9').include? char
19
+ if index.even?
20
+ even_sum += char.to_i * 3
21
+ else
22
+ odd_sum += char.to_i
23
+ end
24
+ end
25
+ index += 1
26
+ end
27
+ sum = even_sum + odd_sum
28
+
29
+ value = 10 - (sum % 10)
30
+ if value == 10
31
+ value = 0
32
+ end
33
+
34
+ if (0..9).include? value
35
+ return value.to_s
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ require 'barcodes/symbology/base'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Msi < Base
6
+ def self.charset
7
+ ['0','1','2','3','4','5','6','7','8','9','S','E'].collect {|c| c.bytes.to_a[0] }
8
+ end
9
+
10
+ def self.valueset
11
+ [
12
+ '100100100100','100100100110','100100110100','100100110110',
13
+ '100110100100','100110100110','100110110100','100110110110',
14
+ '110100100100','110100100110','110','1001'
15
+ ]
16
+ end
17
+
18
+ def initialize(args={})
19
+ super(args)
20
+
21
+ @start_character = 'S'
22
+ @stop_character = 'E'
23
+ end
24
+
25
+ def formatted_data
26
+ @start_character + @data + @stop_character
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,50 @@
1
+ require 'barcodes/symbology/msi'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class MsiMod10 < Msi
6
+ def formatted_data
7
+ checksum = self.checksum
8
+ unless checksum.nil?
9
+ @start_character + @data + checksum + @stop_character
10
+ end
11
+ end
12
+
13
+ def checksum
14
+ if self.valid?
15
+ used_nums = []
16
+ unused_nums = []
17
+ index = 0
18
+ data.reverse.each_char do |char|
19
+ if index.even?
20
+ used_nums << char.to_i
21
+ else
22
+ unused_nums << char.to_i
23
+ end
24
+ index += 1
25
+ end
26
+ used_nums.reverse!
27
+ unused_nums.reverse!
28
+
29
+ sum = 0
30
+ (used_nums.join.to_i * 2).to_s.split(//).each do |n|
31
+ sum += n.to_i
32
+ end
33
+ unused_nums.each do |n|
34
+ sum += n
35
+ end
36
+
37
+ value = 10 - (sum % 10)
38
+
39
+ if value == 10
40
+ value = 0
41
+ end
42
+
43
+ if (0..9).include? value
44
+ return value.to_s
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,37 @@
1
+ require 'barcodes/symbology/msi'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class MsiMod11 < Msi
6
+ def formatted_data
7
+ checksum = self.checksum
8
+ unless checksum.nil?
9
+ @start_character + @data + checksum + @stop_character
10
+ end
11
+ end
12
+
13
+ def checksum
14
+ if self.valid?
15
+ sum = 0
16
+ weight = 2
17
+ data.reverse.each_char do |char|
18
+ if ('0'..'9').include? char
19
+ sum += weight * char.to_i
20
+ end
21
+ if weight < 7
22
+ weight += 1
23
+ else
24
+ weight = 2
25
+ end
26
+ end
27
+
28
+ value = sum % 11
29
+
30
+ if (0..9).include? value
31
+ return value.to_s
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,83 @@
1
+ require 'barcodes/symbology/base'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Planet < Base
6
+ def self.charset
7
+ ['0','1','2','3','4','5','6','7','8','9'].collect {|c| c.bytes.to_a[0] }
8
+ end
9
+
10
+ def self.valueset
11
+ [
12
+ '00111','11100','11010','11001',
13
+ '10110','10101','10011','01110',
14
+ '01101','01011'
15
+ ]
16
+ end
17
+
18
+ def initialize(args={})
19
+ unless args.has_key? :data
20
+ args[:data] = '012345678999'
21
+ end
22
+
23
+ super(args)
24
+ end
25
+
26
+ def formatted_data
27
+ checksum = self.checksum
28
+ unless checksum.nil?
29
+ @data + checksum
30
+ end
31
+ end
32
+
33
+ def encoded_data
34
+ if self.valid?
35
+ encoded_data = ''
36
+ self.formatted_data.each_byte do |char|
37
+ encoded_data += self._encode_character char
38
+ end
39
+ return '1' + encoded_data + '1'
40
+ end
41
+ end
42
+
43
+ def checksum
44
+ if self.valid?
45
+ sum = 0
46
+ @data.each_char do |char|
47
+ sum += char.to_i
48
+ end
49
+
50
+ value = 10 - (sum % 10)
51
+ if value == 10
52
+ value = 0
53
+ end
54
+
55
+ if (0..9).include? value
56
+ return value.to_s
57
+ end
58
+ end
59
+ end
60
+
61
+ def width
62
+ if self.valid?
63
+ return (((self.encoded_data.length * 2) - 1) * 20)
64
+ end
65
+ return 0
66
+ end
67
+
68
+ def height
69
+ 125
70
+ end
71
+
72
+ def valid?
73
+ @data.each_byte do |char|
74
+ if self._encode_character(char).nil?
75
+ return false
76
+ end
77
+ end
78
+
79
+ return @data.length == 12 || @data.length == 14
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,83 @@
1
+ require 'barcodes/symbology/base'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Postnet < Base
6
+ def self.charset
7
+ ['0','1','2','3','4','5','6','7','8','9'].collect {|c| c.bytes.to_a[0] }
8
+ end
9
+
10
+ def self.valueset
11
+ [
12
+ '11000','00011','00101','00110',
13
+ '01001','01010','01100','10001',
14
+ '10010','10100'
15
+ ]
16
+ end
17
+
18
+ def initialize(args={})
19
+ unless args.has_key? :data
20
+ args[:data] = '01234'
21
+ end
22
+
23
+ super(args)
24
+ end
25
+
26
+ def formatted_data
27
+ checksum = self.checksum
28
+ unless checksum.nil?
29
+ @data + checksum
30
+ end
31
+ end
32
+
33
+ def encoded_data
34
+ if self.valid?
35
+ encoded_data = ''
36
+ self.formatted_data.each_byte do |char|
37
+ encoded_data += self._encode_character char
38
+ end
39
+ return '1' + encoded_data + '1'
40
+ end
41
+ end
42
+
43
+ def checksum
44
+ if self.valid?
45
+ sum = 0
46
+ @data.each_char do |char|
47
+ sum += char.to_i
48
+ end
49
+
50
+ value = 10 - (sum % 10)
51
+ if value == 10
52
+ value = 0
53
+ end
54
+
55
+ if (0..9).include? value
56
+ return value.to_s
57
+ end
58
+ end
59
+ end
60
+
61
+ def width
62
+ if self.valid?
63
+ return (((self.encoded_data.length * 2) - 1) * 20)
64
+ end
65
+ return 0
66
+ end
67
+
68
+ def height
69
+ 125
70
+ end
71
+
72
+ def valid?
73
+ @data.each_byte do |char|
74
+ if self._encode_character(char).nil?
75
+ return false
76
+ end
77
+ end
78
+
79
+ return @data.length == 5 || @data.length == 9 || @data.length == 11
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,30 @@
1
+ require 'barcodes/symbology/base'
2
+
3
+ module Barcodes
4
+ module Symbology
5
+ class Standard2Of5 < Base
6
+ def self.charset
7
+ ['0','1','2','3','4','5','6','7','8','9','S','E'].collect {|c| c.bytes.to_a[0] }
8
+ end
9
+
10
+ def self.valueset
11
+ [
12
+ '10101110111010','11101010101110','10111010101110','11101110101010',
13
+ '10101110101110','11101011101010','10111011101010','10101011101110',
14
+ '11101010111010','10111010111010','11011010','1101011'
15
+ ]
16
+ end
17
+
18
+ def initialize(args={})
19
+ super(args)
20
+
21
+ @start_character = 'S'
22
+ @stop_character = 'E'
23
+ end
24
+
25
+ def formatted_data
26
+ @start_character + @data + @stop_character
27
+ end
28
+ end
29
+ end
30
+ end