flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
authorrgbecker
Wed, 28 Sep 2005 13:22:53 +0000
changeset 2529 dced304f8584
parent 2528 39a609be559a
child 2530 aa87633edde3
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
reportlab/platypus/doctemplate.py
reportlab/platypus/flowables.py
reportlab/platypus/paragraph.py
--- a/reportlab/platypus/doctemplate.py	Tue Sep 27 21:12:08 2005 +0000
+++ b/reportlab/platypus/doctemplate.py	Wed Sep 28 13:22:53 2005 +0000
@@ -142,7 +142,8 @@
         return r
 
     def apply(self,doc):
-        if getattr(self,'_ix',None): doc._nextFrameIndex = self._ix
+        if getattr(self,'_ix',None):
+            doc.handle_nextFrame(self._ix)
         ActionFlowable.apply(self,doc)
 
 FrameBreak = _FrameBreak('frameEnd')
@@ -467,7 +468,7 @@
         self._rightExtraIndent = self.frame._rightExtraIndent
 
         if hasattr(self,'_nextFrameIndex'):
-            frame = self.pageTemplate.frames[self._nextFrameIndex]
+            self.frame = self.pageTemplate.frames[self._nextFrameIndex]
             del self._nextFrameIndex
             self.handle_frameBegin(resume)
         elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
@@ -511,7 +512,7 @@
         else:
             raise TypeError, "argument pt should be string or integer or list"
 
-    def handle_nextFrame(self,fx):
+    def handle_nextFrame(self,fx,resume=0):
         '''On endFrame change to the frame with name or index fx'''
         if type(fx) is StringType:
             for f in self.pageTemplate.frames:
@@ -524,18 +525,10 @@
         else:
             raise TypeError, "argument fx should be string or integer"
 
-    def handle_currentFrame(self,fx):
+    def handle_currentFrame(self,fx,resume=0):
         '''change to the frame with name or index fx'''
-        if type(fx) is StringType:
-            for f in self.pageTemplate.frames:
-                if f.id == fx:
-                    self._nextFrameIndex = self.pageTemplate.frames.index(f)
-                    return
-            raise ValueError, "can't find frame('%s')"%fx
-        elif type(fx) is IntType:
-            self._nextFrameIndex = fx
-        else:
-            raise TypeError, "argument fx should be string or integer"
+        self.handle_nextFrame(fx,resume)
+        self.handle_frameEnd(resume)
 
     def handle_breakBefore(self, flowables):
         '''preprocessing step to allow pageBreakBefore and frameBreakBefore attributes'''
--- a/reportlab/platypus/flowables.py	Tue Sep 27 21:12:08 2005 +0000
+++ b/reportlab/platypus/flowables.py	Wed Sep 28 13:22:53 2005 +0000
@@ -744,17 +744,18 @@
     return max(1./s1, 1./s2)
 
 class KeepInFrame(_Container,Flowable):
-    def __init__(self, maxWidth, maxHeight, content=[], mergeSpace=1, mode=0, name=None):
+    def __init__(self, maxWidth, maxHeight, content=[], mergeSpace=1, mode='shrink', name=None):
         '''mode describes the action to take when overflowing
-            0   raise an error in the normal way
-            1   ignore ie just draw it and report maxWidth, maxHeight
-            2   shrinkToFit
+            error       raise an error in the normal way
+            continue    ignore ie just draw it and report maxWidth, maxHeight
+            shrink      shrinkToFit
+            truncate    fit as much as possible
         '''
         self.name = name or str(id(self))
         self.maxWidth = maxWidth
         self.maxHeight = maxHeight
         self.mode = mode
-        assert mode in (0,1,2), '%s invalid mode value %s' % (self.identity(),mode)
+        assert mode in ('error','overflow','shrink','truncate'), '%s invalid mode value %s' % (self.identity(),mode)
         assert maxHeight>=0,  '%s invalid maxHeight value %s' % (self.identity(),maxHeight)
         if mergeSpace is None: mergeSpace = overlapAttachedSpace
         self.mergespace = mergeSpace
@@ -771,16 +772,14 @@
         maxWidth = float(self.maxWidth or availWidth)
         maxHeight = float(self.maxHeight or availHeight)
         W, H = _listWrapOn(self._content,availWidth,self.canv)
-        if mode==0 or (W<=maxWidth and H<=maxHeight):
+        if mode=='error' or (W<=maxWidth and H<=maxHeight):
             self.width = W  #we take what we get
             self.height = H
-        elif mode==1:   #we lie
+        elif mode in ('overflow','truncate'):   #we lie
             self.width = min(maxWidth,W)-_FUZZ
             self.height = min(maxHeight,H)-_FUZZ
         else:
             def func(x):
-                for c in self._content: #hack for multi-frag paras
-                    if hasattr(c,'blPara'): del c.blPara
                 W, H = _listWrapOn(self._content,x*availWidth,self.canv)
                 W /= x
                 H /= x
@@ -823,11 +822,17 @@
 
     def drawOn(self, canv, x, y, _sW=0):
         scale = getattr(self,'_scale',1.0)
-        if scale!=1.0:
+        truncate = self.mode=='truncate'
+        ss = scale!=1.0 or truncate
+        if ss:
             canv.saveState()
-            canv.translate(x,y)
-            x=y=0
-            canv.scale(1.0/scale, 1.0/scale)
+            if truncate:
+                p = canv.beginPath()
+                p.rect(x, y, self.width,self.height)
+                canv.clipPath(p,stroke=0)
+            else:
+                canv.translate(x,y)
+                x=y=0
+                canv.scale(1.0/scale, 1.0/scale)
         _Container.drawOn(self, canv, x, y, _sW=_sW, scale=scale)
-        if scale!=1.0:
-            canv.restoreState()
+        if ss: canv.restoreState()
--- a/reportlab/platypus/paragraph.py	Tue Sep 27 21:12:08 2005 +0000
+++ b/reportlab/platypus/paragraph.py	Wed Sep 28 13:22:53 2005 +0000
@@ -476,6 +476,7 @@
         #this is a major hack
         P1.blPara = ParaLines(kind=1,lines=blPara.lines[0:s],aH=availHeight,aW=availWidth)
         P1._JustifyLast = 1
+        P1._splitpara = 1
         if style.firstLineIndent != 0:
             style = deepcopy(style)
             style.firstLineIndent = 0
@@ -569,7 +570,7 @@
             return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
                             textColor=style.textColor, lines=[])
         else:
-            if hasattr(self,'blPara'):
+            if hasattr(self,'blPara') and getattr(self,'_splitpara',0):
                 #NB this is an utter hack that awaits the proper information
                 #preserving splitting algorithm
                 return self.blPara