From: Christian Heller Date: Sat, 23 Sep 2023 23:17:59 +0000 (+0200) Subject: In Bookmaker, allow multiple (optiopnally page-ranged) crop definitions. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Bprefix%7D%7D?a=commitdiff_plain;h=8551551339083bbba32b241e4aa53399f5051d01;p=misc In Bookmaker, allow multiple (optiopnally page-ranged) crop definitions. --- diff --git a/bookmaker.py b/bookmaker.py index ca3548b..4a7ed31 100755 --- a/bookmaker.py +++ b/bookmaker.py @@ -9,30 +9,36 @@ parser = argparse.ArgumentParser(description="build print-ready book PDF") parser.add_argument("-i", "--input", dest="input_file", action="append", required=True, help="input PDF file") parser.add_argument("-o", "--output", dest="output_file", required=True, help="output PDF file") parser.add_argument("-p", "--pages", dest="page_range", action="append", help="page range, e.g., '3-end'") -parser.add_argument("-c", "--crop", dest="crop_range", help="crops left, bottom, right, top – e.g., '10,10,10,10'") +parser.add_argument("-c", "--crop", dest="crop_range", action="append", help="crops left, bottom, right, top – e.g., '10,10,10,10'; prefix with ':'-delimited page range to limit effect") parser.add_argument("-n", "--nup4", dest="nup4", action='store_true', help="puts 4 input pages onto 1 output page") parser.add_argument("-a", "--analyze", dest="analyze", action="store_true", help="print lines identifying spine, page borders") parser.add_argument("-t", "--symmetry", dest="symmetry", action="store_true", help="alternate horizontal crops between odd and even pages") parser.add_argument("-r", "--rotate", dest="rotate", type=int, action="append", help="rotate page of number by 90° (usable multiple times on same page!)") -parser.add_argument("-m", "--margin", type=float, default=4.3, help="print margin (default 4.3)") +parser.add_argument("-m", "--margin", type=float, default=4.3, help="print margin in mm (default 4.3)") args = parser.parse_args() # select pages from input files +def parse_page_range(range_string, pages): + start_page = 0 + end_page = len(pages) + if range_string: + start, end = range_string.split('-') + if not (len(start) == 0 or start == "start"): + start_page = int(start) - 1 + if not (len(end) == 0 or end == "end"): + end_page = int(end) + return start_page, end_page pages_to_add = [] opened_files = [] for i, input_file in enumerate(args.input_file): file = open(input_file, 'rb') opened_files += [file] reader = pypdf.PdfReader(file) - start_page = 0 - end_page = len(reader.pages) + range_string = None if args.page_range and len(args.page_range) > i: - start, end = args.page_range[i].split('-') - if not (len(start) == 0 or start == "start"): - start_page = int(start) - 1 - if not (len(end) == 0 or end == "end"): - end_page = int(end) + range_string = args.page_range[i] + start_page, end_page = parse_page_range(range_string, reader.pages) for page_num in range(start_page, end_page): page = reader.pages[page_num] pages_to_add += [page] @@ -56,49 +62,67 @@ for page in pages_to_add: page.mediabox.right = a4_width page.cropbox = page.mediabox -# determine page crop -crop_left, crop_bottom, crop_right, crop_top = 0, 0, 0, 0 +# determine page crops, zooms +crops_at_page = [(0,0,0,0)]*len(pages_to_add) +zoom_at_page = [1]*len(pages_to_add) if args.crop_range: - crop_left, crop_bottom, crop_right, crop_top = [float(x) for x in args.crop_range.split(',')] -cropped_width = a4_width - crop_left - crop_right -cropped_height = a4_height - crop_bottom - crop_top -zoom = 1 -if args.crop_range: - 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): - print("Error: opposing zooms.") - exit(1) - elif zoom_horizontal + zoom_vertical > 2: - zoom = min(zoom_horizontal, zoom_vertical) - else: - zoom = max(zoom_horizontal, zoom_vertical) + for crop_range in args.crop_range: + initial_split = crop_range.split(':') + if len(initial_split) > 1: + page_range = initial_split[0] + crops = initial_split[1] + else: + page_range = None + crops = initial_split[0] + start_page, end_page = parse_page_range(page_range, pages_to_add) + crop_left, crop_bottom, crop_right, crop_top = [float(x) for x in crops.split(',')] + 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): + print("Error: opposing zooms.") + exit(1) + 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): + crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top) + zoom_at_page[page_num] = zoom writer = pypdf.PdfWriter() if not args.nup4: odd_page = True - for page in pages_to_add: + 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] if args.symmetry and odd_page: page.add_transformation(pypdf.Transformation().translate(tx=-crop_right, ty=-crop_bottom)) else: 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 + else: - n_pages_per_axis = 2 points_per_mm = 2.83465 + n_pages_per_axis = 2 + spine_limit = 10 printable_margin = args.margin * points_per_mm printable_scale = (a4_width - 2*printable_margin)/a4_width - spine_limit = 10 * points_per_mm half_width = a4_width / n_pages_per_axis half_height = a4_height / n_pages_per_axis section_scale_factor = 1 / n_pages_per_axis spine_part_of_page = (spine_limit / half_width) / printable_scale bonus_shrink_factor = 1 - spine_part_of_page new_page_order = [] + new_i_order = [] eight_pack = [] mod_to_8 = len(pages_to_add) % 8 if mod_to_8 > 0: @@ -106,6 +130,7 @@ else: new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height) pages_to_add += [new_page] i = 0 + n_eights = 0 for page in pages_to_add: if i == 0: eight_pack = [] @@ -113,6 +138,15 @@ else: i += 1 if i == 8: i = 0 + new_i_order += [8 * n_eights + 3, + 8 * n_eights + 0, + 8 * n_eights + 7, + 8 * n_eights + 4, + 8 * n_eights + 1, + 8 * n_eights + 2, + 8 * n_eights + 5, + 8 * n_eights + 6] + n_eights += 1 new_page_order += [eight_pack[3]] # page front, upper left new_page_order += [eight_pack[0]] # page front, upper right new_page_order += [eight_pack[7]] # page front, lower left @@ -124,11 +158,14 @@ else: i = 0 page_count = 0 front_page = True - for page in new_page_order: + for j, page in enumerate(new_page_order): if i == 0: new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height) # in-section transformations: align pages on top, left-hand pages to left, right-hand to right + new_i = new_i_order[j] + crop_left, crop_bottom, crop_right, crop_top = crops_at_page[new_i] + zoom = zoom_at_page[new_i] page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top)))) if i == 0 or i == 2: if args.symmetry: