Code

Add doctormo's barcode extension. Patch 1681456. Still need to make it work with...
[inkscape.git] / share / extensions / Barcode / Code128.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
20 import math
21 import re
23 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' ]
25 def mapExtra(sd, chars):
26         result = list(sd)
27         for char in chars:
28                 result.append(chr(char))
29         result.append('FNC3')
30         result.append('FNC2')
31         result.append('SHIFT')
32         return result
34 # The mapExtra method is used to slim down the amount
35 # of pre code and instead we generate the lists
36 charAB = list(r' !"#$%&\()*+,./0123456789:;<=>?@ABCDEFGHIJKLMNOPQSTUVWXYZ\]^_')
37 charA = mapExtra(charAB, range(0, 31)) # Offset 64
38 charB = mapExtra(charAB, range(96, 125)) # Offset -32
40 class Object(Barcode):
41         def encode(self, text):
42                 result = ''
43                 blocks = []
44                 block  = ''
46                 # Split up into sections of numbers, or charicters
47                 # This makes sure that all the charicters are encoded
48                 # In the best way posible for Code128
49                 for datum in re.findall(r'(?:(?:\d\d){2,})|.', text):
50                         if len(datum) == 1:
51                                 block = block + datum
52                         else:
53                                 if block:
54                                         blocks.append(self.bestBlock(block))
55                                         block = ''
56                                 blocks.append( [ 'C', datum ] )
58                 if block:
59                         blocks.append(self.bestBlock(block))
60                         block = '';
61                 
62                 self.inclabel = text
63                 return self.encodeBlocks(blocks)
65         def bestBlock(self, block):
66                 # If this has lower case then select B over A
67                 if block.upper() == block:
68                         return [ 'B', block ]
69                 return [ 'A', block ]
70                 
71         def encodeBlocks(self, blocks):
72                 total  = 0
73                 pos    = 0
74                 encode = '';
75         
76                 for block in blocks:
77                         set   = block[0]
78                         datum = block[1]
80                         # POS :   0,   1
81                         # A   : 101, 103
82                         # B   : 100, 104
83                         # C   :  99, 105
84                         num = 0;
85                         if set == 'A':
86                                 num = 101
87                         elif set == 'B':
88                                 num = 100
89                         elif set == 'C':
90                                 num = 99
92                         i = pos
93                         if pos:
94                                 num = num + (math.abs(num - 102) * 2)
95                         else:
96                                 i = 1
98                         total = total + num * i
99                         encode = encode + map[num]
100                         pos = pos + 1
102                         if set == 'A' or set == 'B':
103                                 chars = charB
104                                 if set == 'B':
105                                         chars = charA
107                                 for char in datum:
108                                         total = total + (chars.index(char) * pos)
109                                         encode = encode + map[chars.index(char)]
110                                         pos = pos + 1
111                         else:
112                                 for char in (datum[i:i+2] for i in range(0, len(datum), 2)):
113                                         total = total + (int(char) * pos)
114                                         encode = encode + map[int(char)]
115                                         pos = pos + 1
116         
117                 checksum = total % 103
118                 encode = encode + map[checksum]
119                 encode = encode + map[106]
120                 encode = encode + map[107]
121         
122                 return encode