usps.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #
  2. # Copyright (c) 1996-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.graphics.barcode.common import Barcode
  34. from string import digits as string_digits, whitespace as string_whitespace
  35. from reportlab.lib.utils import asNative
  36. _fim_patterns = {
  37. 'A' : "|| | ||",
  38. 'B' : "| || || |",
  39. 'C' : "|| | | ||",
  40. 'D' : "||| | |||",
  41. # XXX There is an E.
  42. # The below has been seen, but dunno if it is E or not:
  43. # 'E' : '|||| ||||'
  44. }
  45. _postnet_patterns = {
  46. '1' : "...||", '2' : "..|.|", '3' : "..||.", '4' : ".|..|",
  47. '5' : ".|.|.", '6' : ".||..", '7' : "|...|", '8' : "|..|.",
  48. '9' : "|.|..", '0' : "||...", 'S' : "|",
  49. }
  50. class FIM(Barcode):
  51. """
  52. FIM (Facing ID Marks) encode only one letter.
  53. There are currently four defined:
  54. A Courtesy reply mail with pre-printed POSTNET
  55. B Business reply mail without pre-printed POSTNET
  56. C Business reply mail with pre-printed POSTNET
  57. D OCR Readable mail without pre-printed POSTNET
  58. Options that may be passed to constructor:
  59. value (single character string from the set A - D. required.):
  60. The value to encode.
  61. quiet (bool, default 0):
  62. Whether to include quiet zones in the symbol.
  63. The following may also be passed, but doing so will generate nonstandard
  64. symbols which should not be used. This is mainly documented here to
  65. show the defaults:
  66. barHeight (float, default 5/8 inch):
  67. Height of the code. This might legitimately be overriden to make
  68. a taller symbol that will 'bleed' off the edge of the paper,
  69. leaving 5/8 inch remaining.
  70. lquiet (float, default 1/4 inch):
  71. Quiet zone size to left of code, if quiet is true.
  72. Default is the greater of .25 inch, or .15 times the symbol's
  73. length.
  74. rquiet (float, default 15/32 inch):
  75. Quiet zone size to right left of code, if quiet is true.
  76. Sources of information on FIM:
  77. USPS Publication 25, A Guide to Business Mail Preparation
  78. http://new.usps.com/cpim/ftp/pubs/pub25.pdf
  79. """
  80. barWidth = inch * (1.0/32.0)
  81. spaceWidth = inch * (1.0/16.0)
  82. barHeight = inch * (5.0/8.0)
  83. rquiet = inch * (0.25)
  84. lquiet = inch * (15.0/32.0)
  85. quiet = 0
  86. def __init__(self, value='', **args):
  87. value = str(value) if isinstance(value,int) else asNative(value)
  88. for k, v in args.items():
  89. setattr(self, k, v)
  90. Barcode.__init__(self, value)
  91. def validate(self):
  92. self.valid = 1
  93. self.validated = ''
  94. for c in self.value:
  95. if c in string_whitespace:
  96. continue
  97. elif c in "abcdABCD":
  98. self.validated = self.validated + c.upper()
  99. else:
  100. self.valid = 0
  101. if len(self.validated) != 1:
  102. raise ValueError("Input must be exactly one character")
  103. return self.validated
  104. def decompose(self):
  105. self.decomposed = ''
  106. for c in self.encoded:
  107. self.decomposed = self.decomposed + _fim_patterns[c]
  108. return self.decomposed
  109. def computeSize(self):
  110. self._width = (len(self.decomposed) - 1) * self.spaceWidth + self.barWidth
  111. if self.quiet:
  112. self._width += self.lquiet + self.rquiet
  113. self._height = self.barHeight
  114. def draw(self):
  115. self._calculate()
  116. left = self.quiet and self.lquiet or 0
  117. for c in self.decomposed:
  118. if c == '|':
  119. self.rect(left, 0.0, self.barWidth, self.barHeight)
  120. left += self.spaceWidth
  121. self.drawHumanReadable()
  122. def _humanText(self):
  123. return self.value
  124. class POSTNET(Barcode):
  125. """
  126. POSTNET is used in the US to encode "zip codes" (postal codes) on
  127. mail. It can encode 5, 9, or 11 digit codes. I've read that it's
  128. pointless to do 5 digits, since USPS will just have to re-print
  129. them with 9 or 11 digits.
  130. Sources of information on POSTNET:
  131. USPS Publication 25, A Guide to Business Mail Preparation
  132. http://new.usps.com/cpim/ftp/pubs/pub25.pdf
  133. """
  134. quiet = 0
  135. shortHeight = inch * 0.050
  136. barHeight = inch * 0.125
  137. barWidth = inch * 0.018
  138. spaceWidth = inch * 0.0275
  139. def __init__(self, value='', **args):
  140. value = str(value) if isinstance(value,int) else asNative(value)
  141. for k, v in args.items():
  142. setattr(self, k, v)
  143. Barcode.__init__(self, value)
  144. def validate(self):
  145. self.validated = ''
  146. self.valid = 1
  147. count = 0
  148. for c in self.value:
  149. if c in (string_whitespace + '-'):
  150. pass
  151. elif c in string_digits:
  152. count = count + 1
  153. if count == 6:
  154. self.validated = self.validated + '-'
  155. self.validated = self.validated + c
  156. else:
  157. self.valid = 0
  158. if len(self.validated) not in [5, 10, 12]:
  159. self.valid = 0
  160. return self.validated
  161. def encode(self):
  162. self.encoded = "S"
  163. check = 0
  164. for c in self.validated:
  165. if c in string_digits:
  166. self.encoded = self.encoded + c
  167. check = check + int(c)
  168. elif c == '-':
  169. pass
  170. else:
  171. raise ValueError("Invalid character in input")
  172. check = (10 - check) % 10
  173. self.encoded = self.encoded + repr(check) + 'S'
  174. return self.encoded
  175. def decompose(self):
  176. self.decomposed = ''
  177. for c in self.encoded:
  178. self.decomposed = self.decomposed + _postnet_patterns[c]
  179. return self.decomposed
  180. def computeSize(self):
  181. self._width = len(self.decomposed) * self.barWidth + (len(self.decomposed) - 1) * self.spaceWidth
  182. self._height = self.barHeight
  183. def draw(self):
  184. self._calculate()
  185. sdown = self.barHeight - self.shortHeight
  186. left = 0
  187. for c in self.decomposed:
  188. if c == '.':
  189. h = self.shortHeight
  190. else:
  191. h = self.barHeight
  192. self.rect(left, 0.0, self.barWidth, h)
  193. left = left + self.barWidth + self.spaceWidth
  194. self.drawHumanReadable()
  195. def _humanText(self):
  196. return self.encoded[1:-1]