docs: def calculate_crop_list(full_page_box_list, bounding_box_list, angle_list,
                                                                 page_nums_to_crop):
      """Given a list of full-page boxes (media boxes) and a list of tight
      bounding boxes for each page, calculate and return another list giving the
      list of bounding boxes to crop down to.  The parameter `angle_list` is
      a list of rotation angles which correspond to the pages.  The pages
      selected to crop are in the set `page_nums_to_crop`."""
  
      # Definition: the deltas are the four differences, one for each margin,
      # between the original full page box and the final, cropped full-page box.
      # In the usual case where margin sizes decrease these are the same as the
      # four margin-reduction values (in absolute points).   The deltas are
      # usually positive but they can be negative due to either percentRetain>100
      # or a large enough absolute offset (in which case the size of the
      # corresponding margin will increase).  When percentRetain<0 the deltas are
      # always greater than the absolute difference between the full page and a
      # tight bounding box, and so part of the text within the tight bounding box
      # will also be cropped (unless absolute offsets are used to counter that).
  
      num_pages = len(bounding_box_list)
      page_range = range(num_pages)
      num_pages_to_crop = len(page_nums_to_crop)
  
      # Handle the '--samePageSize' option.
      # Note that this is always done first, even before evenodd is handled.  It
      # is only applied to the pages in the set `page_nums_to_crop`.
  
      order_n = 0
      if args.samePageSizeOrderStat:
          args.samePageSize = True
          order_n = min(args.samePageSizeOrderStat[0], num_pages_to_crop - 1)
          order_n = max(order_n, 0)
  
      if args.samePageSize:
          if args.verbose:
              print("\nSetting each page size to the smallest box bounding all the pages.")
              if order_n != 0:
                  print("But ignoring the largest {} pages in calculating each edge."
                          .format(order_n))
  
          same_size_bounding_box = [
                # We want the smallest of the left and bottom edges.
                sorted(full_page_box_list[pg][0] for pg in page_nums_to_crop),
                sorted(full_page_box_list[pg][1] for pg in page_nums_to_crop),
                # We want the largest of the right and top edges.
                sorted((full_page_box_list[pg][2] for pg in page_nums_to_crop), reverse=True),
                sorted((full_page_box_list[pg][3] for pg in page_nums_to_crop), reverse=True)
                ]
          same_size_bounding_box = [sortlist[order_n] for sortlist in same_size_bounding_box]
  
          new_full_page_box_list = []
          for p_num, box in enumerate(full_page_box_list):
              if p_num not in page_nums_to_crop:
                  new_full_page_box_list.append(box)
              else:
                  new_full_page_box_list.append(same_size_bounding_box)
          full_page_box_list = new_full_page_box_list
  
      # Handle the '--evenodd' option if it was selected.
      if args.evenodd:
          even_page_nums_to_crop = {p_num for p_num in page_nums_to_crop if p_num % 2 == 0}
          odd_page_nums_to_crop = {p_num for p_num in page_nums_to_crop if p_num % 2 != 0}
  
          if args.uniform:
              uniform_set_with_even_odd = True
          else:
              uniform_set_with_even_odd = False
  
          # Recurse on even and odd pages, after resetting some options.
          if args.verbose:
              print("\nRecursively calculating crops for even and odd pages.")
          args.evenodd = False # Avoid infinite recursion.
          args.uniform = True  # --evenodd implies uniform, just on each separate group
          even_crop_list = calculate_crop_list(full_page_box_list, bounding_box_list,
                                               angle_list, even_page_nums_to_crop)
          odd_crop_list = calculate_crop_list(full_page_box_list, bounding_box_list,
                                              angle_list, odd_page_nums_to_crop)
  
          # Recombine the even and odd pages.
          combine_even_odd = []
          for p_num in page_range:
              if p_num % 2 == 0:
                  combine_even_odd.append(even_crop_list[p_num])
              else:
                  combine_even_odd.append(odd_crop_list[p_num])
  
          # Handle the case where --uniform was set with --evenodd.
          if uniform_set_with_even_odd:
              min_bottom_margin = min(box[1] for p_num, box in enumerate(combine_even_odd)
                                                            if p_num in page_nums_to_crop)
              max_top_margin = max(box[3] for p_num, box in enumerate(combine_even_odd)
                                                         if p_num in page_nums_to_crop)
              combine_even_odd = [[box[0], min_bottom_margin, box[2], max_top_margin]
                                for box in combine_even_odd]
          return combine_even_odd
  
      # Before calculating the crops we modify the percentRetain and
      # absoluteOffset values for all the pages according to any specified.
      # rotations for the pages.  This is so, for example, uniform cropping is
      # relative to what the user actually sees.
      rotated_percent_retain = [mod_box_for_rotation(args.percentRetain, angle_list[m_val])
                                                           for m_val in range(num_pages)]
      rotated_absolute_offset = [mod_box_for_rotation(args.absoluteOffset, angle_list[m_val])
                                                           for m_val in range(num_pages)]
  
      # Calculate the list of deltas to be used to modify the original page
      # sizes.  Basically, a delta is the absolute diff between the full and
      # tight-bounding boxes, scaled according to the user's percentRetain, with
      # any absolute offset then added (lb) or subtracted (tr) as appropriate.
      #
      # The deltas are all positive unless absoluteOffset changes that or
      # percent>100.  They are added (lb) or subtracted (tr) as appropriate.
  
      delta_list = []
      for p_num, t_box, f_box in zip(list(range(len(full_page_box_list))),
                                                 bounding_box_list, full_page_box_list):
          deltas = [abs(t_box[m_val] - f_box[m_val]) for m_val in range(4)]
          adj_deltas = [deltas[m_val] * (100.0-rotated_percent_retain[p_num][m_val]) / 100.0
                       for m_val in range(4)]
          adj_deltas = [adj_deltas[m_val] + rotated_absolute_offset[p_num][m_val] for m_val in range(4)]
          delta_list.append(adj_deltas)
  
      # Handle the '--uniform' options if one was selected.
      if args.uniformOrderPercent:
          percent_val = args.uniformOrderPercent[0]
          if percent_val < 0.0: percent_val = 0.0
          if percent_val > 100.0: percent_val = 100.0
          args.uniformOrderStat = [int(round(num_pages_to_crop * percent_val / 100.0))]
  
      if args.uniform or args.uniformOrderStat or args.uniformOrderStat4:
          if args.verbose:
              print("\nAll the selected pages will be uniformly cropped.")
          # Expand to tuples containing page nums, to better print verbose information.
          delta_list = [(delta_list[j], j+1) for j in page_range] # Note +1 added here.
  
          # Only look at the deltas which correspond to pages selected for cropping.
          # The values will then be sorted for each margin and selected.
          crop_delta_list = [delta_list[j] for j in page_range if j in page_nums_to_crop]
  
          # Handle order stats; m_vals are the four index values into the sorted
          # delta lists, one per margin.
          m_vals = [0, 0, 0, 0]
          if args.uniformOrderStat4:
              m_vals = args.uniformOrderStat4
          elif args.uniformOrderStat:
              m_vals = [args.uniformOrderStat[0]] * 4
          fixed_m_vals = []
          for m_val in m_vals:
              if m_val < 0 or m_val >= num_pages_to_crop:
                  print("\nWarning: The selected order statistic is out of range.",
                        "Setting to closest value.", file=sys.stderr)
                  if m_val >= num_pages_to_crop:
                      m_val = num_pages_to_crop - 1
                  if m_val < 0:
                      m_val = 0
              fixed_m_vals.append(m_val)
          m_vals = fixed_m_vals
          if args.verbose and (args.uniformOrderStat or args.uniformOrderPercent
                                                     or args.uniformOrderStat4):
              print("\nPer-margin, the", m_vals,
                    "smallest delta values over the selected pages\nwill be ignored"
                    " when choosing common, uniform delta values.")
  
          # Get a sorted list of (delta, page_num) tuples for each margin.
          left_vals = sorted([(box[0][0], box[1]) for box in crop_delta_list])
          lower_vals = sorted([(box[0][1], box[1]) for box in crop_delta_list])
          right_vals = sorted([(box[0][2], box[1]) for box in crop_delta_list])
          upper_vals = sorted([(box[0][3], box[1]) for box in crop_delta_list])
          delta_list = [[left_vals[m_vals[0]][0], lower_vals[m_vals[1]][0],
                        right_vals[m_vals[2]][0], upper_vals[m_vals[3]][0]]] * num_pages
  
          if args.verbose:
              delta_page_nums = [left_vals[m_vals[0]][1], lower_vals[m_vals[1]][1],
                                 right_vals[m_vals[2]][1], upper_vals[m_vals[3]][1]]
              print("\nThe smallest delta values actually used to set the uniform"
                    " cropping\namounts (ignoring any '-m' skips and pages in ranges"
                    " not cropped) were\nfound on these pages, numbered from 1:\n   ",
                    delta_page_nums)
              print("\nThe final delta values themselves are:\n   ", delta_list[0])
  
      # Apply the delta modifications to the full boxes to get the final sizes.
      final_crop_list = []
      for f_box, deltas in zip(full_page_box_list, delta_list):
          final_crop_list.append((f_box[0] + deltas[0], f_box[1] + deltas[1],
                                  f_box[2] - deltas[2], f_box[3] - deltas[3]))
  
      # Set the page ratios if user chose that option.
      if args.setPageRatios:
          ratio = args.setPageRatios[0]
          if args.verbose:
              print("\nSetting all page width to height ratios to:", ratio)
          ratio_set_crop_list = []
          for left, bottom, right, top in final_crop_list:
              width = right - left
              horizontal_center = (right + left) / 2.0
              height = top - bottom
              vertical_center = (top + bottom) / 2.0
              new_height = width / ratio
              if new_height < height:
                  new_width = height * ratio
                  assert new_width >= width
                  ratio_set_crop_list.append((horizontal_center - new_width/2.0, bottom,
                                              horizontal_center + new_width/2.0, top))
              else:
                  ratio_set_crop_list.append((left, vertical_center - new_height/2.0,
                                             right, vertical_center + new_height/2.0))
          final_crop_list = ratio_set_crop_list
  
      return final_crop_list