reportlab/platypus/frames.py
author rgbecker
Mon, 01 Oct 2001 09:58:42 +0000
changeset 1268 158ffc339d38
parent 1231 05dc2104f72d
child 1652 35eb6c38d64e
permissions -rw-r--r--
Fixed slight buglet in checkpagesize
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 470
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 470
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 470
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/frames.py?cvsroot=reportlab
1231
05dc2104f72d disable LayoutError error for production use (option to enable for debug)
aaron_watters
parents: 1035
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/platypus/frames.py,v 1.15 2001/09/24 15:29:23 aaron_watters Exp $
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
     5
1231
05dc2104f72d disable LayoutError error for production use (option to enable for debug)
aaron_watters
parents: 1035
diff changeset
     6
__version__=''' $Id: frames.py,v 1.15 2001/09/24 15:29:23 aaron_watters Exp $ '''
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
     7
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     8
__doc__="""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     9
"""
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    10
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    11
_geomAttr=('x1', 'y1', 'width', 'height', 'leftPadding', 'bottomPadding', 'rightPadding', 'topPadding')
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    12
1002
6ce27679a1f5 added _FUZZ and changed frame add test
rgbecker
parents: 565
diff changeset
    13
_FUZZ=1e-6 # to avoid numerical difficulties we use this as an error band
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    14
1231
05dc2104f72d disable LayoutError error for production use (option to enable for debug)
aaron_watters
parents: 1035
diff changeset
    15
from reportlab import rl_config
05dc2104f72d disable LayoutError error for production use (option to enable for debug)
aaron_watters
parents: 1035
diff changeset
    16
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    17
class Frame:
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    18
	'''
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    19
	A Frame is a piece of space in a document that is filled by the
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    20
	"flowables" in the story.  For example in a book like document most
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    21
	pages have the text paragraphs in one or two frames.  For generality
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    22
	a page might have several frames (for example for 3 column text or
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    23
	for text that wraps around a graphic).
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    24
	
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    25
	After creation a Frame is not usually manipulated directly by the
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    26
	applications program -- it is used internally by the platypus modules.
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    27
	
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    28
	Here is a diagramatid abstraction for the definitional part of a Frame
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    29
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    30
                width                    x2,y2
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    31
    	+---------------------------------+
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    32
    	| l  top padding                r | h
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    33
    	| e +-------------------------+ i | e
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    34
    	| f |                         | g | i
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    35
    	| t |                         | h | g
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    36
    	|   |                         | t | h
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    37
    	| p |                         |   | t
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    38
    	| a |                         | p |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    39
    	| d |                         | a |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    40
    	|   |                         | d |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    41
    	|   +-------------------------+   |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    42
    	|    bottom padding				  |
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    43
    	+---------------------------------+
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    44
    	(x1,y1) <-- lower left corner
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    45
    	
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    46
        NOTE!! Frames are stateful objects.  No single frame should be used in
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    47
        two documents at the same time (especially in the presence of multithreading.
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    48
	'''
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    49
	def __init__(self, x1, y1, width,height, leftPadding=6, bottomPadding=6,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    50
			rightPadding=6, topPadding=6, id=None, showBoundary=0):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    51
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    52
		self.id = id
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    53
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    54
		#these say where it goes on the page
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    55
		self.__dict__['_x1'] = x1
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    56
		self.__dict__['_y1'] = y1
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    57
		self.__dict__['_width'] = width
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    58
		self.__dict__['_height'] = height
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    59
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    60
		#these create some padding.
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    61
		self.__dict__['_leftPadding'] = leftPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    62
		self.__dict__['_bottomPadding'] = bottomPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    63
		self.__dict__['_rightPadding'] = rightPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    64
		self.__dict__['_topPadding'] = topPadding
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    65
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    66
		# if we want a boundary to be shown
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    67
		self.showBoundary = showBoundary
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    68
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    69
		self._geom()
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    70
		self._reset()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    71
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    72
	def __getattr__(self,a):
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    73
		if a in _geomAttr: return self.__dict__['_'+a]
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    74
		raise AttributeError, a
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    75
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    76
	def __setattr__(self,a,v):
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    77
		if a in _geomAttr:
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    78
			self.__dict__['_'+a] = v
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    79
			self._geom()
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    80
		else:
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    81
			self.__dict__[a] = v
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    82
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    83
	def _geom(self):
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    84
		self._x2 = self._x1 + self._width
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    85
		self._y2 = self._y1 + self._height
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    86
		#efficiency
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    87
		self._y1p = self._y1 + self._bottomPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    88
		#work out the available space
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    89
		self._aW = self._x2 - self._x1 - self._leftPadding - self._rightPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    90
		self._aH = self._y2 - self._y1p - self._topPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    91
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    92
	def _reset(self):
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    93
		#drawing starts at top left
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    94
		self._x = self._x1 + self._leftPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    95
		self._y = self._y2 - self._topPadding
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    96
		self._atTop = 1
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    97
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    98
	def _add(self, flowable, canv, trySplit=0):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    99
		""" Draws the flowable at the current position.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   100
		Returns 1 if successful, 0 if it would not fit.
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   101
		Raises a LayoutError if the object is too wide,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   102
		or if it is too high for a totally empty frame,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   103
		to avoid infinite loops"""
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   104
		y = self._y
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   105
		p = self._y1p
