Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: implement parallel graphic rule row splitting with boundary preservation for enhanced text layout

myhloli0f7908b7a91a
diff --git a/mineru/backend/flash/native_pdf/graphics.py b/mineru/backend/flash/native_pdf/graphics.py
index 89fc71a2..fe2cb664 100644
--- a/mineru/backend/flash/native_pdf/graphics.py
+++ b/mineru/backend/flash/native_pdf/graphics.py
@@ -4,6 +4,7 @@
from __future__ import annotations
+from dataclasses import replace
import math
import re
import statistics
@@ -26,6 +27,7 @@ from .geometry import (
_bbox_center_x,
_bbox_center_y,
_bbox_distance,
+ _bbox_intersects,
_bbox_overlap_in_first,
_bbox_overlap_in_smaller,
_bbox_union,
@@ -35,7 +37,11 @@ from .geometry import (
_point_in_bbox,
_rotate_bbox_to_upright,
)
-from .native_text import _sanitize_pdf_control_text
+from .native_text import (
+ _fill_native_typography,
+ _normalize_native_run_text,
+ _sanitize_pdf_control_text,
+)
from .line_layout import (
_infer_text_lanes,
_line_effective_height,
@@ -354,6 +360,303 @@ def _build_graphic_like_blocks(
return blocks, claimed
+def _parallel_graphic_rule_pairs(
+ drawing_lines: list[_AxisLine],
+ image_bboxes: list[BBox],
+ table_bboxes: list[BBox],
+ page_size: tuple[float, float],
+ median_height: float,
+) -> list[tuple[BBox, BBox]]:
+ """筛选分别贴近两个并排图形上沿的同高长横线。"""
+
+ minimum_rule_width = max(8.0 * median_height, 0.18 * page_size[0])
+ long_rules = [
+ line.bbox
+ for line in drawing_lines
+ if line.orientation == "horizontal"
+ and line.bbox[2] - line.bbox[0] >= minimum_rule_width
+ and not any(
+ _point_in_bbox(
+ (_bbox_center_x(line.bbox), _bbox_center_y(line.bbox)),
+ table_bbox,
+ )
+ for table_bbox in table_bboxes
+ )
+ ]
+ ordered_images = sorted(image_bboxes, key=lambda bbox: (bbox[0], bbox[1]))
+ rule_pairs: list[tuple[BBox, BBox]] = []
+ seen_pairs: set[tuple[BBox, BBox]] = set()
+ for left_index, left_image in enumerate(ordered_images):
+ left_height = max(0.1, left_image[3] - left_image[1])
+ for right_image in ordered_images[left_index + 1 :]:
+ if left_image[2] >= right_image[0]:
+ continue
+ right_height = max(0.1, right_image[3] - right_image[1])
+ image_overlap = max(
+ 0.0,
+ min(left_image[3], right_image[3])
+ - max(left_image[1], right_image[1]),
+ )
+ if image_overlap < 0.7 * min(left_height, right_height):
+ continue
+ if any(
+ _bbox_overlap_in_smaller(image_bbox, table_bbox) >= 0.5
+ for image_bbox in (left_image, right_image)
+ for table_bbox in table_bboxes
+ ):
+ continue
+
+ left_rules = [
+ rule_bbox
+ for rule_bbox in long_rules
+ if _bbox_axis_overlap_ratio(rule_bbox, left_image, axis="x") >= 0.8
+ and -0.25 * median_height
+ <= left_image[1] - rule_bbox[3]
+ <= 3.0 * median_height
+ ]
+ right_rules = [
+ rule_bbox
+ for rule_bbox in long_rules
+ if _bbox_axis_overlap_ratio(rule_bbox, right_image, axis="x") >= 0.8
+ and -0.25 * median_height
+ <= right_image[1] - rule_bbox[3]
+ <= 3.0 * median_height
+ ]
+ for left_rule in left_rules:
+ for right_rule in right_rules:
+ if left_rule[2] >= right_rule[0]:
+ continue
+ if (
+ abs(_bbox_center_y(left_rule) - _bbox_center_y(right_rule))
+ > 0.5 * median_height
+ ):
+ continue
+ rule_gap = right_rule[0] - left_rule[2]
+ if not 0.5 * median_height <= rule_gap <= 5.0 * median_height:
+ continue
+ pair = (left_rule, right_rule)
+ if pair not in seen_pairs:
+ seen_pairs.add(pair)
+ rule_pairs.append(pair)
+ return rule_pairs
+
+
+def _parallel_graphic_row_split_boundary(
+ members: list[_LineItem],
+ left_rule: BBox,
+ right_rule: BBox,
+ table_bboxes: list[BBox],
+ page_size: tuple[float, float],
+ median_height: float,
+) -> float | None:
+ """用横线栏沟和字符投影确认并排图形上方文本的安全切分点。"""
+
+ if (
+ not members
+ or any(member.angle != 0 for member in members)
+ or len({member.semantic_type for member in members}) != 1
+ ):
+ return None
+ row_bbox = _bbox_union_many([member.bbox for member in members])
+ if any(_bbox_intersects(row_bbox, table_bbox) for table_bbox in table_bboxes):
+ return None
+ rule_top = min(left_rule[1], right_rule[1])
+ if not -0.2 * median_height <= rule_top - row_bbox[3] <= 1.5 * median_height:
+ return None
+
+ glyph_bboxes = [
+ bbox
+ for member in members
+ for char in member.chars
+ if str(char.get("char") or "").isprintable()
+ and not str(char.get("char") or "").isspace()
+ and (bbox := _clip_bbox(_coerce_bbox(char.get("bbox")), page_size))
+ is not None
+ ]
+ if not glyph_bboxes:
+ return None
+ boundary = 0.5 * (left_rule[2] + right_rule[0])
+ if any(
+ _bbox_center_x(bbox) < left_rule[0] - median_height
+ or _bbox_center_x(bbox) > right_rule[2] + median_height
+ for bbox in glyph_bboxes
+ ):
+ return None
+ left_glyphs = [bbox for bbox in glyph_bboxes if _bbox_center_x(bbox) < boundary]
+ right_glyphs = [bbox for bbox in glyph_bboxes if _bbox_center_x(bbox) > boundary]
+ if len(left_glyphs) < 3 or len(right_glyphs) < 3:
+ return None
+ left_width = max(bbox[2] for bbox in left_glyphs) - min(
+ bbox[0] for bbox in left_glyphs
+ )
+ right_width = max(bbox[2] for bbox in right_glyphs) - min(
+ bbox[0] for bbox in right_glyphs
+ )
+ if min(left_width, right_width) < 4.0 * median_height:
+ return None
+ left_edge = max(bbox[2] for bbox in left_glyphs)
+ right_edge = min(bbox[0] for bbox in right_glyphs)
+ if right_edge - left_edge < 0.75 * median_height:
+ return None
+ if not (
+ left_edge <= left_rule[2] <= right_edge
+ and left_edge <= right_rule[0] <= right_edge
+ ):
+ return None
+ return boundary
+
+
+def _split_parallel_graphic_rule_rows(
+ lines: list[_LineItem],
+ drawing_lines: list[_AxisLine],
+ image_bboxes: list[BBox],
+ table_bboxes: list[BBox],
+ page_size: tuple[float, float],
+ *,
+ source_index_start: int | None = None,
+) -> list[_LineItem]:
+ """按成对图形、独立顶边横线和栏沟字符投影拆分并排图形上方文本。"""
+
+ horizontal_lines = [
+ line for line in lines if line.angle == 0 and line.effective_height > 0
+ ]
+ if len(horizontal_lines) < 1 or len(image_bboxes) < 2:
+ return list(lines)
+ median_height = statistics.median(
+ line.effective_height for line in horizontal_lines
+ )
+ rule_pairs = _parallel_graphic_rule_pairs(
+ drawing_lines,
+ image_bboxes,
+ table_bboxes,
+ page_size,
+ median_height,
+ )
+ if not rule_pairs:
+ return list(lines)
+
+ row_groups: dict[tuple[int, int], list[_LineItem]] = {}
+ for line in horizontal_lines:
+ if line.visual_row_id is not None:
+ row_groups.setdefault((line.angle, line.visual_row_id), []).append(line)
+ boundaries_by_row: dict[tuple[int, int], list[float]] = {}
+ for row_key, members in row_groups.items():
+ for left_rule, right_rule in rule_pairs:
+ boundary = _parallel_graphic_row_split_boundary(
+ members,
+ left_rule,
+ right_rule,
+ table_bboxes,
+ page_size,
+ median_height,
+ )
+ if boundary is None:
+ continue
+ row_boundaries = boundaries_by_row.setdefault(row_key, [])
+ if not any(
+ abs(boundary - existing) <= 0.5 * median_height
+ for existing in row_boundaries
+ ):
+ row_boundaries.append(boundary)
+ if not boundaries_by_row:
+ return list(lines)
+
+ next_source_index = max(
+ max((line.source_index for line in lines), default=-1) + 1,
+ source_index_start or 0,
+ )
+ consumed_source_indices: set[int] = set()
+ split_lines: list[_LineItem] = []
+ for row_key, boundaries in boundaries_by_row.items():
+ members = sorted(
+ row_groups[row_key],
+ key=lambda line: (line.bbox[0], line.run_index, line.source_index),
+ )
+ ordered_chars = [char for member in members for char in member.chars]
+ split_indices: list[int] = []
+ for boundary in sorted(boundaries):
+ split_index = next(
+ (
+ index
+ for index, char in enumerate(ordered_chars)
+ if str(char.get("char") or "").isprintable()
+ and not str(char.get("char") or "").isspace()
+ and (
+ bbox := _clip_bbox(
+ _coerce_bbox(char.get("bbox")),
+ page_size,
+ )
+ )
+ is not None
+ and _bbox_center_x(bbox) > boundary
+ ),
+ None,
+ )
+ if split_index is not None and split_index not in split_indices:
+ split_indices.append(split_index)
+ if not split_indices:
+ continue
+ ranges: list[tuple[int, int]] = []
+ start = 0
+ for split_index in sorted(split_indices):
+ ranges.append((start, split_index))
+ start = split_index
+ ranges.append((start, len(ordered_chars)))
+
+ source_indices = [member.source_index for member in members]
+ rebuilt: list[_LineItem] = []
+ for run_index, (start, end) in enumerate(ranges):
+ run_chars = ordered_chars[start:end]
+ run_bboxes = [
+ bbox
+ for char in run_chars
+ if str(char.get("char") or "").isprintable()
+ and not str(char.get("char") or "").isspace()
+ and (
+ bbox := _clip_bbox(
+ _coerce_bbox(char.get("bbox")),
+ page_size,
+ )
+ )
+ is not None
+ ]
+ run_text = _normalize_native_run_text(
+ "".join(str(char.get("char") or "") for char in run_chars)
+ )
+ if not run_text or not run_bboxes:
+ continue
+ if run_index < len(source_indices):
+ source_index = source_indices[run_index]
+ else:
+ source_index = next_source_index
+ next_source_index += 1
+ template = members[min(run_index, len(members) - 1)]
+ rebuilt_line = replace(
+ template,
+ text=run_text,
+ bbox=_bbox_union_many(run_bboxes),
+ source_index=source_index,
+ chars=list(run_chars),
+ visual_row_id=row_key[1],
+ run_index=run_index,
+ split_from_row=True,
+ preserve_split_boundary=True,
+ )
+ _fill_native_typography(rebuilt_line, page_size)
+ rebuilt.append(rebuilt_line)
+ if len(rebuilt) < 2:
+ continue
+ consumed_source_indices.update(member.source_index for member in members)
+ split_lines.extend(rebuilt)
+
+ output = [
+ line for line in lines if line.source_index not in consumed_source_indices
+ ]
+ output.extend(split_lines)
+ output.sort(key=lambda line: (line.angle, line.bbox[1], line.bbox[0], line.source_index))
+ return output
+
+
def _graphic_caption_line_indices_to_preserve(
lines: list[_LineItem],
candidates: list[_GraphicCandidate],
diff --git a/mineru/backend/flash/native_pdf/line_merging.py b/mineru/backend/flash/native_pdf/line_merging.py
index bed66634..e1d83289 100644
--- a/mineru/backend/flash/native_pdf/line_merging.py
+++ b/mineru/backend/flash/native_pdf/line_merging.py
@@ -331,6 +331,9 @@ def _merge_overlapping_inline_cluster(
median_glyph_width=host.median_glyph_width,
leading_emphasis_width=ordered_members[0].leading_emphasis_width,
split_from_row=any(line.split_from_row for line in ordered_members),
+ preserve_split_boundary=any(
+ line.preserve_split_boundary for line in ordered_members
+ ),
semantic_type=host.semantic_type,
restored_inline_cluster=True,
compact_formula_cluster=compact_formula_cluster,
@@ -605,6 +608,9 @@ def _merge_same_baseline_group(
else None,
leading_emphasis_width=members[0].leading_emphasis_width,
split_from_row=any(member.split_from_row for member in members),
+ preserve_split_boundary=any(
+ member.preserve_split_boundary for member in members
+ ),
semantic_type=members[0].semantic_type,
restored_inline_cluster=any(
member.restored_inline_cluster for member in members
@@ -726,6 +732,8 @@ def _merge_title_resolved_visual_rows(
for members in row_groups.values():
if len(members) < 2 or not all(member.split_from_row for member in members):
continue
+ if any(member.preserve_split_boundary for member in members):
+ continue
semantic_types = {member.semantic_type for member in members}
if len(semantic_types) != 1:
continue
@@ -790,6 +798,7 @@ def _is_dense_same_font_two_run_row(
if (
len(members) != 2
or not all(member.split_from_row for member in members)
+ or any(member.preserve_split_boundary for member in members)
or any(member.semantic_type is not None for member in members)
or any(member.font_signature is None for member in members)
or any(member.font_coverage < 0.75 for member in members)
@@ -840,7 +849,11 @@ def _can_restore_dense_split_visual_row(
) -> bool:
"""检查 hard-split run 是否构成同字体且占用充分的完整视觉行。"""
- if len(members) < 2 or not all(member.split_from_row for member in members):
+ if (
+ len(members) < 2
+ or not all(member.split_from_row for member in members)
+ or any(member.preserve_split_boundary for member in members)
+ ):
return False
if len({member.semantic_type for member in members}) != 1:
return False
@@ -968,6 +981,9 @@ def _merge_dense_split_visual_row(
else None,
leading_emphasis_width=ordered_members[0].leading_emphasis_width,
split_from_row=False,
+ preserve_split_boundary=any(
+ member.preserve_split_boundary for member in ordered_members
+ ),
semantic_type=ordered_members[0].semantic_type,
restored_inline_cluster=any(
member.restored_inline_cluster for member in ordered_members
diff --git a/mineru/backend/flash/native_pdf/models.py b/mineru/backend/flash/native_pdf/models.py
index 8b483d39..435abcb7 100644
--- a/mineru/backend/flash/native_pdf/models.py
+++ b/mineru/backend/flash/native_pdf/models.py
@@ -31,6 +31,7 @@ class _LineItem:
median_glyph_width: float | None = None
leading_emphasis_width: float | None = None
split_from_row: bool = False
+ preserve_split_boundary: bool = False
semantic_type: str | None = None
restored_inline_cluster: bool = False
compact_formula_cluster: bool = False
diff --git a/mineru/backend/flash/native_pdf/pipeline.py b/mineru/backend/flash/native_pdf/pipeline.py
index fa15adf7..5b109988 100644
--- a/mineru/backend/flash/native_pdf/pipeline.py
+++ b/mineru/backend/flash/native_pdf/pipeline.py
@@ -55,6 +55,7 @@ from .graphics import (
_detect_strong_graphic_bboxes,
_form_supersedes_nested_bbox,
_select_form_image_bboxes,
+ _split_parallel_graphic_rule_rows,
)
from .formulas import (
_build_formula_like_blocks,
@@ -357,8 +358,31 @@ def _prepare_page_source(source: _PageSource) -> _PreparedPage:
| claimed_raster_line_indices
| claimed_vector_number_indices
)
+ remaining_lines = _split_parallel_graphic_rule_rows(
+ [
+ line
+ for line in source.lines
+ if line.source_index not in claimed_line_indices
+ ],
+ source.drawing_lines,
+ [
+ block["bbox"]
+ for block in (
+ form_image_blocks
+ + graphic_blocks
+ + raster_image_blocks
+ )
+ ],
+ table_bboxes,
+ source.page_size,
+ source_index_start=max(
+ (line.source_index for line in source.lines),
+ default=-1,
+ )
+ + 1,
+ )
remaining_lines = _merge_same_baseline_text_lines(
- [line for line in source.lines if line.source_index not in claimed_line_indices],
+ remaining_lines,
source.page_size,
table_bboxes,
)
diff --git a/tests/unittest/_flash_pdf_test_utils.py b/tests/unittest/_flash_pdf_test_utils.py
index 3ca6d10a..6208f820 100644
--- a/tests/unittest/_flash_pdf_test_utils.py
+++ b/tests/unittest/_flash_pdf_test_utils.py
@@ -22,6 +22,7 @@ def _text_line(
dominant_font_weight: float | None = None,
median_glyph_width: float | None = None,
leading_emphasis_width: float | None = None,
+ preserve_split_boundary: bool = False,
semantic_type: str | None = None,
) -> models._LineItem:
"""构造栏带、排版恢复与图形标签测试使用的原生文本行。"""
@@ -40,6 +41,7 @@ def _text_line(
median_glyph_width=median_glyph_width,
leading_emphasis_width=leading_emphasis_width,
split_from_row=split_from_row,
+ preserve_split_boundary=preserve_split_boundary,
semantic_type=semantic_type,
)
@@ -57,4 +59,3 @@ def _prepared_text_page(
drawing_lines=[],
fixed_blocks=[],
)
-
diff --git a/tests/unittest/test_flash_pdf_graphics.py b/tests/unittest/test_flash_pdf_graphics.py
index 43914527..9b7668ce 100644
--- a/tests/unittest/test_flash_pdf_graphics.py
+++ b/tests/unittest/test_flash_pdf_graphics.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import inspect
import pytest
@@ -51,6 +52,129 @@ def _path_info(
)
+def _parallel_rule_split_fixture(
+ *,
+ cross_gutter: bool = False,
+) -> tuple[
+ models._LineItem,
+ list[models._AxisLine],
+ list[tuple[float, float, float, float]],
+]:
+ """构造无语义文本、成对图形、独立横线和可选栏沟干扰字符。"""
+
+ left_chars = [
+ {
+ "char": "L",
+ "bbox": (
+ 100.0 + 40.0 * index,
+ 80.0,
+ 120.0 + 40.0 * index,
+ 89.0,
+ ),
+ }
+ for index in range(8)
+ ]
+ if cross_gutter:
+ left_chars[-1]["bbox"] = (475.0, 80.0, 505.0, 89.0)
+ right_chars = [
+ {
+ "char": "R",
+ "bbox": (
+ 540.0 + 40.0 * index,
+ 80.0,
+ 560.0 + 40.0 * index,
+ 89.0,
+ ),
+ }
+ for index in range(8)
+ ]
+ line = _text_line(
+ "LLLLLLLL RRRRRRRR",
+ (100.0, 80.0, 840.0, 89.0),
+ 7,
+ visual_row_id=12,
+ effective_height=10.0,
+ )
+ line.chars = [*left_chars, {"char": " ", "bbox": (400.0, 80.0, 540.0, 89.0)}, *right_chars]
+ rules = [
+ _drawing_axis_line("horizontal", (80.0, 90.0, 480.0, 90.5)),
+ _drawing_axis_line("horizontal", (520.0, 90.0, 920.0, 90.5)),
+ ]
+ images = [
+ (100.0, 100.0, 470.0, 300.0),
+ (530.0, 100.0, 900.0, 300.0),
+ ]
+ return line, rules, images
+
+
+def test_parallel_graphic_rule_rows_split_without_reading_text_content() -> None:
+ """验证完整空间证据把同一视觉行拆成左右两个受保护 run。"""
+
+ line, rules, images = _parallel_rule_split_fixture()
+
+ split = graphics._split_parallel_graphic_rule_rows(
+ [line],
+ rules,
+ images,
+ [],
+ (1000.0, 500.0),
+ source_index_start=50,
+ )
+
+ assert [(item.text, item.bbox) for item in split] == [
+ ("LLLLLLLL", (100.0, 80.0, 400.0, 89.0)),
+ ("RRRRRRRR", (540.0, 80.0, 840.0, 89.0)),
+ ]
+ assert [item.source_index for item in split] == [7, 50]
+ assert [item.run_index for item in split] == [0, 1]
+ assert {item.visual_row_id for item in split} == {12}
+ assert all(item.split_from_row for item in split)
+ assert all(item.preserve_split_boundary for item in split)
+
+ source = "\n".join(
+ inspect.getsource(function)
+ for function in (
+ graphics._parallel_graphic_rule_pairs,
+ graphics._parallel_graphic_row_split_boundary,
+ graphics._split_parallel_graphic_rule_rows,
+ )
+ )
+ assert ".text" not in source
+
+
+ "missing_evidence",
+ ["right_rule", "right_image", "independent_rules", "clear_gutter", "outside_table"],
+)
+def test_parallel_graphic_rule_rows_require_all_spatial_evidence(
+ missing_evidence: str,
+) -> None:
+ """验证横线、图形、栏沟或表格排除任一不成立时都不拆分。"""
+
+ line, rules, images = _parallel_rule_split_fixture(
+ cross_gutter=missing_evidence == "clear_gutter"
+ )
+ table_bboxes: list[tuple[float, float, float, float]] = []
+ if missing_evidence == "right_rule":
+ rules = rules[:1]
+ elif missing_evidence == "right_image":
+ images = images[:1]
+ elif missing_evidence == "independent_rules":
+ rules = [_drawing_axis_line("horizontal", (80.0, 90.0, 920.0, 90.5))]
+ elif missing_evidence == "outside_table":
+ table_bboxes = [(70.0, 70.0, 930.0, 310.0)]
+
+ output = graphics._split_parallel_graphic_rule_rows(
+ [line],
+ rules,
+ images,
+ table_bboxes,
+ (1000.0, 500.0),
+ )
+
+ assert output == [line]
+
+
def _graphic_source_fixture() -> models._PageSource:
"""构造双框图形、六个标签、拆分 caption 与邻近正文。"""
diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.py
index 5abcaf5a..7e2d172a 100644
--- a/tests/unittest/test_flash_pdf_samples.py
+++ b/tests/unittest/test_flash_pdf_samples.py
@@ -1110,6 +1110,52 @@ def test_caibao_table_reclaims_repeated_dates_but_keeps_real_marginals() -> None
assert tables_on_page[0]["bbox"][3] < page_numbers[0]["bbox"][1]
+def test_caibao_page2_parallel_chart_captions_stay_separate() -> None:
+ """验证第二页左右图表的同行图注按各自横线和图形范围独立输出。"""
+
+ model_list = _txt_model_list("caibao1.pdf")
+ page = model_list[1]
+ first_caption = [
+ block
+ for block in page
+ if block["type"] == "text"
+ and str(block["content"]).startswith("图表1")
+ ]
+ second_caption = [
+ block
+ for block in page
+ if block["type"] == "text"
+ and str(block["content"]).startswith("图表2")
+ ]
+
+ assert len(page) == 20
+ assert len(first_caption) == len(second_caption) == 1
+ assert first_caption[0]["type"] == second_caption[0]["type"] == "text"
+ assert first_caption[0]["bbox"] == [0.073, 0.235, 0.485, 0.245]
+ assert second_caption[0]["bbox"] == [0.513, 0.235, 0.843, 0.245]
+ assert "行业周涨幅" not in str(first_caption[0]["content"])
+ assert "汽车指数上周下跌" not in str(second_caption[0]["content"])
+ assert [
+ block["bbox"]
+ for block in page
+ if block["type"] == "image" and block["bbox"][1] < 0.5
+ ] == [
+ [0.082, 0.267, 0.48, 0.447],
+ [0.522, 0.254, 0.92, 0.46],
+ ]
+
+ page3_captions = [
+ block
+ for block in model_list[2]
+ if block["type"] == "text"
+ and str(block["content"]).startswith(("图表4", "图表5"))
+ ]
+ assert [block["bbox"] for block in page3_captions] == [
+ [0.073, 0.181, 0.298, 0.191],
+ [0.514, 0.181, 0.738, 0.191],
+ ]
+
+
def test_iebm_left_indented_compact_formula_is_equation() -> None:
"""验证右栏左缩进紧凑公式通过公共 auto 入口输出为 equation。"""
diff --git a/tests/unittest/test_flash_pdf_titles.py b/tests/unittest/test_flash_pdf_titles.py
index b8b958bc..e43a2713 100644
--- a/tests/unittest/test_flash_pdf_titles.py
+++ b/tests/unittest/test_flash_pdf_titles.py
@@ -974,6 +974,69 @@ def test_dense_same_font_two_run_row_requires_complete_high_occupancy_geometry()
assert len([line for line in merged if line.visual_row_id == 20]) == 2
+def test_preserved_split_boundary_blocks_dense_and_title_row_restoration() -> None:
+ """验证空间分栏保护同时阻止密集正文恢复和段落标题同行恢复。"""
+
+ body_font = ("Body", 0)
+ protected_members = [
+ _text_line(
+ "left",
+ (0.0, 0.0, 48.0, 10.0),
+ 0,
+ visual_row_id=10,
+ run_index=0,
+ split_from_row=True,
+ preserve_split_boundary=True,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "right",
+ (52.0, 0.0, 100.0, 10.0),
+ 1,
+ visual_row_id=10,
+ run_index=1,
+ split_from_row=True,
+ preserve_split_boundary=True,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ ]
+
+ assert not line_merging._is_dense_same_font_two_run_row(
+ protected_members,
+ (100.0, 100.0),
+ )
+ assert not line_merging._can_restore_dense_split_visual_row(
+ protected_members,
+ (100.0, 100.0),
+ [],
+ {0: (0, 0), 1: (0, 0)},
+ )
+
+ protected_titles = [
+ _text_line(
+ member.text,
+ member.bbox,
+ member.source_index,
+ visual_row_id=member.visual_row_id,
+ run_index=member.run_index,
+ split_from_row=True,
+ preserve_split_boundary=True,
+ font_signature=body_font,
+ font_coverage=1.0,
+ semantic_type="paragraph_title",
+ )
+ for member in protected_members
+ ]
+ merged = line_merging._merge_title_resolved_visual_rows(
+ protected_titles,
+ (100.0, 100.0),
+ )
+
+ assert [line.text for line in merged] == ["left", "right"]
+
+
def test_paragraph_title_detector_does_not_read_line_text() -> None:
"""守卫段落标题候选、打分和邻行扩展不读取文本内容。"""