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.
@@ -16,14 +16,15 @@ module Barcode1DTools
16
16
  # checksum and wish to have it validated, or :skip_checksum =>
17
17
  # true if you don't wish to add one or have it validated.
18
18
  #
19
- # COOP 2 of 5 is low-density and limited. It should not be
19
+ # *Note:* COOP 2 of 5 is low-density and limited. It should not be
20
20
  # used in any new applications.
21
21
  #
22
- # val = "3423"
23
- # bc = Barcode1DTools::Coop2of5.new(val)
24
- # pattern = bc.bars
25
- # rle_pattern = bc.rle
26
- # width = bc.width
22
+ # == Example
23
+ # val = "3423"
24
+ # bc = Barcode1DTools::Coop2of5.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
  # Coop2of5 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
- # bars - 1s and 0s specifying black lines and white spaces. Actual
40
- # characters can be changed from "1" and 0" with options
41
- # :line_character and :space_character.
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
- # rle - Run-length-encoded version of the pattern. The first
44
- # number is always a black line, with subsequent digits
45
- # alternating between spaces and lines. The digits specify
46
- # the width of each line or space.
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
- # wn - The native format for this barcode type. The string
49
- # consists of a series of "w" and "n" characters. The first
50
- # item is always a black line, with subsequent characters
51
- # alternating between spaces and lines. A "wide" item
52
- # is twice the width of a "narrow" item.
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
- #== Rendering
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.
@@ -62,7 +64,7 @@ module Barcode1DTools
62
64
  class Coop2of5 < Barcode1D
63
65
 
64
66
  # Character sequence - 0-based offset in this string is character
65
- # number
67
+ # number.
66
68
  CHAR_SEQUENCE = "0123456789"
67
69
 
68
70
  # Patterns for making bar codes. Note that the position
@@ -82,7 +84,9 @@ module Barcode1DTools
82
84
  '9'=> {'val'=>9 ,'wn'=>'wnwnn'}
83
85
  }
84
86
 
87
+ # Left guard pattern
85
88
  GUARD_PATTERN_LEFT_WN = 'wnw'
89
+ # Right guard pattern
86
90
  GUARD_PATTERN_RIGHT_WN = 'nww'
87
91
 
88
92
  DEFAULT_OPTIONS = {
@@ -94,11 +98,14 @@ module Barcode1DTools
94
98
  }
95
99
 
96
100
  class << self
97
- # Coop2of5 can encode digits
101
+ # Coop2of5 can encode digits. This will return
102
+ # true given a string of 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. This uses a Luhn
108
+ # algorithm.
102
109
  def generate_check_digit_for(value)
103
110
  raise UnencodableCharactersError unless self.can_encode?(value)
104
111
  mult = 3
@@ -106,13 +113,16 @@ module Barcode1DTools
106
113
  (10 - (value % 10)) % 10
107
114
  end
108
115
 
116
+ # Validates a check digit given a string of digits. It is assumed
117
+ # that the last digit is the check digit. Returns "true" if
118
+ # the check digit is correct.
109
119
  def validate_check_digit_for(value)
110
120
  raise UnencodableCharactersError unless self.can_encode?(value)
111
121
  md = value.match(/^(\d+?)(\d)$/)
112
122
  self.generate_check_digit_for(md[1]) == md[2].to_i
113
123
  end
114
124
 
115
- # Decode a string in rle format. This will return a Coop2of5
125
+ # Decode a string in rle or w/n format. This will return a Coop2of5
116
126
  # object.
117
127
  def decode(str, options = {})
118
128
  if str =~ /[^1-3]/ && str =~ /[^wn]/
@@ -163,6 +173,7 @@ module Barcode1DTools
163
173
 
164
174
  end
165
175
 
176
+ # Create a new Coop2of5 object with a given value.
166
177
  # Options are :line_character, :space_character, :w_character,
167
178
  # :n_character, :checksum_included, and :skip_checksum.
168
179
  def initialize(value, options = {})
@@ -195,17 +206,17 @@ module Barcode1DTools
195
206
  @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
196
207
  end
197
208
 
198
- # returns a run-length-encoded string representation
209
+ # Returns a run-length-encoded string representation
199
210
  def rle
200
211
  @rle ||= self.class.wn_to_rle(self.wn, @options)
