src/reportlab/graphics/barcode/usps.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3966 d3d2b5e09c88
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     1
#
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     2
# Copyright (c) 1996-2000 Tyler C. Sarna <tsarna@sarna.org>
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     3
# All rights reserved.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     4
#
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     5
# Redistribution and use in source and binary forms, with or without
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     6
# modification, are permitted provided that the following conditions
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     7
# are met:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     8
# 1. Redistributions of source code must retain the above copyright
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
     9
#    notice, this list of conditions and the following disclaimer.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    10
# 2. Redistributions in binary form must reproduce the above copyright
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    11
#    notice, this list of conditions and the following disclaimer in the
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    12
#    documentation and/or other materials provided with the distribution.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    13
# 3. All advertising materials mentioning features or use of this software
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    14
#    must display the following acknowledgement:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    15
#      This product includes software developed by Tyler C. Sarna.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    16
# 4. Neither the name of the author nor the names of contributors
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    17
#    may be used to endorse or promote products derived from this software
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    18
#    without specific prior written permission.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    19
#
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    20
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    21
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    22
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    23
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    24
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    25
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    26
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    27
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    28
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    29
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    30
# POSSIBILITY OF SUCH DAMAGE.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    31
#
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    32
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    33
from reportlab.lib.units import inch
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3326
diff changeset
    34
