Code

8a93b497b747849529953db0b23a5587e0a90f43
[inkscape.git] / share / extensions / Barcode / EAN5.py
1 #!/usr/bin/env python 
2 ''' 
3 Copyright (C) 2007 Martin Owens 
4 Copyright (C) 2009 Aaron C Spike 
6 Thanks to Lineaire Chez of Inkbar ( www.inkbar.lineaire.net ) 
8 This program is free software; you can redistribute it and/or modify 
9 it under the terms of the GNU General Public License as published by 
10 the Free Software Foundation; either version 2 of the License, or 
11 (at your option) any later version. 
13 This program is distributed in the hope that it will be useful, 
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16 GNU General Public License for more details. 
18 You should have received a copy of the GNU General Public License 
19 along with this program; if not, write to the Free Software 
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
21 '''
23 from Base import Barcode
24 import sys
26 mapLeftFamily = [
27     [ "0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011" ], #L
28     [ "0100111","0110011","0011011","0100001","0011101","0111001","0000101","0010001","0001001","0010111" ], #G
29 ]
30 mapFamily  = [ '11000','10100','10010','10001','01100','00110','00011','01010','01001','00101' ]
32 startBar = '01011';
33 sepBar = '01';
35 class Object(Barcode):
36     def encode(self, number):
37         result = []
38         self.x += 110.0             # horizontal offset so it does not overlap EAN13
39         self.y -= self.height + 5   # move the text to the top
40         if len(number) != 5 or not number.isdigit():
41             sys.stderr.write("Can not encode '" + number + "' into EAN5 Barcode, Size must be 5 numbers only\n")
42             return
44         check = self.getChecksum(number)
45         family = mapFamily[check]
47         for i in range(5):
48             mapLeft = mapLeftFamily[int(family[i])]
49             result.append(mapLeft[int(number[i])])
51         self.label = number[0]
52         for i in range(1,5):
53             self.label += ' ' + number[i]
54         self.inclabel = self.label
55         return startBar + '01'.join(result)
57     def getChecksum(self, number):
58         return sum([int(n)*int(m) for n,m in zip(number, '39393')]) % 10
60     def getStyle(self, index):
61         result = { 'width' : '1', 'top' : int(self.y) + self.height + 5 + int(self.fontSize()), 'write' : True }
62         if index==0: # White Space
63             result['write'] = False
64         elif index==1: # Black Bar
65             result['height'] = int(self.height)
66         elif index==2: # Guide Bar
67             result['height'] = int(self.height) + 5
68         return result