201
212
  end
202
213
 
203
- # returns 1s and 0s (for "black" and "white")
214
+ # Returns 1s and 0s (for "black" and "white")
204
215
  def bars
205
216
  @bars ||= self.class.rle_to_bars(self.rle, @options)
206
217
  end
207
218
 
208
- # returns the total unit width of the bar code
219
+ # Returns the total unit width of the bar code
209
220
  def width
210
221
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
211
222
  end
@@ -7,60 +7,51 @@
7
7
 
8
8
  module Barcode1DTools
9
9
 
10
- # Barcode1DTools::EAN_13 - Create pattern for EAN-13 barcodes
11
-
10
+ # Barcode1DTools::EAN_13 - Create pattern for EAN-13 barcodes.
12
11
  # The value encoded is an
13
12
  # integer, and a checksum digit will be added. You can add the option
14
13
  # :checksum_included => true when initializing to specify that you
15
14
  # have already included a checksum.
16
15
  #
17
- # # Note that this number is a UPC-A, with the number system of 08,
18
- # # manufacturer's code of "28999", product code of "00682", and a
19
- # # checksum of "3" (not included)
20
- # num = '082899900682'
21
- # bc = Barcode1DTools::EAN13.new(num)
22
- # pattern = bc.bars
23
- # rle_pattern = bc.rle
24
- # width = bc.width
25
- # check_digit = Barcode1DTools::EAN13.generate_check_digit_for(num)
16
+ # == Example
17
+ # # Note that this number is a UPC-A, with the number system of 08,
18
+ # # manufacturer's code of "28999", product code of "00682", and a
19
+ # # checksum of "3" (not included)
20
+ # num = '082899900682'
21
+ # bc = Barcode1DTools::EAN13.new(num)
22
+ # pattern = bc.bars
23
+ # rle_pattern = bc.rle
24
+ # width = bc.width
25
+ # check_digit = Barcode1DTools::EAN13.generate_check_digit_for(num)
26
26
  #
27
27
  # The object created is immutable.
28
28
  #
29
+ # == Formats
29
30
  # There are two formats for the returned pattern (wn format is
30
31
  # not available):
31
32
  #
32
- # bars - 1s and 0s specifying black lines and white spaces. Actual
33
- # characters can be changed from "1" and 0" with options
34
- # :line_character and :space_character. Each character
35
- # in the string renders to a single unit width.
33
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
34
+ # characters can be changed from "1" and 0" with options
35
+ # :line_character and :space_character.
36
36
  #
37
- # rle - Run-length-encoded version of the pattern. The first
38
- # number is always a black line, with subsequent digits
39
- # alternating between spaces and lines. The digits specify
40
- # the width of each line or space.
37
+ # *rle* - Run-length-encoded version of the pattern. The first
38
+ # number is always a black line, with subsequent digits
39
+ # alternating between spaces and lines. The digits specify
40
+ # the width of each line or space.
41
41
  #
42
42
  # The "width" method will tell you the total end-to-end width, in
43
43
  # units, of the entire barcode.
44
44
  #
45
- # Unlike some of the other barcodes, e.g. Code 3 of 9, there is no "wnstr" for
45
+ # Unlike some of the other barcodes, e.g. Code 3 of 9, there is no "w/n" format for
46
46
  # EAN & UPC style barcodes because the bars and spaces are variable width from
47
- # 1 to 3 units.
47
+ # 1 to 4 units.
48
48
  #
49
- # Note that JAN codes (Japanese) are simply EAN-13's, and they always start with
50
- # "49". The table below shows "49" to be "Japan".
51
- #
52
- # Also note that many books use a "bookland" code, perhaps along with a UPC
53
- # Supplemental. The bookland code is really an EAN-13 with the initial 3 digits
54
- # of "978". The next 9 digits are the first 9 digits of the ISBN, and of course
55
- # we still include the final check digit. An ISBN is 10 digits, however, the
56
- # final digit is also a check digit, so it is not necessary.
57
- #
58
- # MISCELLANEOUS INFORMATION
49
+ # == Miscellaneous Information
59
50
  #
60
51
  # An EAN-13 with an initial "number system" digit of "0" is a UPC-A.
61
52
  # The BarcodeTools::UPC_A module actually just uses this EAN13 module.
62
53
  #
