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 Code93 barcodes. Designed for use with Inkscape.
20 """
22 from Base import Barcode
24 chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'
25 encode = list(chars)
26 encode.append('($)')
27 encode.append('(/)')
28 encode.append('(+)')
29 encode.append('(%)')
30 encode.append('MARKER')
32 map = {}
34 i = 0
35 for char in encode:
36 map[char] = i
37 i = i + 1
39 # Extended encoding maps for full ASCII Code93
40 def getMap(array):
42 result = {}
43 y = 10
45 for x in array:
46 result[chr(x)] = encode[y]
47 y = y + 1
49 return result;
51 # MapA is eclectic, but B, C, D are all ASCII ranges
52 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]) # %
53 mapB = getMap(range(1, 26)) # $
54 mapC = getMap(range(33, 58)) # /
55 mapD = getMap(range(97, 122)) # +
57 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()
59 class Object(Barcode):
60 def encode(self, text):
61 # start marker
62 bits = self.encode93('MARKER')
64 # Extend to ASCII charset ( return Array )
65 text = self.encodeAscii(text)
67 # Calculate the checksums
68 text.append(self.checksum(text, 20)) # C
69 text.append(self.checksum(text, 15)) # K
71 # Now convert text into the encoding bits (black and white stripes)
72 for char in text:
73 bits = bits + self.encode93(char)
75 # end marker
76 bits = bits + self.encode93('MARKER')
78 # termination bar
79 bits = bits + '1'
81 self.inclabel = text
82 return bits
84 def checksum(self, text, mod):
85 weight = len(text) % mod
86 check = 0
87 for char in text:
88 check = check + (map[char] * weight)
89 # Reset the weight is required
90 weight = weight - 1
91 if weight == 0:
92 weight = mod
94 return encode[check % 47]
96 # Some charicters need re-encoding into the code93 specification
97 def encodeAscii(self, text):
98 result = []
99 for char in text:
100 if map.has_key(char):
101 result.append(char)
102 elif mapA.has_key(char):
103 result.append('(%)')
104 result.append(mapA[char])
105 elif mapB.has_key(char):
106 result.append('($)')
107 result.append(mapB[char])
108 elif mapC.has_key(char):
109 result.append('(/)')
110 result.append(mapC[char])
111 elif mapD.has_key(char):
112 result.append('(+)')
113 result.append(mapD[char])
115 return result
117 def encode93(self, char):
118 if map.has_key(char):
119 return encoding[map[char]]
120 return ''