src/reportlab/platypus/paraparser.py
changeset 4249 8fc7d11bdee0
parent 4220 c0e82d246798
child 4252 fe660f227cac
equal deleted inserted replaced
4248:b902f0b891b0 4249:8fc7d11bdee0
    28 
    28 
    29 _re_para = re.compile(r'^\s*<\s*para(?:\s+|>|/>)')
    29 _re_para = re.compile(r'^\s*<\s*para(?:\s+|>|/>)')
    30 
    30 
    31 sizeDelta = 2       # amount to reduce font size by for super and sub script
    31 sizeDelta = 2       # amount to reduce font size by for super and sub script
    32 subFraction = 0.5   # fraction of font size that a sub script should be lowered
    32 subFraction = 0.5   # fraction of font size that a sub script should be lowered
    33 superFraction = 0.5 # fraction of font size that a super script should be raised
    33 supFraction = 0.5 # fraction of font size that a super script should be raised
    34 
    34 
    35 DEFAULT_INDEX_NAME='_indexAdd'
    35 DEFAULT_INDEX_NAME='_indexAdd'
    36 
    36 
    37 def _convnum(s, unit=1, allowRelative=True):
    37 def _convnum(s, unit=1, allowRelative=True):
    38     if s[0] in ('+','-') and allowRelative:
    38     if s[0] in ('+','-') and allowRelative:
   247 _indexAttrMap = {
   247 _indexAttrMap = {
   248                 'name': ('name',None),
   248                 'name': ('name',None),
   249                 'item': ('item',None),
   249                 'item': ('item',None),
   250                 'offset': ('offset',None),
   250                 'offset': ('offset',None),
   251                 'format': ('format',None),
   251                 'format': ('format',None),
       
   252                 }
       
   253 _supAttrMap = {
       
   254                 'rise': ('supr', _num),
       
   255                 'size': ('sups', _num),
   252                 }
   256                 }
   253 
   257 
   254 def _addAttributeNames(m):
   258 def _addAttributeNames(m):
   255     K = list(m.keys())
   259     K = list(m.keys())
   256     for k in K:
   260     for k in K:
   579 # tags:
   583 # tags:
   580 #       < /b > - bold
   584 #       < /b > - bold
   581 #       < /i > - italics
   585 #       < /i > - italics
   582 #       < u > < /u > - underline
   586 #       < u > < /u > - underline
   583 #       < strike > < /strike > - strike through
   587 #       < strike > < /strike > - strike through
   584 #       < super > < /super > - superscript
   588 #       < super [size="pts"] [rise="pts"]> < /super > - superscript
   585 #       < sup > < /sup > - superscript
   589 #       < sup ="pts"] [rise="pts"]> < /sup > - superscript
   586 #       < sub > < /sub > - subscript
   590 #       < sub ="pts"] [rise="pts"]> < /sub > - subscript
   587 #       <font name=fontfamily/fontname color=colorname size=float>
   591 #       <font name=fontfamily/fontname color=colorname size=float>
   588 #        <span name=fontfamily/fontname color=colorname backcolor=colorname size=float style=stylename>
   592 #        <span name=fontfamily/fontname color=colorname backcolor=colorname size=float style=stylename>
   589 #       < bullet > </bullet> - bullet text (at head of para only)
   593 #       < bullet > </bullet> - bullet text (at head of para only)
   590 #       <onDraw name=callable label="a label"/>
   594 #       <onDraw name=callable label="a label"/>
   591 #       <index [name="callablecanvasattribute"] label="a label"/>
   595 #       <index [name="callablecanvasattribute"] label="a label"/>
   743         self.handle_data('')
   747         self.handle_data('')
   744         self._pop('img')
   748         self._pop('img')
   745 
   749 
   746     #### super script
   750     #### super script
   747     def start_super( self, attributes ):
   751     def start_super( self, attributes ):
   748         self._push('super',super=1)
   752         A = self.getAttributes(attributes,_supAttrMap)
       
   753         A['sup']=1
       
   754         self._push('super',**A)
   749 
   755 
   750     def end_super( self ):
   756     def end_super( self ):
   751         self._pop('super')
   757         self._pop('super')
   752 
   758 
   753     def start_sup( self, attributes ):
   759     def start_sup( self, attributes ):
   754         self._push('sup',super=1)
   760         A = self.getAttributes(attributes,_supAttrMap)
       
   761         A['sup']=1
       
   762         self._push('sup',**A)
   755 
   763 
   756     def end_sup( self ):
   764     def end_sup( self ):
   757         self._pop('sup')
   765         self._pop('sup')
   758 
   766 
   759     #### sub script
   767     #### sub script
   760     def start_sub( self, attributes ):
   768     def start_sub( self, attributes ):
   761         self._push('sub',sub=1)
   769         A = self.getAttributes(attributes,_supAttrMap)
       
   770         A['sub']=1
       
   771         self._push('sub',**A)
   762 
   772 
   763     def end_sub( self ):
   773     def end_sub( self ):
   764         self._pop('sub')
   774         self._pop('sub')
   765 
   775 
   766     #### greek script
   776     #### greek script
   864             self._style = style
   874             self._style = style
   865 
   875 
   866         # initialize semantic values
   876         # initialize semantic values
   867         frag = ParaFrag()
   877         frag = ParaFrag()
   868         frag.sub = 0
   878         frag.sub = 0
   869         frag.super = 0
   879         frag.sup = 0
   870         frag.rise = 0
   880         frag.rise = 0
   871         frag.underline = 0
   881         frag.underline = 0
   872         frag.strike = 0
   882         frag.strike = 0
   873         frag.greek = 0
   883         frag.greek = 0
   874         frag.link = None
   884         frag.link = None
  1103             if data: self._syntax_error('Only empty <%s> tag allowed' % kind)
  1113             if data: self._syntax_error('Only empty <%s> tag allowed' % kind)
  1104         elif hasattr(frag,'_selfClosingTag'):
  1114         elif hasattr(frag,'_selfClosingTag'):
  1105             if data!='': self._syntax_error('No content allowed in %s tag' % frag._selfClosingTag)
  1115             if data!='': self._syntax_error('No content allowed in %s tag' % frag._selfClosingTag)
  1106             return
  1116             return
  1107         else:
  1117         else:
  1108             # if sub and super are both on they will cancel each other out
  1118             # if sub and sup are both on they will cancel each other out
  1109             if frag.sub == 1 and frag.super == 1:
  1119             if frag.sub == 1 and frag.sup == 1:
  1110                 frag.sub = 0
  1120                 frag.sub = 0
  1111                 frag.super = 0
  1121                 frag.sup = 0
  1112 
  1122 
  1113             if frag.sub:
  1123             if frag.sub:
  1114                 frag.rise = -frag.fontSize*subFraction
  1124                 frag.rise = -getattr(frag,'supr',frag.fontSize*subFraction)
  1115                 frag.fontSize = max(frag.fontSize-sizeDelta,3)
  1125                 frag.fontSize = getattr(frag,'sups',frag.fontSize-min(sizeDelta,0.2*frag.fontSize))
  1116             elif frag.super:
  1126             elif frag.sup:
  1117                 frag.rise = frag.fontSize*superFraction
  1127                 frag.rise = getattr(frag,'supr',frag.fontSize*supFraction)
  1118                 frag.fontSize = max(frag.fontSize-sizeDelta,3)
  1128                 frag.fontSize = getattr(frag,'sups',frag.fontSize-min(sizeDelta,0.2*frag.fontSize))
  1119 
  1129 
  1120             if frag.greek:
  1130             if frag.greek:
  1121                 frag.fontName = 'symbol'
  1131                 frag.fontName = 'symbol'
  1122                 data = _greekConvert(data)
  1132                 data = _greekConvert(data)
  1123 
  1133