63
- # A EAN-13 barcode has 4 elements:
54
+ # An EAN-13 barcode has 4 elements:
64
55
  # 1. A two-digit "number system" designation
65
56
  # 2. A 5-digit manufacturer's code
66
57
  # 3. A 5-digit product code
@@ -71,45 +62,54 @@ module Barcode1DTools
71
62
  # systems are further split up. An example is "74", which is used for
72
63
  # Central America with "740" for Guatemala, "741" for El Salvador, etc.
73
64
  #
74
- # Here is the complete table from www.barcodeisland.com:
65
+ # Note that JAN codes (Japanese) are simply EAN-13's, and they always start with
66
+ # "49". The table below shows "49" to be "Japan".
67
+ #
68
+ # Also note that many books use a "bookland" code, perhaps along with a UPC
69
+ # Supplemental. The bookland code is really an EAN-13 with the initial 3 digits
70
+ # of "978". The next 9 digits are the first 9 digits of the ISBN, and of course
71
+ # we still include the final check digit. An ISBN is 10 digits, however, the
72
+ # final digit is also a check digit, so it is not necessary.
75
73
  #
76
- # 00-13: USA & Canada 590: Poland 780: Chile
77
- # 20-29: In-Store Functions 594: Romania 784: Paraguay
78
- # 30-37: France 599: Hungary 785: Peru
79
- # 40-44: Germany 600 & 601: South Africa 786: Ecuador
80
- # 45: Japan (also 49) 609: Mauritius 789: Brazil
81
- # 46: Russian Federation 611: Morocco 80 - 83: Italy
82
- # 471: Taiwan 613: Algeria 84: Spain
83
- # 474: Estonia 619: Tunisia 850: Cuba
84
- # 475: Latvia 622: Egypt 858: Slovakia
85
- # 477: Lithuania 625: Jordan 859: Czech Republic
86
- # 479: Sri Lanka 626: Iran 860: Yugloslavia
87
- # 480: Philippines 64: Finland 869: Turkey
88
- # 482: Ukraine 690-692: China 87: Netherlands
89
- # 484: Moldova 70: Norway 880: South Korea
90
- # 485: Armenia 729: Israel 885: Thailand
91
- # 486: Georgia 73: Sweden 888: Singapore
92
- # 487: Kazakhstan 740: Guatemala 890: India
93
- # 489: Hong Kong 741: El Salvador 893: Vietnam
94
- # 49: Japan (JAN-13) 742: Honduras 899: Indonesia
95
- # 50: United Kingdom 743: Nicaragua 90 & 91: Austria
96
- # 520: Greece 744: Costa Rica 93: Australia
97
- # 528: Lebanon 746: Dominican Republic 94: New Zealand
98
- # 529: Cyprus 750: Mexico 955: Malaysia
99
- # 531: Macedonia 759: Venezuela 977: ISSN
100
- # 535: Malta 76: Switzerland 978: ISBN
101
- # 539: Ireland 770: Colombia 979: ISMN
102
- # 54: Belgium & Luxembourg 773: Uruguay 980: Refund receipts
103
- # 560: Portugal 775: Peru 981 & 982: CCC
104
- # 569: Iceland 777: Bolivia 99: Coupons
105
- # 57: Denmark 779: Argentina
74
+ # Here is the complete table from www.barcodeisland.com:
106
75
  #
