+def collect_per_page_crops_and_zooms(args_crops, args_symmetry, pages_to_add):
+ crops_at_page = [(0,0,0,0)]*len(pages_to_add)
+ zoom_at_page = [1]*len(pages_to_add)
+ if args_crops:
+ for c_string in args_crops:
+ page_range, crops = split_crops_string(c_string)
+ start_page, end_page = parse_page_range(page_range, pages_to_add)
+ crop_left_cm, crop_bottom_cm, crop_right_cm, crop_top_cm = [float(x) for x in crops.split(',')]
+ crop_left = crop_left_cm * POINTS_PER_CM
+ crop_bottom = crop_bottom_cm * POINTS_PER_CM
+ crop_right = crop_right_cm * POINTS_PER_CM
+ crop_top = crop_top_cm * POINTS_PER_CM
+ prefix = "-c, -t" if args_symmetry else "-c"
+ suffix = " (but alternating left and right crop between even and odd pages)" if args_symmetry else ""
+ print(f"{prefix}: to pages {start_page + 1} to {end_page} applying crops: left {crop_left_cm}cm, bottom {crop_bottom_cm}cm, right {crop_right_cm}cm, top {crop_top_cm}cm{suffix}")
+ cropped_width = A4_WIDTH - crop_left - crop_right
+ cropped_height = A4_HEIGHT - crop_bottom - crop_top
+ zoom = 1
+ zoom_horizontal = A4_WIDTH / (A4_WIDTH - crop_left - crop_right)
+ zoom_vertical = A4_HEIGHT / (A4_HEIGHT - crop_bottom - crop_top)
+ if (zoom_horizontal > 1 and zoom_vertical < 1) or (zoom_horizontal < 1 and zoom_vertical > 1):
+ raise HandledException("-c: crops would create opposing zoom directions")
+ elif zoom_horizontal + zoom_vertical > 2:
+ zoom = min(zoom_horizontal, zoom_vertical)
+ else:
+ zoom = max(zoom_horizontal, zoom_vertical)
+ for page_num in range(start_page, end_page):
+ if args_symmetry and page_num % 2:
+ crops_at_page[page_num] = (crop_right, crop_bottom, crop_left, crop_top)
+ else:
+ crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top)
+ zoom_at_page[page_num] = zoom
+ return crops_at_page, zoom_at_page
+
+def build_single_pages_output(writer, pages_to_add, crops_at_page, zoom_at_page):
+ print("building 1-input-page-per-output-page book")
+ odd_page = True
+ for i, page in enumerate(pages_to_add):
+ crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
+ zoom = zoom_at_page[i]
+ page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom))
+ page.add_transformation(pypdf.Transformation().scale(zoom, zoom))
+ cropped_width = A4_WIDTH - crop_left - crop_right
+ cropped_height = A4_HEIGHT - crop_bottom - crop_top
+ page.mediabox.right = cropped_width * zoom
+ page.mediabox.top = cropped_height * zoom
+ writer.add_page(page)
+ odd_page = not odd_page
+ print(f"built page number {i+1} (of {len(pages_to_add)})")
+
+def resort_pages_for_nup4(pages_to_add):
+ new_page_order = []
+ new_i_order = []
+ eight_pack = []
+ i = 0
+ n_eights = 0
+ for page in pages_to_add:
+ if i == 0:
+ eight_pack = []
+ eight_pack += [page]
+ i += 1
+ if i == 8:
+ i = 0
+ for n in PAGE_ORDER_FOR_NUP4:
+ new_i_order += [8 * n_eights + n]
+ new_page_order += [eight_pack[n]]
+ n_eights += 1
+ return new_page_order, new_i_order
+
+def nup4_inner_page_transform(page, crops, zoom, bonus_shrink_factor, printable_margin, printable_scale, nup4_position):
+ crop_left, crop_bottom, crop_right, crop_top = crops
+ page.add_transformation(pypdf.Transformation().translate(ty=(A4_HEIGHT / zoom - (A4_HEIGHT - crop_top))))
+ if nup4_position == 0 or nup4_position == 2:
+ page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
+ elif nup4_position == 1 or nup4_position == 3:
+ page.add_transformation(pypdf.Transformation().translate(tx=(A4_WIDTH / zoom - (A4_WIDTH - crop_right))))
+ page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor))
+ if nup4_position == 2 or nup4_position == 3:
+ page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale))
+
+def nup4_outer_page_transform(page, bonus_shrink_factor, nup4_position):
+ page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*A4_HEIGHT))
+ if nup4_position == 0 or nup4_position == 1:
+ y_section = A4_HEIGHT
+ page.mediabox.bottom = A4_HALF_HEIGHT
+ page.mediabox.top = A4_HEIGHT
+ if nup4_position == 2 or nup4_position == 3:
+ y_section = 0
+ page.mediabox.bottom = 0
+ page.mediabox.top = A4_HALF_HEIGHT
+ if nup4_position == 0 or nup4_position == 2:
+ x_section = 0
+ page.mediabox.left = 0
+ page.mediabox.right = A4_HALF_WIDTH
+ if nup4_position == 1 or nup4_position == 3:
+ page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*A4_WIDTH))
+ x_section = A4_WIDTH
+ page.mediabox.left = A4_HALF_WIDTH
+ page.mediabox.right = A4_WIDTH
+ page.add_transformation(pypdf.Transformation().translate(tx=x_section, ty=y_section))
+ page.add_transformation(pypdf.Transformation().scale(QUARTER_SCALE_FACTOR, QUARTER_SCALE_FACTOR))
+
+def ornate_nup4(writer, args_analyze, is_front_page, new_page, printable_margin, printable_scale, bonus_shrink_factor, canvas_class):
+ if args_analyze:
+ # borders
+ packet = io.BytesIO()
+ c = canvas_class(packet, pagesize=A4)
+ c.setLineWidth(0.1)
+ c.line(0, A4_HEIGHT, A4_WIDTH, A4_HEIGHT)
+ c.line(0, A4_HALF_HEIGHT, A4_WIDTH, A4_HALF_HEIGHT)
+ c.line(0, 0, A4_WIDTH, 0)
+ c.line(0, A4_HEIGHT, 0, 0)
+ c.line(A4_HALF_WIDTH, A4_HEIGHT, A4_HALF_WIDTH, 0)
+ c.line(A4_WIDTH, A4_HEIGHT, A4_WIDTH, 0)
+ c.save()
+ new_pdf = pypdf.PdfReader(packet)
+ new_page.merge_page(new_pdf.pages[0])
+ printable_offset_x = printable_margin
+ printable_offset_y = printable_margin * A4_HEIGHT / A4_WIDTH
+ new_page.add_transformation(pypdf.Transformation().scale(printable_scale, printable_scale))
+ new_page.add_transformation(pypdf.Transformation().translate(tx=printable_offset_x, ty=printable_offset_y))
+ x_left_spine_limit = A4_HALF_WIDTH * bonus_shrink_factor
+ x_right_spine_limit = A4_WIDTH - x_left_spine_limit
+ if args_analyze or is_front_page:
+ packet = io.BytesIO()
+ c = canvas_class(packet, pagesize=A4)
+ if args_analyze:
+ # spine lines
+ c.setLineWidth(0.1)
+ c.line(x_left_spine_limit, A4_HEIGHT, x_left_spine_limit, 0)
+ c.line(x_right_spine_limit, A4_HEIGHT, x_right_spine_limit, 0)
+ if is_front_page:
+ c.setLineWidth(0.2)
+ draw_cut(c, x_left_spine_limit, (1))
+ draw_cut(c, x_right_spine_limit, (-1))
+ if args_analyze or is_front_page:
+ c.save()
+ new_pdf = pypdf.PdfReader(packet)
+ new_page.merge_page(new_pdf.pages[0])