add support for rml <sup>/<sub> rise and size attributes; version --> 3.2.17
authorrobin
Wed, 10 Feb 2016 14:01:29 +0000
changeset 4249 8fc7d11bdee0
parent 4248 b902f0b891b0
child 4250 3ed1ea9c1fc4
add support for rml <sup>/<sub> rise and size attributes; version --> 3.2.17
docs/userguide/ch5_paragraphs.py
src/reportlab/__init__.py
src/reportlab/platypus/paraparser.py
tests/test_platypus_paragraphs.py
--- a/docs/userguide/ch5_paragraphs.py	Wed Feb 10 12:57:53 2016 +0000
+++ b/docs/userguide/ch5_paragraphs.py	Wed Feb 10 14:01:29 2016 +0000
@@ -282,15 +282,18 @@
 
 heading3("Superscripts and Subscripts")
 disc("""Superscripts and subscripts are supported with the
-<![CDATA[<super> and <sub> tags, which work exactly
-as you might expect.  In addition, most greek letters
+<![CDATA[<super>/<sup> and <sub> tags, which work exactly
+as you might expect. Additionally these three tags have
+attributes rise and size to optionally set the rise/descent
+and font size for the  superscript/subscript text.
+In addition, most greek letters
 can be accessed by using the <greek></greek>
 tag, or with mathML entity names.]]>""")
 
 ##parabox2("""<greek>epsilon</greek><super><greek>iota</greek>
 ##<greek>pi</greek></super> = -1""", "Greek letters and subscripts")
 
-parabox2("""Equation (&alpha;): <greek>e</greek> <super><greek>ip</greek></super>  = -1""",
+parabox2("""Equation (&alpha;): <greek>e</greek> <super rise=9 size=6><greek>ip</greek></super>  = -1""",
          "Greek letters and superscripts")
 
 heading3("Inline Images")
--- a/src/reportlab/__init__.py	Wed Feb 10 12:57:53 2016 +0000
+++ b/src/reportlab/__init__.py	Wed Feb 10 14:01:29 2016 +0000
@@ -1,7 +1,7 @@
 #Copyright ReportLab Europe Ltd. 2000-2015
 #see license.txt for license details
 __doc__="""The Reportlab PDF generation library."""
-Version = "3.2.16"
+Version = "3.2.17"
 __version__=Version
 __date__='20160112'
 
--- a/src/reportlab/platypus/paraparser.py	Wed Feb 10 12:57:53 2016 +0000
+++ b/src/reportlab/platypus/paraparser.py	Wed Feb 10 14:01:29 2016 +0000
@@ -30,7 +30,7 @@
 
 sizeDelta = 2       # amount to reduce font size by for super and sub script
 subFraction = 0.5   # fraction of font size that a sub script should be lowered
-superFraction = 0.5 # fraction of font size that a super script should be raised
+supFraction = 0.5 # fraction of font size that a super script should be raised
 
 DEFAULT_INDEX_NAME='_indexAdd'
 
@@ -250,6 +250,10 @@
                 'offset': ('offset',None),
                 'format': ('format',None),
                 }
+_supAttrMap = {
+                'rise': ('supr', _num),
+                'size': ('sups', _num),
+                }
 
 def _addAttributeNames(m):
     K = list(m.keys())
@@ -581,9 +585,9 @@
 #       < /i > - italics
 #       < u > < /u > - underline
 #       < strike > < /strike > - strike through
-#       < super > < /super > - superscript
-#       < sup > < /sup > - superscript
-#       < sub > < /sub > - subscript
+#       < super [size="pts"] [rise="pts"]> < /super > - superscript
+#       < sup ="pts"] [rise="pts"]> < /sup > - superscript
+#       < sub ="pts"] [rise="pts"]> < /sub > - subscript
 #       <font name=fontfamily/fontname color=colorname size=float>
 #        <span name=fontfamily/fontname color=colorname backcolor=colorname size=float style=stylename>
 #       < bullet > </bullet> - bullet text (at head of para only)
@@ -745,20 +749,26 @@
 
     #### super script
     def start_super( self, attributes ):
-        self._push('super',super=1)
+        A = self.getAttributes(attributes,_supAttrMap)
+        A['sup']=1
+        self._push('super',**A)
 
     def end_super( self ):
         self._pop('super')
 
     def start_sup( self, attributes ):
-        self._push('sup',super=1)
+        A = self.getAttributes(attributes,_supAttrMap)
+        A['sup']=1
+        self._push('sup',**A)
 
     def end_sup( self ):
         self._pop('sup')
 
     #### sub script
     def start_sub( self, attributes ):
-        self._push('sub',sub=1)
+        A = self.getAttributes(attributes,_supAttrMap)
+        A['sub']=1
+        self._push('sub',**A)
 
     def end_sub( self ):
         self._pop('sub')
@@ -866,7 +876,7 @@
         # initialize semantic values
         frag = ParaFrag()
         frag.sub = 0
-        frag.super = 0
+        frag.sup = 0
         frag.rise = 0
         frag.underline = 0
         frag.strike = 0