107
- # ISSN - International Standard Serial Number for Periodicals
108
- # ISBN - International Standard Book Numbering
109
- # ISMN - International Standard Music Number
110
- # CCC - Common Currency Coupons
76
+ # 00-13: USA & Canada 590: Poland 780: Chile
77
+ # 20-29: In-Store Functions 594: Romania 784: Paraguay
78
+ # 30-37: France 599: Hungary 785: Peru
79
+ # 40-44: Germany 600 & 601: South Africa 786: Ecuador
80
+ # 45: Japan (also 49) 609: Mauritius 789: Brazil
81
+ # 46: Russian Federation 611: Morocco 80 - 83: Italy
82
+ # 471: Taiwan 613: Algeria 84: Spain
83
+ # 474: Estonia 619: Tunisia 850: Cuba
84
+ # 475: Latvia 622: Egypt 858: Slovakia
85
+ # 477: Lithuania 625: Jordan 859: Czech Republic
86
+ # 479: Sri Lanka 626: Iran 860: Yugloslavia
87
+ # 480: Philippines 64: Finland 869: Turkey
88
+ # 482: Ukraine 690-692: China 87: Netherlands
89
+ # 484: Moldova 70: Norway 880: South Korea
90
+ # 485: Armenia 729: Israel 885: Thailand
91
+ # 486: Georgia 73: Sweden 888: Singapore
92
+ # 487: Kazakhstan 740: Guatemala 890: India
93
+ # 489: Hong Kong 741: El Salvador 893: Vietnam
94
+ # 49: Japan (JAN-13) 742: Honduras 899: Indonesia
95
+ # 50: United Kingdom 743: Nicaragua 90 & 91: Austria
96
+ # 520: Greece 744: Costa Rica 93: Australia
97
+ # 528: Lebanon 746: Dominican Republic 94: New Zealand
98
+ # 529: Cyprus 750: Mexico 955: Malaysia
99
+ # 531: Macedonia 759: Venezuela 977: ISSN
100
+ # 535: Malta 76: Switzerland 978: ISBN
101
+ # 539: Ireland 770: Colombia 979: ISMN
102
+ # 54: Belgium & Luxembourg 773: Uruguay 980: Refund receipts
103
+ # 560: Portugal 775: Peru 981 & 982: CCC
104
+ # 569: Iceland 777: Bolivia 99: Coupons
105
+ # 57: Denmark 779: Argentina
106
+ #
107
+ # ISSN:: International Standard Serial Number for Periodicals
108
+ # ISBN:: International Standard Book Numbering
109
+ # ISMN:: International Standard Music Number
110
+ # CCC:: Common Currency Coupons
111
111
  #
112
- # RENDERING
112
+ # == Rendering
113
113
  #
114
114
  # When rendered, the initial digit of the number system is shown to the
115
115
  # left and above the rest of the digits. The other two sets of six
@@ -117,13 +117,13 @@ module Barcode1DTools
117
117
  # bottom of the code, and with the middle guard pattern bars extending
118
118
  # down between them. The lower digits may be aligned flush with the
119
119
  # bottom of the barcode, or the center of the text may be aligned with the
120
- # bottom of the barcode.
120
+ # bottom of the barcode. Typically, the barcode is 1-1.5" across and 60%
121
+ # or more of the width in height. There should be at least 10 units of
122
+ # quiet space on each side.
121
123
 
122
124
  class EAN13 < Barcode1D
123
125
 
124
- # patterns to create the bar codes:
125
-
126
- # left side, odd/even
126
+ # Patterns for the left side, split between odd and even parity
127
127
  LEFT_PATTERNS = {
128
128
  '0' => { 'o' => '0001101', 'e' => '0100111'},
129
129
  '1' => { 'o' => '0011001', 'e' => '0110011'},
@@ -135,9 +135,11 @@ module Barcode1DTools
135
135
  '7' => { 'o' => '0111011', 'e' => '0010001'},
136
136
  '8' => { 'o' => '0110111', 'e' => '0001001'},
137
137
  '9' => { 'o' => '0001011', 'e' => '0010111'},
138
- };
138
+ }
139
139
 
140
- # All left patterns start with a space and end with a bar
140
+ # Patterns for the left side, split between odd and even parity, in
141
+ # RLE format. Note that all left side patterns start with a space
142
+ # and end with a bar.
141
143
  LEFT_PATTERNS_RLE = {
142
144
  '0' => { 'o' => '3211', 'e' => '1123'},
143
145
  '1' => { 'o' => '2221', 'e' => '1222'},
@@ -149,8 +151,9 @@ module Barcode1DTools
149
151
  '7' => { 'o' => '1312', 'e' => '2131'},
150
152
  '8' => { 'o' => '1213', 'e' => '3121'},
151
153
  '9' => { 'o' => '3112', 'e' => '2113'},
152
- };
154
+ }
153
155
 
156
+ # Left parity patterns. An extra digit is encoded in the parity.
154
157
  LEFT_PARITY_PATTERNS = {
155
158
  '0' => 'oooooo',
156
159
  '1' => 'ooeoee',
@@ -162,9 +165,9 @@ module Barcode1DTools
162
165
  '7' => 'oeoeoe',
163
166
  '8' => 'oeoeeo',
164
167
  '9' => 'oeeoeo',
165
- };
168
+ }
166
169
 