from .common import Barcode
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    35
import string
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    36
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    37
_fim_patterns = {
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    38
    'A' : "||  |  ||",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    39
    'B' : "| || || |",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    40
    'C' : "|| | | ||",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    41
    'D' : "||| | |||",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    42
    # XXX There is an E.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    43
    # The below has been seen, but dunno if it is E or not:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    44
    # 'E' : '|||| ||||'
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    45
}
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    46
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    47
_postnet_patterns = {
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    48
    '1' : "...||",    '2' : "..|.|",    '3' : "..||.",    '4' : ".|..|",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    49
    '5' : ".|.|.",    '6' : ".||..",    '7' : "|...|",    '8' : "|..|.",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    50
    '9' : "|.|..",    '0' : "||...",    'S' : "|",
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    51
}
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    52
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    53
class FIM(Barcode):
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
    54
    """
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    55
    FIM (Facing ID Marks) encode only one letter.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    56
    There are currently four defined:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    57
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    58
    A   Courtesy reply mail with pre-printed POSTNET
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    59
    B   Business reply mail without pre-printed POSTNET
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    60
    C   Business reply mail with pre-printed POSTNET
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    61
    D   OCR Readable mail without pre-printed POSTNET
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    62
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    63
    Options that may be passed to constructor:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    64
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    65
        value (single character string from the set A - D. required.):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    66
            The value to encode.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    67
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    68
        quiet (bool, default 0):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    69
            Whether to include quiet zones in the symbol.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    70
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    71
    The following may also be passed, but doing so will generate nonstandard
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    72
    symbols which should not be used. This is mainly documented here to
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    73
    show the defaults:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    74
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    75
        barHeight (float, default 5/8 inch):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    76
            Height of the code. This might legitimately be overriden to make
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    77
            a taller symbol that will 'bleed' off the edge of the paper,
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    78
            leaving 5/8 inch remaining.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    79
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    80
        lquiet (float, default 1/4 inch):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    81
            Quiet zone size to left of code, if quiet is true.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    82
            Default is the greater of .25 inch, or .15 times the symbol's
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    83
            length.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    84
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    85
        rquiet (float, default 15/32 inch):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    86
            Quiet zone size to right left of code, if quiet is true.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    87
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    88
    Sources of information on FIM:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    89
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    90
    USPS Publication 25, A Guide to Business Mail Preparation
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    91
    http://new.usps.com/cpim/ftp/pubs/pub25.pdf
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    92
    """
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    93
    barWidth = inch * (1.0/32.0)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    94
    spaceWidth = inch * (1.0/16.0)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    95
    barHeight = inch * (5.0/8.0)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    96
    rquiet = inch * (0.25)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    97
    lquiet = inch * (15.0/32.0)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    98
    quiet = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
    99
    def __init__(self, value='', **args):
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   100
        for k, v in args.items():
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   101
            setattr(self, k, v)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   102
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   103
        Barcode.__init__(self, value)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   104
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   105
    def validate(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   106
        self.valid = 1
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   107
        self.validated = ''
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   108
        for c in self.value:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   109
            if c in string.whitespace:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   110
                continue
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   111
            elif c in "abcdABCD":
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   112
                self.validated = self.validated + c.upper()
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   113
            else:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   114
                self.valid = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   115
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   116
        if len(self.validated) != 1:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3326
diff changeset
   117
            raise ValueError("Input must be exactly one character")
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   118
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   119
        return self.validated
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   120
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   121
    def decompose(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   122
        self.decomposed = ''
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   123
        for c in self.encoded:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   124
            self.decomposed = self.decomposed + _fim_patterns[c]
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   125
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   126
        return self.decomposed
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   127
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   128
    def computeSize(self):
2779
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   129
        self._width = (len(self.decomposed) - 1) * self.spaceWidth + self.barWidth
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   130
        if self.quiet:
2779
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   131
            self._width += self.lquiet + self.rquiet
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   132
        self._height = self.barHeight
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   133
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   134
    def draw(self):
2779
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   135
        self._calculate()
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   136
        left = self.quiet and self.lquiet or 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   137
        for c in self.decomposed:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   138
            if c == '|':
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   139
                self.rect(left, 0.0, self.barWidth, self.barHeight)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   140
            left += self.spaceWidth
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   141
        self.drawHumanReadable()
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   142
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   143
    def _humanText(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   144
        return self.value
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   145
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   146
class POSTNET(Barcode):
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   147
    """
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   148
    POSTNET is used in the US to encode "zip codes" (postal codes) on
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   149
    mail. It can encode 5, 9, or 11 digit codes. I've read that it's
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   150
    pointless to do 5 digits, since USPS will just have to re-print
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   151
    them with 9 or 11 digits.
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   152
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   153
    Sources of information on POSTNET:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   154
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   155
    USPS Publication 25, A Guide to Business Mail Preparation
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   156
    http://new.usps.com/cpim/ftp/pubs/pub25.pdf
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   157
    """
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   158
    quiet = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   159
    shortHeight = inch * 0.050
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   160
    barHeight = inch * 0.125
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   161
    barWidth = inch * 0.018
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   162
    spaceWidth = inch * 0.0275
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   163
    def __init__(self, value='', **args):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   164
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   165
        for k, v in args.items():
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   166
            setattr(self, k, v)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   167
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   168
        Barcode.__init__(self, value)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   169
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   170
    def validate(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   171
        self.validated = ''
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   172
        self.valid = 1
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   173
        count = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   174
        for c in self.value:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   175
            if c in (string.whitespace + '-'):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   176
                pass
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   177
            elif c in string.digits:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   178
                count = count + 1
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   179
                if count == 6:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   180
                    self.validated = self.validated + '-'
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   181
                self.validated = self.validated + c
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   182
            else:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   183
                self.valid = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   184
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   185
        if len(self.validated) not in [5, 10, 12]:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   186
            self.valid = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   187
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   188
        return self.validated
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   189
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   190
    def encode(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   191
        self.encoded = "S"
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   192
        check = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   193
        for c in self.validated:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   194
            if c in string.digits:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   195
                self.encoded = self.encoded + c
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   196
                check = check + int(c)
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   197
            elif c == '-':
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   198
                pass
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   199
            else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3326
diff changeset
   200
                raise ValueError("Invalid character in input")
2779
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   201
        check = (10 - check) % 10
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3028
diff changeset
   202
        self.encoded = self.encoded + repr(check) + 'S'
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   203
        return self.encoded
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   204
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   205
    def decompose(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   206
        self.decomposed = ''
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   207
        for c in self.encoded:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   208
            self.decomposed = self.decomposed + _postnet_patterns[c]
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   209
        return self.decomposed
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   210
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   211
    def computeSize(self):
2779
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   212
        self._width = len(self.decomposed) * self.barWidth + (len(self.decomposed) - 1) * self.spaceWidth
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   213
        self._height = self.barHeight
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   214
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   215
    def draw(self):
2779
4df8f8a6b40e barcode: minor changes designed to make these more widget friendly
rgbecker
parents: 2587
diff changeset
   216
        self._calculate()
2587
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   217
        sdown = self.barHeight - self.shortHeight
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   218
        left = 0
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   219
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   220
        for c in self.decomposed:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   221
            if c == '.':
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   222
                h = self.shortHeight
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   223
            else:
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   224
                h = self.barHeight
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   225
            self.rect(left, 0.0, self.barWidth, h)
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   226
            left = left + self.barWidth + self.spaceWidth
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   227
        self.drawHumanReadable()
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   228
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   229
    def _humanText(self):
8984967879af reportlab: barcode moved to reportlab/graphics
rgbecker
parents:
diff changeset
   230
        return self.encoded[1:-1]