466
9718adc58793 Fixed atTop bugs thanks to the effbot
rgbecker
parents: 426
diff changeset
   106
		s = 0
469
a83bc8619950 Fixed the atTop test
rgbecker
parents: 466
diff changeset
   107
		if not self._atTop: s = flowable.getSpaceBefore()
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   108
		h = y - p - s
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   109
		if h>0:
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 268
diff changeset
   110
			flowable.canv = canv #so they can use stringWidth etc
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   111
			w, h = flowable.wrap(self._aW, h)
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 268
diff changeset
   112
			del flowable.canv
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   113
		else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   114
			return 0
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   115
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   116
		h = h + s
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   117
		y = y - h
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   118
1002
6ce27679a1f5 added _FUZZ and changed frame add test
rgbecker
parents: 565
diff changeset
   119
		if y < p-_FUZZ:
1035
b877144b341f disabled width test, user will have to notice problem by viewing it in PDF
aaron_watters
parents: 1002
diff changeset
   120
			# width test disabled!  Just allow it to overflow and let the user
b877144b341f disabled width test, user will have to notice problem by viewing it in PDF
aaron_watters
parents: 1002
diff changeset
   121
			# fix the problem (XXXX MAYBE CHANGE THIS TO OPTIONAL WARNING/ERROR)
b877144b341f disabled width test, user will have to notice problem by viewing it in PDF
aaron_watters
parents: 1002
diff changeset
   122
			#if ((h > self._aH and not trySplit) or w > self._aW):
1231
05dc2104f72d disable LayoutError error for production use (option to enable for debug)
aaron_watters
parents: 1035
diff changeset
   123
			if (not rl_config.allowTableBoundsErrors) and (h > self._aH and not trySplit):
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 494
diff changeset
   124
				raise "LayoutError", "Flowable %s (%sx%s points) too large for frame (%sx%s points)." % (
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 494
diff changeset
   125
					flowable.__class__, w,h, self._aW,self._aH)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   126
			return 0
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   127
		else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   128
			#now we can draw it, and update the current point.
426
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 410
diff changeset
   129
			flowable.drawOn(canv, self._x, y, _sW=self._aW-w)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   130
			y = y - flowable.getSpaceAfter()
466
9718adc58793 Fixed atTop bugs thanks to the effbot
rgbecker
parents: 426
diff changeset
   131
			if y<>self._y: self._atTop = 0
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   132
			self._y = y
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   133
			return 1
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   134
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   135
	add = _add
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   136
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 268
diff changeset
   137
	def split(self,flowable,canv):
332
6e57f2c4ae97 Cosmetics
rgbecker
parents: 325
diff changeset
   138
		'''Ask the flowable to split using up the available space.'''
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   139
		y = self._y
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   140
		p = self._y1p
466
9718adc58793 Fixed atTop bugs thanks to the effbot
rgbecker
parents: 426
diff changeset
   141
		s = 0
470
16ef901ac455 Fixed the atTop test again
rgbecker
parents: 469
diff changeset
   142
		if not self._atTop: s = flowable.getSpaceBefore()
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 268
diff changeset
   143
		flowable.canv = canv	#some flowables might need this
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   144
		r = flowable.split(self._aW, y-p-s)
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 268
diff changeset
   145
		del flowable.canv
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 268
diff changeset
   146
		return r
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   147
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   148
	def drawBoundary(self,canv):
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   149
		"draw the frame boundary as a rectangle (primarily for debugging)."
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   150
		canv.rect(
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   151
				self._x1,
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   152
				self._y1,
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   153
				self._x2 - self._x1,
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   154
				self._y2 - self._y1
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   155
				)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   156
		
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   157
	def addFromList(self, drawlist, canv):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   158
		"""Consumes objects from the front of the list until the
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   159
		frame is full.	If it cannot fit one object, raises
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   160
		an exception."""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   161
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   162
		if self.showBoundary:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   163
			self.drawBoundary(canv)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   164
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   165
		while len(drawlist) > 0:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   166
			head = drawlist[0]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   167
			if self.add(head,canv,trySplit=0):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   168
				del drawlist[0]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   169
			else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   170
				#leave it in the list for later
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   171
				break