Class: Barcode1DTools::Code11
- Defined in:
- lib/barcode1dtools/code11.rb
Overview
Barcode1DTools::Code11 - Create and decode bar patterns for Code11. The value encoded is a string which may contain the digits 0-9 and the dash symbol “-”. The standard specifies one or two check digits may be added depending on the length of the payload. Use :checksum_included => true if you have already added a checksum and wish to have it validated, or :skip_checksum => true if you don’t wish to add one or have it validated.
Code 11 is used in the telecom industry for equipment labeling. It should not be used in any new applications.
Example
val = "29382-38"
bc = Barcode1DTools::Code11.new(val)
pattern = bc.
rle_pattern = bc.rle
width = bc.width
The object created is immutable.
Barcode1DTools::Code11 creates the patterns that you need to display Code11 barcodes. It can also decode a simple w/n string.
Code11 characters consist of 3 bars and 2 spaces, with a narrow space between them. Three of the characters- 0, 9, and “-” - are 6 units wide. The rest are 7 units wide.
Formats
There are three formats for the returned pattern:
bars - 1s and 0s specifying black lines and white spaces. Actual characters can be changed from “1” and 0“ with options :line_character and :space_character.
rle - Run-length-encoded version of the pattern. The first number is always a black line, with subsequent digits alternating between spaces and lines. The digits specify the width of each line or space.
wn - The native format for this barcode type. The string consists of a series of “w” and “n” characters. The first item is always a black line, with subsequent characters alternating between spaces and lines. A “wide” item is twice the width of a “narrow” item.
The “width” method will tell you the total end-to-end width, in units, of the entire barcode.
Rendering
The standard w/n ratio seems to be 2:1. There seem to be no real standards for display.
Constant Summary collapse
- CHAR_SEQUENCE =
Character sequence - 0-based offset in this string is character number
"0123456789-"
- PATTERNS =
Patterns for making bar codes
{ '0'=> {'val'=>0 ,'wn'=>'nnnnw'}, '1'=> {'val'=>1 ,'wn'=>'wnnnw'}, '2'=> {'val'=>2 ,'wn'=>'nwnnw'}, '3'=> {'val'=>3 ,'wn'=>'wwnnn'}, '4'=> {'val'=>4 ,'wn'=>'nnwnw'}, '5'=> {'val'=>5 ,'wn'=>'wnwnn'}, '6'=> {'val'=>6 ,'wn'=>'nwwnn'}, '7'=> {'val'=>7 ,'wn'=>'nnnww'}, '8'=> {'val'=>8 ,'wn'=>'wnnwn'}, '9'=> {'val'=>9 ,'wn'=>'wnnnn'}, '-'=> {'val'=>10 ,'wn'=>'nnwnn'} }
- GUARD_PATTERN_WN =
Guard pattern for Code 11
'nnwwn'
- DEFAULT_OPTIONS =
{ :line_character => '1', :space_character => '0', :w_character => 'w', :n_character => 'n', :wn_ratio => '2' }
Instance Attribute Summary
Attributes inherited from Barcode1D
#check_digit, #encoded_string, #options, #value
Class Method Summary collapse
-
.can_encode?(value) ⇒ Boolean
Returns true if the value presented can be encoded in Code 11.
-
.decode(str, options = {}) ⇒ Object
Decode a string in rle format.
-
.generate_check_digit_for(value) ⇒ Object
Generates a check digit for the given value.
-
.split_payload_and_check_digits(value) ⇒ Object
Split the given value into a payload and check digit or digits.
-
.validate_check_digit_for(value) ⇒ Object
Returns true if the given check digit(s) is correct.
Instance Method Summary collapse
-
#bars ⇒ Object
Returns the bar/space pattern as 1s and 0s.
-
#initialize(value, options = {}) ⇒ Code11
constructor
Create a new Code 11 object with a value.
-
#rle ⇒ Object
Returns a run-length-encoded string representation.
-
#width ⇒ Object
Returns the total unit width of the bar code.
-
#wn ⇒ Object
Returns a string of “w” or “n” (“wide” and “narrow”).
Methods inherited from Barcode1D
bar_pair, bars_to_rle, rle_to_bars, rle_to_wn, wn_pair, wn_to_rle
Constructor Details
#initialize(value, options = {}) ⇒ Code11
Create a new Code 11 object with a value. Options are :line_character, :space_character, :w_character, :n_character, :checksum_included, and :skip_checksum.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/barcode1dtools/code11.rb', line 196 def initialize(value, = {}) @options = DEFAULT_OPTIONS.merge() # Can we encode this value? raise UnencodableCharactersError unless self.class.can_encode?(value) @value = value.to_s if @options[:skip_checksum] @encoded_string = value.to_s @value = value.to_s @check_digit = nil elsif @options[:checksum_included] raise ChecksumError unless self.class.validate_check_digit_for(value) @encoded_string = value.to_s @value, @check_digit = self.class.split_payload_and_check_digits(value) else @value = value.to_s @check_digit = self.class.generate_check_digit_for(@value) @encoded_string = "#{@value}#{@check_digit}" end end |
Class Method Details
.can_encode?(value) ⇒ Boolean
Returns true if the value presented can be encoded in Code 11. Code11 can encode digits and dashes.
100 101 102 |
# File 'lib/barcode1dtools/code11.rb', line 100 def can_encode?(value) value.to_s =~ /\A[0-9\-]+\z/ end |
.decode(str, options = {}) ⇒ Object
Decode a string in rle format. This will return a Code11 object.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/barcode1dtools/code11.rb', line 143 def decode(str, = {}) if str =~ /[^1-3]/ && str =~ /[^wn]/ raise UnencodableCharactersError, "Pattern must be rle or wn" end # ensure a wn string if str =~ /[1-3]/ str = str.tr('123','nww') end if str.reverse =~ /\A#{GUARD_PATTERN_WN}n.*?#{GUARD_PATTERN_WN}\z/ str.reverse! end unless str =~ /\A#{GUARD_PATTERN_WN}n(.*?)#{GUARD_PATTERN_WN}\z/ raise UnencodableCharactersError, "Start/stop pattern is not detected." end # Adding an "n" to make it easier to scan wn_pattern = $1 # Each pattern is 3 bars and 2 spaces, with a space between. unless wn_pattern.size % 6 == 0 raise UnencodableCharactersError, "Wrong number of bars." end decoded_string = '' wn_pattern.scan(/(.{5})n/).each do |chunk| chunk = chunk.first found = false PATTERNS.each do |char,hsh| if chunk == hsh['wn'] decoded_string += char found = true break; end end raise UndecodableCharactersError, "Invalid sequence: #{chunk}" unless found end Code11.new(decoded_string, ) end |
.generate_check_digit_for(value) ⇒ Object
Generates a check digit for the given value. Note that Code 11 barcodes may have two check digits if the size of the value is 10 or more characters.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/barcode1dtools/code11.rb', line 107 def generate_check_digit_for(value) mult = 0 sum_c = value.to_s.reverse.split('').inject(0) { |a,c| mult = (mult == 11 ? 1 : mult + 1); a + mult * PATTERNS[c]['val'] } check_c = CHAR_SEQUENCE[sum_c % 11,1] if value.to_s.size > 9 mult = 0 sum_k = (value.to_s + check_c).reverse.split('').inject(0) { |a,c| mult = (mult == 10 ? 1 : mult + 1); a + mult * PATTERNS[c]['val'] } check_k = CHAR_SEQUENCE[sum_k % 9,1] else check_k = '' end "#{check_c}#{check_k}" end |
.split_payload_and_check_digits(value) ⇒ Object
Split the given value into a payload and check digit or digits.
131 132 133 134 135 136 137 138 139 |
# File 'lib/barcode1dtools/code11.rb', line 131 def split_payload_and_check_digits(value) if value.to_s.size > 11 # two check digits md = value.to_s.match(/\A(.*)(..)\z/) else md = value.to_s.match(/\A(.*)(.)\z/) end [md[1], md[2]] end |
.validate_check_digit_for(value) ⇒ Object
Returns true if the given check digit(s) is correct. The check digit is the last one or two characters of the value that is passed.
124 125 126 127 |
# File 'lib/barcode1dtools/code11.rb', line 124 def validate_check_digit_for(value) payload, check_digits = split_payload_and_check_digits(value) self.generate_check_digit_for(payload) == check_digits end |
Instance Method Details
#bars ⇒ Object
Returns the bar/space pattern as 1s and 0s.
231 232 233 |
# File 'lib/barcode1dtools/code11.rb', line 231 def @bars ||= self.class.(self.rle, @options) end |
#rle ⇒ Object
Returns a run-length-encoded string representation
226 227 228 |
# File 'lib/barcode1dtools/code11.rb', line 226 def rle @rle ||= self.class.wn_to_rle(self.wn, @options) end |
#width ⇒ Object
Returns the total unit width of the bar code
236 237 238 |
# File 'lib/barcode1dtools/code11.rb', line 236 def width @width ||= rle.split('').inject(0) { |a,c| a + c.to_i } end |
#wn ⇒ Object
Returns a string of “w” or “n” (“wide” and “narrow”)
221 222 223 |
# File 'lib/barcode1dtools/code11.rb', line 221 def wn @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s) end |