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
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico#
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico"""
1ef3c8b1b935901dd133c337031a7300334db424JazzyNicoGenerate barcodes for Code25-interleaved 2 of 5, for Inkscape.
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico"""
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owensfrom .Base import Barcode
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico# 1 means thick, 0 means thin
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin OwensENCODE = {
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '0': '00110',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '1': '10001',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '2': '01001',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '3': '11000',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '4': '00101',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '5': '10100',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '6': '01100',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '7': '00011',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '8': '10010',
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens '9': '01010',
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico}
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
12a239f8730c2bb6e3738d5e75b3237877594d07Martin Owensclass Code25i(Barcode):
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens """Convert a text into string binary of black and white markers"""
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens # Start and stop code are already encoded into white (0) and black(1) bars
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico def encode(self, number):
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico if not number.isdigit():
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens return self.error(number, "CODE25 can only encode numbers.")
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens # Number of figures to encode must be even,
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens # a 0 is added to the left in case it's odd.
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens if len(number) % 2 > 0:
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico number = '0' + number
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico # Number is encoded by pairs of 2 figures
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens size = len(number) / 2
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens encoded = '1010'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico for i in range(size):
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico # First in the pair is encoded in black (1), second in white (0)
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens black = ENCODE[number[i*2]]
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens white = ENCODE[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'
ad9933c3bdd809813acc78673fcc580cd326bcd6Martin Owens return encoded + '1101'
1ef3c8b1b935901dd133c337031a7300334db424JazzyNico