The input for the laying out stage is a hierarchical data structure which is essentially a lossless representation of the source code. The purpose of this stage is to lay out all the items from the input as graphics elements on a canvas for further drawing. This is done with a help of a data structure which I called a virtual canvas. A virtual canvas is a two dimensional array which starts as an empty one and which can grow right and down as needed. Each cell in a virtual canvas contains an instance of a class which in most of the cases represents a graphics primitive we have discussed earlier or another virtual canvas. Nested virtual canvases are used for example for all the elements that use the scope idea.
Lets see how it works for the example above. The virtual canvas is created as an empty one and we start with the input. The most outer container of the input is a file so a rounded rectangle should be drawn. The virtual canvas is extended with one row and one column in it. The cell is set to the upper left corner of the file scope rectangle. This cell is enough to draw the whole rounded rectangle later on so there is no need to allocate any cells for the top of the scope rectangle. The next item is the file header. Therefore the canvas is extended with one more row. The first allocated column in the row is for the left vertical edge of the file scope rectangle. The second column is set to the header of the file scope i.e. a text for the encoding and the hash bang line. We do not have a file docstring in the example so there is basically nothing else to be done for the file scope header.
The next item in the input is a file scope suite. The first element in the suite container is a code block. So the canvas is extended with one more row. The first column in the row is set as the file scope left edge. The next column is allocated for the code block. The code block in the example has a side comment so another cell is allocated in the row and its content is set as the corresponding side comment.
There is no need to allocate cells neither for the bottom nor for the right edges of the file scope because the scope rectangle will be drawn when the top left corner is found.