167
- # right side
170
+ # Bar patterns for right side.
168
171
  RIGHT_PATTERNS = {
169
172
  '0' => '1110010',
170
173
  '1' => '1100110',
@@ -176,9 +179,10 @@ module Barcode1DTools
176
179
  '7' => '1000100',
177
180
  '8' => '1001000',
178
181
  '9' => '1110100',
179
- };
182
+ }
180
183
 
181
- # All right patterns start with a bar and end with a space
184
+ # Bar patterns for right side as RLE. Note that on the right
185
+ # side each pattern starts with a bar and ends with a space.
182
186
  RIGHT_PATTERNS_RLE = {
183
187
  '0' => '3211',
184
188
  '1' => '2221',
@@ -190,15 +194,15 @@ module Barcode1DTools
190
194
  '7' => '1312',
191
195
  '8' => '1213',
192
196
  '9' => '3112',
193
- };
197
+ }
194
198
 
195
199
  # AAAAHHHHHHHHH side + middle + side is 666, the number of the beast
196
200
  SIDE_GUARD_PATTERN='101';
197
201
  MIDDLE_GUARD_PATTERN='01010';
198
202
 
199
- # Starts with bar
203
+ # Side guard pattern - Starts with bar
200
204
  SIDE_GUARD_PATTERN_RLE='111';
201
- # Starts with space
205
+ # Middle guard pattern - Starts with space
202
206
  MIDDLE_GUARD_PATTERN_RLE='11111';
203
207
 
204
208
  DEFAULT_OPTIONS = {
@@ -206,13 +210,15 @@ module Barcode1DTools
206
210
  :space_character => '0'
207
211
  }
208
212
 
209
- # Specific for EAN
213
+ # Specific for EAN - the number system part of the payload
210
214
  attr_reader :number_system
215
+ # Specific for EAN - the manufacturer's code part of the payload
211
216
  attr_reader :manufacturers_code
217
+ # Specific for EAN - the product code part of the payload
212
218
  attr_reader :product_code
213
219
 
214
220
  class << self
215
- # returns true or false - must be 12-13 digits
221
+ # Returns true if value can be encoded. Must be 12 or 13 digits.
216
222
  def can_encode?(value, options = nil)
217
223
  if !options
218
224
  value.to_s =~ /^[0-9]{12,13}$/
@@ -232,7 +238,7 @@ module Barcode1DTools
232
238
  (10 - (value % 10)) % 10
233
239
  end
234
240
 
235
- # validates the check digit given a string - assumes check digit
241
+ # Validates the check digit given a string - assumes check digit
236
242
  # is last digit of string.
237
243
  def validate_check_digit_for(value)
238
244
  raise UnencodableCharactersError unless self.can_encode?(value, :checksum_included => true)
@@ -329,6 +335,7 @@ module Barcode1DTools
329
335
  end
330
336
  end
331
337
 
338
+ # Create a new EAN-13 barcode object.
332
339
  # Options are :line_character, :space_character, and
333
340
  # :checksum_included.
334
341
  def initialize(value, options = {})
@@ -354,12 +361,13 @@ module Barcode1DTools
354
361
  @number_system, @manufacturers_code, @product_code = md[1], md[2], md[3]
355
362
  end
356
363
 
357
- # not usable with EAN codes
364
+ # Note that EANs cannot produce a w/n string, so this
365
+ # will raise an error.
358
366
  def wn
359
367
  raise NotImplementedError
360
368
  end
361
369
 
362
- # returns a run-length-encoded string representation
370
+ # Returns a run-length-encoded string representation
363
371
  def rle
364
372
  if @rle
365
373
  @rle
@@ -369,12 +377,12 @@ module Barcode1DTools
369
377
  end
370
378
  end
371
379
 
372
- # returns 1s and 0s (for "black" and "white")
380
+ # Returns 1s and 0s (for "black" and "white")
373
381
  def bars
374
382
  @bars ||= self.class.rle_to_bars(self.rle, @options)
375
383
  end
376
384
 
377
- # returns the total unit width of the bar code
385
+ # Returns the total unit width of the bar code
378
386
  def width
379
387
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
380
388
  end