EAN8.py revision 56dcb73b5a1df7498fc6c7722ac4a380b9646fad
210N/A#!/usr/bin/env python
210N/A'''
210N/ACopyright (C) 2007 Martin Owens
210N/A
943N/AThis program is free software; you can redistribute it and/or modify
210N/Ait under the terms of the GNU General Public License as published by
210N/Athe Free Software Foundation; either version 2 of the License, or
919N/A(at your option) any later version.
919N/A
919N/AThis program is distributed in the hope that it will be useful,
919N/Abut WITHOUT ANY WARRANTY; without even the implied warranty of
919N/AMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
919N/AGNU General Public License for more details.
919N/A
919N/AYou should have received a copy of the GNU General Public License
919N/Aalong with this program; if not, write to the Free Software
919N/AFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
919N/A'''
919N/A
919N/Afrom Base import Barcode
919N/Aimport sys
919N/A
919N/AleftMap = [ '0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011' ]
919N/ArightMap = [ '1110010', '1100110', '1101100', '1000010', '1011100', '1001110', '1010000', '1000100', '1001000', '1110100' ]
210N/AweightMap = [ 3, 1, 3, 1, 3, 1, 3 ]
210N/A
210N/AguardBar = '202';
210N/AcenterBar = '02020';
493N/A
210N/Aclass Object(Barcode):
210N/A def encode(self, number):
851N/A result = ''
210N/A
911N/A # Rejig the label for use
911N/A self.label = number[:4] + ' ' + number[4:]
911N/A
911N/A if len(number) < 7 or len(number) > 8 or not number.isdigit():
210N/A sys.stderr.write("Can not encode '" + number + "' into EAN8 Barcode, Size must be 7 or 8 Numbers only\n")
210N/A
210N/A if len(number) == 7:
210N/A number = number + self.calculateChecksum(number)
210N/A
210N/A result = result + guardBar
210N/A
210N/A i = 0
493N/A for num in number:
969N/A if i >= 4:
210N/A result = result + rightMap[int(num)]
970N/A else:
970N/A result = result + leftMap[int(num)]
970N/A
493N/A i = i + 1
210N/A if i == 4:
210N/A result = result + centerBar;
210N/A
210N/A result = result + guardBar;
210N/A
493N/A self.inclabel = ' ' + number[:4] + ' ' + number[4:]
210N/A return result;
210N/A
970N/A
970N/A def calculateChecksum(self, number):
970N/A weight = 0;
210N/A i = 0;
for num in number:
weight = weight + (int(num) * weightMap[i])
i = i + 1
weight = 10 - (weight % 10)
if weight == 10:
weight = 0
return str(weight);
def getStyle(self, index):
result = { 'width' : '1', 'top' : int(self.y), 'write' : True }
if index==0: # White Space
result['write'] = False
elif index==1: # Black Bar
result['height'] = int(self.height)
elif index==2: # Guide Bar
result['height'] = int(self.height) + 8
return result