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