Code

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