Skip to content

Commit

Permalink
Stitch pages with scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
angsch committed Apr 4, 2021
1 parent 00d288b commit 06e0f51
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pdfarranger/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _lshift(lrotate, lsize):
}
return shift[lrotate]

def _create_tmp_cropped_page(tmpdir, page, angle, crop):
def _create_tmp_page(tmpdir, page, angle, crop, scale):
rotation_matrices = {0 : [1, 0, 0, 1], # 0 degrees
1 : [0, -1, 1, 0], # 270 degrees
2 : [-1, 0, 0, -1], # 180 degrees
Expand Down Expand Up @@ -129,7 +129,7 @@ def _create_tmp_cropped_page(tmpdir, page, angle, crop):
# Shrink the mediabox.
new_page = pikepdf.Dictionary(
Type=pikepdf.Name.Page,
MediaBox=[0, 0, w * (1 - crop[0] - crop[1]), h * (1 - crop[2] - crop[3])],
MediaBox=[0, 0, scale * w * (1 - crop[0] - crop[1]), scale * h * (1 - crop[2] - crop[3])],
Resources=pikepdf.Dictionary(XObject=content_dict),
Contents=pikepdf.Stream(f, content_txt.encode())
)
Expand All @@ -139,7 +139,7 @@ def _create_tmp_cropped_page(tmpdir, page, angle, crop):
for operands, operator in pikepdf.parse_content_stream(new_page):
commands.append([operands, operator])
original = pikepdf.PdfMatrix(commands[1][0])
new_matrix = original.translated(-crop[0] * w - float(mb[0]), -crop[3] * h - float(mb[1]))
new_matrix = original.translated(-crop[0] * w - float(mb[0]), -crop[3] * h - float(mb[1])).scaled(scale, scale)
commands[1][0] = pikepdf.Array([*new_matrix.shorthand])
new_content_stream = pikepdf.unparse_content_stream(commands)
new_page.Contents = f.make_stream(new_content_stream)
Expand Down Expand Up @@ -172,8 +172,8 @@ def create_stitched_page(tmpdir, input_files, pages):
angle0 = current_page.Rotate if '/Rotate' in current_page else 0
bottom_left_corner = [current_page.MediaBox[0], current_page.MediaBox[1]]
rotate_times = int(round(((angle + angle0) % 360) / 90) % 4)
if cur_page.crop != [0., 0., 0., 0.] or bottom_left_corner != [0., 0.]:
current_page = _create_tmp_cropped_page(tmpdir, current_page, angle + angle0, cur_page.crop)
if cur_page.crop != [0., 0., 0., 0.] or bottom_left_corner != [0., 0.] or cur_page.scale != 1.0:
current_page = _create_tmp_page(tmpdir, current_page, angle + angle0, cur_page.crop, cur_page.scale)
rotate_times = 0

x1, y1, x2, y2 = [float(x) for x in current_page.MediaBox]
Expand Down

0 comments on commit 06e0f51

Please sign in to comment.