"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.32/src/reportlab/graphics/barcode/widgets.py" (1 Oct 2019, 16858 Bytes) of package /linux/privat/reportlab-3.5.32.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "widgets.py" see the Fossies "Dox" file reference documentation.

    1 #copyright ReportLab Europe Limited. 2000-2016
    2 #see license.txt for license details
    3 __version__='3.3.0'
    4 __all__= (
    5         'BarcodeI2of5',
    6         'BarcodeCode128',
    7         'BarcodeStandard93',
    8         'BarcodeExtended93',
    9         'BarcodeStandard39',
   10         'BarcodeExtended39',
   11         'BarcodeMSI',
   12         'BarcodeCodabar',
   13         'BarcodeCode11',
   14         'BarcodeFIM',
   15         'BarcodePOSTNET',
   16         'BarcodeUSPS_4State',
   17         )
   18 
   19 from reportlab.lib.validators import isInt, isNumber, isColor, isString, isColorOrNone, OneOf, isBoolean, EitherOr, isNumberOrNone
   20 from reportlab.lib.attrmap import AttrMap, AttrMapValue
   21 from reportlab.lib.colors import black
   22 from reportlab.lib.utils import rl_exec
   23 from reportlab.graphics.shapes import Line, Rect, Group, NotImplementedError, String
   24 from reportlab.graphics.charts.areas import PlotArea
   25 
   26 '''
   27 #snippet
   28 
   29 #first make your Drawing
   30 from reportlab.graphics.shapes import Drawing
   31 d= Drawing(100,50)
   32 
   33 #create and set up the widget
   34 from reportlab.graphics.barcode.widgets import BarcodeStandard93
   35 bc = BarcodeStandard93()
   36 bc.value = 'RGB-123456'
   37 
   38 #add to the drawing and save
   39 d.add(bc)
   40 #   d.save(formats=['gif','pict'],fnRoot='bc_sample')
   41 '''
   42 
   43 class _BarcodeWidget(PlotArea):
   44     _attrMap = AttrMap(BASE=PlotArea,
   45         barStrokeColor = AttrMapValue(isColorOrNone, desc='Color of bar borders.'),
   46         barFillColor = AttrMapValue(isColorOrNone, desc='Color of bar interior areas.'),
   47         barStrokeWidth = AttrMapValue(isNumber, desc='Width of bar borders.'),
   48         value = AttrMapValue(EitherOr((isString,isNumber)), desc='Value.'),
   49         textColor = AttrMapValue(isColorOrNone, desc='Color of human readable text.'),
   50         valid = AttrMapValue(isBoolean),
   51         validated = AttrMapValue(isString,desc="validated form of input"),
   52         encoded = AttrMapValue(None,desc="encoded form of input"),
   53         decomposed = AttrMapValue(isString,desc="decomposed form of input"),
   54         canv = AttrMapValue(None,desc="temporarily used for internal methods"),
   55         gap = AttrMapValue(isNumberOrNone, desc='Width of inter character gaps.'),
   56         )
   57 
   58     textColor = barFillColor = black
   59     barStrokeColor = None
   60     barStrokeWidth = 0
   61     _BCC = None
   62     def __init__(self,_value='',**kw):
   63         PlotArea.__init__(self)
   64         del self.__dict__['width']
   65         del self.__dict__['height']
   66         self.x = self.y = 0
   67         kw.setdefault('value',_value)
   68         self._BCC.__init__(self,**kw)
   69 
   70     def rect(self,x,y,w,h,**kw):
   71         self._Gadd(Rect(self.x+x,self.y+y,w,h,
   72                     strokeColor=self.barStrokeColor,strokeWidth=self.barStrokeWidth, fillColor=self.barFillColor))
   73 
   74     def draw(self):
   75         if not self._BCC: raise NotImplementedError("Abstract class %s cannot be drawn" % self.__class__.__name__)
   76         self.canv = self
   77         G = Group()
   78         self._Gadd = G.add
   79         self._Gadd(Rect(self.x,self.y,self.width,self.height,fillColor=None,strokeColor=None,strokeWidth=0.0001))
   80         self._BCC.draw(self)
   81         del self.canv, self._Gadd
   82         return G
   83 
   84     def annotate(self,x,y,text,fontName,fontSize,anchor='middle'):
   85         self._Gadd(String(self.x+x,self.y+y,text,fontName=fontName,fontSize=fontSize,
   86                             textAnchor=anchor,fillColor=self.textColor))
   87 
   88 def _BCW(doc,codeName,attrMap,mod,value,**kwds):
   89     """factory for Barcode Widgets"""
   90     _pre_init = kwds.pop('_pre_init','')
   91     _methods = kwds.pop('_methods','')
   92     name = 'Barcode'+codeName
   93     ns = vars().copy()
   94     code = 'from %s import %s' % (mod,codeName)
   95     rl_exec(code,ns)
   96     ns['_BarcodeWidget'] = _BarcodeWidget
   97     code = '''class %(name)s(_BarcodeWidget,%(codeName)s):
   98 \t_BCC = %(codeName)s
   99 \tcodeName = %(codeName)r
  100 \tdef __init__(self,**kw):%(_pre_init)s
  101 \t\t_BarcodeWidget.__init__(self,%(value)r,**kw)%(_methods)s''' % ns
  102     rl_exec(code,ns)
  103     Klass = ns[name]
  104     if attrMap: Klass._attrMap = attrMap
  105     if doc: Klass.__doc__ = doc
  106     for k, v in kwds.items():
  107         setattr(Klass,k,v)
  108     return Klass
  109 
  110 BarcodeI2of5 = _BCW(
  111     """Interleaved 2 of 5 is used in distribution and warehouse industries.
  112 
  113     It encodes an even-numbered sequence of numeric digits. There is an optional
  114     module 10 check digit; if including this, the total length must be odd so that
  115     it becomes even after including the check digit.  Otherwise the length must be
  116     even. Since the check digit is optional, our library does not check it.
  117     """,
  118     "I2of5",
  119     AttrMap(BASE=_BarcodeWidget,
  120         barWidth = AttrMapValue(isNumber,'''(float, default .0075):
  121             X-Dimension, or width of the smallest element
  122             Minumum is .0075 inch (7.5 mils).'''),
  123         ratio = AttrMapValue(isNumber,'''(float, default 2.2):
  124             The ratio of wide elements to narrow elements.
  125             Must be between 2.0 and 3.0 (or 2.2 and 3.0 if the
  126             barWidth is greater than 20 mils (.02 inch))'''),
  127         gap = AttrMapValue(isNumberOrNone,'''(float or None, default None):
  128             width of intercharacter gap. None means "use barWidth".'''),
  129         barHeight = AttrMapValue(isNumber,'''(float, see default below):
  130             Height of the symbol.  Default is the height of the two
  131             bearer bars (if they exist) plus the greater of .25 inch
  132             or .15 times the symbol's length.'''),
  133         checksum = AttrMapValue(isBoolean,'''(bool, default 1):
  134             Whether to compute and include the check digit'''),
  135         bearers = AttrMapValue(isNumber,'''(float, in units of barWidth. default 3.0):
  136             Height of bearer bars (horizontal bars along the top and
  137             bottom of the barcode). Default is 3 x-dimensions.
  138             Set to zero for no bearer bars. (Bearer bars help detect
  139             misscans, so it is suggested to leave them on).'''),
  140         quiet = AttrMapValue(isBoolean,'''(bool, default 1):
  141             Whether to include quiet zones in the symbol.'''),
  142 
  143         lquiet = AttrMapValue(isNumber,'''(float, see default below):
  144             Quiet zone size to left of code, if quiet is true.
  145             Default is the greater of .25 inch, or .15 times the symbol's
  146             length.'''),
  147 
  148         rquiet = AttrMapValue(isNumber,'''(float, defaults as above):
  149             Quiet zone size to right left of code, if quiet is true.'''),
  150         fontName = AttrMapValue(isString, desc='human readable font'),
  151         fontSize = AttrMapValue(isNumber, desc='human readable font size'),
  152         humanReadable = AttrMapValue(isBoolean, desc='if human readable'),
  153         stop = AttrMapValue(isBoolean, desc='if we use start/stop symbols (default 1)'),
  154         ),
  155     'reportlab.graphics.barcode.common',
  156     1234,
  157     _tests = [
  158         '12',
  159         '1234',
  160         '123456',
  161         '12345678',
  162         '1234567890'
  163         ],
  164     )
  165 
  166 BarcodeCode128 = _BCW("""Code 128 encodes any number of characters in the ASCII character set.""",
  167                 "Code128",
  168                 AttrMap(BASE=BarcodeI2of5,UNWANTED=('bearers','checksum','ratio','checksum','stop')),
  169                 'reportlab.graphics.barcode.code128',
  170                 "AB-12345678",
  171                 _tests = ['ReportLab Rocks!', 'PFWZF'],
  172                 )
  173 
  174 BarcodeCode128Auto = _BCW(
  175                 'Modified Code128 to use auto encoding',
  176                 'Code128Auto',
  177                 AttrMap(BASE=BarcodeCode128),
  178                 'reportlab.graphics.barcode.code128',
  179                 'XY149740345GB'
  180                 )
  181 
  182 BarcodeStandard93=_BCW("""This is a compressed form of Code 39""",
  183                         "Standard93",
  184                         AttrMap(BASE=BarcodeCode128,
  185                                 stop = AttrMapValue(isBoolean, desc='if we use start/stop symbols (default 1)'),
  186                                 ),
  187                         'reportlab.graphics.barcode.code93',
  188                         "CODE 93",
  189                         )
  190 
  191 BarcodeExtended93=_BCW("""This is a compressed form of Code 39, allowing the full ASCII charset""",
  192                         "Extended93",
  193                         AttrMap(BASE=BarcodeCode128,
  194                                 stop = AttrMapValue(isBoolean, desc='if we use start/stop symbols (default 1)'),
  195                                 ),
  196                         'reportlab.graphics.barcode.code93',
  197                         "L@@K! Code 93 ;-)",
  198                         )
  199 
  200 BarcodeStandard39=_BCW("""Code39 is widely used in non-retail, especially US defence and health.
  201                         Allowed characters are 0-9, A-Z (caps only), space, and -.$/+%*.""",
  202                         "Standard39",
  203                         AttrMap(BASE=BarcodeI2of5),
  204                         'reportlab.graphics.barcode.code39',
  205                         "A012345B%R",
  206                         )
  207 
  208 BarcodeExtended39=_BCW("""Extended 39 encodes the full ASCII character set by encoding
  209                         characters as pairs of Code 39 characters; $, /, % and + are used as
  210                         shift characters.""",
  211                         "Extended39",
  212                         AttrMap(BASE=BarcodeI2of5),
  213                         'reportlab.graphics.barcode.code39',
  214                         "A012345B}",
  215                         )
  216 
  217 BarcodeMSI=_BCW("""MSI is used for inventory control in retail applications.
  218 
  219                 There are several methods for calculating check digits so we
  220                 do not implement one.
  221                 """,
  222                 "MSI",
  223                 AttrMap(BASE=BarcodeI2of5),
  224                 'reportlab.graphics.barcode.common',
  225                 1234,
  226                 )
  227 
  228 BarcodeCodabar=_BCW("""Used in blood banks, photo labs and FedEx labels.
  229                     Encodes 0-9, -$:/.+, and four start/stop characters A-D.""",
  230                     "Codabar",
  231                     AttrMap(BASE=BarcodeI2of5),
  232                     'reportlab.graphics.barcode.common',
  233                     "A012345B",
  234                     )
  235 
  236 BarcodeCode11=_BCW("""Used mostly for labelling telecommunications equipment.
  237                     It encodes numeric digits.""",
  238                     'Code11',
  239                     AttrMap(BASE=BarcodeI2of5,
  240                         checksum = AttrMapValue(isInt,'''(integer, default 2):
  241                             Whether to compute and include the check digit(s).
  242                             (0 none, 1 1-digit, 2 2-digit, -1 auto, default -1):
  243                             How many checksum digits to include. -1 ("auto") means
  244                             1 if the number of digits is 10 or less, else 2.'''),
  245                             ),
  246                     'reportlab.graphics.barcode.common',
  247                     "01234545634563",
  248                     )
  249 
  250 BarcodeFIM=_BCW("""
  251                 FIM was developed as part of the POSTNET barcoding system.
  252                 FIM (Face Identification Marking) is used by the cancelling machines
  253                 to sort mail according to whether or not they have bar code
  254                 and their postage requirements. There are four types of FIM
  255                 called FIM A, FIM B, FIM C, and FIM D.
  256 
  257                 The four FIM types have the following meanings:
  258                     FIM A- Postage required pre-barcoded
  259                     FIM B - Postage pre-paid, no bar code exists
  260                     FIM C- Postage prepaid prebarcoded
  261                     FIM D- Postage required, no bar code exists""",
  262                 "FIM",
  263                 AttrMap(BASE=_BarcodeWidget,
  264                     barWidth = AttrMapValue(isNumber,'''(float, default 1/32in): the bar width.'''),
  265                     spaceWidth = AttrMapValue(isNumber,'''(float or None, default 1/16in):
  266                         width of intercharacter gap. None means "use barWidth".'''),
  267                     barHeight = AttrMapValue(isNumber,'''(float, default 5/8in): The bar height.'''),
  268                     quiet = AttrMapValue(isBoolean,'''(bool, default 0):
  269                         Whether to include quiet zones in the symbol.'''),
  270                     lquiet = AttrMapValue(isNumber,'''(float, default: 15/32in):
  271                         Quiet zone size to left of code, if quiet is true.'''),
  272                     rquiet = AttrMapValue(isNumber,'''(float, default 1/4in):
  273                         Quiet zone size to right left of code, if quiet is true.'''),
  274                     fontName = AttrMapValue(isString, desc='human readable font'),
  275                     fontSize = AttrMapValue(isNumber, desc='human readable font size'),
  276                     humanReadable = AttrMapValue(isBoolean, desc='if human readable'),
  277                     ),
  278                 'reportlab.graphics.barcode.usps',
  279                 "A",
  280                 )
  281 
  282 BarcodePOSTNET=_BCW('',
  283                     "POSTNET",
  284                     AttrMap(BASE=_BarcodeWidget,
  285                             barWidth = AttrMapValue(isNumber,'''(float, default 0.018*in): the bar width.'''),
  286                             spaceWidth = AttrMapValue(isNumber,'''(float or None, default 0.0275in): width of intercharacter gap.'''),
  287                             shortHeight = AttrMapValue(isNumber,'''(float, default 0.05in): The short bar height.'''),
  288                             barHeight = AttrMapValue(isNumber,'''(float, default 0.125in): The full bar height.'''),
  289                             fontName = AttrMapValue(isString, desc='human readable font'),
  290                             fontSize = AttrMapValue(isNumber, desc='human readable font size'),
  291                             humanReadable = AttrMapValue(isBoolean, desc='if human readable'),
  292                             ),
  293                     'reportlab.graphics.barcode.usps',
  294                     "78247-1043",
  295                     )
  296 
  297 BarcodeUSPS_4State=_BCW('',
  298                         "USPS_4State",
  299                         AttrMap(BASE=_BarcodeWidget,
  300                             widthSize = AttrMapValue(isNumber,'''(float, default 1): the bar width size adjustment between 0 and 1.'''),
  301                             heightSize = AttrMapValue(isNumber,'''(float, default 1): the bar height size adjustment between 0 and 1.'''),
  302                             fontName = AttrMapValue(isString, desc='human readable font'),
  303                             fontSize = AttrMapValue(isNumber, desc='human readable font size'),
  304                             tracking = AttrMapValue(isString, desc='tracking data'),
  305                             routing = AttrMapValue(isString, desc='routing data'),
  306                             humanReadable = AttrMapValue(isBoolean, desc='if human readable'),
  307                             ),
  308                         'reportlab.graphics.barcode.usps4s',
  309                         '01234567094987654321',
  310                         _pre_init="\n\t\tkw.setdefault('routing','01234567891')\n",
  311                         _methods = "\n\tdef annotate(self,x,y,text,fontName,fontSize,anchor='middle'):\n\t\t_BarcodeWidget.annotate(self,x,y,text,fontName,fontSize,anchor='start')\n"
  312                         )
  313 BarcodeECC200DataMatrix = _BCW(
  314     'ECC200DataMatrix',
  315     'ECC200DataMatrix',
  316     AttrMap(BASE=_BarcodeWidget,
  317         x=AttrMapValue(isNumber, desc='X position of the lower-left corner of the barcode.'),
  318         y=AttrMapValue(isNumber, desc='Y position of the lower-left corner of the barcode.'),
  319         barWidth=AttrMapValue(isNumber, desc='Size of data modules.'),
  320         barFillColor=AttrMapValue(isColorOrNone, desc='Color of data modules.'),
  321         value=AttrMapValue(EitherOr((isString,isNumber)), desc='Value.'),
  322         height=AttrMapValue(None, desc='ignored'),
  323         width=AttrMapValue(None, desc='ignored'),
  324         strokeColor=AttrMapValue(None, desc='ignored'),
  325         strokeWidth=AttrMapValue(None, desc='ignored'),
  326         fillColor=AttrMapValue(None, desc='ignored'),
  327         background=AttrMapValue(None, desc='ignored'),
  328         debug=AttrMapValue(None, desc='ignored'),
  329         gap=AttrMapValue(None, desc='ignored'),
  330         row_modules=AttrMapValue(None, desc='???'),
  331         col_modules=AttrMapValue(None, desc='???'),
  332         row_regions=AttrMapValue(None, desc='???'),
  333         col_regions=AttrMapValue(None, desc='???'),
  334         cw_data=AttrMapValue(None, desc='???'),
  335         cw_ecc=AttrMapValue(None, desc='???'),
  336         row_usable_modules = AttrMapValue(None, desc='???'),
  337         col_usable_modules = AttrMapValue(None, desc='???'),
  338         valid = AttrMapValue(None, desc='???'),
  339         validated = AttrMapValue(None, desc='???'),
  340         decomposed = AttrMapValue(None, desc='???'),
  341     ),
  342     'reportlab.graphics.barcode.ecc200datamatrix',
  343     'JGB 0204H20B012722900021AC35B2100001003241014241014TPS01  WJ067073605GB185 MOUNT PLEASANT MAIL CENTER         EC1A1BB9ZGBREC1A1BB  EC1A1BB  STEST FILE       FOR SPEC                                       '
  344     )
  345 
  346 if __name__=='__main__':
  347     raise ValueError('widgets.py has no script function')