Code

650d27d95dc1b5082ef9ad0f98f633de19c1a296
[inkscape.git] / Code39.py
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
21 encoding = {
22         '0' : '000110100',
23         '1' : '100100001',
24         '2' : '001100001',
25         '3' : '101100000', 
26         '4' : '000110001',
27         '5' : '100110000',
28         '6' : '001110000',
29         '7' : '000100101',
30         '8' : '100100100',
31         '9' : '001100100',
32         'A' : '100001001',
33         'B' : '001001001',
34         'C' : '101001000',
35         'D' : '000011001',
36         'E' : '100011000',
37         'F' : '001011000',
38         'G' : '000001101',
39         'H' : '100001100',
40         'I' : '001001100',
41         'J' : '000011100',
42         'K' : '100000011',
43         'L' : '001000011',
44         'M' : '101000010',
45         'N' : '000010011',
46         'O' : '100010010',
47         'P' : '001010010',
48         'Q' : '000000111',
49         'R' : '100000110',
50         'S' : '001000110',
51         'T' : '000010110',
52         'U' : '110000001',
53         'V' : '011000001',
54         'W' : '111000000',
55         'X' : '010010001',
56         'Y' : '110010000',
57         'Z' : '011010000',
58         '-' : '010000101',
59         '*' : '010010100',
60         '+' : '010001010',
61         '$' : '010101000',
62         '%' : '000101010',
63         '/' : '010100010',
64         '.' : '110000100',
65         ' ' : '011000100',
66 }
68 class Object(Barcode):
69         # Convert a text into string binary of black and white markers
70         def encode(self, text):
71                 text       = text.upper()
72                 self.label = text
73                 text       = '*' + text + '*'
74                 result     = ''
75                 # It isposible for us to encode code39
76                 # into full ascii, but this feature is
77                 # not enabled here
78                 for char in text:
79                         if not encoding.has_key(char):
80                                 char = '-';
82                         result = result + encoding[char] + '0';
84                 # Now we need to encode the code39, best read
85                 # the code to understand what it's up to:
86                 encoded = '';
87                 colour   = '1'; # 1 = Black, 0 = White
88                 for data in result:
89                         if data == '1':
90                                 encoded = encoded + colour + colour
91                         else:
92                                 encoded = encoded + colour
93                         if colour == '1':
94                                 colour = '0'
95                         else:
96                                 colour = '1'
98                 self.inclabel = text
99                 return encoded;