barcode1dtools 1.0.1.0 → 1.0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,23 +9,24 @@ require 'barcode1dtools/ean13'
9
9
 
10
10
  module Barcode1DTools
11
11
 
12
- # Barcode1DTools::UPC_A - Create pattern for UPC-A barcodes
13
- #
12
+ # Barcode1DTools::UPC_A - Create pattern for UPC-A barcodes.
14
13
  # The value encoded is an 11-digit integer, and a checksum digit
15
14
  # will 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
- # # Note that this number is a UPC-A, with the number system of 08,
20
- # # manufacturer's code of "28999", product code of "00682", and a
21
- # # checksum of "3" (not included)
22
- # num = '82899900682'
23
- # bc = Barcode1DTools::UPC_A.new(num)
24
- # pattern = bc.bars
25
- # rle_pattern = bc.rle
26
- # width = bc.width
27
- # check_digit = Barcode1DTools::UPC_A.generate_check_digit_for(num)
18
+ # == Example
19
+ # # Note that this number is a UPC-A, with the number system of 08,
20
+ # # manufacturer's code of "28999", product code of "00682", and a
21
+ # # checksum of "3" (not included)
22
+ # num = '82899900682'
23
+ # bc = Barcode1DTools::UPC_A.new(num)
24
+ # pattern = bc.bars
25
+ # rle_pattern = bc.rle
26
+ # width = bc.width
27
+ # check_digit = Barcode1DTools::UPC_A.generate_check_digit_for(num)
28
28
  #
29
+ # == Other Information
29
30
  # A UPC-A barcode is an EAN-13 with an initial digit of "0" (that
30
31
  # is the left digit of the number system designator). Like the
31
32
  # EAN-13, the code is broken into a single-digit number system,
@@ -33,13 +34,13 @@ module Barcode1DTools
33
34
  # digit checksum.
34
35
  #
35
36
  # The number system is:
36
- # 0, 1, 6, 7, 8 - standard UPC codes
37
- # 2 - a product weight- generally calculated at the store.
38
- # 3 - pharmaceuticals
39
- # 4 - used for loyalty cards at stores
40
- # 5 - coupons
41
- # 9 - coupons
42
- # 978 - for books, with a 10-digit ISBN coming after 978
37
+ # 0, 1, 6, 7, 8 - standard UPC codes
38
+ # 2 - a product weight- generally calculated at the store.
39
+ # 3 - pharmaceuticals
40
+ # 4 - used for loyalty cards at stores
41
+ # 5 - coupons
42
+ # 9 - coupons
43
+ # 978 - for books, with a 10-digit ISBN coming after 978
43
44
  #
44
45
  # For code 2, the manufacturer code becomes an item number, and the
45
46
  # product number is used for either the weight or the price, with
@@ -52,7 +53,27 @@ module Barcode1DTools
52
53
  # based on a table released by the GS1 US (formerly the UCC).
53
54
  # Code 5 coupons may be doubled or tripled, but code 9 may not.
54
55
  #
55
- #== Rendering
56
+ # == Formats
57
+ # There are two formats for the returned pattern (wn format is
58
+ # not available):
59
+ #
60
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
61
+ # characters can be changed from "1" and 0" with options
62
+ # :line_character and :space_character.
63
+ #
64
+ # *rle* - Run-length-encoded version of the pattern. The first
65
+ # number is always a black line, with subsequent digits
66
+ # alternating between spaces and lines. The digits specify
67
+ # the width of each line or space.
68
+ #
69
+ # The "width" method will tell you the total end-to-end width, in
70
+ # units, of the entire barcode.
71
+ #
72
+ # Unlike some of the other barcodes, e.g. Code 3 of 9, there is no "w/n" format for
73
+ # EAN & UPC style barcodes because the bars and spaces are variable width from
74
+ # 1 to 4 units.
75
+ #
76
+ # == Rendering
56
77
  #
57
78
  # The UPC-A is typically rendered at 1-1.5 inch across, and half
58
79
  # an inch high. The number system digit and checksum digit are
@@ -86,7 +107,7 @@ module Barcode1DTools
86
107
  super('0' + value)
87
108
  end
88
109
 
89
- # validates the check digit given a string - assumes check digit
110
+ # Validates the check digit given a string - assumes check digit
90
111
  # is last digit of string.
91
112
  def validate_check_digit_for(value)
92
113
  raise UnencodableCharactersError unless self.can_encode?(value, :checksum_included => true)
