Class: Barcodes::Symbology::Ean

Inherits:
Base
  • Object
show all
Defined in:
lib/barcodes/symbology/ean.rb

Overview

This is the base class for all EAN type (EAN8, EAN13, UPC-A) barcode symbologies. EAN type barcodes can encode only the numbers 0-9

More info: en.wikipedia.org/wiki/European_Article_Number

Direct Known Subclasses

Ean13, Ean8, UpcA

Instance Attribute Summary

Attributes inherited from Base

#alpha, #bar_height, #bar_width, #caption_height, #caption_size, #captioned, #color, #data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#formatted_data, #height, #valid?, #width

Constructor Details

#initialize(args = {}) ⇒ Ean

Creates a new EAN instance



65
66
67
68
69
70
71
# File 'lib/barcodes/symbology/ean.rb', line 65

def initialize(args={})
  super(args)

  @start_character = 'S'
  @stop_character = 'S'
  @center_character = 'C'
end

Class Method Details

.charsetObject

EAN character set



19
20
21
# File 'lib/barcodes/symbology/ean.rb', line 19

def self.charset
  ['0','1','2','3','4','5','6','7','8','9','S','C'].collect {|c| c.bytes.to_a[0] }
end

.left_hand_evenObject

EAN left hand even values table



33
34
35
36
37
38
39
# File 'lib/barcodes/symbology/ean.rb', line 33

def self.left_hand_even
  [
    "0001101", "0011001", "0010011", "0111101",
    "0100011", "0110001", "0101111", "0111011",
    "0110111", "0001011"
  ]
end

.left_hand_oddObject

EAN left hand odd values table



42
43
44
45
46
47
48
# File 'lib/barcodes/symbology/ean.rb', line 42

def self.left_hand_odd
  [
    "0100111", "0110011", "0011011", "0100001",
    "0011101", "0111001", "0000101", "0010001",
    "0001001", "0010111"
  ]
end

.parityObject

EAN parity values table



24
25
26
27
28
29
30
# File 'lib/barcodes/symbology/ean.rb', line 24

def self.parity
  [
    "000000", "001011", "001101", "001110",
    "010011", "011001", "011100", "010101",
    "010110", "011010"
  ]
end

.right_handObject

EAN right hand values table



51
52
53
54
55
56
57
# File 'lib/barcodes/symbology/ean.rb', line 51

def self.right_hand
  [
    "1110010", "1100110", "1101100", "1000010",
    "1011100", "1001110", "1010000", "1000100",
    "1001000", "1110100"
  ]
end

.valuesetObject

EAN uses special value sets so this returns an empty array



60
61
62
# File 'lib/barcodes/symbology/ean.rb', line 60

def self.valueset
  []
end

Instance Method Details

#caption_dataObject

Returns data + checksum



74
75
76
# File 'lib/barcodes/symbology/ean.rb', line 74

def caption_data
  @data + self.checksum
end

#checksumObject

Calculates the checksum



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/barcodes/symbology/ean.rb', line 113

def checksum
  if self.valid?
    sum = 0
    index = 1
    @data.reverse.each_char do |char|
      if ('0'..'9').include? char
        if index.even?
          sum += char.to_i
        else
          sum += char.to_i * 3
        end
      end
      index += 1
    end

    value = 10 - (sum % 10)

    if value == 10
      value = 0
    end

    return value.to_s
  end
end

#encoded_dataObject

Encodes data into 1’s and 0’s



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/barcodes/symbology/ean.rb', line 79

def encoded_data
  if self.valid?
    formatted_data = self.formatted_data
    encoded_data = ""
    parity = nil
    index = 0
    formatted_data.each_byte do |char|
      if char.chr == 'S'
        encoded_data += "101"
      elsif char.chr == 'C'
        encoded_data += "01010"
      else
        if index < 8
          unless parity.nil?
            encoded_data += self._encode_character_with_parity(char, parity[index - 2])
          else
            parity = self.class.parity.at(self.class.charset.index(char))
          end
        else
          encoded_data += self._encode_character_with_right_hand(char)
        end
      end
      index += 1
    end
    encoded_data
  end
end

#quiet_zone_widthObject

EAN uses quiet zone that is 9 times the bar width in mils



108
109
110
# File 'lib/barcodes/symbology/ean.rb', line 108

def quiet_zone_width
  self.bar_width * 9
end