author | robin |
Tue, 19 Nov 2013 13:50:34 +0000 | |
branch | py33 |
changeset 3794 | 398ea04239b5 |
parent 3723 | 99aa837b6703 |
child 3975 | 4a3599863c11 |
permissions | -rw-r--r-- |
2587 | 1 |
# |
2 |
# Copyright (c) 2000 Tyler C. Sarna <tsarna@sarna.org> |
|
3 |
# All rights reserved. |
|
4 |
# |
|
5 |
# Redistribution and use in source and binary forms, with or without |
|
6 |
# modification, are permitted provided that the following conditions |
|
7 |
# are met: |
|
8 |
# 1. Redistributions of source code must retain the above copyright |
|
9 |
# notice, this list of conditions and the following disclaimer. |
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright |
|
11 |
# notice, this list of conditions and the following disclaimer in the |
|
12 |
# documentation and/or other materials provided with the distribution. |
|
13 |
# 3. All advertising materials mentioning features or use of this software |
|
14 |
# must display the following acknowledgement: |
|
15 |
# This product includes software developed by Tyler C. Sarna. |
|
16 |
# 4. Neither the name of the author nor the names of contributors |
|
17 |
# may be used to endorse or promote products derived from this software |
|
18 |
# without specific prior written permission. |
|
19 |
# |
|
20 |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|
21 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
22 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
23 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS |
|
24 |
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
25 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
26 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
27 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
28 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
29 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
30 |
# POSSIBILITY OF SUCH DAMAGE. |
|
31 |
# |
|
32 |
||
33 |
from reportlab.lib.units import inch |
|
3721 | 34 |
from .common import MultiWidthBarcode |
2587 | 35 |
|
36 |
_patterns = { |
|
37 |
'0' : ('AcAaAb', 0), '1' : ('AaAbAc', 1), '2' : ('AaAcAb', 2), |
|
38 |
'3' : ('AaAdAa', 3), '4' : ('AbAaAc', 4), '5' : ('AbAbAb', 5), |
|
39 |
'6' : ('AbAcAa', 6), '7' : ('AaAaAd', 7), '8' : ('AcAbAa', 8), |
|
40 |
'9' : ('AdAaAa', 9), 'A' : ('BaAaAc', 10), 'B' : ('BaAbAb', 11), |
|
41 |
'C' : ('BaAcAa', 12), 'D' : ('BbAaAb', 13), 'E' : ('BbAbAa', 14), |
|
42 |
'F' : ('BcAaAa', 15), 'G' : ('AaBaAc', 16), 'H' : ('AaBbAb', 17), |
|
43 |
'I' : ('AaBcAa', 18), 'J' : ('AbBaAb', 19), 'K' : ('AcBaAa', 20), |
|
44 |
'L' : ('AaAaBc', 21), 'M' : ('AaAbBb', 22), 'N' : ('AaAcBa', 23), |
|
45 |
'O' : ('AbAaBb', 24), 'P' : ('AcAaBa', 25), 'Q' : ('BaBaAb', 26), |
|
46 |
'R' : ('BaBbAa', 27), 'S' : ('BaAaBb', 28), 'T' : ('BaAbBa', 29), |
|
47 |
'U' : ('BbAaBa', 30), 'V' : ('BbBaAa', 31), 'W' : ('AaBaBb', 32), |
|
48 |
'X' : ('AaBbBa', 33), 'Y' : ('AbBaBa', 34), 'Z' : ('AbCaAa', 35), |
|
49 |
'-' : ('AbAaCa', 36), '.' : ('CaAaAb', 37), ' ' : ('CaAbAa', 38), |
|
50 |
'$' : ('CbAaAa', 39), '/' : ('AaBaCa', 40), '+' : ('AaCaBa', 41), |
|
51 |
'%' : ('BaAaCa', 42), '#' : ('AbAbBa', 43), '!' : ('CaBaAa', 44), |
|
52 |
'=' : ('CaAaBa', 45), '&' : ('AbBbAa', 46), |
|
53 |
'start' : ('AaAaDa', -1), 'stop' : ('AaAaDaA', -2) |
|
54 |
} |
|
55 |
||
56 |
_charsbyval = {} |
|
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
57 |
for k, v in _patterns.items(): |
2587 | 58 |
_charsbyval[v[1]] = k |
59 |
||
60 |
_extended = { |
|
61 |
'\x00' : '!U', '\x01' : '#A', '\x02' : '#B', '\x03' : '#C', |
|
62 |
'\x04' : '#D', '\x05' : '#E', '\x06' : '#F', '\x07' : '#G', |
|
63 |
'\x08' : '#H', '\x09' : '#I', '\x0a' : '#J', '\x0b' : '#K', |
|
64 |
'\x0c' : '#L', '\x0d' : '#M', '\x0e' : '#N', '\x0f' : '#O', |
|
65 |
'\x10' : '#P', '\x11' : '#Q', '\x12' : '#R', '\x13' : '#S', |
|
66 |
'\x14' : '#T', '\x15' : '#U', '\x16' : '#V', '\x17' : '#W', |
|
67 |
'\x18' : '#X', '\x19' : '#Y', '\x1a' : '#Z', '\x1b' : '!A', |
|
68 |
'\x1c' : '!B', '\x1d' : '!C', '\x1e' : '!D', '\x1f' : '!E', |
|
69 |
'!' : '=A', '"' : '=B', '#' : '=C', '$' : '=D', |
|
70 |
'%' : '=E', '&' : '=F', '\'' : '=G', '(' : '=H', |
|
71 |
')' : '=I', '*' : '=J', '+' : '=K', ',' : '=L', |
|
72 |
'/' : '=O', ':' : '=Z', ';' : '!F', '<' : '!G', |
|
73 |
'=' : '!H', '>' : '!I', '?' : '!J', '@' : '!V', |
|
74 |
'[' : '!K', '\\' : '!L', ']' : '!M', '^' : '!N', |
|
75 |
'_' : '!O', '`' : '!W', 'a' : '&A', 'b' : '&B', |
|
76 |
'c' : '&C', 'd' : '&D', 'e' : '&E', 'f' : '&F', |
|
77 |
'g' : '&G', 'h' : '&H', 'i' : '&I', 'j' : '&J', |
|
78 |
'k' : '&K', 'l' : '&L', 'm' : '&M', 'n' : '&N', |
|
79 |
'o' : '&O', 'p' : '&P', 'q' : '&Q', 'r' : '&R', |
|
80 |
's' : '&S', 't' : '&T', 'u' : '&U', 'v' : '&V', |
|
81 |
'w' : '&W', 'x' : '&X', 'y' : '&Y', 'z' : '&Z', |
|
82 |
'{' : '!P', '|' : '!Q', '}' : '!R', '~' : '!S', |
|
83 |
'\x7f' : '!T' |
|
84 |
} |
|
85 |
||
86 |
def _encode93(str): |
|
3721 | 87 |
s = list(str) |
2587 | 88 |
s.reverse() |
89 |
||
90 |
# compute 'C' checksum |
|
91 |
i = 0; v = 1; c = 0 |
|
92 |
while i < len(s): |
|
93 |
c = c + v * _patterns[s[i]][1] |
|
94 |
i = i + 1; v = v + 1 |
|
95 |
if v > 20: |
|
96 |
v = 1 |
|
97 |
s.insert(0, _charsbyval[c % 47]) |
|
98 |
||
99 |
# compute 'K' checksum |
|
100 |
i = 0; v = 1; c = 0 |
|
101 |
while i < len(s): |
|
102 |
c = c + v * _patterns[s[i]][1] |
|
103 |
i = i + 1; v = v + 1 |
|
104 |
if v > 15: |
|
105 |
v = 1 |
|
106 |
s.insert(0, _charsbyval[c % 47]) |
|
107 |
||
108 |
s.reverse() |
|
109 |
||
3794 | 110 |
return ''.join(s) |
2587 | 111 |
|
112 |
class _Code93Base(MultiWidthBarcode): |
|
113 |
barWidth = inch * 0.0075 |
|
114 |
lquiet = None |
|
115 |
rquiet = None |
|
116 |
quiet = 1 |
|
117 |
barHeight = None |
|
118 |
stop = 1 |
|
119 |
def __init__(self, value='', **args): |
|
120 |
||
121 |
if type(value) is type(1): |
|
122 |
value = str(value) |
|
123 |
||
3721 | 124 |
for (k, v) in args.items(): |
2587 | 125 |
setattr(self, k, v) |
126 |
||
127 |
if self.quiet: |
|
128 |
if self.lquiet is None: |
|
129 |
self.lquiet = max(inch * 0.25, self.barWidth * 10.0) |
|
130 |
self.rquiet = max(inch * 0.25, self.barWidth * 10.0) |
|
131 |
else: |
|
132 |
self.lquiet = self.rquiet = 0.0 |
|
133 |
||
134 |
MultiWidthBarcode.__init__(self, value) |
|
135 |
||
136 |
def decompose(self): |
|
137 |
dval = self.stop and [_patterns['start'][0]] or [] |
|
138 |
dval += [_patterns[c][0] for c in self.encoded] |
|
139 |
if self.stop: dval.append(_patterns['stop'][0]) |
|
140 |
self.decomposed = ''.join(dval) |
|
141 |
return self.decomposed |
|
142 |
||
143 |
class Standard93(_Code93Base): |
|
144 |
""" |
|
145 |
Code 93 is a Uppercase alphanumeric symbology with some punctuation. |
|
146 |
See Extended Code 93 for a variant that can represent the entire |
|
147 |
128 characrter ASCII set. |
|
148 |
||
149 |
Options that may be passed to constructor: |
|
150 |
||
151 |
value (int, or numeric string. required.): |
|
152 |
The value to encode. |
|
153 |
||
154 |
barWidth (float, default .0075): |
|
155 |
X-Dimension, or width of the smallest element |
|
156 |
Minumum is .0075 inch (7.5 mils). |
|
157 |
||
158 |
barHeight (float, see default below): |
|
159 |
Height of the symbol. Default is the height of the two |
|
160 |
bearer bars (if they exist) plus the greater of .25 inch |
|
161 |
or .15 times the symbol's length. |
|
162 |
||
163 |
quiet (bool, default 1): |
|
164 |
Wether to include quiet zones in the symbol. |
|
165 |
||
166 |
lquiet (float, see default below): |
|
167 |
Quiet zone size to left of code, if quiet is true. |
|
168 |
Default is the greater of .25 inch, or 10 barWidth |
|
169 |
||
170 |
rquiet (float, defaults as above): |
|
171 |
Quiet zone size to right left of code, if quiet is true. |
|
172 |
||
173 |
stop (bool, default 1): |
|
174 |
Whether to include start/stop symbols. |
|
175 |
||
176 |
Sources of Information on Code 93: |
|
177 |
||
178 |
http://www.semiconductor.agilent.com/barcode/sg/Misc/code_93.html |
|
179 |
||
180 |
Official Spec, "NSI/AIM BC5-1995, USS" is available for US$45 from |
|
181 |
http://www.aimglobal.org/aimstore/ |
|
182 |
""" |
|
183 |
def validate(self): |
|
184 |
vval = "" |
|
185 |
self.valid = 1 |
|
3794 | 186 |
for c in self.value.upper(): |
3326 | 187 |
if c not in _patterns: |
2587 | 188 |
self.valid = 0 |
189 |
continue |
|
190 |
vval = vval + c |
|
191 |
self.validated = vval |
|
192 |
return vval |
|
193 |
||
194 |
def encode(self): |
|
195 |
self.encoded = _encode93(self.validated) |
|
196 |
return self.encoded |
|
197 |
||
198 |
||
199 |
class Extended93(_Code93Base): |
|
200 |
""" |
|
201 |
Extended Code 93 is a convention for encoding the entire 128 character |
|
202 |
set using pairs of characters to represent the characters missing in |
|
203 |
Standard Code 93. It is very much like Extended Code 39 in that way. |
|
204 |
||
205 |
See Standard93 for arguments. |
|
206 |
""" |
|
207 |
||
208 |
def validate(self): |
|
209 |
vval = [] |
|
210 |
self.valid = 1 |
|
211 |
a = vval.append |
|
212 |
for c in self.value: |
|
3326 | 213 |
if c not in _patterns and c not in _extended: |
2587 | 214 |
self.valid = 0 |
215 |
continue |
|
216 |
a(c) |
|
217 |
self.validated = ''.join(vval) |
|
218 |
return self.validated |
|
219 |
||
220 |
def encode(self): |
|
221 |
self.encoded = "" |
|
222 |
for c in self.validated: |
|
3326 | 223 |
if c in _patterns: |
2587 | 224 |
self.encoded = self.encoded + c |
3326 | 225 |
elif c in _extended: |
2587 | 226 |
self.encoded = self.encoded + _extended[c] |
227 |
else: |
|
228 |
raise ValueError |
|
229 |
self.encoded = _encode93(self.encoded) |
|
230 |
return self.encoded |
|
231 |
||
232 |
def _humanText(self): |
|
233 |
return self.validated+self.encoded[-2:] |