Code

22902f2e4d22d73c57a2f8434440bc1248e0345b
[inkscape.git] / share / extensions / Barcode / RM4CC.py
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2007 Martin Owens
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
20 from Base import Barcode
22 map = {
23         '(' : '25',
24         ')' : '3',
25         '0' : '05053535',
26         '1' : '05152535',
27         '2' : '05153525',
28         '3' : '15052535',
29         '4' : '15053525',
30         '5' : '15152525',
31         '6' : '05251535',
32         '7' : '05350535',
33         '8' : '05351525',
34         '9' : '15250535',
35         'A' : '15251525',
36         'B' : '15350525',
37         'C' : '05253515',
38         'D' : '05352515',
39         'E' : '05353505',
40         'F' : '15252515',
41         'G' : '15253505',
42         'H' : '15352505',
43         'I' : '25051535',
44         'J' : '25150535',
45         'K' : '25151525',
46         'L' : '35050535',
47         'M' : '35051525',
48         'N' : '35150525',
49         'O' : '25053525',
50         'P' : '25152515',
51         'Q' : '25153505',
52         'R' : '35052515',
53         'S' : '35053505',
54         'T' : '35152505',
55         'U' : '25251515',
56         'V' : '25350515',
57         'W' : '25351505',
58         'X' : '35250515',
59         'Y' : '35251505',
60         'Z' : '35350505',
61 }
63 check = ['ZUVWXY','501234','B6789A','HCDEFG','NIJKLM','TOPQRS']
65 class Object(Barcode):
66         def encode(self, text):
67                 result = ''
69                 self.height = 18
70                 text = text.upper()
71                 text.replace('(', '')
72                 text.replace(')', '')
74                 text = '(' + text + self.checksum(text) + ')'
76                 i = 0
77                 for char in text:
78                         if map.has_key(char):
79                                 result = result + map[char]
80                         
81                                 i = i + 1
83                 self.inclabel = text
84                 return result;
86         # given a string of data, return the check character
87         def checksum(self, text):
88                 total_lower = 0
89                 total_upper = 0
90                 for char in text:
91                         if map.has_key(char):
92                                 bars = map[char][0:8:2]
93                                 lower = 0
94                                 upper = 0
96                                 if int(bars[0]) & 1:
97                                         lower = lower + 4 
98                                 if int(bars[1]) & 1:
99                                         lower = lower + 2
100                                 if int(bars[2]) & 1:
101                                         lower = lower + 1
102                                 if int(bars[0]) & 2:
103                                         upper = upper + 4
104                                 if int(bars[1]) & 2:
105                                         upper = upper + 2
106                                 if int(bars[2]) & 2:
107                                         upper = upper + 1
108                         total_lower = total_lower + (lower % 6)
109                         total_upper = total_upper + (upper % 6)
111                 total_lower = total_upper % 6
112                 total_upper = total_upper % 6
113         
114                 checkchar = check[total_upper][total_lower]
115                 return checkchar
117         def getStyle(self, index):
118                 result = { 'width' : 2, 'write' : True, 'top' : int(self.y) }
119                 if index==0: # Track Bar
120                         result['top']   = result['top'] + 6
121                         result['height'] = 5
122                 elif index==1: # Decender Bar
123                         result['top']   = result['top'] + 6
124                         result['height'] = 11
125                 elif index==2: # Accender Bar
126                         result['height'] = 11
127                 elif index==3: # Full Bar
128                         result['height'] = 17
129                 elif index==5: # White Space
130                         result['write']  = False
131                 return result