reportlab: attempt fix to docIf vs keepTogether
authorrgbecker
Thu, 16 Oct 2008 12:56:05 +0000
changeset 3009 71ae9bbac9fb
parent 3008 feba79d281ea
child 3010 1a6fc2f5876a
reportlab: attempt fix to docIf vs keepTogether
src/reportlab/platypus/doctemplate.py
src/reportlab/platypus/flowables.py
--- a/src/reportlab/platypus/doctemplate.py	Wed Oct 15 22:35:07 2008 +0000
+++ b/src/reportlab/platypus/doctemplate.py	Thu Oct 16 12:56:05 2008 +0000
@@ -261,6 +261,13 @@
         this page."""
         pass
 
+def _addGeneratedContent(flowables,frame):
+    S = getattr(frame,'_generated_content',None)
+    if S:
+        for i,f in enumerate(S):
+            flowables.insert(i,f)
+        del frame._generated_content
+
 class BaseDocTemplate:
     """
     First attempt at defining a document template class.
@@ -621,13 +628,6 @@
         finally:
             if frame: del f._frame
 
-    def _addGeneratedContent(self,flowables,frame):
-        S = getattr(frame,'_generated_content',None)
-        if S:
-            for i,f in enumerate(S):
-                flowables.insert(i,f)
-            del frame._generated_content
-
     def handle_flowable(self,flowables):
         '''try to handle one flowable from the front of list flowables.'''
 
@@ -659,7 +659,7 @@
                 if not isinstance(f,FrameActionFlowable):
                     self._curPageFlowableCount += 1
                     self.afterFlowable(f)
-                self._addGeneratedContent(flowables,frame)
+                _addGeneratedContent(flowables,frame)
             else:
                 if self.allowSplitting:
                     # see if this is a splittable thing
@@ -672,7 +672,7 @@
                         if frame.add(S[0], canv, trySplit=0):
                             self._curPageFlowableCount += 1
                             self.afterFlowable(S[0])
-                            self._addGeneratedContent(flowables,frame)
+                            _addGeneratedContent(flowables,frame)
                         else:
                             ident = "Splitting error(n==%d) on page %d in\n%s" % (n,self.page,self._fIdent(f,60,frame))
                             #leave to keep apart from the raise
--- a/src/reportlab/platypus/flowables.py	Wed Oct 15 22:35:07 2008 +0000
+++ b/src/reportlab/platypus/flowables.py	Thu Oct 16 12:56:05 2008 +0000
@@ -36,8 +36,9 @@
 from reportlab.rl_config import _FUZZ, overlapAttachedSpace
 __all__=('TraceInfo','Flowable','XBox','Preformatted','Image','Spacer','PageBreak','SlowPageBreak',
         'CondPageBreak','KeepTogether','Macro','CallerMacro','ParagraphAndImage',
-        'FailOnWrap','HRFlowable','PTOContainer','KeepInFrame','UseUpSpace')
-
+        'FailOnWrap','HRFlowable','PTOContainer','KeepInFrame','UseUpSpace',
+        'DocAssign', 'DocExec', 'DocAssert', 'DocPara', 'DocIf', 'DocWhile',
+        )
 class TraceInfo:
     "Holder for info about where an object originated"
     def __init__(self):
@@ -455,28 +456,45 @@
 
 def _listWrapOn(F,availWidth,canv,mergeSpace=1,obj=None,dims=None):
     '''return max width, required height for a list of flowables F'''
-    W = 0
-    H = 0
-    pS = 0
-    atTop = 1
-    for f in F:
-        if hasattr(f,'frameAction'): continue
-        w,h = f.wrapOn(canv,availWidth,0xfffffff)
-        if dims is not None: dims.append((w,h))
-        if w<=_FUZZ or h<=_FUZZ: continue
-        W = max(W,w)
-        H += h
-        if not atTop:
-            h = f.getSpaceBefore()
-            if mergeSpace: h = max(h-pS,0)
+    doct = getattr(canv,'_doctemplate',None)
+    if doct:
+        from reportlab.platypus.doctemplate import _addGeneratedContent
+        doct_frame = doct.frame
+        cframe = doct.frame = deepcopy(doct_frame)
+        cframe._generated_content = None
+        del cframe._generated_content
+    else:
+        cframe = None
+    try:
+        W = 0
+        H = 0
+        pS = 0
+        atTop = 1
+        F = F[:]
+        while F:
+            f = F.pop(0)
+            if hasattr(f,'frameAction'): continue
+            w,h = f.wrapOn(canv,availWidth,0xfffffff)
+            if dims is not None: dims.append((w,h))
+            if cframe:
+                _addGeneratedContent(F,cframe)
+            if w<=_FUZZ or h<=_FUZZ: continue
+            W = max(W,w)
             H += h
-        else:
-            if obj is not None: obj._spaceBefore = f.getSpaceBefore()
-            atTop = 0
-        pS = f.getSpaceAfter()
-        H += pS
-    if obj is not None: obj._spaceAfter = pS
-    return W, H-pS
+            if not atTop:
+                h = f.getSpaceBefore()
+                if mergeSpace: h = max(h-pS,0)
+                H += h
+            else:
+                if obj is not None: obj._spaceBefore = f.getSpaceBefore()
+                atTop = 0
+            pS = f.getSpaceAfter()
+            H += pS
+        if obj is not None: obj._spaceAfter = pS
+        return W, H-pS
+    finally:
+        if doct:
+            doct.frame = doct_frame
 
 def _flowableSublist(V):
     "if it isn't a list or tuple, wrap it in a list"