def draw_border(stream, box):
    """Draw the box borders and column rules to a ``pdf.stream.Stream``."""

    # The box is hidden, easy.
    if box.style['visibility'] != 'visible':
        return

    # Draw column borders.
    columns = (
        isinstance(box, boxes.BlockContainerBox) and (
            box.style['column_width'] != 'auto' or
            box.style['column_count'] != 'auto'))
    if columns and box.style['column_rule_width']:
        border_widths = (0, 0, 0, box.style['column_rule_width'])
        skip_next = True
        for child in box.children:
            if child.style['column_span'] == 'all':
                skip_next = True
                continue
            elif skip_next:
                skip_next = False
                continue
            with stacked(stream):
                rule_width = box.style['column_rule_width']
                rule_style = box.style['column_rule_style']
                if box.style['column_gap'] == 'normal':
                    gap = box.style['font_size']  # normal equals 1em
                else:
                    gap = percentage(box.style['column_gap'], box.width)
                position_x = (
                    child.position_x - (box.style['column_rule_width'] + gap) / 2)
                border_box = (position_x, child.position_y, rule_width, child.height)
                clip_border_segment(
                    stream, rule_style, rule_width, 'left', border_box, border_widths)
                color = styled_color(
                    rule_style, get_color(box.style, 'column_rule_color'), 'left')
                draw_rect_border(stream, border_box, border_widths, rule_style, color)

    # If there's a border image, that takes precedence.
    if box.style['border_image_source'][0] != 'none' and box.border_image is not None:
        draw_border_image(
            box, stream, box.border_image, box.style['border_image_slice'],
            box.style['border_image_repeat'], box.style['border_image_outset'],
            box.style['border_image_width'])
        return

    widths = [getattr(box, f'border_{side}_width') for side in SIDES]

    if set(widths) == {0}:
        # No border, return early.
        return

    colors = [get_color(box.style, f'border_{side}_color') for side in SIDES]
    styles = [
        colors[i].alpha and box.style[f'border_{side}_style']
        for (i, side) in enumerate(SIDES)]

    simple_style = set(styles) in ({'solid'}, {'double'})  # one style, simple lines
    single_color = len(set(colors)) == 1  # one color
    four_sides = 0 not in widths  # no 0-width border, to avoid PDF artifacts
    if simple_style and single_color and four_sides:
        # Simple case, we only draw rounded rectangles.
        draw_rounded_border(stream, box, styles[0], colors[0])
        return

    # We're not smart enough to find a good way to draw the borders, we must
    # draw them side by side. Order is not specified, but this one seems to be
    # close to what other browsers do.
    values = tuple(zip(SIDES, widths, colors, styles))
    for index in (2, 3, 1, 0):
        side, width, color, style = values[index]
        if width == 0 or not color:
            continue
        with stacked(stream):
            clip_border_segment(
                stream, style, width, side, box.rounded_border_box()[:4],
                widths, box.rounded_border_box()[4:])
            draw_rounded_border(
                stream, box, style, styled_color(style, color, side))
Draw the box borders and column rules to a ``pdf.stream.Stream``.
weasyprint/draw/border.py