@@ -94,6 +115,7 @@ module Barcode1DTools
94
115
  self.generate_check_digit_for(md[1]) == md[2].to_i
95
116
  end
96
117
 
118
+ # Decode a bar pattern or rle pattern and return a UPC_A object.
97
119
  def decode(value)
98
120
  ean = super(value)
99
121
  if ean.value[0,1] == '0'
@@ -104,6 +126,7 @@ module Barcode1DTools
104
126
  end
105
127
  end
106
128
 
129
+ # Create a new UPC_A object with a given value.
107
130
  # Options are :line_character, :space_character, and
108
131
  # :checksum_included.
109
132
  def initialize(value, options = {})
@@ -129,7 +152,7 @@ module Barcode1DTools
129
152
  @number_system, @manufacturers_code, @product_code = md[1], md[2], md[3]
130
153
  end
131
154
 
132
- # returns a run-length-encoded string representation
155
+ # Returns a run-length-encoded string representation.
133
156
  def rle
134
157
  if @rle
135
158
  @rle
@@ -139,12 +162,13 @@ module Barcode1DTools
139
162
  end
140
163
  end
141
164
 
142
- # Returns a UPC_E object with the same value
165
+ # Returns a UPC_E object with the same value as the
166
+ # current UPC_A object, if possible.
143
167
  def to_upc_e
144
168
  UPC_E.new(UPC_E.upca_value_to_upce_value(@value), options.merge(:checksum_included => false))
145
169
  end
146
170
 
147
- # Returns true if the current value may be encoded as UPC-E
171
+ # Returns true if the current value may be encoded as UPC-E.
148
172
  def upc_e_encodable?
149
173
  begin
150
174
  UPC_E.new(UPC_E.upca_value_to_upce_value(@value), options.merge(:checksum_included => false))
@@ -9,20 +9,21 @@ require 'barcode1dtools/upc_a'
9
9
 
10
10
  module Barcode1DTools
11
11
 
12
- # Barcode1DTools::UPC_E - Create pattern for UPC-E barcodes
13
- #
12
+ # Barcode1DTools::UPC_E - Create pattern for UPC-E barcodes.
14
13
  # The value encoded is an 6-digit integer, and a checksum digit
15
14
  # will 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
- # num = '394932'
20
- # bc = Barcode1DTools::UPC_E.new(num)
21
- # pattern = bc.bars
22
- # rle_pattern = bc.rle
23
- # width = bc.width
24
- # check_digit = Barcode1DTools::UPC_E.generate_check_digit_for(num)
18
+ # == Example
19
+ # num = '394932'
20
+ # bc = Barcode1DTools::UPC_E.new(num)
21
+ # pattern = bc.bars
22
+ # rle_pattern = bc.rle
23
+ # width = bc.width
24
+ # check_digit = Barcode1DTools::UPC_E.generate_check_digit_for(num)
25
25
  #
26
+ # == Other Information
26
27
  # A UPC-E barcode is an abbreviated form of UPC-A, but can encode
27
28
  # only a few codes. The checksum is derived from the full UPC-A
28
29
  # digit sequence rather than the 6 digits of the UPC-E. The checksum
@@ -32,23 +33,43 @@ module Barcode1DTools
32
33
  # The last digit of the UPC-E determines the pattern used to convert
33
34
  # to a UPC-A.
34
35
  #
