Code39.py revision ad9933c3bdd809813acc78673fcc580cd326bcd6
0N/A#
0N/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
0N/A#
0N/A"""
0N/APython barcode renderer for Code39 barcodes. Designed for use with Inkscape.
0N/A"""
0N/A
0N/Afrom Base import Barcode
0N/A
0N/AENCODE = {
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',
0N/A ' ': '011000100',
0N/A}
0N/A
0N/Aclass Code39(Barcode):
0N/A """Convert a text into string binary of black and white markers"""
0N/A def encode(self, text):
0N/A self.text = text.upper()
0N/A result = ''
0N/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 '*' + self.text + '*':
0N/A if not ENCODE.has_key(char):
0N/A char = '-'
0N/A result = result + ENCODE[char] + '0'
0N/A
0N/A # Now we need to encode the code39, best read
0N/A # the code to understand what it's up to:
0N/A encoded = ''
0N/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:
0N/A encoded = encoded + colour
0N/A colour = colour == '1' and '0' or '1'
0N/A
0N/A return encoded
0N/A
0N/A