reportlab/platypus/frames.py
author rgbecker
Thu, 01 Jun 2000 15:23:06 +0000
changeset 253 cfcf8d555a2c
child 268 8414113fa500
permissions -rw-r--r--
Platypus re-organisation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     1
###############################################################################
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     2
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     3
#	ReportLab Public License Version 1.0
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     4
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     5
#	Except for the change of names the spirit and intention of this
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     6
#	license is the same as that of Python
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     7
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     9
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    10
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    11
# All Rights Reserved
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    12
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    16
# copyright notice and this permission notice appear in supporting
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    17
# documentation, and that the name of ReportLab not be used
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    19
# without specific, written prior permission.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    20
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    21
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    22
# Disclaimer
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    23
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    31
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    32
###############################################################################
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    33
#	$Log: frames.py,v $
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    34
#	Revision 1.1  2000/06/01 15:23:06  rgbecker
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    35
#	Platypus re-organisation
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    36
#
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    37
__version__=''' $Id: frames.py,v 1.1 2000/06/01 15:23:06 rgbecker Exp $ '''
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    38
__doc__="""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    39
"""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    40
class Frame:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    41
	'''Abstraction for the definitional part of a Frame
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    42
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    43
                width                    x2,y2
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    44
    	+---------------------------------+
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    45
    	| l  top padding                r | h
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    46
    	| e +-------------------------+ i | e
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    47
    	| f |                         | g | i
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    48
    	| t |                         | h | g
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    49
    	|   |                         | t | h
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    50
    	| p |                         |   | t
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    51
    	| a |                         | p |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    52
    	| d |                         | a |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    53
    	|   |                         | d |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    54
    	|   +-------------------------+   |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    55
    	|    bottom padding				  |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    56
    	+---------------------------------+
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    57
    	(x1,y1)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    58
	'''
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    59
	def __init__(self, x1, y1, width,height, leftPadding=6, bottomPadding=6,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    60
			rightPadding=6, topPadding=6, id=None, showBoundary=0):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    61
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    62
		self.id = id
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    63
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    64
		#these say where it goes on the page
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    65
		self.x1 = x1
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    66
		self.y1 = y1
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    67
		self.x2 = x1 + width
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    68
		self.y2 = y1 + height
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    69
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    70
		#these create some padding.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    71
		self.leftPadding = leftPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    72
		self.bottomPadding = bottomPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    73
		self.rightPadding = rightPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    74
		self.topPadding = topPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    75
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    76
		#efficiency
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    77
		self.y1p = self.y1 + bottomPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    78
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    79
		# if we want a boundary to be shown
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    80
		self.showBoundary = showBoundary
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    81
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    82
		self._reset()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    83
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    84
	def	_reset(self):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    85
		#work out the available space
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    86
		self.width = self.x2 - self.x1 - self.leftPadding - self.rightPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    87
		self.height = self.y2 - self.y1 - self.topPadding - self.bottomPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    88
		#drawing starts at top left
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    89
		self.x = self.x1 + self.leftPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    90
		self.y = self.y2 - self.topPadding
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    91
		self.atTop = 1
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    92
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    93
	def _add(self, flowable, canv, trySplit=0):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    94
		""" Draws the flowable at the current position.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    95
		Returns 1 if successful, 0 if it would not fit.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    96
		Raises a LayoutError if the object is too wide,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    97
		or if it is too high for a totally empty frame,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    98
		to avoid infinite loops"""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    99
		y = self.y
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   100
		p = self.y1p
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   101
		s = self.atTop and 0 or flowable.getSpaceBefore()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   102
		h = y - p - s
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   103
		if h>0:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   104
			w, h = flowable.wrap(self.width, h)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   105
		else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   106
			return 0
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   107
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   108
		h = h + s
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   109
		y = y - h
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   110
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   111
		if y < p:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   112
			if ((h > self.height and not trySplit) or w > self.width):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   113
				raise "LayoutError", "Flowable (%dx%d points) too large for frame (%dx%d points)." % (w,h, self.width,self.height)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   114
			return 0
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   115
		else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   116
			#now we can draw it, and update the current point.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   117
			flowable.drawOn(canv, self.x, y)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   118
			y = y - flowable.getSpaceAfter()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   119
			self.atTop = 0
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   120
			self.y = y
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   121
			return 1
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   122
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   123
	add = _add
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   124
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   125
	def split(self,flowable):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   126
		'''calls split on the flowable'''
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   127
		y = self.y
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   128
		p = self.y1p
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   129
		s = self.atTop and 0 or flowable.getSpaceBefore()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   130
		return flowable.split(self.width, y-p-s)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   131
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   132
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   133
	def drawBoundary(self,canv):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   134
		canv.rect(
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   135
				self.x1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   136
				self.y1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   137
				self.x2 - self.x1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   138
				self.y2 - self.y1
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   139
				)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   140
		
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   141
	def addFromList(self, drawlist, canv):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   142
		"""Consumes objects from the front of the list until the
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   143
		frame is full.	If it cannot fit one object, raises
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   144
		an exception."""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   145
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   146
		if self.showBoundary:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   147
			self.drawBoundary(canv)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   148
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   149
		while len(drawlist) > 0:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   150
			head = drawlist[0]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   151
			if self.add(head,canv,trySplit=0):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   152
				del drawlist[0]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   153
			else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   154
				#leave it in the list for later
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   155
				break