1 '''
2 Copyright (C) 2007 Martin Owens
4 Thanks to Lineaire Chez of Inkbar ( www.inkbar.lineaire.net )
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 '''
21 from Base import Barcode
22 import sys
24 mapLeftFaimly = [
25 [ "0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011" ],
26 [ "0100111","0110011","0011011","0100001","0011101","0111001","0000101","0010001","0001001","0010111" ],
27 ]
28 mapRight = [ "1110010","1100110","1101100","1000010","1011100","1001110","1010000","1000100","1001000","1110100" ]
29 mapFaimly = [ '000000','001011','001101','001110','010011','011001','011100','010101','010110','011010' ]
31 guardBar = '202';
32 centerBar = '02020';
34 class Object(Barcode):
35 def encode(self, number):
36 result = ''
38 if len(number) < 12 or len(number) > 13 or not number.isdigit():
39 sys.stderr.write("Can not encode '" + number + "' into EAN13 Barcode, Size must be 12 numbers only\n")
40 return
42 if len(number) == 12:
43 number = number + self.getChecksum(number)
44 else:
45 if not self.varifyChecksum(number):
46 sys.stderr.write("EAN13 Checksum not correct for this barcode, omit last charicter to generate new checksum.\n")
47 return
49 result = result + guardBar
50 family = mapFaimly[int(number[0])]
52 i = 0
53 for i in range(0,6):
54 mapLeft = mapLeftFaimly[int(family[i])]
55 result += mapLeft[int(number[i+1])]
57 result += centerBar
59 for i in range (7,13):
60 result += mapRight[int(number[i])]
62 result = result + guardBar;
64 self.label = number[0] + ' ' + number[1:7] + ' ' + number[7:] + ' '
65 self.inclabel = self.label
66 return result;
68 def getChecksum(self, number):
69 # UPCA/EAN13
70 weight=[3,1]*6
71 magic=10
72 sum = 0
73 # We need to work from left to right so reverse
74 number = number[::-1]
75 # checksum based on first 12 digits.
76 for i in range(len(number)):
77 sum = sum + int(number[i]) * weight[i]
79 # Mod it down to a single digit
80 z = ( magic - (sum % magic) ) % magic
81 if z < 0 or z >= magic:
82 return 0
84 return str(z)
86 def varifyChecksum(self, number):
87 new = self.computeChecksum(number[:12])
88 existing = number[12]
89 return new == existing
91 def getStyle(self, index):
92 result = { 'width' : '1', 'top' : int(self.y), 'write' : True }
93 if index==0: # White Space
94 result['write'] = False
95 elif index==1: # Black Bar
96 result['height'] = int(self.height)
97 elif index==2: # Guide Bar
98 result['height'] = int(self.height) + 5
99 return result