01fe0cb7addac812abfb35b629e6d2f02302e6c0
1 """document layout engine."""
3 """the document layout engine
5 .widgets -- elements are kept in this list. read-only, use add to add items to it.
8 def __init__(self
,rect
=None):
9 """initialize the object with the size of the box."""
14 """add a document element to the layout.
16 a document element may be
17 - a tuple (w,h) if it is a whitespace element
18 - a tuple (0,h) if it is a linebreak element
19 - an integer -1,0,1 if it is a command to start a new block of elements that are aligned either left,center, or right.
20 - an object with a .rect (for size) -- such as a word element
21 - an object with a .rect (for size) and .align -- such as an image element
24 self
._widgets
.append(e
)
29 this method recalculates the position of all document elements
30 after they have been added to the document. .rect.x,y will be updated for all
35 for e
in self
._widgets
:
36 if type(e
) is tuple and e
[0] != 0:
38 elif type(e
) is tuple and e
[0] == 0:
41 self
.do_block(align
=e
)
42 elif hasattr(e
,'align'):
47 self
.rect
.h
= max(self
.y
,self
.left_bottom
,self
.right_bottom
)
50 self
.x
,self
.y
= self
.rect
.x
,self
.rect
.y
51 self
.left
= self
.rect
.left
52 self
.right
= self
.rect
.right
63 if self
.y
> self
.left_bottom
:
64 self
.left
= self
.rect
.left
68 if self
.y
> self
.right_bottom
:
69 self
.right
= self
.rect
.right
76 def do_block(self
,align
=-1):
82 ox
,oy
,oh
= self
.x
,self
.y
,self
.h
83 w
,h
= e
.rect
.w
,e
.rect
.h
87 self
.x
= self
.rect
.left
+ (self
.rect
.width
-w
)/2
91 self
.y
= max(self
.left_bottom
,self
.y
+ self
.h
)
93 self
.x
= self
.rect
.left
96 self
.y
= max(self
.right_bottom
,self
.y
+ self
.h
)
98 self
.x
= self
.rect
.left
+ (self
.rect
.width
-w
)
100 e
.rect
.x
,e
.rect
.y
= self
.x
,self
.y
106 self
.h
= max(self
.h
,h
)
107 self
.y
= self
.y
+ self
.h
108 self
.x
= self
.getleft()
112 self
.left_bottom
= self
.y
+ h
113 self
.x
,self
.y
,self
.h
= ox
+ w
,oy
,oh
115 self
.right
= self
.x
- w
116 self
.right_bottom
= self
.y
+ h
117 self
.x
,self
.y
,self
.h
= ox
,oy
,oh
119 self
.widgets
.append(e
)
121 def do_space(self
,e
):
123 if self
.x
+w
>= self
.getright():
127 self
.h
= max(self
.h
,h
)
131 w
,h
= e
.rect
.w
,e
.rect
.h
132 if self
.x
+w
>= self
.getright():
135 self
.h
= max(self
.h
,h
)
144 if len(self
.items
) != 0 and type(self
.items
[-1]) == tuple:
149 if type(e
) == tuple: w
+= e
[0]
152 if align
== -1: x
= x1
154 x
= x1
+ ((x2
-x1
)-w
)/2
156 elif align
== 1: x
= x2
- w
159 if type(e
) == tuple: x
+= e
[0]
161 e
.rect
.x
,e
.rect
.y
= x
,y
162 self
.widgets
.append(e
)
166 self
.y
= self
.y
+ self
.h
167 self
.x
= self
.getleft()
172 # vim: set filetype=python sts=4 sw=4 noet si :