1 #
2 # Copyright (C) 2010 Geoffrey Mosini
3 #
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.
8 #
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.
13 #
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 #
18 """
19 Generate barcodes for Code25-interleaved 2 of 5, for Inkscape.
20 """
22 from Base import Barcode
23 import sys
25 # 1 means thick, 0 means thin
26 encoding = {
27 '0' : '00110',
28 '1' : '10001',
29 '2' : '01001',
30 '3' : '11000',
31 '4' : '00101',
32 '5' : '10100',
33 '6' : '01100',
34 '7' : '00011',
35 '8' : '10010',
36 '9' : '01010',
38 }
40 # Start and stop code are already encoded into white (0) and black(1) bars
41 start_code = '1010'
42 stop_code = '1101'
44 class Object(Barcode):
45 # Convert a text into string binary of black and white markers
46 def encode(self, number):
47 self.label = number
49 if not number.isdigit():
50 sys.stderr.write("CODE25 can only encode numbers.\n")
51 return
53 # Number of figures to encode must be even, a 0 is added to the left in case it's odd.
54 if len(number) % 2 > 0 :
55 number = '0' + number
57 # Number is encoded by pairs of 2 figures
58 size = len(number) / 2;
59 encoded = start_code;
60 for i in range(size):
61 # First in the pair is encoded in black (1), second in white (0)
62 black = encoding[number[i*2]]
63 white = encoding[number[i*2+1]]
64 for j in range(5):
65 if black[j] == '1':
66 encoded += '11'
67 else:
68 encoded += '1'
69 if white[j] == '1':
70 encoded += '00'
71 else:
72 encoded += '0'
74 encoded += stop_code
76 self.inclabel = number
77 return encoded;