03c31adf15d9ea1af00ea9c4fdf1102d3f82fe28
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2007 Martin Owens
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
20 from Base import Barcode
22 chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'
23 encode = list(chars)
24 encode.append('($)')
25 encode.append('(/)')
26 encode.append('(+)')
27 encode.append('(%)')
28 encode.append('MARKER')
30 map = {}
32 i = 0
33 for char in encode:
34 map[char] = i
35 i = i + 1
37 # Extended encoding maps for full ASCII Code93
38 def getMap(array):
40 result = {}
41 y = 10
43 for x in array:
44 result[chr(x)] = encode[y]
45 y = y + 1
47 return result;
49 # MapA is eclectic, but B, C, D are all ASCII ranges
50 mapA = getMap([27,28,29,30,31,59,60,61,62,63,91,92,93,94,95,123,124,125,126,127,0,64,96,127,127,127]) # %
51 mapB = getMap(range(1, 26)) # $
52 mapC = getMap(range(33, 58)) # /
53 mapD = getMap(range(97, 122)) # +
55 encoding = '100010100 101001000 101000100 101000010 100101000 100100100 100100010 101010000 100010010 100001010 110101000 110100100 110100010 110010100 110010010 110001010 101101000 101100100 101100010 100110100 100011010 101011000 101001100 101000110 100101100 100010110 110110100 110110010 110101100 110100110 110010110 110011010 101101100 101100110 100110110 100111010 100101110 111010100 111010010 111001010 101101110 101110110 110101110 100100110 111011010 111010110 100110010 101011110'.split()
57 class Object(Barcode):
58 def encode(self, text):
59 # start marker
60 bits = self.encode93('MARKER')
62 # Extend to ASCII charset ( return Array )
63 text = self.encodeAscii(text)
65 # Calculate the checksums
66 text.append(self.checksum(text, 20)) # C
67 text.append(self.checksum(text, 15)) # K
69 # Now convert text into the encoding bits (black and white stripes)
70 for char in text:
71 bits = bits + self.encode93(char)
73 # end marker
74 bits = bits + self.encode93('MARKER')
76 # termination bar
77 bits = bits + '1'
79 self.inclabel = text
80 return bits
82 def checksum(self, text, mod):
83 weight = len(text) % mod
84 check = 0
85 for char in text:
86 check = check + (map[char] * weight)
87 # Reset the weight is required
88 weight = weight - 1
89 if weight == 0:
90 weight = mod
92 return encode[check % 47]
94 # Some charicters need re-encoding into the code93 specification
95 def encodeAscii(self, text):
96 result = []
97 for char in text:
98 if map.has_key(char):
99 result.append(char)
100 elif mapA.has_key(char):
101 result.append('(%)')
102 result.append(mapA[char])
103 elif mapB.has_key(char):
104 result.append('($)')
105 result.append(mapB[char])
106 elif mapC.has_key(char):
107 result.append('(/)')
108 result.append(mapC[char])
109 elif mapD.has_key(char):
110 result.append('(+)')
111 result.append(mapD[char])
113 return result
115 def encode93(self, char):
116 if map.has_key(char):
117 return encoding[map[char]]
118 return ''