@@ -1105,17 +1115,17 @@
             if data!='': self._syntax_error('No content allowed in %s tag' % frag._selfClosingTag)
             return
         else:
-            # if sub and super are both on they will cancel each other out
-            if frag.sub == 1 and frag.super == 1:
+            # if sub and sup are both on they will cancel each other out
+            if frag.sub == 1 and frag.sup == 1:
                 frag.sub = 0
-                frag.super = 0
+                frag.sup = 0
 
             if frag.sub:
-                frag.rise = -frag.fontSize*subFraction
-                frag.fontSize = max(frag.fontSize-sizeDelta,3)
-            elif frag.super:
-                frag.rise = frag.fontSize*superFraction
-                frag.fontSize = max(frag.fontSize-sizeDelta,3)
+                frag.rise = -getattr(frag,'supr',frag.fontSize*subFraction)
+                frag.fontSize = getattr(frag,'sups',frag.fontSize-min(sizeDelta,0.2*frag.fontSize))
+            elif frag.sup:
+                frag.rise = getattr(frag,'supr',frag.fontSize*supFraction)
+                frag.fontSize = getattr(frag,'sups',frag.fontSize-min(sizeDelta,0.2*frag.fontSize))
 
             if frag.greek:
                 frag.fontName = 'symbol'
--- a/tests/test_platypus_paragraphs.py	Wed Feb 10 12:57:53 2016 +0000
+++ b/tests/test_platypus_paragraphs.py	Wed Feb 10 14:01:29 2016 +0000
@@ -528,6 +528,32 @@
             a(Paragraph(fmt % dict(valign=valign,testsFolder=testsFolder),p_style))
             a(XPreformatted(fmt % dict(valign=valign,testsFolder=testsFolder),p_style))
 
+        a(Paragraph('<br/><b>Some Paragraph tests of &lt;sup rise="pts" size="pts"</b>...', normal))
+        a(Paragraph("<br/>This is a <sup><span color='red'>sup</span></sup>.",p_style))
+        a(XPreformatted("This is a <sup><span color='red'>sup</span></sup>.",p_style))
+        a(Paragraph("This is a <sup rise=5><span color='red'>sup</span></sup>rise=5.",p_style))
+        a(XPreformatted("This is a <sup rise=5><span color='red'>sup</span></sup>rise=5.",p_style))
+        a(Paragraph("This is a <sup rise=6><span color='red'>sup</span></sup>rise=6.",p_style))
+        a(XPreformatted("This is a <sup rise=6><span color='red'>sup</span></sup>rise=6.",p_style))
+        a(Paragraph("This is a <sup rise=7><span color='red'>sup</span></sup>rise=7.",p_style))
+        a(XPreformatted("This is a <sup rise=7><span color='red'>sup</span></sup>rise=7.",p_style))
+        a(Paragraph("This is a <sup rise=8><span color='red'>sup</span></sup>rise=8.",p_style))
+        a(XPreformatted("This is a <sup rise=8><span color='red'>sup</span></sup>rise=8.",p_style))
+        a(Paragraph("This is a <sup rise=9><span color='red'>sup</span></sup>rise=9.",p_style))
+        a(XPreformatted("This is a <sup rise=9><span color='red'>sup</span></sup>rise=9.",p_style))
+        a(Paragraph("This is a <sup size=7><span color='red'>sup</span></sup>size=7.",p_style))
+        a(XPreformatted("This is a <sup size=7><span color='red'>sup</span></sup>size=7.",p_style))
+        a(Paragraph("This is a <sup rise=5 size=7><span color='red'>sup</span></sup>rise=5 size=7.",p_style))
+        a(XPreformatted("This is a <sup rise=5 size=7><span color='red'>sup</span></sup>rise=5 size=7.",p_style))
+        a(Paragraph("This is a <sup rise=6 size=7><span color='red'>sup</span></sup>rise=6 size=7.",p_style))
+        a(XPreformatted("This is a <sup rise=6 size=7><span color='red'>sup</span></sup>rise=6 size=7.",p_style))
+        a(Paragraph("This is a <sup rise=7 size=7><span color='red'>sup</span></sup>rise=7 size=7.",p_style))
+        a(XPreformatted("This is a <sup rise=7 size=7><span color='red'>sup</span></sup>rise=7 size=7.",p_style))
+        a(Paragraph("This is a <sup rise=8 size=7><span color='red'>sup</span></sup>rise=8 size=7.",p_style))
+        a(XPreformatted("This is a <sup rise=8 size=7><span color='red'>sup</span></sup>rise=8 size=7.",p_style))
+        a(Paragraph("This is a <sup rise=9 size=7><span color='red'>sup</span></sup>rise=9 size=7.",p_style))
+        a(XPreformatted("This is a <sup rise=9 size=7><span color='red'>sup</span></sup>rise=9 size=7.",p_style))
+        a(PageBreak())
 
         a(Paragraph('<br/><b>Some Paragraph tests of &lt;img width="x%" height="x%"</b>...', normal))
         a(Paragraph('H=10%% <img src="%(testsFolder)s/../docs/images/testimg.gif" width="0.57in" height="10%%" />'%dict(testsFolder=testsFolder), normal))