35
- # UPC-E UPC-A equivalent
36
- # 2 digits for manufacturer code (plus last digit), 3 digits for product
37
- # XXNNN0 0XX000-00NNN
38
- # XXNNN1 0XX100-00NNN
39
- # XXNNN2 0XX200-00NNN
40
- # 3 digits for manufacturer code, 2 digits for product
41
- # XXXNN3 0XXX00-000NN
42
- # 4 digits for manufacturer code, 1 digit for product
43
- # XXXXN4 0XXXX0-0000N
44
- # 5 digits for manufacturer code, 1 digit for product (5-9)
45
- # XXXXX5 0XXXXX-00005
46
- # XXXXX6 0XXXXX-00006
47
- # XXXXX7 0XXXXX-00007
48
- # XXXXX8 0XXXXX-00008
49
- # XXXXX9 0XXXXX-00009
36
+ # UPC-E UPC-A equivalent
37
+ # 2 digits for manufacturer code (plus last digit), 3 digits for product
38
+ # XXNNN0 0XX000-00NNN
39
+ # XXNNN1 0XX100-00NNN
40
+ # XXNNN2 0XX200-00NNN
41
+ # 3 digits for manufacturer code, 2 digits for product
42
+ # XXXNN3 0XXX00-000NN
43
+ # 4 digits for manufacturer code, 1 digit for product
44
+ # XXXXN4 0XXXX0-0000N
45
+ # 5 digits for manufacturer code, 1 digit for product (5-9)
46
+ # XXXXX5 0XXXXX-00005
47
+ # XXXXX6 0XXXXX-00006
48
+ # XXXXX7 0XXXXX-00007
49
+ # XXXXX8 0XXXXX-00008
50
+ # XXXXX9 0XXXXX-00009
51
+ #
52
+ # == Formats
53
+ # There are two formats for the returned pattern (wn format is
54
+ # not available):
55
+ #
56
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
57
+ # characters can be changed from "1" and 0" with options
58
+ # :line_character and :space_character.
59
+ #
60
+ # *rle* - Run-length-encoded version of the pattern. The first
61
+ # number is always a black line, with subsequent digits
62
+ # alternating between spaces and lines. The digits specify
63
+ # the width of each line or space.
64
+ #
65
+ # The "width" method will tell you the total end-to-end width, in
66
+ # units, of the entire barcode.
50
67
  #
51
- #== Rendering
68
+ # Unlike some of the other barcodes, e.g. Code 3 of 9, there is no "w/n" format for
69
+ # EAN & UPC style barcodes because the bars and spaces are variable width from
70
+ # 1 to 4 units.
71
+ #
72
+ # == Rendering
52
73
  #
53
74
  # The UPC-E is made for smaller items. Generally, they are rendered
54
75
  # with the number system digit (0) on the left of the bars and the
@@ -61,9 +82,12 @@ module Barcode1DTools
61
82
 
62
83
  class UPC_E < Barcode1D
63
84
 
85
+ # Using the left patterns from UPC-A
64
86
  LEFT_PATTERNS = UPC_A::LEFT_PATTERNS
87
+ # Using the left patterns from UPC-A
65
88
  LEFT_PATTERNS_RLE = UPC_A::LEFT_PATTERNS_RLE
66
89
 
90
+ # UPC-E uses a different set of parity patterns to encode the check digit.
67
91
  PARITY_PATTERNS = {
68
92
  '0' => 'eeeooo',
69
93
  '1' => 'eeoeoo',
@@ -77,9 +101,13 @@ module Barcode1DTools
77
101
  '9' => 'eooeoe',
78
102
  };
79
103
 
104
+ # Left guard pattern
80
105
  LEFT_GUARD_PATTERN = '101'
106
+ # Right guard pattern
81
107
  RIGHT_GUARD_PATTERN = '010101'
108
+ # Left guard pattern as RLE
82
109
  LEFT_GUARD_PATTERN_RLE = '111'
110
+ # Right guard pattern as RLE
83
111
  RIGHT_GUARD_PATTERN_RLE = '111111'
84
112
 
85
113
  DEFAULT_OPTIONS = {
@@ -87,10 +115,13 @@ module Barcode1DTools
87
115
  :space_character => '0'
88
116
  }
89
117
 
90
- # For UPC-E
118
+ # For UPC-E - the number system part of the item number
91
119
  attr_reader :number_system
120
+ # For UPC-E - the manufacturer's code part of the item number
92
121
  attr_reader :manufacturers_code
122
+ # For UPC-E - the product code part of the item number
93
123
  attr_reader :product_code
124
+ # For UPC-E - the UPC-A value for this UPC-E value
94
125
  attr_reader :upca_value
95
126
 
96
127
  class << self
@@ -112,7 +143,7 @@ module Barcode1DTools
112
143
  UPC_A.generate_check_digit_for(self.upce_value_to_upca_value(value))
113
144
  end
114
145
 
115
- # validates the check digit given a string - assumes check digit
146
+ # Validates the check digit given a string - assumes check digit
116
147
  # is last digit of string.
117
148
  def validate_check_digit_for(value)
118
149
  raise UnencodableCharactersError unless self.can_encode?(value, :checksum_included => true)
@@ -120,6 +151,8 @@ module Barcode1DTools
120
151
  self.generate_check_digit_for(md[1]) == md[2].to_i
121
152
  end
122
153
 
154
+ # Decodes a bar pattern or rle string that represents a UPC-E. Returns
155
+ # a UPC_E object.
123
156
  def decode(str)
124
157
  if str.length == 51
125
158
  # bar pattern
