Code25i.py revision 12a239f8730c2bb6e3738d5e75b3237877594d07
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico#
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico# Copyright (C) 2010 Geoffrey Mosini
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
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico#
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico"""
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicoGenerate barcodes for Code25-interleaved 2 of 5, for Inkscape.
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico"""
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicofrom Base import Barcode
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicoimport sys
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico# 1 means thick, 0 means thin
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicoencoding = {
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '0' : '00110',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '1' : '10001',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '2' : '01001',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '3' : '11000',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '4' : '00101',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '5' : '10100',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '6' : '01100',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '7' : '00011',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '8' : '10010',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico '9' : '01010',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico}
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico# Start and stop code are already encoded into white (0) and black(1) bars
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicostart_code = '1010'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicostop_code = '1101'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
12a239f8730c2bb6e3738d5e75b3237877594d07Martin Owensclass Code25i(Barcode):
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico # Convert a text into string binary of black and white markers
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico def encode(self, number):
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico self.label = number
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico if not number.isdigit():
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico sys.stderr.write("CODE25 can only encode numbers.\n")
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico return
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico # Number of figures to encode must be even, a 0 is added to the left in case it's odd.
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico if len(number) % 2 > 0 :
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico number = '0' + number
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico # Number is encoded by pairs of 2 figures
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico size = len(number) / 2;
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico encoded = start_code;
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico for i in range(size):
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico # First in the pair is encoded in black (1), second in white (0)
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico black = encoding[number[i*2]]
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico white = encoding[number[i*2+1]]
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico for j in range(5):
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico if black[j] == '1':
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico encoded += '11'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico else:
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico encoded += '1'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico if white[j] == '1':
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico encoded += '00'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico else:
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico encoded += '0'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico encoded += stop_code
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico self.inclabel = number
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico return encoded;
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico