barcode1dtools 1.0.1.0 → 1.0.2.0
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.
- data/lib/barcode1dtools.rb +174 -61
- data/lib/barcode1dtools/codabar.rb +40 -28
- data/lib/barcode1dtools/code11.rb +34 -23
- data/lib/barcode1dtools/code128.rb +56 -27
- data/lib/barcode1dtools/code3of9.rb +42 -28
- data/lib/barcode1dtools/code93.rb +41 -26
- data/lib/barcode1dtools/coop2of5.rb +36 -25
- data/lib/barcode1dtools/ean13.rb +97 -89
- data/lib/barcode1dtools/ean8.rb +39 -27
- data/lib/barcode1dtools/iata2of5.rb +32 -23
- data/lib/barcode1dtools/industrial2of5.rb +33 -24
- data/lib/barcode1dtools/interleaved2of5.rb +30 -26
- data/lib/barcode1dtools/matrix2of5.rb +33 -24
- data/lib/barcode1dtools/msi.rb +35 -23
- data/lib/barcode1dtools/plessey.rb +33 -23
- data/lib/barcode1dtools/postnet.rb +29 -21
- data/lib/barcode1dtools/upc_a.rb +47 -23
- data/lib/barcode1dtools/upc_e.rb +67 -31
- data/lib/barcode1dtools/upc_supplemental_2.rb +50 -20
- data/lib/barcode1dtools/upc_supplemental_5.rb +49 -18
- metadata +13 -13
data/lib/barcode1dtools/ean8.rb
CHANGED
@@ -9,34 +9,36 @@ require 'barcode1dtools/ean13'
|
|
9
9
|
|
10
10
|
module Barcode1DTools
|
11
11
|
|
12
|
-
# Barcode1DTools::EAN_8 - Create pattern for EAN-8 barcodes
|
13
|
-
|
12
|
+
# Barcode1DTools::EAN_8 - Create pattern for EAN-8 barcodes.
|
14
13
|
# The value encoded is a 7-digit number, and a checksum digit will
|
15
14
|
# be added. You can add the option # :checksum_included => true
|
16
15
|
# when initializing to specify that you have already included a
|
17
16
|
# checksum.
|
18
17
|
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
18
|
+
# == Example
|
19
|
+
# num = '96385074'
|
20
|
+
# bc = Barcode1DTools::EAN8.new(num)
|
21
|
+
# pattern = bc.bars
|
22
|
+
# rle_pattern = bc.rle
|
23
|
+
# width = bc.width
|
24
|
+
# check_digit = Barcode1DTools::EAN83.generate_check_digit_for(num)
|
25
|
+
#
|
26
|
+
# == Other Information
|
25
27
|
#
|
26
28
|
# The object created is immutable.
|
27
29
|
#
|
30
|
+
# == Formats
|
28
31
|
# There are two formats for the returned pattern (wn format is
|
29
32
|
# not available):
|
30
33
|
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
# in the string renders to a single unit width.
|
34
|
+
# *bars* - 1s and 0s specifying black lines and white spaces. Actual
|
35
|
+
# characters can be changed from "1" and 0" with options
|
36
|
+
# :line_character and :space_character.
|
35
37
|
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
38
|
+
# *rle* - Run-length-encoded version of the pattern. The first
|
39
|
+
# number is always a black line, with subsequent digits
|
40
|
+
# alternating between spaces and lines. The digits specify
|
41
|
+
# the width of each line or space.
|
40
42
|
#
|
41
43
|
# The "width" method will tell you the total end-to-end width, in
|
42
44
|
# units, of the entire barcode.
|
@@ -45,19 +47,19 @@ module Barcode1DTools
|
|
45
47
|
# EAN & UPC style barcodes because the bars and spaces are variable width from
|
46
48
|
# 1 to 4 units.
|
47
49
|
#
|
48
|
-
#
|
50
|
+
# An EAN-8 barcode has 3 elements:
|
49
51
|
# 1. A 2 or 3 digit "number system" designation
|
50
52
|
# 2. A 4 or 5 digit manufacturer's code
|
51
53
|
# 3. A single digit checksum
|
52
54
|
#
|
53
55
|
# Note than an EAN-8 is not analogous to a UPC-E. In particular, there
|
54
56
|
# is no way to create an EAN-13 from and EAN-8 and vice versa. The
|
55
|
-
# numbers are assigned within
|
57
|
+
# numbers are assigned within EAN-8 by a central authority.
|
56
58
|
#
|
57
59
|
# The bar patterns are the same as EAN-13, with nothing encoded in the
|
58
60
|
# parity. All bars on the left use the "odd" parity set.
|
59
61
|
#
|
60
|
-
#
|
62
|
+
# == Rendering
|
61
63
|
#
|
62
64
|
# When rendered, two sets of four digits are shown at the bottom of the
|
63
65
|
# code, aligned with the bottom of the code, and with the middle guard
|
@@ -66,15 +68,22 @@ module Barcode1DTools
|
|
66
68
|
|
67
69
|
class EAN8 < Barcode1D
|
68
70
|
|
69
|
-
# patterns
|
71
|
+
# Left patterns from EAN-13
|
70
72
|
LEFT_PATTERNS = EAN13::LEFT_PATTERNS
|
73
|
+
# Left rle patterns from EAN-13
|
71
74
|
LEFT_PATTERNS_RLE = EAN13::LEFT_PATTERNS_RLE
|
75
|
+
# Right patterns from EAN-13
|
72
76
|
RIGHT_PATTERNS = EAN13::RIGHT_PATTERNS
|
77
|
+
# Right rle patterns from EAN-13
|
73
78
|
RIGHT_PATTERNS_RLE = EAN13::RIGHT_PATTERNS_RLE
|
74
79
|
|
80
|
+
# Guard pattern from EAN-13
|
75
81
|
SIDE_GUARD_PATTERN=EAN13::SIDE_GUARD_PATTERN
|
82
|
+
# Middle Guard pattern from EAN-13
|
76
83
|
MIDDLE_GUARD_PATTERN=EAN13::MIDDLE_GUARD_PATTERN
|
84
|
+
# Guard pattern from EAN-13 as an rle
|
77
85
|
SIDE_GUARD_PATTERN_RLE=EAN13::SIDE_GUARD_PATTERN_RLE
|
86
|
+
# Middle Guard pattern from EAN-13 as an rle
|
78
87
|
MIDDLE_GUARD_PATTERN_RLE=EAN13::MIDDLE_GUARD_PATTERN_RLE
|
79
88
|
|
80
89
|
DEFAULT_OPTIONS = {
|
@@ -82,12 +91,13 @@ module Barcode1DTools
|
|
82
91
|
:space_character => '0'
|
83
92
|
}
|
84
93
|
|
85
|
-
# Specific for EAN
|
94
|
+
# Specific for EAN-8 - the number system part of the payload
|
86
95
|
attr_reader :number_system
|
96
|
+
# Specific for EAN-8 - the product code part of the payload
|
87
97
|
attr_reader :product_code
|
88
98
|
|
89
99
|
class << self
|
90
|
-
#
|
100
|
+
# Returns true if value can be encoded in EAN-8 - must be 7-8 digits.
|
91
101
|
def can_encode?(value, options = nil)
|
92
102
|
if !options
|
93
103
|
value.to_s =~ /^\d{7,8}$/
|
@@ -107,7 +117,7 @@ module Barcode1DTools
|
|
107
117
|
(10 - (value % 10)) % 10
|
108
118
|
end
|
109
119
|
|
110
|
-
#
|
120
|
+
# Validates the check digit given a string - assumes check digit
|
111
121
|
# is last digit of string.
|
112
122
|
def validate_check_digit_for(value)
|
113
123
|
raise UnencodableCharactersError unless self.can_encode?(value, :checksum_included => true)
|
@@ -192,6 +202,7 @@ module Barcode1DTools
|
|
192
202
|
end
|
193
203
|
end
|
194
204
|
|
205
|
+
# Create an EAN8 object with a given value.
|
195
206
|
# Options are :line_character, :space_character, and
|
196
207
|
# :checksum_included.
|
197
208
|
def initialize(value, options = {})
|
@@ -217,12 +228,13 @@ module Barcode1DTools
|
|
217
228
|
@number_system, @product_code = md[1], md[2]
|
218
229
|
end
|
219
230
|
|
220
|
-
#
|
231
|
+
# EAN-based codes cannot create a w/n string, so this
|
232
|
+
# will raise an error.
|
221
233
|
def wn
|
222
234
|
raise NotImplementedError
|
223
235
|
end
|
224
236
|
|
225
|
-
# returns a run-length-encoded string representation
|
237
|
+
# returns a run-length-encoded string representation.
|
226
238
|
def rle
|
227
239
|
if @rle
|
228
240
|
@rle
|
@@ -232,12 +244,12 @@ module Barcode1DTools
|
|
232
244
|
end
|
233
245
|
end
|
234
246
|
|
235
|
-
#
|
247
|
+
# Returns 1s and 0s (for "black" and "white")
|
236
248
|
def bars
|
237
249
|
@bars ||= self.class.rle_to_bars(self.rle, @options)
|
238
250
|
end
|
239
251
|
|
240
|
-
#
|
252
|
+
# Returns the total unit width of the bar code.
|
241
253
|
def width
|
242
254
|
@width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
|
243
255
|
end
|
@@ -20,11 +20,12 @@ module Barcode1DTools
|
|
20
20
|
# IATA 2 of 5 is low-density and limited. It should not be
|
21
21
|
# used in any new applications.
|
22
22
|
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
23
|
+
# == Example
|
24
|
+
# val = "3423"
|
25
|
+
# bc = Barcode1DTools::IATA2of5.new(val)
|
26
|
+
# pattern = bc.bars
|
27
|
+
# rle_pattern = bc.rle
|
28
|
+
# width = bc.width
|
28
29
|
#
|
29
30
|
# The object created is immutable.
|
30
31
|
#
|
@@ -35,27 +36,28 @@ module Barcode1DTools
|
|
35
36
|
# IATA2of5 characters consist of 3 bars and 2 spaces, with a narrow
|
36
37
|
# space between them. 2 of the bars/spaces in each symbol are wide.
|
37
38
|
#
|
39
|
+
# == Formats
|
38
40
|
# There are three formats for the returned pattern:
|
39
41
|
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
42
|
+
# *bars* - 1s and 0s specifying black lines and white spaces. Actual
|
43
|
+
# characters can be changed from "1" and 0" with options
|
44
|
+
# :line_character and :space_character.
|
43
45
|
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
46
|
+
# *rle* - Run-length-encoded version of the pattern. The first
|
47
|
+
# number is always a black line, with subsequent digits
|
48
|
+
# alternating between spaces and lines. The digits specify
|
49
|
+
# the width of each line or space.
|
48
50
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
51
|
+
# *wn* - The native format for this barcode type. The string
|
52
|
+
# consists of a series of "w" and "n" characters. The first
|
53
|
+
# item is always a black line, with subsequent characters
|
54
|
+
# alternating between spaces and lines. A "wide" item
|
55
|
+
# is twice the width of a "narrow" item.
|
54
56
|
#
|
55
57
|
# The "width" method will tell you the total end-to-end width, in
|
56
58
|
# units, of the entire barcode.
|
57
59
|
#
|
58
|
-
|
60
|
+
# == Rendering
|
59
61
|
#
|
60
62
|
# The standard w/n ratio seems to be 2:1. There seem to be no real
|
61
63
|
# standards for display.
|
@@ -82,7 +84,9 @@ module Barcode1DTools
|
|
82
84
|
'9'=> {'val'=>9 ,'wn'=>'nnwnnnwnn'}
|
83
85
|
}
|
84
86
|
|
87
|
+
# Left guard pattern
|
85
88
|
GUARD_PATTERN_LEFT_WN = 'nnn'
|
89
|
+
# Right guard pattern
|
86
90
|
GUARD_PATTERN_RIGHT_WN = 'wnn'
|
87
91
|
|
88
92
|
DEFAULT_OPTIONS = {
|
@@ -94,11 +98,13 @@ module Barcode1DTools
|
|
94
98
|
}
|
95
99
|
|
96
100
|
class << self
|
97
|
-
#
|
101
|
+
# Returns true if passed a string of digits.
|
102
|
+
# IATA2of5 can encode digits.
|
98
103
|
def can_encode?(value)
|
99
104
|
value.to_s =~ /\A[0-9]+\z/
|
100
105
|
end
|
101
106
|
|
107
|
+
# Generates a check digit for a given value using the Luhn algorithm.
|
102
108
|
def generate_check_digit_for(value)
|
103
109
|
raise UnencodableCharactersError unless self.can_encode?(value)
|
104
110
|
mult = 3
|
@@ -106,13 +112,15 @@ module Barcode1DTools
|
|
106
112
|
(10 - (value % 10)) % 10
|
107
113
|
end
|
108
114
|
|
115
|
+
# Returns true if the final digit if the given string is a valid
|
116
|
+
# check digit for the rest of the string.
|
109
117
|
def validate_check_digit_for(value)
|
110
118
|
raise UnencodableCharactersError unless self.can_encode?(value)
|
111
119
|
md = value.match(/^(\d+?)(\d)$/)
|
112
120
|
self.generate_check_digit_for(md[1]) == md[2].to_i
|
113
121
|
end
|
114
122
|
|
115
|
-
# Decode a string in rle format. This will return a IATA2of5
|
123
|
+
# Decode a string in rle or w/n format. This will return a IATA2of5
|
116
124
|
# object.
|
117
125
|
def decode(str, options = {})
|
118
126
|
if str =~ /[^1-3]/ && str =~ /[^wn]/
|
@@ -164,6 +172,7 @@ module Barcode1DTools
|
|
164
172
|
|
165
173
|
end
|
166
174
|
|
175
|
+
# Create a new IATA2of5 object with the given value.
|
167
176
|
# Options are :line_character, :space_character, :w_character,
|
168
177
|
# :n_character, :checksum_included, and :skip_checksum.
|
169
178
|
def initialize(value, options = {})
|
@@ -196,17 +205,17 @@ module Barcode1DTools
|
|
196
205
|
@wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
|
197
206
|
end
|
198
207
|
|
199
|
-
#
|
208
|
+
# Returns a run-length-encoded string representation
|
200
209
|
def rle
|
201
210
|
@rle ||= self.class.wn_to_rle(self.wn, @options)
|
202
211
|
end
|
203
212
|
|
204
|
-
#
|
213
|
+
# Returns 1s and 0s (for "black" and "white")
|
205
214
|
def bars
|
206
215
|
@bars ||= self.class.rle_to_bars(self.rle, @options)
|
207
216
|
end
|
208
217
|
|
209
|
-
#
|
218
|
+
# Returns the total unit width of the bar code
|
210
219
|
def width
|
211
220
|
@width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
|
212
221
|
end
|
@@ -19,11 +19,12 @@ module Barcode1DTools
|
|
19
19
|
# Industrial 2 of 5 is low-density and limited. It should not be
|
20
20
|
# used in any new applications.
|
21
21
|
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
22
|
+
# == Example
|
23
|
+
# val = "3423"
|
24
|
+
# bc = Barcode1DTools::Industrial2of5.new(val)
|
25
|
+
# pattern = bc.bars
|
26
|
+
# rle_pattern = bc.rle
|
27
|
+
# width = bc.width
|
27
28
|
#
|
28
29
|
# The object created is immutable.
|
29
30
|
#
|
@@ -34,27 +35,28 @@ module Barcode1DTools
|
|
34
35
|
# Industrial2of5 characters consist of 3 bars and 2 spaces, with a narrow
|
35
36
|
# space between them. 2 of the bars/spaces in each symbol are wide.
|
36
37
|
#
|
38
|
+
# == Formats
|
37
39
|
# There are three formats for the returned pattern:
|
38
40
|
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
41
|
+
# *bars* - 1s and 0s specifying black lines and white spaces. Actual
|
42
|
+
# characters can be changed from "1" and 0" with options
|
43
|
+
# :line_character and :space_character.
|
42
44
|
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
45
|
+
# *rle* - Run-length-encoded version of the pattern. The first
|
46
|
+
# number is always a black line, with subsequent digits
|
47
|
+
# alternating between spaces and lines. The digits specify
|
48
|
+
# the width of each line or space.
|
47
49
|
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
50
|
+
# *wn* - The native format for this barcode type. The string
|
51
|
+
# consists of a series of "w" and "n" characters. The first
|
52
|
+
# item is always a black line, with subsequent characters
|
53
|
+
# alternating between spaces and lines. A "wide" item
|
54
|
+
# is twice the width of a "narrow" item.
|
53
55
|
#
|
54
56
|
# The "width" method will tell you the total end-to-end width, in
|
55
57
|
# units, of the entire barcode.
|
56
58
|
#
|
57
|
-
|
59
|
+
# == Rendering
|
58
60
|
#
|
59
61
|
# The standard w/n ratio seems to be 2:1. There seem to be no real
|
60
62
|
# standards for display.
|
@@ -81,7 +83,9 @@ module Barcode1DTools
|
|
81
83
|
'9'=> {'val'=>9 ,'wn'=>'nnwnnnwnn'}
|
82
84
|
}
|
83
85
|
|
86
|
+
# Guard pattern for the left side.
|
84
87
|
GUARD_PATTERN_LEFT_WN = 'wnwnn'
|
88
|
+
# Guard pattern for the right side.
|
85
89
|
GUARD_PATTERN_RIGHT_WN = 'wnnnw'
|
86
90
|
|
87
91
|
DEFAULT_OPTIONS = {
|
@@ -94,11 +98,13 @@ module Barcode1DTools
|
|
94
98
|
}
|
95
99
|
|
96
100
|
class << self
|
97
|
-
#
|
101
|
+
# Returns true if the string is encodable in this
|
102
|
+
# symbology. Industrial2of5 can encode digits.
|
98
103
|
def can_encode?(value)
|
99
104
|
value.to_s =~ /\A[0-9]+\z/
|
100
105
|
end
|
101
106
|
|
107
|
+
# Generates a check digit for a given value using the Luhn algorithm.
|
102
108
|
def generate_check_digit_for(value)
|
103
109
|
raise UnencodableCharactersError unless self.can_encode?(value)
|
104
110
|
mult = 3
|
@@ -106,13 +112,15 @@ module Barcode1DTools
|
|
106
112
|
(10 - (value % 10)) % 10
|
107
113
|
end
|
108
114
|
|
115
|
+
# Returns true if the final digit if the given string is a valid
|
116
|
+
# check digit for the rest of the string.
|
109
117
|
def validate_check_digit_for(value)
|
110
118
|
raise UnencodableCharactersError unless self.can_encode?(value)
|
111
119
|
md = value.match(/^(\d+?)(\d)$/)
|
112
120
|
self.generate_check_digit_for(md[1]) == md[2].to_i
|
113
121
|
end
|
114
122
|
|
115
|
-
# Decode a string in rle format. This will return a Industrial2of5
|
123
|
+
# Decode a string in rle or w/n format. This will return a Industrial2of5
|
116
124
|
# object.
|
117
125
|
def decode(str, options = {})
|
118
126
|
if str =~ /[^1-3]/ && str =~ /[^wn]/
|
@@ -164,6 +172,7 @@ module Barcode1DTools
|
|
164
172
|
|
165
173
|
end
|
166
174
|
|
175
|
+
# Create a new Industrial2of5 object with the given value.
|
167
176
|
# Options are :line_character, :space_character, :w_character,
|
168
177
|
# :n_character, :checksum_included, and :skip_checksum.
|
169
178
|
def initialize(value, options = {})
|
@@ -191,22 +200,22 @@ module Barcode1DTools
|
|
191
200
|
end
|
192
201
|
end
|
193
202
|
|
194
|
-
# Returns a string of "w" or "n" ("wide" and "narrow")
|
203
|
+
# Returns a string of "w" or "n" ("wide" and "narrow").
|
195
204
|
def wn
|
196
205
|
@wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
|
197
206
|
end
|
198
207
|
|
199
|
-
#
|
208
|
+
# Returns a run-length-encoded string representation
|
200
209
|
def rle
|
201
210
|
@rle ||= self.class.wn_to_rle(self.wn, @options)
|
202
211
|
end
|
203
212
|
|
204
|
-
#
|
213
|
+
# Returns 1s and 0s (for "black" and "white")
|
205
214
|
def bars
|
206
215
|
@bars ||= self.class.rle_to_bars(self.rle, @options)
|
207
216
|
end
|
208
217
|
|
209
|
-
#
|
218
|
+
# Returns the total unit width of the bar code
|
210
219
|
def width
|
211
220
|
@width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
|
212
221
|
end
|
@@ -22,13 +22,14 @@ module Barcode1DTools
|
|
22
22
|
# an odd number of digits is encoded with 5 narrow spaces for the last
|
23
23
|
# digit. We do not encode this way but can handle decoding.
|
24
24
|
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
25
|
+
# == Example
|
26
|
+
# num = 238982
|
27
|
+
# bc = Barcode1DTools::Interleaved2of5.new(num)
|
28
|
+
# pattern = bc.bars
|
29
|
+
# rle_pattern = bc.rle
|
30
|
+
# wn_pattern = bc.wn
|
31
|
+
# width = bc.width
|
32
|
+
# check_digit = Barcode1DTools::Interleaved2of5.generate_check_digit_for(num)
|
32
33
|
#
|
33
34
|
# The object created is immutable.
|
34
35
|
#
|
@@ -40,22 +41,23 @@ module Barcode1DTools
|
|
40
41
|
# "narrow", with "wide" lines or spaces being twice the width of
|
41
42
|
# narrow lines or spaces.
|
42
43
|
#
|
44
|
+
# == Formats
|
43
45
|
# There are three formats for the returned pattern:
|
44
46
|
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
47
|
+
# *bars* - 1s and 0s specifying black lines and white spaces. Actual
|
48
|
+
# characters can be changed from "1" and 0" with options
|
49
|
+
# :line_character and :space_character.
|
48
50
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
51
|
+
# *rle* - Run-length-encoded version of the pattern. The first
|
52
|
+
# number is always a black line, with subsequent digits
|
53
|
+
# alternating between spaces and lines. The digits specify
|
54
|
+
# the width of each line or space.
|
53
55
|
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
56
|
+
# *wn* - The native format for this barcode type. The string
|
57
|
+
# consists of a series of "w" and "n" characters. The first
|
58
|
+
# item is always a black line, with subsequent characters
|
59
|
+
# alternating between spaces and lines. A "wide" item
|
60
|
+
# is twice the width of a "narrow" item.
|
59
61
|
#
|
60
62
|
# The "width" method will tell you the total end-to-end width, in
|
61
63
|
# units, of the entire barcode.
|
@@ -71,7 +73,7 @@ module Barcode1DTools
|
|
71
73
|
|
72
74
|
class Interleaved2of5 < Barcode1D
|
73
75
|
|
74
|
-
# patterns and
|
76
|
+
# The patterns for each number and the guard patterns
|
75
77
|
PATTERNS = {
|
76
78
|
'start' => 'nnnn',
|
77
79
|
'0' => 'nnwwn',
|
@@ -87,6 +89,7 @@ module Barcode1DTools
|
|
87
89
|
'stop' => 'wnn'
|
88
90
|
}
|
89
91
|
|
92
|
+
# Default w/n ratio.
|
90
93
|
WN_RATIO = 2
|
91
94
|
|
92
95
|
DEFAULT_OPTIONS = {
|
@@ -98,7 +101,7 @@ module Barcode1DTools
|
|
98
101
|
}
|
99
102
|
|
100
103
|
class << self
|
101
|
-
#
|
104
|
+
# Returns true if the value can be encoded.
|
102
105
|
def can_encode?(value)
|
103
106
|
value.is_a?(Integer) || value.to_s =~ /^[0-9]+$/
|
104
107
|
end
|
@@ -115,7 +118,7 @@ module Barcode1DTools
|
|
115
118
|
(10 - (value % 10)) % 10
|
116
119
|
end
|
117
120
|
|
118
|
-
#
|
121
|
+
# Validates the check digit given a string - assumes check digit
|
119
122
|
# is last digit of string.
|
120
123
|
def validate_check_digit_for(value)
|
121
124
|
raise UnencodableCharactersError unless self.can_encode?(value)
|
@@ -123,7 +126,7 @@ module Barcode1DTools
|
|
123
126
|
self.generate_check_digit_for(md[1]) == md[2].to_i
|
124
127
|
end
|
125
128
|
|
126
|
-
# Decode a string in
|
129
|
+
# Decode a string in w/n format. This will return an Interleaved2of5
|
127
130
|
# object.
|
128
131
|
def decode(str, options = {})
|
129
132
|
if str =~ /[^wn]/
|
@@ -180,6 +183,7 @@ module Barcode1DTools
|
|
180
183
|
end
|
181
184
|
end
|
182
185
|
|
186
|
+
# Create a new Interleaved2of5 object with the given value.
|
183
187
|
# Options are :line_character, :space_character, :w_character,
|
184
188
|
# :n_character, :skip_checksum, and :checksum_included.
|
185
189
|
def initialize(value, options = {})
|
@@ -213,17 +217,17 @@ module Barcode1DTools
|
|
213
217
|
@wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
|
214
218
|
end
|
215
219
|
|
216
|
-
#
|
220
|
+
# Returns a run-length-encoded string representation
|
217
221
|
def rle
|
218
222
|
@rle ||= self.class.wn_to_rle(self.wn, @options)
|
219
223
|
end
|
220
224
|
|
221
|
-
#
|
225
|
+
# Returns 1s and 0s (for "black" and "white")
|
222
226
|
def bars
|
223
227
|
@bars ||= self.class.rle_to_bars(self.rle, @options)
|
224
228
|
end
|
225
229
|
|
226
|
-
#
|
230
|
+
# Returns the total unit width of the bar code
|
227
231
|
def width
|
228
232
|
@width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
|
229
233
|
end
|