Code

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