1 #
2 # Copyright (C) 2007 Martin Owens
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #
18 """
19 Python barcode renderer for RM4CC barcodes. Designed for use with Inkscape.
20 """
22 from Base import Barcode
24 map = {
25 '(' : '25',
26 ')' : '3',
27 '0' : '05053535',
28 '1' : '05152535',
29 '2' : '05153525',
30 '3' : '15052535',
31 '4' : '15053525',
32 '5' : '15152525',
33 '6' : '05251535',
34 '7' : '05350535',
35 '8' : '05351525',
36 '9' : '15250535',
37 'A' : '15251525',
38 'B' : '15350525',
39 'C' : '05253515',
40 'D' : '05352515',
41 'E' : '05353505',
42 'F' : '15252515',
43 'G' : '15253505',
44 'H' : '15352505',
45 'I' : '25051535',
46 'J' : '25150535',
47 'K' : '25151525',
48 'L' : '35050535',
49 'M' : '35051525',
50 'N' : '35150525',
51 'O' : '25053525',
52 'P' : '25152515',
53 'Q' : '25153505',
54 'R' : '35052515',
55 'S' : '35053505',
56 'T' : '35152505',
57 'U' : '25251515',
58 'V' : '25350515',
59 'W' : '25351505',
60 'X' : '35250515',
61 'Y' : '35251505',
62 'Z' : '35350505',
63 }
65 check = ['ZUVWXY','501234','B6789A','HCDEFG','NIJKLM','TOPQRS']
66 (BAR_TRACK, BAR_DOWN, BAR_UP, BAR_FULL, BAR_NONE, WHITE_SPACE) = range(6)
68 class Object(Barcode):
69 def encode(self, text):
70 result = ''
72 self.height = 18
73 text = text.upper()
74 text.replace('(', '')
75 text.replace(')', '')
77 text = '(' + text + self.checksum(text) + ')'
79 i = 0
80 for char in text:
81 if map.has_key(char):
82 result = result + map[char]
84 i = i + 1
86 self.inclabel = text
87 return result;
89 # given a string of data, return the check character
90 def checksum(self, text):
91 total_lower = 0
92 total_upper = 0
93 for char in text:
94 if map.has_key(char):
95 bars = map[char][0:8:2]
96 lower = 0
97 upper = 0
99 if int(bars[0]) & 1:
100 lower = lower + 4
101 if int(bars[1]) & 1:
102 lower = lower + 2
103 if int(bars[2]) & 1:
104 lower = lower + 1
105 if int(bars[0]) & 2:
106 upper = upper + 4
107 if int(bars[1]) & 2:
108 upper = upper + 2
109 if int(bars[2]) & 2:
110 upper = upper + 1
111 total_lower = total_lower + (lower % 6)
112 total_upper = total_upper + (upper % 6)
114 total_lower = total_upper % 6
115 total_upper = total_upper % 6
117 checkchar = check[total_upper][total_lower]
118 return checkchar
120 def getStyle(self, index):
121 """Royal Mail Barcodes use a completely different style"""
122 result = { 'width' : 2, 'write' : True, 'top' : 0 }
123 if index == BAR_TRACK: # Track Bar
124 result['top'] = 6
125 result['height'] = 5
126 elif index == BAR_DOWN: # Decender Bar
127 result['top'] = 6
128 result['height'] = 11
129 elif index == BAR_UP: # Accender Bar
130 result['height'] = 11
131 elif index == BAR_FULL: # Full Bar
132 result['height'] = 17
133 elif index == WHITE_SPACE: # White Space
134 result['write'] = False
135 return result