@@ -175,6 +208,7 @@ module Barcode1DTools
175
208
  UPC_E.new('0' + digits + parity_digit, :checksum_included => true)
176
209
  end
177
210
 
211
+ # Converts the given UPC-E value to a UPC-A value.
178
212
  def upce_value_to_upca_value(value, options = {})
179
213
  raise UnencodableCharactersError unless self.can_encode?(value, options)
180
214
  # remove the check digit if it was included
@@ -194,6 +228,7 @@ module Barcode1DTools
194
228
  upca_value
195
229
  end
196
230
 
231
+ # Converts the given UPC-A value to a UPC-E value.
197
232
  def upca_value_to_upce_value(value, options = {})
198
233
  raise UnencodableCharactersError unless UPC_A.can_encode?(value, options)
199
234
  value = value % 10 if options[:checksum_included]
@@ -213,6 +248,7 @@ module Barcode1DTools
213
248
  end
214
249
  end
215
250
 
251
+ # Create a new UPC-E object for a given value.
216
252
  # Options are :line_character, :space_character, and
217
253
  # :checksum_included.
218
254
  def initialize(value, options = {})
@@ -239,12 +275,12 @@ module Barcode1DTools
239
275
  @number_system, @manufacturers_code, @product_code = md[1], md[2], md[3]
240
276
  end
241
277
 
242
- # not usable with EAN-style codes
278
+ # W/N strings are not usable with EAN-style codes.
243
279
  def wn
244
280
  raise NotImplementedError
245
281
  end
246
282
 
247
- # returns a run-length-encoded string representation
283
+ # Returns a run-length-encoded string representation.
248
284
  def rle
249
285
  if @rle
250
286
  @rle
@@ -254,17 +290,17 @@ module Barcode1DTools
254
290
  end
255
291
  end
256
292
 
257
- # returns 1s and 0s (for "black" and "white")
293
+ # Returns a bar pattern.
258
294
  def bars
259
295
  @bars ||= self.class.rle_to_bars(self.rle, @options)
260
296
  end
261
297
 
262
- # returns the total unit width of the bar code
298
+ # Returns the total unit width of the bar code.
263
299
  def width
264
300
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
265
301
  end
266
302
 
267
- # Returns a UPC_A object with the same value
303
+ # Returns a UPC_A object with the same value.
268
304
  def to_upc_a
269
305
  UPC_A.new(self.class.upce_value_to_upca_value(@value), options.merge(:checksum_included => false))
270
306
  end
@@ -10,20 +10,20 @@ require 'barcode1dtools/upc_a'
10
10
  module Barcode1DTools
11
11
 
12
12
  # Barcode1DTools::UPC_Supplemental_2 - Create pattern for UPC
13
- # Supplemental 2 barcodes
13
+ # Supplemental 2 barcodes. The value encoded is an 2-digit
14
+ # integer, and a checksum digit will be added. You can add the
15
+ # option :checksum_included => true when initializing to
16
+ # specify that you have already included a checksum.
14
17
  #
15
- # The value encoded is an 2-digit integer, and a checksum digit
16
- # will be added. You can add the option :checksum_included => true
17
- # when initializing to specify that you have already included a
18
- # checksum.
19
- #
20
- # num = '24'
21
- # bc = Barcode1DTools::UPC_Supplemental_2.new(num)
22
- # pattern = bc.bars
23
- # rle_pattern = bc.rle
24
- # width = bc.width
25
- # check_digit = Barcode1DTools::UPC_E.generate_check_digit_for(num)
18
+ # == Example
19
+ # num = '24'
20
+ # bc = Barcode1DTools::UPC_Supplemental_2.new(num)
21
+ # pattern = bc.bars
22
+ # rle_pattern = bc.rle
23
+ # width = bc.width
24
+ # check_digit = Barcode1DTools::UPC_E.generate_check_digit_for(num)
26
25
  #
26
+ # == Other Information
27
27
  # This type of barcode consists of 2 digits, and a check digit
28
28
  # (simply a modulus 4 of the number encoded) that is encoded in
29
29
  # the "parity" of the two barcode digits. The bar patterns are the
@@ -37,7 +37,27 @@ module Barcode1DTools
37
37
  # will likely need to use the Barcode::UPC_A module in addition
38
38
  # to this one to create the full code.
39
39
  #
