add support for Indenter in PTOs
authorrobin
Mon, 22 Apr 2013 17:48:33 +0100
changeset 3686 0ef2cb9578d7
parent 3685 2d8552a5d431
child 3687 1fead6079288
add support for Indenter in PTOs
src/reportlab/platypus/doctemplate.py
src/reportlab/platypus/flowables.py
--- a/src/reportlab/platypus/doctemplate.py	Tue Apr 16 14:56:29 2013 +0100
+++ b/src/reportlab/platypus/doctemplate.py	Mon Apr 22 17:48:33 2013 +0100
@@ -222,6 +222,9 @@
     This allows one to have a 'context-sensitive' indentation
     and makes nested lists way easier.
     """
+    _ZEROSIZE=True
+    width=0
+    height=0
     def __init__(self, left=0, right=0):
         self.left = _evalMeasurement(left)
         self.right = _evalMeasurement(right)
--- a/src/reportlab/platypus/flowables.py	Tue Apr 16 14:56:29 2013 +0100
+++ b/src/reportlab/platypus/flowables.py	Mon Apr 22 17:48:33 2013 +0100
@@ -557,7 +557,7 @@
     doct = getattr(canv,'_doctemplate',None)
     cframe = getattr(doct,'frame',None)
     if cframe:
-        from reportlab.platypus.doctemplate import _addGeneratedContent
+        from reportlab.platypus.doctemplate import _addGeneratedContent, Indenter
         doct_frame = cframe
         cframe = doct.frame = deepcopy(doct_frame)
         cframe._generated_content = None
@@ -570,7 +570,11 @@
         F = F[:]
         while F:
             f = F.pop(0)
-            if hasattr(f,'frameAction'): continue
+            if hasattr(f,'frameAction'):
+                from reportlab.platypus.doctemplate import Indenter
+                if isinstance(f,Indenter):
+                    availWidth -= f.left+f.right
+                continue
             w,h = f.wrapOn(canv,availWidth,0xfffffff)
             if dims is not None: dims.append((w,h))
             if cframe:
@@ -825,7 +829,7 @@
 class _Container(_ContainerSpace):  #Abstract some common container like behaviour
     def drawOn(self, canv, x, y, _sW=0, scale=1.0, content=None, aW=None):
         '''we simulate being added to a frame'''
-        from doctemplate import ActionFlowable
+        from doctemplate import ActionFlowable, Indenter
         pS = 0
         if aW is None: aW = self.width
         aW *= scale
@@ -837,6 +841,10 @@
             if not ignoreContainerActions and isinstance(c,ActionFlowable):
                 c.apply(self.canv._doctemplate)
                 continue
+            if isinstance(c,Indenter):
+                x += c.left*scale
+                aW -= (c.left+c.right)*scale
+                continue
             w, h = c.wrapOn(canv,aW,0xfffffff)
             if (w<_FUZZ or h<_FUZZ) and not getattr(c,'_ZEROSIZE',None): continue
             if c is not content[0]: h += max(c.getSpaceBefore()-pS,0)
@@ -874,12 +882,14 @@
         return self.width,self.height
 
     def split(self, availWidth, availHeight):
+        from reportlab.platypus.doctemplate import Indenter
         if availHeight<0: return []
         canv = self.canv
         C = self._content
         x = i = H = pS = hx = 0
         n = len(C)
         I2W = {}
+        dLeft = dRight = 0
         for x in xrange(n):
             c = C[x]
             I = c._ptoinfo
@@ -895,10 +905,18 @@
             else:
                 T,tW,tH,tSB = I2W[I]
             _, h = c.wrapOn(canv,availWidth,0xfffffff)
-            if x:
-                hx = max(c.getSpaceBefore()-pS,0)
-                h += hx
-            pS = c.getSpaceAfter()
+            if isinstance(c,Indenter):
+                dw = c.left+c.right
+                dLeft += c.left
+                dRight += c.right
+                availWidth -= dw
+                pS = 0
+                hx = 0
+            else:
+                if x:
+                    hx = max(c.getSpaceBefore()-pS,0)
+                    h += hx
+                pS = c.getSpaceAfter()
             H += h+pS
             tHS = tH+max(tSB,pS)
             if H+tHS>=availHeight-_FUZZ: break
@@ -914,6 +932,12 @@
         else:
             SS = []
 
+        if abs(dLeft)+abs(dRight)>1e-8:
+            R1I = [Indenter(-dLeft,-dRight)]
+            R2I = [Indenter(dLeft,dRight)]
+        else:
+            R1I = R2I = []
+
         if not SS:
             j = i
             while i>1 and C[i-1].getKeepWithNext():
@@ -928,13 +952,13 @@
         F = [UseUpSpace()]
 
         if len(SS)>1:
-            R1 = C[:i] + SS[:1] + T + F
-            R2 = Hdr + SS[1:]+C[i+1:]
+            R1 = C[:i]+SS[:1]+R1I+T+F
+            R2 = Hdr+R2I+SS[1:]+C[i+1:]
         elif not i:
             return []
         else:
-            R1 = C[:i]+T+F
-            R2 = Hdr + C[i:]
+            R1 = C[:i]+R1I+T+F
+            R2 = Hdr+R2I+C[i:]
         T =  R1 + [PTOContainer(R2,[copy(x) for x in I.trailer],[copy(x) for x in I.header])]
         return T