1 '''
2 Copyright (C) 2007 Martin Owens
4 Debugged by Ralf Heinecke & Martin Siepmann 09/07/2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 '''
24 from Base import Barcode
25 import math
26 import re
28 map = [ '11011001100','11001101100','11001100110','10010011000','10010001100','10001001100','10011001000','10011000100','10001100100','11001001000','11001000100','11000100100','10110011100','10011011100','10011001110','10111001100','10011101100','10011100110','11001110010','11001011100','11001001110','11011100100','11001110100','11101101110','11101001100','11100101100','11100100110','11101100100','11100110100','11100110010','11011011000','11011000110','11000110110','10100011000','10001011000','10001000110','10110001000','10001101000','10001100010','11010001000','11000101000','11000100010','10110111000','10110001110','10001101110','10111011000','10111000110','10001110110','11101110110','11010001110','11000101110','11011101000','11011100010','11011101110','11101011000','11101000110','11100010110','11101101000','11101100010','11100011010','11101111010','11001000010','11110001010','10100110000','10100001100','10010110000','10010000110','10000101100','10000100110','10110010000','10110000100','10011010000','10011000010','10000110100','10000110010','11000010010','11001010000','11110111010','11000010100','10001111010','10100111100','10010111100','10010011110','10111100100','10011110100','10011110010','11110100100','11110010100','11110010010','11011011110','11011110110','11110110110','10101111000','10100011110','10001011110','10111101000','10111100010','11110101000','11110100010','10111011110','10111101110','11101011110','11110101110','11010000100','11010010000','11010011100','11000111010','11' ]
30 def mapExtra(sd, chars):
31 result = list(sd)
32 for char in chars:
33 result.append(chr(char))
34 result.append('FNC3')
35 result.append('FNC2')
36 result.append('SHIFT')
37 return result
39 # The mapExtra method is used to slim down the amount
40 # of pre code and instead we generate the lists
41 charAB = list(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_')
42 charA = mapExtra(charAB, range(0, 31)) # Offset 64
43 charB = mapExtra(charAB, range(96, 125)) # Offset -32
45 class Object(Barcode):
46 def encode(self, text):
47 result = ''
48 blocks = []
49 block = ''
51 # Split up into sections of numbers, or charicters
52 # This makes sure that all the charicters are encoded
53 # In the best way posible for Code128
54 for datum in re.findall(r'(?:(?:\d\d){2,})|.', text):
55 if len(datum) == 1:
56 block = block + datum
57 else:
58 if block:
59 blocks.append(self.bestBlock(block))
60 block = ''
61 blocks.append( [ 'C', datum ] )
63 if block:
64 blocks.append(self.bestBlock(block))
65 block = '';
67 self.inclabel = text
68 return self.encodeBlocks(blocks)
70 def bestBlock(self, block):
71 # If this has lower case then select B over A
72 if block.upper() == block:
73 return [ 'A', block ]
74 return [ 'B', block ]
76 def encodeBlocks(self, blocks):
77 total = 0
78 pos = 0
79 encode = '';
81 for block in blocks:
82 set = block[0]
83 datum = block[1]
85 # POS : 0, 1
86 # A : 101, 103
87 # B : 100, 104
88 # C : 99, 105
89 num = 0;
90 if set == 'A':
91 num = 103
92 elif set == 'B':
93 num = 104
94 elif set == 'C':
95 num = 105
97 i = pos
98 if pos:
99 num = num + (math.abs(num - 102) * 2)
100 else:
101 i = 1
103 total = total + num * i
104 encode = encode + map[num]
105 pos = pos + 1
107 if set == 'A' or set == 'B':
108 chars = charB
109 if set == 'A':
110 chars = charA
112 for char in datum:
113 total = total + (chars.index(char) * pos)
114 encode = encode + map[chars.index(char)]
115 pos = pos + 1
116 else:
117 for char in (datum[i:i+2] for i in range(0, len(datum), 2)):
118 total = total + (int(char) * pos)
119 encode = encode + map[int(char)]
120 pos = pos + 1
122 checksum = total % 103
123 encode = encode + map[checksum]
124 encode = encode + map[106]
125 encode = encode + map[107]
127 return encode