40
- #== Rendering
40
+ # == Formats
41
+ # There are two formats for the returned pattern (wn format is
42
+ # not available):
43
+ #
44
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
45
+ # characters can be changed from "1" and 0" with options
46
+ # :line_character and :space_character.
47
+ #
48
+ # *rle* - Run-length-encoded version of the pattern. The first
49
+ # number is always a black line, with subsequent digits
50
+ # alternating between spaces and lines. The digits specify
51
+ # the width of each line or space.
52
+ #
53
+ # The "width" method will tell you the total end-to-end width, in
54
+ # units, of the entire barcode.
55
+ #
56
+ # Unlike some of the other barcodes, e.g. Code 3 of 9, there is no "w/n" format for
57
+ # EAN & UPC style barcodes because the bars and spaces are variable width from
58
+ # 1 to 4 units.
59
+ #
60
+ # == Rendering
41
61
  #
42
62
  # The 2-digit supplement is positioned to the right of the main UPC
43
63
  # code, and the human-readable digits are usually printed above the
@@ -52,11 +72,13 @@ module Barcode1DTools
52
72
 
53
73
  class UPC_Supplemental_2 < Barcode1D
54
74
 
75
+ # The bar patterns are the left bar patterns of UPC-A/EAN-13
55
76
  LEFT_PATTERNS = UPC_A::LEFT_PATTERNS
77
+ # The rle bar patterns are the left bar patterns of UPC-A/EAN-13
56
78
  LEFT_PATTERNS_RLE = UPC_A::LEFT_PATTERNS_RLE
57
79
 
58
- # parity patterns, essentially binary counting where "e" is "1"
59
- # and "o" is "0"
80
+ # Parity patterns, essentially binary counting where "e" is "1"
81
+ # and "o" is "0".
60
82
  PARITY_PATTERNS = {
61
83
  '0' => 'oo',
62
84
  '1' => 'oe',
@@ -64,9 +86,13 @@ module Barcode1DTools
64
86
  '3' => 'ee',
65
87
  };
66
88
 
89
+ # Left guard pattern
67
90
  LEFT_GUARD_PATTERN = '1011'
91
+ # Middle guard pattern
68
92
  MIDDLE_GUARD_PATTERN = '01'
93
+ # Left guard pattern as an rle
69
94
  LEFT_GUARD_PATTERN_RLE = '112'
95
+ # Middle guard pattern as an rle
70
96
  MIDDLE_GUARD_PATTERN_RLE = '11'
71
97
 
72
98
  DEFAULT_OPTIONS = {
@@ -93,7 +119,7 @@ module Barcode1DTools
93
119
  value.to_i % 4
94
120
  end
95
121
 
96
- # validates the check digit given a string - assumes check digit
122
+ # Validates the check digit given a string - assumes check digit
97
123
  # is last digit of string.
98
124
  def validate_check_digit_for(value)
99
125
  raise UnencodableCharactersError unless self.can_encode?(value, :checksum_included => true)
@@ -101,6 +127,8 @@ module Barcode1DTools
101
127
  self.generate_check_digit_for(md[1]) == md[2].to_i
102
128
  end
103
129
 
130
+ # Decodes a bar or rle pattern and returns a
131
+ # UPC_Supplemental_2 object.
104
132
  def decode(str)
105
133
  if str.length == 20
106
134
  # bar pattern
@@ -156,6 +184,7 @@ module Barcode1DTools
156
184
 
157
185
  end
158
186
 
187
+ # Create a new UPC_Supplemental_2 object from a given value.
159
188
  # Options are :line_character, :space_character, and
160
189
  # :checksum_included.
161
190
  def initialize(value, options = {})
@@ -178,12 +207,13 @@ module Barcode1DTools
178
207
  end
179
208
  end
180
209
 
181
- # not usable with EAN-style codes
210
+ # W/N patterns are not usable with EAN-style codes. This
211
+ # will raise an error.
182
212
  def wn
183
213
  raise NotImplementedError
184
214
  end
185
215
 
186
- # returns a run-length-encoded string representation
216
+ # Returns a run-length-encoded string representation.
187
217
  def rle
188
218
  if @rle
189
219
  @rle
@@ -193,12 +223,12 @@ module Barcode1DTools
193
223
  end
194
224
  end
195
225
 
196
- # returns 1s and 0s (for "black" and "white")
226
+ # Returns a bar pattern.
197
227
  def bars
198
228
  @bars ||= self.class.rle_to_bars(self.rle, @options)
199
229
  end
200
230
 
201
- # returns the total unit width of the bar code
231
+ # Returns the total unit width of the bar code.
202
232
  def width
203
233
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
204
234
  end