def draw_stacking_context(stream, stacking_context):
    """Draw a ``stacking_context`` on ``stream``."""
    # See https://www.w3.org/TR/CSS2/zindex.html.
    with stacked(stream):
        box = stacking_context.box

        stream.begin_marked_content(box, mcid=True)

        # Apply the viewport_overflow to the html box, see #35.
        if box.is_for_root_element and (
                stacking_context.page.style['overflow'] != 'visible'):
            rounded_box(stream, stacking_context.page.rounded_padding_box())
            stream.clip()
            stream.end()

        if box.is_absolutely_positioned() and box.style['clip']:
            top, right, bottom, left = box.style['clip']
            if top == 'auto':
                top = 0
            if right == 'auto':
                right = 0
            if bottom == 'auto':
                bottom = box.border_height()
            if left == 'auto':
                left = box.border_width()
            stream.rectangle(
                box.border_box_x() + right, box.border_box_y() + top,
                left - right, bottom - top)
            stream.clip()
            stream.end()

        if box.style['opacity'] < 1:
            original_stream = stream
            stream = stream.add_group(*stream.page_rectangle)

        if box.transformation_matrix:
            if box.transformation_matrix.determinant:
                stream.transform(*box.transformation_matrix.values)
            else:
                stream.end_marked_content()
                return

        # Point 1 is done in draw_page.

        # Point 2.
        if isinstance(box, (boxes.BlockBox, boxes.MarginBox,
                            boxes.InlineBlockBox, boxes.TableCellBox,
                            boxes.FlexContainerBox, boxes.ReplacedBox)):
            set_mask_border(stream, box)
            # The canvas background was removed by layout_backgrounds
            draw_background(stream, box.background)
            draw_border(stream, box)

        with stacked(stream):
            # Dont clip the page box, see #35.
            clip = (
                box.style['overflow'] != 'visible' and
                not isinstance(box, boxes.PageBox))
            if clip:
                # Only clip the content and the children:
                # - the background is already clipped,
                # - the border must *not* be clipped.
                rounded_box(stream, box.rounded_padding_box())
                stream.clip()
                stream.end()

            # Point 3.
            for child_context in stacking_context.negative_z_contexts:
                draw_stacking_context(stream, child_context)

            # Point 4.
            for block in stacking_context.block_level_boxes:
                set_mask_border(stream, block)

                if isinstance(block, boxes.TableBox):
                    draw_table(stream, block)
                else:
                    draw_background(stream, block.background)
                    draw_border(stream, block)

            # Point 5.
            for child_context in stacking_context.float_contexts:
                draw_stacking_context(stream, child_context)

            # Point 6.
            if isinstance(box, boxes.InlineBox):
                draw_inline_level(stream, stacking_context.page, box)

            # Point 7.
            for block in (box, *stacking_context.blocks_and_cells):
                if isinstance(block, boxes.ReplacedBox):
                    draw_replacedbox(stream, block)
                elif block.children:
                    if block != box:
                        stream.begin_marked_content(block, mcid=True)
                    if isinstance(block.children[-1], boxes.LineBox):
                        for child in block.children:
                            draw_inline_level(stream, stacking_context.page, child)
                    if block != box:
                        stream.end_marked_content()

            # Point 8.
            for child_context in stacking_context.zero_z_contexts:
                draw_stacking_context(stream, child_context)

            # Point 9.
            for child_context in stacking_context.positive_z_contexts:
                draw_stacking_context(stream, child_context)

        # Point 10.
        draw_outline(stream, box)

        if box.style['opacity'] < 1:
            group_id = stream.id
            stream = original_stream
            with stacked(stream):
                stream.set_alpha(box.style['opacity'], stroke=True, fill=True)
                stream.draw_x_object(group_id)

        stream.end_marked_content()
Draw a ``stacking_context`` on ``stream``.
weasyprint/draw/__init__.py