Renders the given layout manager on a page of the given context.

    Assumes the given context has not yet been reversed in the y-direction
    (i.e. it is still the default for Cairo, where y increases up
    from the bottom of the page). This method performs the reversal and
    resets it before it returns.

def render_to_cairo_context(cairo_context, papersize_tuple, layout):
    """Renders the given layout manager on a page of the given context.

    Assumes the given context has not yet been reversed in the y-direction
    (i.e. it is still the default for Cairo, where y increases up
    from the bottom of the page). This method performs the reversal and
    resets it before it returns.
    """
    try:
        cairo_context.save()
        cairo_context.translate(0, papersize_tuple[1])
        cairo_context.scale(1, -1)
        layout.render(
            Rectangle(0, 0, *papersize_tuple),
            dict(output=CairoOutput(cairo_context))
            )
    finally:
        cairo_context.restore()