XPreformatted first fixes; now runs
authorrgbecker
Thu, 24 Aug 2000 11:33:31 +0000
changeset 442 e3eac15cddbd
parent 441 4b7abf4f9565
child 443 0b0fff7823f0
XPreformatted first fixes; now runs
reportlab/platypus/__init__.py
reportlab/platypus/flowables.py
reportlab/platypus/xpreformatted.py
--- a/reportlab/platypus/__init__.py	Thu Aug 24 11:30:29 2000 +0000
+++ b/reportlab/platypus/__init__.py	Thu Aug 24 11:33:31 2000 +0000
@@ -31,9 +31,12 @@
 #
 ###############################################################################
 #	$Log: __init__.py,v $
+#	Revision 1.10  2000/08/24 11:33:31  rgbecker
+#	XPreformatted first fixes; now runs
+#
 #	Revision 1.9  2000/07/13 11:41:00  rgbecker
 #	Added KeepTogether
-#
+#	
 #	Revision 1.8  2000/06/27 10:07:55  rgbecker
 #	Added CondPageBreak
 #	
@@ -55,7 +58,7 @@
 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
 #	Added license, __version__ and Logi comment
 #	
-__version__=''' $Id: __init__.py,v 1.9 2000/07/13 11:41:00 rgbecker Exp $ '''
+__version__=''' $Id: __init__.py,v 1.10 2000/08/24 11:33:31 rgbecker Exp $ '''
 __doc__=''
 from reportlab.platypus.flowables import Flowable, Image, Macro, PageBreak, Preformatted, Spacer, XBox, \
 						CondPageBreak, KeepTogether
@@ -65,3 +68,4 @@
 from reportlab.platypus.frames import Frame
 from reportlab.platypus.doctemplate import BaseDocTemplate, NextPageTemplate, PageTemplate, ActionFlowable, \
 						SimpleDocTemplate, FrameBreak
+from xpreformatted import XPreformatted
--- a/reportlab/platypus/flowables.py	Thu Aug 24 11:30:29 2000 +0000
+++ b/reportlab/platypus/flowables.py	Thu Aug 24 11:33:31 2000 +0000
@@ -31,9 +31,12 @@
 #
 ###############################################################################
 #	$Log: flowables.py,v $
+#	Revision 1.8  2000/08/24 11:33:31  rgbecker
+#	XPreformatted first fixes; now runs
+#
 #	Revision 1.7  2000/08/03 14:12:53  rgbecker
 #	Changing to packer led positioning
-#
+#	
 #	Revision 1.6  2000/07/13 11:42:10  rgbecker
 #	removed debug prints
 #	
@@ -53,7 +56,7 @@
 #	Platypus re-organisation
 #	
 #	
-__version__=''' $Id: flowables.py,v 1.7 2000/08/03 14:12:53 rgbecker Exp $ '''
+__version__=''' $Id: flowables.py,v 1.8 2000/08/24 11:33:31 rgbecker Exp $ '''
 __doc__="""
 A flowable is a "floating element" in a document whose exact position is determined by the
 other elements that precede it, such as a paragraph, a diagram interspersed between paragraphs,
@@ -173,6 +176,25 @@
 		self.canv.setFont('Times-Roman',12)
 		self.canv.drawCentredString(0.5*self.width, 0.5*self.height, self.text)
 
+def _dedenter(text,dedent=0):
+	'''
+	tidy up text - carefully, it is probably code.  If people want to
+	indent code within a source script, you can supply an arg to dedent
+	and it will chop off that many character, otherwise it leaves
+	left edge intact.
+	'''
+	templines = string.split(text, '\n')
+	lines = []
+	for line in templines:
+		line = string.rstrip(line[dedent:])
+		lines.append(line)
+	#don't want the first or last to be empty
+	while string.strip(lines[0]) == '':
+		lines = lines[1:]
+	while string.strip(lines[-1]) == '':
+		lines = lines[:-1]
+	return lines
+
 class Preformatted(Flowable):
 	"""This is like the HTML <PRE> tag.  
 	It attempts to display text exactly as you typed it in a fixed width "typewriter" font.
@@ -185,22 +207,7 @@
 		"""
 		self.style = style
 		self.bulletText = bulletText
-
-		#tidy up text - carefully, it is probably code.  If people want to
-		#indent code within a source script, you can supply an arg to dedent
-		#and it will chop off that many character, otherwise it leaves
-		#left edge intact.
-
-		templines = string.split(text, '\n')
-		self.lines = []
-		for line in templines:
-			line = string.rstrip(line[dedent:])
-			self.lines.append(line)
-		#don't want the first or last to be empty
-		while string.strip(self.lines[0]) == '':
-			self.lines = self.lines[1:]
-		while string.strip(self.lines[-1]) == '':
-			self.lines = self.lines[:-1]
+		self.lines = _dedenter(text,dedent)
 
 	def wrap(self, availWidth, availHeight):
 		self.width = availWidth
--- a/reportlab/platypus/xpreformatted.py	Thu Aug 24 11:30:29 2000 +0000
+++ b/reportlab/platypus/xpreformatted.py	Thu Aug 24 11:33:31 2000 +0000
@@ -2,6 +2,7 @@
 from types import StringType, ListType
 from paragraph import Paragraph, cleanBlockQuotedText, _handleBulletWidth, ParaFrag, _getFragWords, \
 	stringWidth, _sameFrag
+from flowables import _dedenter
 
 def _getFragLines(frags):
 	lines = []
@@ -25,12 +26,10 @@
 	if cline!=[]: lines.append(cline)
 	return lines
 
-def _cleanBlockQuotedTextRetainLines(text):
-	return cleanBlockQuotedText(text,'\n')
-
 class XPreformatted(Paragraph):
-	def __init__(self, text, style, bulletText = None, frags=None):
-		self._setup(text, style, bulletText, frags, _cleanBlockQuotedTextRetainLines)
+	def __init__(self, text, style, bulletText = None, dedent=0, frags=None):
+		cleaner = lambda text, dedent=dedent: string.join(_dedenter(text,dedent),'\n')
+		self._setup(text, style, bulletText, frags, cleaner)
 
 	def breakLines(self, width):
 		"""
@@ -82,14 +81,13 @@
 			fontName = f.fontName
 			if hasattr(f,'text'):
 				L=string.split(f.text, '\n')
-				lineno = 0
 				for l in L:
 					currentWidth = stringWidth(l,fontName,fontSize)
 					requiredWidth = max(currentWidth,requiredWidth)
 					extraSpace = maxWidth-currentWidth
 					lines.append((extraSpace,string.split(l,' ')))
 					lineno = lineno+1
-					maxWidth = lineno<len(maxWidths) and maxWidths[lineno] or maxWidth[-1]
+					maxWidth = lineno<len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
 			else:
 				lines=f.lines
 
@@ -133,6 +131,8 @@
 
 					currentWidth = currentWidth + spaceWidth + wordWidth
 
+				lineno = lineno+1
+				maxWidth = lineno<len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
 				requiredWidth = max(currentWidth,requiredWidth)
 				extraSpace = maxWidth - currentWidth
 				lines.append(ParaFrag(extraSpace=extraSpace,wordCount=n, words=words, fontSize=maxSize))