~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/printing/barcodes/postnet.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2004-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# barcodes/code39.py
 
23
#
 
24
# DESCRIPTION:
 
25
"""
 
26
Implements the USPS PostNet barcode spec
 
27
"""
 
28
#
 
29
 
 
30
from Base import Barcode, InvalidBarcode
 
31
 
 
32
class PostNet(Barcode):
 
33
  """
 
34
  PostNet
 
35
  """
 
36
  validLengths = [5,9,11]
 
37
  chars = '0123456789'
 
38
  mapping = {
 
39
    # 1 = tall bar, 2=short bar, 0=space
 
40
    '0': '1010202020',
 
41
    '1': '2020201010',
 
42
    '2': '2020102010',
 
43
    '3': '2020101020',
 
44
    '4': '2010202010',
 
45
    '5': '2010201020',
 
46
    '6': '2010102020',
 
47
    '7': '1020202010',
 
48
    '8': '1020201020',
 
49
    '9': '1020102020'
 
50
  }
 
51
 
 
52
  start = '10'
 
53
  stop  = '1'
 
54
 
 
55
  lineWidth = 1.44 # points
 
56
  lineHeight = 8.5 # (.125")
 
57
  spaceWidth = 1.9 #1.66
 
58
 
 
59
  encodingMap = {
 
60
    # Stroke?, X Multiplier, Y Multiplier
 
61
    '0': (False, 1, 1),   # Spaces
 
62
    '1': (True, 1, 1),    # Tall bars
 
63
    '2': (True, 1, .45)   # Short bars
 
64
  }
 
65
 
 
66
 
 
67
 
 
68
  defaultIncludeText = False
 
69
 
 
70
  # Calculate a Mod-10 check digit
 
71
  def checkdigit(self, value):
 
72
    v = 0
 
73
    for ch in value:
 
74
      try:
 
75
        v += int(ch)
 
76
      except ValueError:
 
77
        raise InvalidBarcode
 
78
    cd = abs(10-divmod(v,10)[1])
 
79
    if cd == 10:
 
80
      cd = 0
 
81
    return self.chars[cd]
 
82
 
 
83
 
 
84
if __name__ == '__main__':
 
85
 
 
86
  postnet = PostNet()
 
87
 
 
88
  def test(value, format, file):
 
89
    f = open(file,'wb')
 
90
    postnet.generate(value,f, format)
 
91
    f.close()
 
92
 
 
93
#   test('381072456','png','test1.png')
 
94
#   test('381172459','tiff','test1.tif')
 
95
  test('383759907','eps','postnet-1.eps')
 
96
  test('12345','eps','postnet-2.eps')
 
97
  test('12345678901','eps','postnet-3.eps')