e0534ecc5a74879fe30be36271986f922e54bea0
1 '''
2 Copyright (C) 2007 Martin Owens
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 '''
19 from Base import Barcode
20 import sys
22 leftMap = [ '0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011' ]
23 rightMap = [ '1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100' ]
24 weightMap = [ 3, 1, 3, 1, 3, 1, 3 ]
26 guardBar = '202';
27 centerBar = '02020';
29 class Object(Barcode):
30 def encode(self, number):
31 result = ''
33 # Rejig the label for use
34 self.label = number[:4] + ' ' + number[4:]
36 if len(number) < 7 or len(number) > 8 or not number.isdigit():
37 sys.stderr.write("Can not encode '" + number + "' into EAN8 Barcode, Size must be 7 or 8 Numbers only\n")
39 if len(number) == 7:
40 number = number + self.calculateChecksum(number)
42 result = result + guardBar
44 i = 0
45 for num in number:
46 if i >= 4:
47 result = result + rightMap[int(num)]
48 else:
49 result = result + leftMap[int(num)]
51 i = i + 1
52 if i == 4:
53 result = result + centerBar;
55 result = result + guardBar;
57 self.inclabel = ' ' + number[:4] + ' ' + number[4:]
58 return result;
61 def calculateChecksum(self, number):
62 weight = 0;
63 i = 0;
65 for num in number:
66 weight = weight + (int(num) * weightMap[i])
67 i = i + 1
69 weight = 10 - (weight % 10)
70 if weight == 10:
71 weight = 0
72 return str(weight);
74 def getStyle(self, index):
75 result = { 'width' : '1', 'top' : int(self.y), 'write' : True }
76 if index==0: # White Space
77 result['write'] = False
78 elif index==1: # Black Bar
79 result['height'] = int(self.height)
80 elif index==2: # Guide Bar
81 result['height'] = int(self.height) + 8
82 return result