Code39.py revision 12a239f8730c2bb6e3738d5e75b3237877594d07
0N/A#
1472N/A# Copyright (C) 2007 Martin Owens
0N/A#
0N/A# This program is free software; you can redistribute it and/or modify
0N/A# it under the terms of the GNU General Public License as published by
0N/A# the Free Software Foundation; either version 2 of the License, or
0N/A# (at your option) any later version.
0N/A#
0N/A# This program is distributed in the hope that it will be useful,
0N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
0N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0N/A# GNU General Public License for more details.
0N/A#
0N/A# You should have received a copy of the GNU General Public License
0N/A# along with this program; if not, write to the Free Software
0N/A# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0N/A#
0N/A"""
1472N/APython barcode renderer for Code39 barcodes. Designed for use with Inkscape.
1472N/A"""
1472N/A
0N/Afrom Base import Barcode
0N/A
0N/Aencoding = {
0N/A '0' : '000110100',
0N/A '1' : '100100001',
0N/A '2' : '001100001',
0N/A '3' : '101100000',
0N/A '4' : '000110001',
0N/A '5' : '100110000',
0N/A '6' : '001110000',
0N/A '7' : '000100101',
0N/A '8' : '100100100',
0N/A '9' : '001100100',
0N/A 'A' : '100001001',
0N/A 'B' : '001001001',
0N/A 'C' : '101001000',
0N/A 'D' : '000011001',
0N/A 'E' : '100011000',
0N/A 'F' : '001011000',
0N/A 'G' : '000001101',
0N/A 'H' : '100001100',
0N/A 'I' : '001001100',
0N/A 'J' : '000011100',
0N/A 'K' : '100000011',
0N/A 'L' : '001000011',
0N/A 'M' : '101000010',
0N/A 'N' : '000010011',
0N/A 'O' : '100010010',
0N/A 'P' : '001010010',
0N/A 'Q' : '000000111',
0N/A 'R' : '100000110',
0N/A 'S' : '001000110',
0N/A 'T' : '000010110',
0N/A 'U' : '110000001',
0N/A 'V' : '011000001',
0N/A 'W' : '111000000',
0N/A 'X' : '010010001',
0N/A 'Y' : '110010000',
0N/A 'Z' : '011010000',
0N/A '-' : '010000101',
0N/A '*' : '010010100',
0N/A '+' : '010001010',
0N/A '$' : '010101000',
0N/A '%' : '000101010',
0N/A '/' : '010100010',
0N/A '.' : '110000100',
1015N/A ' ' : '011000100',
1015N/A}
1015N/A
1015N/Aclass Code39(Barcode):
0N/A # Convert a text into string binary of black and white markers
1601N/A def encode(self, text):
0N/A text = text.upper()
0N/A self.label = text
0N/A text = '*' + text + '*'
0N/A result = ''
1601N/A # It isposible for us to encode code39
0N/A # into full ascii, but this feature is
0N/A # not enabled here
0N/A for char in text:
0N/A if not encoding.has_key(char):
0N/A char = '-';
1601N/A
1488N/A result = result + encoding[char] + '0';
1488N/A
1601N/A # Now we need to encode the code39, best read
1601N/A # the code to understand what it's up to:
1601N/A encoded = '';
1601N/A colour = '1'; # 1 = Black, 0 = White
0N/A for data in result:
0N/A if data == '1':
0N/A encoded = encoded + colour + colour
0N/A else:
encoded = encoded + colour
colour = colour == '1' and '0' or '1'
self.inclabel = text
return encoded;