Exemple #1
0
def layout(
    document: vp.Document,
    size: Tuple[float, float],
    landscape: bool,
    margin: Optional[float],
    align: str,
    valign: str,
) -> vp.Document:
    """Layout command"""

    size = _normalize_page_size(size, landscape)

    document.page_size = size
    bounds = document.bounds()

    if bounds is None:
        # nothing to layout
        return document

    min_x, min_y, max_x, max_y = bounds
    width = max_x - min_x
    height = max_y - min_y
    if margin is not None:
        document.translate(-min_x, -min_y)
        scale = min((size[0] - 2 * margin) / width,
                    (size[1] - 2 * margin) / height)
        document.scale(scale)
        min_x = min_y = 0.0
        width *= scale
        height *= scale
    else:
        margin = 0.0

    if align == "left":
        h_offset = margin - min_x
    elif align == "right":
        h_offset = size[0] - margin - width - min_x
    else:
        h_offset = margin + (size[0] - width - 2 * margin) / 2 - min_x

    if valign == "top":
        v_offset = margin - min_y
    elif valign == "bottom":
        v_offset = size[1] - margin - height - min_y
    else:
        v_offset = margin + (size[1] - height - 2 * margin) / 2 - min_y

    document.translate(h_offset, v_offset)
    return document
Exemple #2
0
def layout(
    document: vp.Document,
    size: Tuple[float, float],
    landscape: bool,
    margin: Optional[float],
    align: str,
    valign: str,
) -> vp.Document:
    """Layout the geometries on the provided page size.

    By default, this command centers everything on the page. The horizontal and vertical
    alignment can be adjusted using the `--align`, resp. `--valign` options.

    Optionally, this command can scale the geometries to fit specified margins with the
    `--fit-to-margin` option.

    Examples:

        Fit the geometries to 3cm margins with top alignment (a generally pleasing arrangement
        for square designs on portrait-oriented pages):

            vpype read input.svg layout --fit-to-margin 3cm --valign top a4 write.svg
    """

    if landscape and size[0] < size[1]:
        size = size[::-1]

    document.page_size = size
    bounds = document.bounds()

    if bounds is None:
        # nothing to layout
        return document

    min_x, min_y, max_x, max_y = bounds
    width = max_x - min_x
    height = max_y - min_y
    if margin is not None:
        document.translate(-min_x, -min_y)
        scale = min((size[0] - 2 * margin) / width,
                    (size[1] - 2 * margin) / height)
        document.scale(scale)
        min_x = min_y = 0.0
        width *= scale
        height *= scale
    else:
        margin = 0.0

    if align == "left":
        h_offset = margin - min_x
    elif align == "right":
        h_offset = size[0] - margin - width - min_x
    else:
        h_offset = margin + (size[0] - width - 2 * margin) / 2 - min_x

    if valign == "top":
        v_offset = margin - min_y
    elif valign == "bottom":
        v_offset = size[1] - margin - height - min_y
    else:
        v_offset = margin + (size[1] - height - 2 * margin) / 2 - min_y

    document.translate(h_offset, v_offset)
    return document