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