def pagesize(document: vp.Document, size, landscape) -> vp.Document: """Change the current page size. The page size is set (or modified) by the `read` command and used by the `write` command by default. This command can be used to set it to an arbitrary value. See the `write` command help section for more information on valid size value (`vpype write --help`). Note: this command only changes the current page size and has no effect on the geometries. Use the `translate` and `scale` commands to change the position and/or the scale of the geometries. Examples: Set the page size to A4: vpype [...] pagesize a4 [...] Set the page size to landscape A4: vpype [...] pagesize --landscape a4 [...] Set the page size to 11x15in: vpype [...] pagesize 11inx15in [...] """ document.page_size = _normalize_page_size(size, landscape) return document
def test_document_empty_copy(): doc = Document() doc.add(LineCollection([(0, 1)]), 1) doc.page_size = 3, 4 new_doc = doc.empty_copy() assert len(new_doc.layers) == 0 assert new_doc.page_size == (3, 4)
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
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