Code

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