code93.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. from reportlab.lib.units import inch
  33. from reportlab.lib.utils import asNative
  34. from reportlab.graphics.barcode.common import MultiWidthBarcode
  35. _patterns = {
  36. '0' : ('AcAaAb', 0), '1' : ('AaAbAc', 1), '2' : ('AaAcAb', 2),
  37. '3' : ('AaAdAa', 3), '4' : ('AbAaAc', 4), '5' : ('AbAbAb', 5),
  38. '6' : ('AbAcAa', 6), '7' : ('AaAaAd', 7), '8' : ('AcAbAa', 8),
  39. '9' : ('AdAaAa', 9), 'A' : ('BaAaAc', 10), 'B' : ('BaAbAb', 11),
  40. 'C' : ('BaAcAa', 12), 'D' : ('BbAaAb', 13), 'E' : ('BbAbAa', 14),
  41. 'F' : ('BcAaAa', 15), 'G' : ('AaBaAc', 16), 'H' : ('AaBbAb', 17),
  42. 'I' : ('AaBcAa', 18), 'J' : ('AbBaAb', 19), 'K' : ('AcBaAa', 20),
  43. 'L' : ('AaAaBc', 21), 'M' : ('AaAbBb', 22), 'N' : ('AaAcBa', 23),
  44. 'O' : ('AbAaBb', 24), 'P' : ('AcAaBa', 25), 'Q' : ('BaBaAb', 26),
  45. 'R' : ('BaBbAa', 27), 'S' : ('BaAaBb', 28), 'T' : ('BaAbBa', 29),
  46. 'U' : ('BbAaBa', 30), 'V' : ('BbBaAa', 31), 'W' : ('AaBaBb', 32),
  47. 'X' : ('AaBbBa', 33), 'Y' : ('AbBaBa', 34), 'Z' : ('AbCaAa', 35),
  48. '-' : ('AbAaCa', 36), '.' : ('CaAaAb', 37), ' ' : ('CaAbAa', 38),
  49. '$' : ('CbAaAa', 39), '/' : ('AaBaCa', 40), '+' : ('AaCaBa', 41),
  50. '%' : ('BaAaCa', 42), '#' : ('AbAbBa', 43), '!' : ('CaBaAa', 44),
  51. '=' : ('CaAaBa', 45), '&' : ('AbBbAa', 46),
  52. 'start' : ('AaAaDa', -1), 'stop' : ('AaAaDaA', -2)
  53. }
  54. _charsbyval = {}
  55. for k, v in _patterns.items():
  56. _charsbyval[v[1]] = k
  57. _extended = {
  58. '\x00' : '!U', '\x01' : '#A', '\x02' : '#B', '\x03' : '#C',
  59. '\x04' : '#D', '\x05' : '#E', '\x06' : '#F', '\x07' : '#G',
  60. '\x08' : '#H', '\x09' : '#I', '\x0a' : '#J', '\x0b' : '#K',
  61. '\x0c' : '#L', '\x0d' : '#M', '\x0e' : '#N', '\x0f' : '#O',
  62. '\x10' : '#P', '\x11' : '#Q', '\x12' : '#R', '\x13' : '#S',
  63. '\x14' : '#T', '\x15' : '#U', '\x16' : '#V', '\x17' : '#W',
  64. '\x18' : '#X', '\x19' : '#Y', '\x1a' : '#Z', '\x1b' : '!A',
  65. '\x1c' : '!B', '\x1d' : '!C', '\x1e' : '!D', '\x1f' : '!E',
  66. '!' : '=A', '"' : '=B', '#' : '=C', '$' : '=D',
  67. '%' : '=E', '&' : '=F', '\'' : '=G', '(' : '=H',
  68. ')' : '=I', '*' : '=J', '+' : '=K', ',' : '=L',
  69. '/' : '=O', ':' : '=Z', ';' : '!F', '<' : '!G',
  70. '=' : '!H', '>' : '!I', '?' : '!J', '@' : '!V',
  71. '[' : '!K', '\\' : '!L', ']' : '!M', '^' : '!N',
  72. '_' : '!O', '`' : '!W', 'a' : '&A', 'b' : '&B',
  73. 'c' : '&C', 'd' : '&D', 'e' : '&E', 'f' : '&F',
  74. 'g' : '&G', 'h' : '&H', 'i' : '&I', 'j' : '&J',
  75. 'k' : '&K', 'l' : '&L', 'm' : '&M', 'n' : '&N',
  76. 'o' : '&O', 'p' : '&P', 'q' : '&Q', 'r' : '&R',
  77. 's' : '&S', 't' : '&T', 'u' : '&U', 'v' : '&V',
  78. 'w' : '&W', 'x' : '&X', 'y' : '&Y', 'z' : '&Z',
  79. '{' : '!P', '|' : '!Q', '}' : '!R', '~' : '!S',
  80. '\x7f' : '!T'
  81. }
  82. def _encode93(str):
  83. s = list(str)
  84. s.reverse()
  85. # compute 'C' checksum
  86. i = 0; v = 1; c = 0
  87. while i < len(s):
  88. c = c + v * _patterns[s[i]][1]
  89. i = i + 1; v = v + 1
  90. if v > 20:
  91. v = 1
  92. s.insert(0, _charsbyval[c % 47])
  93. # compute 'K' checksum
  94. i = 0; v = 1; c = 0
  95. while i < len(s):
  96. c = c + v * _patterns[s[i]][1]
  97. i = i + 1; v = v + 1
  98. if v > 15:
  99. v = 1
  100. s.insert(0, _charsbyval[c % 47])
  101. s.reverse()
  102. return ''.join(s)
  103. class _Code93Base(MultiWidthBarcode):
  104. barWidth = inch * 0.0075
  105. lquiet = None
  106. rquiet = None
  107. quiet = 1
  108. barHeight = None
  109. stop = 1
  110. def __init__(self, value='', **args):
  111. if type(value) is type(1):
  112. value = asNative(value)
  113. for (k, v) in args.items():
  114. setattr(self, k, v)
  115. if self.quiet:
  116. if self.lquiet is None:
  117. self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
  118. self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
  119. else:
  120. self.lquiet = self.rquiet = 0.0
  121. MultiWidthBarcode.__init__(self, value)
  122. def decompose(self):
  123. dval = self.stop and [_patterns['start'][0]] or []
  124. dval += [_patterns[c][0] for c in self.encoded]
  125. if self.stop: dval.append(_patterns['stop'][0])
  126. self.decomposed = ''.join(dval)
  127. return self.decomposed
  128. class Standard93(_Code93Base):
  129. """
  130. Code 93 is a Uppercase alphanumeric symbology with some punctuation.
  131. See Extended Code 93 for a variant that can represent the entire
  132. 128 characrter ASCII set.
  133. Options that may be passed to constructor:
  134. value (int, or numeric string. required.):
  135. The value to encode.
  136. barWidth (float, default .0075):
  137. X-Dimension, or width of the smallest element
  138. Minumum is .0075 inch (7.5 mils).
  139. barHeight (float, see default below):
  140. Height of the symbol. Default is the height of the two
  141. bearer bars (if they exist) plus the greater of .25 inch
  142. or .15 times the symbol's length.
  143. quiet (bool, default 1):
  144. Wether to include quiet zones in the symbol.
  145. lquiet (float, see default below):
  146. Quiet zone size to left of code, if quiet is true.
  147. Default is the greater of .25 inch, or 10 barWidth
  148. rquiet (float, defaults as above):
  149. Quiet zone size to right left of code, if quiet is true.
  150. stop (bool, default 1):
  151. Whether to include start/stop symbols.
  152. Sources of Information on Code 93:
  153. http://www.semiconductor.agilent.com/barcode/sg/Misc/code_93.html
  154. Official Spec, "NSI/AIM BC5-1995, USS" is available for US$45 from
  155. http://www.aimglobal.org/aimstore/
  156. """
  157. def validate(self):
  158. vval = ""
  159. self.valid = 1
  160. for c in self.value.upper():
  161. if c not in _patterns:
  162. self.valid = 0
  163. continue
  164. vval = vval + c
  165. self.validated = vval
  166. return vval
  167. def encode(self):
  168. self.encoded = _encode93(self.validated)
  169. return self.encoded
  170. class Extended93(_Code93Base):
  171. """
  172. Extended Code 93 is a convention for encoding the entire 128 character
  173. set using pairs of characters to represent the characters missing in
  174. Standard Code 93. It is very much like Extended Code 39 in that way.
  175. See Standard93 for arguments.
  176. """
  177. def validate(self):
  178. vval = []
  179. self.valid = 1
  180. a = vval.append
  181. for c in self.value:
  182. if c not in _patterns and c not in _extended:
  183. self.valid = 0
  184. continue
  185. a(c)
  186. self.validated = ''.join(vval)
  187. return self.validated
  188. def encode(self):
  189. self.encoded = ""
  190. for c in self.validated:
  191. if c in _patterns:
  192. self.encoded = self.encoded + c
  193. elif c in _extended:
  194. self.encoded = self.encoded + _extended[c]
  195. else:
  196. raise ValueError
  197. self.encoded = _encode93(self.encoded)
  198. return self.encoded
  199. def _humanText(self):
  200. return self.validated+self.encoded[-2:]