Code

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