Rm4scc.py revision 12a239f8730c2bb6e3738d5e75b3237877594d07
369N/A#
369N/A# Copyright (C) 2007 Martin Owens
369N/A#
369N/A# This program is free software; you can redistribute it and/or modify
369N/A# it under the terms of the GNU General Public License as published by
369N/A# the Free Software Foundation; either version 2 of the License, or
369N/A# (at your option) any later version.
369N/A#
369N/A# This program is distributed in the hope that it will be useful,
369N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
369N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
369N/A# GNU General Public License for more details.
369N/A#
369N/A# You should have received a copy of the GNU General Public License
369N/A# along with this program; if not, write to the Free Software
369N/A# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
369N/A#
369N/A"""
369N/APython barcode renderer for RM4CC barcodes. Designed for use with Inkscape.
369N/A"""
5680N/A
5680N/Afrom Base import Barcode
5622N/A
369N/Amap = {
5706N/A '(' : '25',
5680N/A ')' : '3',
369N/A '0' : '05053535',
369N/A '1' : '05152535',
5706N/A '2' : '05153525',
844N/A '3' : '15052535',
5706N/A '4' : '15053525',
5706N/A '5' : '15152525',
5622N/A '6' : '05251535',
369N/A '7' : '05350535',
5680N/A '8' : '05351525',
5680N/A '9' : '15250535',
369N/A 'A' : '15251525',
5680N/A 'B' : '15350525',
369N/A 'C' : '05253515',
5803N/A 'D' : '05352515',
369N/A 'E' : '05353505',
369N/A 'F' : '15252515',
369N/A 'G' : '15253505',
369N/A 'H' : '15352505',
5795N/A 'I' : '25051535',
5795N/A 'J' : '25150535',
369N/A 'K' : '25151525',
5706N/A 'L' : '35050535',
369N/A 'M' : '35051525',
5706N/A 'N' : '35150525',
5706N/A 'O' : '25053525',
5706N/A 'P' : '25152515',
5706N/A 'Q' : '25153505',
5706N/A 'R' : '35052515',
5706N/A 'S' : '35053505',
5706N/A 'T' : '35152505',
5706N/A 'U' : '25251515',
5706N/A 'V' : '25350515',
5706N/A 'W' : '25351505',
5706N/A 'X' : '35250515',
5706N/A 'Y' : '35251505',
369N/A 'Z' : '35350505',
369N/A}
369N/A
369N/Acheck = ['ZUVWXY','501234','B6789A','HCDEFG','NIJKLM','TOPQRS']
369N/A(BAR_TRACK, BAR_DOWN, BAR_UP, BAR_FULL, BAR_NONE, WHITE_SPACE) = range(6)
369N/A
369N/Aclass Rm4scc(Barcode):
369N/A def encode(self, text):
369N/A result = ''
5795N/A
5795N/A self.height = 18
5795N/A text = text.upper()
5795N/A text.replace('(', '')
5795N/A text.replace(')', '')
5795N/A
5795N/A text = '(' + text + self.checksum(text) + ')'
5795N/A
5795N/A i = 0
5795N/A for char in text:
5795N/A if map.has_key(char):
5795N/A result = result + map[char]
369N/A
369N/A i = i + 1
369N/A
369N/A self.inclabel = text
369N/A return result;
369N/A
369N/A # given a string of data, return the check character
369N/A def checksum(self, text):
5680N/A total_lower = 0
5680N/A total_upper = 0
5680N/A for char in text:
5680N/A if map.has_key(char):
5680N/A bars = map[char][0:8:2]
369N/A lower = 0
369N/A upper = 0
5795N/A
5795N/A if int(bars[0]) & 1:
5680N/A lower = lower + 4
369N/A if int(bars[1]) & 1:
369N/A lower = lower + 2
5706N/A if int(bars[2]) & 1:
369N/A lower = lower + 1
if int(bars[0]) & 2:
upper = upper + 4
if int(bars[1]) & 2:
upper = upper + 2
if int(bars[2]) & 2:
upper = upper + 1
total_lower = total_lower + (lower % 6)
total_upper = total_upper + (upper % 6)
total_lower = total_upper % 6
total_upper = total_upper % 6
checkchar = check[total_upper][total_lower]
return checkchar
def getStyle(self, index):
"""Royal Mail Barcodes use a completely different style"""
result = { 'width' : 2, 'write' : True, 'top' : 0 }
if index == BAR_TRACK: # Track Bar
result['top'] = 6
result['height'] = 5
elif index == BAR_DOWN: # Decender Bar
result['top'] = 6
result['height'] = 11
elif index == BAR_UP: # Accender Bar
result['height'] = 11
elif index == BAR_FULL: # Full Bar
result['height'] = 17
elif index == WHITE_SPACE: # White Space
result['write'] = False
return result