home · contact · privacy
Bookmaker: Refactor cut drawing code.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 24 Sep 2023 18:21:53 +0000 (20:21 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 24 Sep 2023 18:21:53 +0000 (20:21 +0200)
bookmaker.py

index 20d9c4dae8dafa43310ef4f7b5b239ef72223e0c..001877920d2cd1bb014ae2850e09726f6971fcbe 100755 (executable)
@@ -138,6 +138,15 @@ def parse_page_range(range_string, pages):
             end_page = int(end)
     return start_page, end_page
 
+def draw_cut(canvas, x_spine_limit, direction, half_height):
+    outer_start_x = x_spine_limit - 0.5 * CUT_WIDTH * direction
+    inner_start_x = x_spine_limit + 0.5 * CUT_WIDTH * direction
+    middle_point_y = half_height + MIDDLE_POINT_DEPTH * direction
+    end_point_y = half_height + CUT_DEPTH * direction
+    canvas.line(inner_start_x, half_height, x_spine_limit, end_point_y)
+    canvas.line(x_spine_limit, end_point_y, x_spine_limit, middle_point_y)
+    canvas.line(x_spine_limit, middle_point_y, outer_start_x, half_height)
+
 def parse_args():
     parser = argparse.ArgumentParser(description=__doc__, epilog=help_epilogue, formatter_class=argparse.RawDescriptionHelpFormatter)
     parser.add_argument("-i", "--input_file", action="append", required=True, help="input PDF file")
@@ -434,22 +443,8 @@ def main():
                     c.line(x_right_spine_limit, A4_HEIGHT, x_right_spine_limit, 0)
                 if front_page:
                     c.setLineWidth(0.2)
-                    # cut upper left
-                    start_up_left_left_x = x_left_spine_limit - 0.5 * CUT_WIDTH
-                    start_up_left_right_x = x_left_spine_limit + 0.5 * CUT_WIDTH
-                    middle_point_up_left_y = half_height + MIDDLE_POINT_DEPTH
-                    end_point_up_left_y = half_height + CUT_DEPTH
-                    c.line(start_up_left_right_x, half_height, x_left_spine_limit, end_point_up_left_y)
-                    c.line(x_left_spine_limit, end_point_up_left_y, x_left_spine_limit, middle_point_up_left_y)
-                    c.line(x_left_spine_limit, middle_point_up_left_y, start_up_left_left_x, half_height)
-                    # cut lower right
-                    start_down_right_left_x = x_right_spine_limit - 0.5 * CUT_WIDTH
-                    start_down_right_right_x = x_right_spine_limit + 0.5 * CUT_WIDTH
-                    middle_point_down_right_y = half_height - MIDDLE_POINT_DEPTH
-                    end_point_down_right_y = half_height - CUT_DEPTH
-                    c.line(start_down_right_left_x, half_height, x_right_spine_limit, end_point_down_right_y)
-                    c.line(x_right_spine_limit, end_point_down_right_y, x_right_spine_limit, middle_point_down_right_y)
-                    c.line(x_right_spine_limit, middle_point_down_right_y, start_down_right_right_x, half_height)
+                    draw_cut(c, x_left_spine_limit, (1), half_height)
+                    draw_cut(c, x_right_spine_limit, (-1), half_height)
                 if args.analyze or front_page:
                     c.save()
                     new_pdf = pypdf.PdfReader(packet)