Text is a sequence of characters, but a page is two-dimensional
How can we put the right things in the right places?
Create a simple version of a [%g layout_engine “layout engine” %] for a browser
But the same ideas apply to print
Teletypes started printing in the upper left corner of the page
So the coordinate systems for screens put (0, 0) in the upper left instead of the lower left
Y increases going down
At least X increases to the right as usual
[% figure slug=“layout-coordinate-system” img=“coordinate_system.svg” alt=“Coordinate system” caption=“Coordinate system with (0, 0) in the upper left corner.” %]
Every cell is a rectangular block
Row arranges sub-blocks horizontally
Column arranges sub-blocks vertically
[% figure slug=“layout-sizing” img=“sizing.svg” alt=“Calculating sizes of fixed blocks” caption=“Calculating sizes of blocks with fixed width and height.” %]
[%inc easy_mode.py mark=block %]
Width: sum of widths of children
Height: height of tallest child
[%inc easy_mode.py mark=row %]
Width: width of widest child
Height: sum of heights of children
[%inc easy_mode.py mark=col %]
Rows can contain blocks and columns but must be contained in a single column (unless it’s the root)
Columns can contain blocks or rows but must be contained in a single row (unless it’s the root)
Can therefore represent document as a tree
[%inc test_easy_mode.py mark=example %]
Once we know sizes we can calculate positions
E.g., if cell is a row at (x0, y0):
Its lower edge is y1 = y0 + height
Its first child’s upper-left corner is
(x0, y1)
Second child’s upper-left corner is
(x0 + width0, y1)
[% figure slug=“layout-layout” img=“layout.svg” alt=“Laying out rows and columns” caption=“Laying out rows and columns of fixed-size blocks.” %]
[%inc placed.py mark=rowplace %]
Draw parents before children so that children over-draw
A simple form of [%g z_buffering “z-buffering” %]
[% figure slug=“layout-draw-over” img=“draw_over.svg” alt=“Children drawing over their parents” caption=“Render blocks by drawing child nodes on top of parent nodes.” %]
[%inc render.py mark=make_screen %]
[%inc rendered.py mark=render %]
[% figure slug=“layout-mixin” img=“mixin.svg” alt=“Adding methods with a mixin class” caption=“Using multiple inheritance and a mixin class to add methods.” %]
Fix width of row (for example)
If total width of children is greater than this, need to wrap the children to a new row
Handle this by modifying the tree
[%inc wrapped.py mark=blockcol %]
[% figure slug=“layout-wrap” img=“wrap.svg” alt=“Wrapping rows” caption=“Wrapping rows by introducing a new row and column.” %]
New row class takes a fixed width and some children
Returns that fixed width when asked for its size
[%inc wrapped.py mark=row %]
Wrapping puts the row’s children into buckets
Converts each bucket to a row with a column of rows
[%inc wrapped.py mark=wrap %]
[%inc wrapped.py mark=bucket %]
[% figure slug=“layout-concept-map” img=“concept_map.svg” alt=“Concept map for page layout” caption=“Concept map.” %]