Code

5b013c509e5c3b4fa9c59555bae966bfada1258f
[inkscape.git] / Code93.py
1 '''
2 Copyright (C) 2007 Martin Owens
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.
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.
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 '''
19 from Base import Barcode
21 chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'
22 encode = list(chars)
23 encode.append('($)')
24 encode.append('(/)')
25 encode.append('(+)')
26 encode.append('(%)')
27 encode.append('MARKER')
29 map = {}
31 i = 0
32 for char in encode:
33         map[char] = i
34         i = i + 1
36 # Extended encoding maps for full ASCII Code93
37 def getMap(array):
39         result = {}
40         y = 10
42         for x in array:
43                 result[chr(x)] = encode[y]
44                 y = y + 1
46         return result;
48 # MapA is eclectic, but B, C, D are all ASCII ranges
49 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]) # %
50 mapB = getMap(range(1, 26)) # $
51 mapC = getMap(range(33, 58)) # /
52 mapD = getMap(range(97, 122)) # +
54 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()
56 class Object(Barcode):
57         def encode(self, text):
58                 bits = self.encode93('MARKER')
60                 # Extend to ASCII charset ( return Array )
61                 text = self.encodeAscii(text)
63                 # Calculate the checksums
64                 text.append(self.checksum(text, 20)) # C
65                 text.append(self.checksum(text, 15)) # K
67                 # Now convert text into the encoding bits (black and white stripes)     
68                 for char in text:
69                         bits = bits + self.encode93(char)
71                 self.inclabel = text
72                 return bits
74         def checksum(self, text, mod):
75                 weight = len(text) % mod
76                 check  = 0
77                 for char in text:
78                         check = check + (map[char] * weight)
79                         # Reset the weight is required
80                         weight = weight - 1
81                         if weight == 0:
82                                 weight = mod
84                 return encode[check % 47]
86         # Some charicters need re-encoding into the code93 specification
87         def encodeAscii(self, text):
88                 result = []
89                 for char in text:
90                         if map.has_key(char):
91                                 result.append(char)
92                         elif mapA.has_key(char):
93                                 result.append('(%)')
94                                 result.append(mapA[char])
95                         elif mapB.has_key(char):
96                                 result.append('($)')
97                                 result.append(mapB[char])
98                         elif mapC.has_key(char):
99                                 result.append('(/)')
100                                 result.append(mapC[char])
101                         elif mapD.has_key(char):
102                                 result.append('(+)')
103                                 result.append(mapD[char])
104                                 
105                 return result
107         def encode93(self, char):
108                 if map.has_key(char):
109                         return encoding[map[char]]
110                 return ''