r"""Estimate the bounding box of a table. Tables in academic papers come in two layouts: - caption-on-top: ``\caption`` precedes ``\begin{tabular}``; - caption-on-bottom: tabular body precedes ``\caption``. LaTeX makes both common, and within a single paper both forms can mix
(
page,
caption_anchor: dict,
prev_anchor: dict | None,
next_anchor: dict | None,
page_rect,
)
| 966 | |
| 967 | |
| 968 | def _estimate_table_bbox_with_rows( |
| 969 | page, |
| 970 | caption_anchor: dict, |
| 971 | prev_anchor: dict | None, |
| 972 | next_anchor: dict | None, |
| 973 | page_rect, |
| 974 | ) -> tuple[tuple[float, float, float, float], int] | None: |
| 975 | r"""Estimate the bounding box of a table. |
| 976 | |
| 977 | Tables in academic papers come in two layouts: |
| 978 | |
| 979 | - caption-on-top: ``\caption`` precedes ``\begin{tabular}``; |
| 980 | - caption-on-bottom: tabular body precedes ``\caption``. |
| 981 | |
| 982 | LaTeX makes both common, and within a single paper both forms can mix |
| 983 | (e.g. wide tables placed with ``[t]`` vs. ``[b]``). We therefore probe |
| 984 | both directions and pick the side with strictly more confirmed table body |
| 985 | rows. Ties go to the downward side, matching the most common ACM / IEEE |
| 986 | template defaults. |
| 987 | |
| 988 | Tables are usually pure text + thin separator lines, so the page rendering |
| 989 | of just the union of text-line bboxes is sufficient. We additionally |
| 990 | union any drawing rects (``\hline``, frames) and image rects that fall in |
| 991 | the same y-range, in case the paper places company-logo plots inside a |
| 992 | table cell. |
| 993 | """ |
| 994 | caption_y0 = caption_anchor["bbox"][1] |
| 995 | caption_y1 = caption_anchor["bbox"][3] |
| 996 | |
| 997 | upper_bound = page_rect.y0 |
| 998 | if prev_anchor is not None: |
| 999 | upper_bound = max(page_rect.y0, prev_anchor["bbox"][3] + 2.0) |
| 1000 | |
| 1001 | lower_bound = page_rect.y1 |
| 1002 | if next_anchor is not None: |
| 1003 | lower_bound = max(caption_y1 + 1.0, next_anchor["bbox"][1] - 2.0) |
| 1004 | |
| 1005 | text_lines = _collect_text_lines(page) |
| 1006 | rows = _cluster_lines_into_rows(text_lines) |
| 1007 | paragraph_blocks = _find_paragraph_blocks(page) |
| 1008 | |
| 1009 | down_lines, down_data = _grow_table_region( |
| 1010 | page, |
| 1011 | caption_anchor, |
| 1012 | rows, |
| 1013 | paragraph_blocks, |
| 1014 | direction="down", |
| 1015 | upper_bound=upper_bound, |
| 1016 | lower_bound=lower_bound, |
| 1017 | ) |
| 1018 | up_lines, up_data = _grow_table_region( |
| 1019 | page, |
| 1020 | caption_anchor, |
| 1021 | rows, |
| 1022 | paragraph_blocks, |
| 1023 | direction="up", |
| 1024 | upper_bound=upper_bound, |
| 1025 | lower_bound=lower_bound, |
no test coverage detected