3cb79b4879ddbc2f6dcde07fa470d9341a7f653b
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2007 Martin Owens
5 Debugged by Ralf Heinecke & Martin Siepmann 09/07/2007
6 Debugged by Horst Schottky Feb. 27. 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 '''
26 from Base import Barcode
27 import math
28 import re
30 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' ]
32 def mapExtra(sd, chars):
33 result = list(sd)
34 for char in chars:
35 result.append(chr(char))
36 result.append('FNC3')
37 result.append('FNC2')
38 result.append('SHIFT')
39 return result
41 # The mapExtra method is used to slim down the amount
42 # of pre code and instead we generate the lists
43 charAB = list(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_')
44 charA = mapExtra(charAB, range(0, 31)) # Offset 64
45 charB = mapExtra(charAB, range(96, 125)) # Offset -32
47 class Object(Barcode):
48 def encode(self, text):
49 result = ''
50 blocks = []
51 block = ''
53 # Split up into sections of numbers, or charicters
54 # This makes sure that all the charicters are encoded
55 # In the best way posible for Code128
56 for datum in re.findall(r'(?:(?:\d\d){2,})|(?:^\d\d)|.', text):
57 if len(datum) == 1:
58 block = block + datum
59 else:
60 if block:
61 blocks.append(self.bestBlock(block))
62 block = ''
63 blocks.append( [ 'C', datum ] )
65 if block:
66 blocks.append(self.bestBlock(block))
67 block = '';
69 self.inclabel = text
70 return self.encodeBlocks(blocks)
72 def bestBlock(self, block):
73 # If this has lower case then select B over A
74 if block.upper() == block:
75 return [ 'A', block ]
76 return [ 'B', block ]
78 def encodeBlocks(self, blocks):
79 total = 0
80 pos = 0
81 encode = '';
83 for block in blocks:
84 set = block[0]
85 datum = block[1]
87 # POS : 0, 1
88 # A : 101, 103
89 # B : 100, 104
90 # C : 99, 105
91 num = 0;
92 if set == 'A':
93 num = 103
94 elif set == 'B':
95 num = 104
96 elif set == 'C':
97 num = 105
99 i = pos
100 if pos:
101 num = 204 - num
102 else:
103 i = 1
105 total = total + num * i
106 encode = encode + map[num]
107 pos = pos + 1
109 if set == 'A' or set == 'B':
110 chars = charB
111 if set == 'A':
112 chars = charA
114 for char in datum:
115 total = total + (chars.index(char) * pos)
116 encode = encode + map[chars.index(char)]
117 pos = pos + 1
118 else:
119 for char in (datum[i:i+2] for i in range(0, len(datum), 2)):
120 total = total + (int(char) * pos)
121 encode = encode + map[int(char)]
122 pos = pos + 1
124 checksum = total % 103
125 encode = encode + map[checksum]
126 encode = encode + map[106]
127 encode = encode + map[107]
129 return encode