1 """Document layout engine."""
4 """The document layout engine."""
6 def __init__(self,rect=None):
7 """initialize the object with the size of the box."""
12 """Add a document element to the layout.
14 The document element may be
15 * a tuple (w,h) if it is a whitespace element
16 * a tuple (0,h) if it is a linebreak element
17 * an integer -1,0,1 if it is a command to start a new block of elements
18 that are aligned either left,center, or right.
19 * an object with a .rect (for size) -- such as a word element
20 * an object with a .rect (for size) and .align -- such as an image element
24 self._widgets.append(e)
30 This method recalculates the position of all document elements after
31 they have been added to the document. .rect.x,y will be updated for
37 for e in self._widgets:
38 if type(e) is tuple and e[0] != 0:
40 elif type(e) is tuple and e[0] == 0:
43 self.do_block(align=e)
44 elif hasattr(e,'align'):
49 self.rect.h = max(self.y,self.left_bottom,self.right_bottom)
52 self.x,self.y = self.rect.x,self.rect.y
53 self.left = self.rect.left
54 self.right = self.rect.right
65 if self.y > self.left_bottom:
66 self.left = self.rect.left
70 if self.y > self.right_bottom:
71 self.right = self.rect.right
78 def do_block(self,align=-1):
84 ox,oy,oh = self.x,self.y,self.h
85 w,h = e.rect.w,e.rect.h
89 self.x = self.rect.left + (self.rect.width-w)/2
93 self.y = max(self.left_bottom,self.y + self.h)
95 self.x = self.rect.left
98 self.y = max(self.right_bottom,self.y + self.h)
100 self.x = self.rect.left + (self.rect.width-w)
102 e.rect.x,e.rect.y = self.x,self.y
108 self.h = max(self.h,h)
109 self.y = self.y + self.h
110 self.x = self.getleft()
114 self.left_bottom = self.y + h
115 self.x,self.y,self.h = ox + w,oy,oh
117 self.right = self.x - w
118 self.right_bottom = self.y + h
119 self.x,self.y,self.h = ox,oy,oh
121 self.widgets.append(e)
123 def do_space(self,e):
125 if self.x+w >= self.getright():
129 self.h = max(self.h,h)
133 w,h = e.rect.w,e.rect.h
134 if self.x+w >= self.getright():
137 self.h = max(self.h,h)
146 if len(self.items) != 0 and type(self.items[-1]) == tuple:
151 if type(e) == tuple: w += e[0]
154 if align == -1: x = x1
156 x = x1 + ((x2-x1)-w)/2
158 elif align == 1: x = x2 - w
161 if type(e) == tuple: x += e[0]
163 e.rect.x,e.rect.y = x,y
164 self.widgets.append(e)
168 self.y = self.y + self.h
169 self.x = self.getleft()