예제 #1
0
파일: pdf417.py 프로젝트: tlanclos/elaphe
 def _code_bbox(self, codestring):
     compact = self.lookup_option('compact')
     columns = self.lookup_option('columns')
     rowmult = self.lookup_option('rowmult')
     rows = self.lookup_option('rows')
     eclevel = self.lookup_option('eclevel')
     raw = self.lookup_option('raw')
     codestring = cap_unescape(codestring)
     m = 0
     if raw==False:
         blen = len(codestring)
         if blen%6==0:
             m = (blen/6)*5+1
         else:
             m = ((blen-blen%6)/6)*5+blen%6+1
     else:
         m = len(codestring)
     if eclevel==-1:
         if m<=40:
             eclevel = 2
         elif 41<=m<=160:
             eclevel = 3
         elif 161<=m<=320:
             eclevel = 4
         else: # m>=321
             eclevel = 5
     maxeclevel = int(math.log(928-1-m)/math.log(2))-1
     if eclevel>maxeclevel:
         eclevel = maxeclevel
     k = 2**(eclevel+1)
     if columns==0:
         columns = int(round(math.sqrt((m+k)/3)))
     if 1<=columns<=30:
         c = columns
     else:
         raise ValueError(u'Column out of bound: %d.' %(columns))
     r = int(math.ceil((m+k+1)/(1.0*columns)))
     if r<rows and rows<=90:
         r = rows
     if r<3:
         r = 3
     maxeclevel = int(math.log(c*r-1-m)/math.log(2))-1
     if maxeclevel>eclevel:
         eclevel = maxeclevel
         k = 2**(eclevel+1)
     if compact:
         rwid = 17*c+17+17+1
     else:
         rwid = 17*c+17+17+17+17+1
     return (0, 0, DPI*rwid/72.0, DPI*(r/72.0)*rowmult)
예제 #2
0
 def _code_bbox(self, codestring):
     rows = self.lookup_option('rows')
     columns = self.lookup_option('columns')
     prefix = self.lookup_option('prefix')
     encoding = self.lookup_option('encoding')
     raw = self.lookup_option('raw')
     parse = self.lookup_option('parse')
     parsefnc = self.lookup_option('parse')
     if parse==True:
         codestring = cap_unescape(codestring)
     premaps = dict(FNC1=[232], PROG=[234], MAC5=[236], MAC6=[237])
     precws = premaps.get(prefix, [])
     prelen = len(precws)
     barlen = len(codestring)
     cw_length = 0
     if raw==True:
         encoding = 'raw'
     if encoding=='raw':
         cw_length = barlen/4+prelen
     elif encoding=='ascii':
         # BY sub(), codestring NO LONGER CARRY ACTUAL MESSAGE.
         if parsefnc==True:
             cw_length += len(re.findall(r'(?<!\^)\^FNC1', codestring))
             codestring = re.sub(r'(?<!\^)\^FNC1', '', codestring)
             codestring.replace('^^', '^')
         residue = codestring
         while residue:
             if residue[:2].isdigit():
                 cw_length+=2
                 residue = residue[2:]
                 continue
             elif residue[0].isdigit():
                 cw_length+=1
                 residue = residue[1:]
                 continue
             elif ord(residue[0])<128:
                 cw_length+=1
                 residue = residue[1:]
                 continue
             else:
                 cw_length+=2
                 residue = residue[1:]
     elif encoding in ['c40', 'text', 'x12']:
         enc_props = dict(
             c40=dict(
                 mode=230, eightbits=True,
                 charmap=(
                     '404142434445464748494a4b4c4d4e4f'
                     '505152535455565758595a5b5c5d5e5f'
                     '03808182838485868788898a8b8c8d8e'
                     '0405060708090a0b0c0d8f9091929394'
                     '950e0f101112131415161718191a1b1c'
                     '1d1e1f2021222324252627969798999a'
                     'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf'
                     'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf')),
             text=dict(
                 mode=239, eightbits=True,
                 charmap=(
                     '404142434445464748494a4b4c4d4e4f'
                     '505152535455565758595a5b5c5d5e5f'
                     '03808182838485868788898a8b8c8d8e'
                     '0405060708090a0b0c0d8f9091929394'
                     '95c1c2c3c4c5c6c7c8c9cacbcccdcecf'
                     'd0d1d2d3d4d5d6d7d8d9da969798999a'
                     'c00e0f101112131415161718191a1b1c'
                     '1d1e1f2021222324252627dbdcdddedf')),
             x12=dict(
                 mode=238, eightbits=False,
                 parsefnc=False,
                 charmap=(
                     'ffffffffffffffffffffffffff00ffff'
                     'ffffffffffffffffffffffffffffffff'
                     '03ffffffffffffffffff01ffffffffff'
                     '0405060708090a0b0c0dffffffff02ff'
                     'ff0e0f101112131415161718191a1b1c'
                     '1d1e1f2021222324252627ffffffffff'
                     'ffffffffffffffffffffffffffffffff'
                     'ffffffffffffffffffffffffffffffff')))
         parsefnc = enc_props.get('parsefnc', parsefnc)
         mode = enc_props.get('mode')
         eightbits = enc_props.get('eightbits')
         charmap = enc_props.get('charmap')
         if parsefnc==True:
             cw_length += 2*len(
                 re.findall(r'(?<!\^)\^FNC1', codestring))
             codestring = re.sub(r'(?<!\^)\^FNC1', '', codestring)
             codestring.replace('^^', '^')
         residue = codestring
         while residue:
             ch, residue = residue[:1], residue[1:]
             if eightbits and ord(ch)>127:
                 cw_length+=2
                 continue
             if 0<ord(ch)<128:
                 m_ord = int(charmap[ord(ch)*2:ord(ch)*2+2], 16)
                 if m_ord!=255:
                     if ((m_ord>>6)-1)!=0:
                         cw_length+=1
                     else:
                         pass
                 else:
                     raise ValueError(
                         'Invalid character in X12 encoding')
         # pad for triples
         if (cw_length%3):
             cw_length+=(3-cw_length%3)
         # triples->2bytes
         cw_length = (cw_length/3)*2+prelen+1
     else:
         raise ValueError('invalid encoding (%s).' %encoding)
     ucols = rows
     urows = columns
     for rows, cols, regh, regv, rscw, rsbl in metrics:
         mrows = rows - (2*regh)
         mcols = cols - (2*regv)
         rrows = mrows//regh
         rcols = mcols//regv
         ncws = mrows*mcols//8-rscw
         if cw_length>ncws:
             continue
         if urows and urows!=rows:
             continue
         if ucols and ucols!=cols:
             continue
         break
     return (0, 0, DPI*cols/72.0*2, DPI*rows/72.0*2)