Skip to content

Commit

Permalink
Added '--orientation' option to pagerotate command, to allow conditio…
Browse files Browse the repository at this point in the history
…nally rotating a page to a target orientation.
  • Loading branch information
gatesphere committed Mar 6, 2024
1 parent dfb37d4 commit 5e192b8
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions vpype_cli/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,21 @@ def layout(
is_flag=True,
help="Rotate clockwise instead of the default counter-clockwise",
)
@click.option(
"--orientation",
"-o",
type=click.Choice(["any", "portrait", "landscape"]),
default="any",
help="Conditionally rotate only if the final orientation matches this option",
)
@global_processor
def pagerotate(document: vp.Document, clockwise: bool):
def pagerotate(document: vp.Document, clockwise: bool, orientation: str):
"""Rotate the page by 90 degrees.
This command rotates the page by 90 degrees counter-clockwise. If the `--clockwise` option
is passed, it rotates the page clockwise instead.
is passed, it rotates the page clockwise instead. If the `--orientation` option is given
a value of either 'portrait' or 'landscape', the page will only be rotated if the final
orientation would match that choice.
Note: if the page size is not defined, an error is printed and the page is not rotated.
"""
Expand All @@ -677,6 +686,11 @@ def pagerotate(document: vp.Document, clockwise: bool):
return document
w, h = page_size

# check orientation constraint, and do nothing if target orientation
# won't match desired result
if (orientation == "portrait" and h > w) or (orientation == "landscape" and w > h):
return document

if clockwise:
document.rotate(math.pi / 2)
document.translate(h, 0)
Expand Down

0 comments on commit 5e192b8

Please sign in to comment.