def combine_horizontally(bounding_boxes, evaled_letter_width): # Sort by position sorted_boxes = sorted(bounding_boxes, key=lambda x: x.x, reverse=False) idx = 0 while idx < len(sorted_boxes) - 1: box = sorted_boxes[idx] next_box = sorted_boxes[idx + 1] combined = BoundingBox.combine(box, next_box) combined_max_width = evaled_letter_width * 1.2 if combined.w < combined_max_width: sorted_boxes.pop(idx) sorted_boxes.pop(idx) sorted_boxes.insert(idx, combined) else: idx += 1 return sorted_boxes