Skip to content

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

fix: repair sparse glyph geometry in inflated runs

myhloli09b59422b3fa
diff --git a/mineru/model/flash/pdf/char_geometry.py b/mineru/model/flash/pdf/char_geometry.py
index cdb038e7..b42cacc0 100644
--- a/mineru/model/flash/pdf/char_geometry.py
+++ b/mineru/model/flash/pdf/char_geometry.py
@@ -57,6 +57,7 @@ STYLE_TIER_MIN_MEMBER_SHARE = 0.15
STYLE_TIER_GAP_RATIO = 1.35
RunKey: TypeAlias = tuple[str, float, int, int, int, str]
+XRunStyleKey: TypeAlias = tuple[str, float, int, int, int]
LineKey: TypeAlias = tuple[int, int]
CharKey: TypeAlias = tuple[int, int]
LooseTierSample: TypeAlias = tuple[float, float, float, float]
@@ -262,12 +263,7 @@ def _line_loose_tier_offsets(
)
tiers: list[list[tuple[float, float, float, float, float]]] = []
for item in normalized:
- if (
- not tiers
- or item[0]
- > STYLE_TIER_GAP_RATIO
- * statistics.median(member[0] for member in tiers[-1])
- ):
+ if not tiers or item[0] > STYLE_TIER_GAP_RATIO * statistics.median(member[0] for member in tiers[-1]):
tiers.append([item])
else:
tiers[-1].append(item)
@@ -279,35 +275,19 @@ def _line_loose_tier_offsets(
STYLE_TIER_MIN_MEMBER_COUNT,
math.ceil(STYLE_TIER_MIN_MEMBER_SHARE * len(samples)),
)
- if (
- len(upper_tier) < minimum_members
- or len(reference_tier) < STYLE_TIER_MIN_MEMBER_COUNT
- ):
+ if len(upper_tier) < minimum_members or len(reference_tier) < STYLE_TIER_MIN_MEMBER_COUNT:
return None
upper_loose_heights = [member[1] + member[2] for member in upper_tier]
upper_font_sizes = [member[4] for member in upper_tier if member[4] > 0]
upper_tight_heights = [member[3] for member in upper_tier]
- if (
- statistics.median(upper_loose_heights)
- <= STYLE_INFLATION_LOOSE_FONT_RATIO
- * (
- statistics.median(upper_font_sizes)
- if upper_font_sizes
- else 0.0
- )
- or statistics.median(upper_loose_heights)
- <= STYLE_INFLATION_LOOSE_TIGHT_RATIO
- * max(0.1, _quantile(upper_tight_heights, 0.75))
+ if statistics.median(upper_loose_heights) <= STYLE_INFLATION_LOOSE_FONT_RATIO * (
+ statistics.median(upper_font_sizes) if upper_font_sizes else 0.0
+ ) or statistics.median(upper_loose_heights) <= STYLE_INFLATION_LOOSE_TIGHT_RATIO * max(
+ 0.1, _quantile(upper_tight_heights, 0.75)
):
return None
- reference_ascent_ratios = [
- member[1] / max(member[4], member[3], 0.1)
- for member in reference_tier
- ]
- reference_descent_ratios = [
- member[2] / max(member[4], member[3], 0.1)
- for member in reference_tier
- ]
+ reference_ascent_ratios = [member[1] / max(member[4], member[3], 0.1) for member in reference_tier]
+ reference_descent_ratios = [member[2] / max(member[4], member[3], 0.1) for member in reference_tier]
return (
statistics.median(reference_ascent_ratios) * em_height,
statistics.median(reference_descent_ratios) * em_height,
@@ -781,36 +761,15 @@ def _apply_style_scale_repairs(
if not style_inflated_runs:
return
for line_key, line_samples in by_line.items():
- anchor_samples = [
- sample
- for sample in line_samples
- if sample.is_anchor
- ]
+ anchor_samples = [sample for sample in line_samples if sample.is_anchor]
if not anchor_samples:
continue
- dominant_run = Counter(
- sample.run_key
- for sample in anchor_samples
- ).most_common(1)[0][0]
- style_samples = [
- sample
- for sample in anchor_samples
- if sample.run_key == dominant_run
- ]
- font_sizes = [
- sample.font_size
- for sample in style_samples
- if sample.font_size > 0
- ]
- tight_heights = [
- sample.local_tight_bbox[3]
- - sample.local_tight_bbox[1]
- for sample in style_samples
- ]
+ dominant_run = Counter(sample.run_key for sample in anchor_samples).most_common(1)[0][0]
+ style_samples = [sample for sample in anchor_samples if sample.run_key == dominant_run]
+ font_sizes = [sample.font_size for sample in style_samples if sample.font_size > 0]
+ tight_heights = [sample.local_tight_bbox[3] - sample.local_tight_bbox[1] for sample in style_samples]
style_scale = max(
- statistics.median(font_sizes)
- if font_sizes
- else 0.0,
+ statistics.median(font_sizes) if font_sizes else 0.0,
_quantile(tight_heights, 0.75),
0.1,
)
@@ -831,10 +790,7 @@ def _line_uses_repaired_style_scale(
style_size = round(line.em_height * 4.0) / 4.0
flags = line.font_signature[1]
return any(
- family == run_key[0]
- and abs(style_size - run_key[1]) <= 0.25
- and flags == run_key[2]
- and line.angle == run_key[4]
+ family == run_key[0] and abs(style_size - run_key[1]) <= 0.25 and flags == run_key[2] and line.angle == run_key[4]
for run_key in style_inflated_runs
)
@@ -883,14 +839,210 @@ def _next_compatible_sample(
return None
+def _x_run_style_key(run_key: RunKey) -> XRunStyleKey:
+ """移除文字类别,返回字体样式和方向一致的 X 修复分组键。"""
+
+ return run_key[:5]
+
+
+def _samples_share_baseline(first: _CharSample, second: _CharSample) -> bool:
+ """按 tight 字形高度判断两个字符是否位于同一局部基线。"""
+
+ tight_height = max(
+ first.local_tight_bbox[3] - first.local_tight_bbox[1],
+ second.local_tight_bbox[3] - second.local_tight_bbox[1],
+ )
+ return abs(first.local_origin[1] - second.local_origin[1]) <= max(0.5, 0.25 * tight_height)
+
+
+def _next_style_compatible_sample(
+ current: _CharSample,
+ line_samples: list[_CharSample],
+) -> _CharSample | None:
+ """返回同字体样式、同基线且 origin 正向的下一可见字符。"""
+
+ style_key = _x_run_style_key(current.run_key)
+ for candidate in line_samples:
+ if (
+ candidate.position <= current.position
+ or _x_run_style_key(candidate.run_key) != style_key
+ or not _samples_share_baseline(current, candidate)
+ ):
+ continue
+ if candidate.local_origin[0] - current.local_origin[0] > 0.1:
+ return candidate
+ return None
+
+
+def _build_x_char_repair(
+ sample: _CharSample,
+ donor_run: _RunStats,
+ next_sample: _CharSample | None,
+ page_size: tuple[float, float],
+ *,
+ left_bearing: float,
+ use_donor_advance: bool = False,
+) -> CharLayoutGeometry | None:
+ """用可靠 run 的 advance 为单个字符构造包含 tight 字形的 X-only 修复。"""
+
+ advance = (
+ donor_run.median_advance
+ if use_donor_advance or next_sample is None
+ else next_sample.local_origin[0] - sample.local_origin[0]
+ )
+ if advance is None or advance <= 0:
+ return None
+ left = min(sample.local_tight_bbox[0], sample.local_origin[0] + left_bearing)
+ right = max(sample.local_tight_bbox[2], sample.local_origin[0] + advance)
+ if next_sample is not None:
+ right = min(right, max(sample.local_tight_bbox[2], next_sample.local_origin[0]))
+ if right <= left:
+ return None
+ layout_bbox = _clip_bbox(
+ _rotate_bbox_from_upright(
+ (
+ left,
+ sample.local_source_bbox[1],
+ right,
+ sample.local_source_bbox[3],
+ ),
+ page_size,
+ sample.line.angle,
+ ),
+ page_size,
+ )
+ if layout_bbox is None:
+ return None
+ return CharLayoutGeometry(
+ source_bbox=sample.source_bbox,
+ tight_bbox=sample.tight_bbox,
+ origin=sample.origin,
+ layout_bbox=layout_bbox,
+ ink_bbox=sample.tight_bbox,
+ baseline=sample.local_origin[1],
+ advance=advance,
+ em_height=max(
+ sample.font_size,
+ sample.local_tight_bbox[3] - sample.local_tight_bbox[1],
+ ),
+ x_state="abnormal",
+ y_state="healthy",
+ confidence=min(1.0, len(donor_run.pair_ratios) / X_RELIABLE_PAIR_MIN),
+ )
+
+
+def _x_repair_donors(
+ runs: dict[RunKey, _RunStats],
+) -> dict[XRunStyleKey, _RunStats]:
+ """按强异常、pair 数和完整键稳定选择各字体样式的 X 修复 donor。"""
+
+ donors: dict[XRunStyleKey, _RunStats] = {}
+ candidates = [
+ run
+ for run in runs.values()
+ if (run.strong_x_bad or run.sibling_x_bad) and run.median_advance is not None and run.median_advance > 0
+ ]
+ for run in sorted(
+ candidates,
+ key=lambda item: (
+ not item.strong_x_bad,
+ -len(item.pair_ratios),
+ item.key,
+ ),
+ ):
+ donors.setdefault(_x_run_style_key(run.key), run)
+ return donors
+
+
+def _has_adjacent_repaired_style_sample(
+ sample: _CharSample,
+ line_samples: list[_CharSample],
+ page_index: int,
+ plan: DocumentGeometryPlan,
+) -> bool:
+ """确认稀疏字符紧邻同字体样式、同基线且已完成 X 修复的字符。"""
+
+ style_key = _x_run_style_key(sample.run_key)
+ return any(
+ abs(candidate.position - sample.position) == 1
+ and _x_run_style_key(candidate.run_key) == style_key
+ and _samples_share_baseline(sample, candidate)
+ and (page_index, candidate.char_idx) in plan.char_repairs
+ for candidate in line_samples
+ )
+
+
+def _sparse_x_repair_affects_layout(
+ sample: _CharSample,
+ line_samples: list[_CharSample],
+ donor_run: _RunStats,
+ page_size: tuple[float, float],
+) -> bool:
+ """仅接纳撑开行尾或遮蔽跨栏硬间隙的稀疏字符 X 异常。"""
+
+ donor_advance = donor_run.median_advance
+ if donor_advance is None or donor_advance <= 0:
+ return False
+ tight_width = max(
+ 0.1,
+ sample.local_tight_bbox[2] - sample.local_tight_bbox[0],
+ )
+ canonical_width = max(donor_advance, tight_width)
+ source_width = sample.local_source_bbox[2] - sample.local_source_bbox[0]
+ canonical_right = max(
+ sample.local_tight_bbox[2],
+ sample.local_origin[0] + donor_advance,
+ )
+ local_page_width = page_size[1] if sample.line.angle in {90, 270} else page_size[0]
+ forward_overhang = sample.local_source_bbox[2] - canonical_right
+ if source_width <= X_STRONG_RATIO_THRESHOLD * canonical_width or forward_overhang < max(
+ 2.0 * canonical_width, 0.01 * local_page_width
+ ):
+ return False
+
+ baseline_samples = sorted(
+ (
+ candidate
+ for candidate in line_samples
+ if candidate.position != sample.position and _samples_share_baseline(sample, candidate)
+ ),
+ key=lambda candidate: candidate.position,
+ )
+ following = next(
+ (candidate for candidate in baseline_samples if candidate.position > sample.position),
+ None,
+ )
+ if following is None:
+ right_edge = max(candidate.local_source_bbox[2] for candidate in [sample, *baseline_samples])
+ return abs(sample.local_source_bbox[2] - right_edge) <= 0.25
+
+ median_tight_width = statistics.median(
+ max(
+ 0.1,
+ candidate.local_tight_bbox[2] - candidate.local_tight_bbox[0],
+ )
+ for candidate in [sample, *baseline_samples]
+ )
+ hard_gap_threshold = max(
+ 15.0,
+ 3.0 * median_tight_width,
+ 0.02 * local_page_width,
+ )
+ return (
+ following.local_tight_bbox[0] - canonical_right >= hard_gap_threshold
+ and sample.local_source_bbox[2] >= following.local_origin[0]
+ )
+
+
def _repair_x_chars(
plan: DocumentGeometryPlan,
runs: dict[RunKey, _RunStats],
by_line: dict[LineKey, list[_CharSample]],
page_sizes: list[tuple[float, float]],
) -> None:
- """仅对强异常或同族高置信 run 重建前进方向 cell。"""
+ """对强异常 run 及其同样式稀疏邻接字符重建前进方向 cell。"""
+ donors = _x_repair_donors(runs)
for line_key, line_samples in by_line.items():
page_index, _source_index = line_key
page_size = page_sizes[page_index]
@@ -899,41 +1051,49 @@ def _repair_x_chars(
if not (run.strong_x_bad or run.sibling_x_bad):
continue
next_sample = _next_compatible_sample(sample, line_samples)
- advance = next_sample.local_origin[0] - sample.local_origin[0] if next_sample is not None else run.median_advance
- if advance is None or advance <= 0:
- continue
- left_bearing = run.median_tight_left_bearing
- left = min(sample.local_tight_bbox[0], sample.local_origin[0] + left_bearing)
- right = max(sample.local_tight_bbox[2], sample.local_origin[0] + advance)
- if next_sample is not None:
- right = min(right, max(sample.local_tight_bbox[2], next_sample.local_origin[0]))
- if right <= left:
- continue
- local_layout = (
- left,
- sample.local_source_bbox[1],
- right,
- sample.local_source_bbox[3],
- )
- layout_bbox = _clip_bbox(
- _rotate_bbox_from_upright(local_layout, page_size, sample.line.angle),
+ repair = _build_x_char_repair(
+ sample,
+ run,
+ next_sample,
page_size,
+ left_bearing=run.median_tight_left_bearing,
)
- if layout_bbox is None:
+ if repair is None:
continue
- plan.char_repairs[(page_index, sample.char_idx)] = CharLayoutGeometry(
- source_bbox=sample.source_bbox,
- tight_bbox=sample.tight_bbox,
- origin=sample.origin,
- layout_bbox=layout_bbox,
- ink_bbox=sample.tight_bbox,
- baseline=sample.local_origin[1],
- advance=advance,
- em_height=max(sample.font_size, sample.local_tight_bbox[3] - sample.local_tight_bbox[1]),
- x_state="abnormal",
- y_state="healthy",
- confidence=min(1.0, len(run.pair_ratios) / X_RELIABLE_PAIR_MIN),
+ plan.char_repairs[(page_index, sample.char_idx)] = repair
+
+ for sample in line_samples:
+ char_key = (page_index, sample.char_idx)
+ run = runs[sample.run_key]
+ donor = donors.get(_x_run_style_key(sample.run_key))
+ if (
+ char_key in plan.char_repairs
+ or donor is None
+ or len(run.pair_ratios) >= X_SIBLING_PAIR_MIN
+ or not _has_adjacent_repaired_style_sample(
+ sample,
+ line_samples,
+ page_index,
+ plan,
+ )
+ or not _sparse_x_repair_affects_layout(
+ sample,
+ line_samples,
+ donor,
+ page_size,
+ )
+ ):
+ continue
+ repair = _build_x_char_repair(
+ sample,
+ donor,
+ _next_style_compatible_sample(sample, line_samples),
+ page_size,
+ left_bearing=sample.local_tight_bbox[0] - sample.local_origin[0],
+ use_donor_advance=True,
)
+ if repair is not None:
+ plan.char_repairs[char_key] = repair
def _baseline_clusters(samples: list[_CharSample]) -> tuple[list[list[_CharSample]], float]:
@@ -1235,16 +1395,13 @@ def _repair_y_lines(
(
max(
0.0,
- sample.local_origin[1]
- - sample.local_source_bbox[1],
+ sample.local_origin[1] - sample.local_source_bbox[1],
),
max(
0.0,
- sample.local_source_bbox[3]
- - sample.local_origin[1],
+ sample.local_source_bbox[3] - sample.local_origin[1],
),
- sample.local_tight_bbox[3]
- - sample.local_tight_bbox[1],
+ sample.local_tight_bbox[3] - sample.local_tight_bbox[1],
sample.font_size,
)
for sample in analysis.dominant
@@ -1303,11 +1460,7 @@ def _repair_y_lines(
continue
current.layout_bbox = layout_bbox
current.ink_bbox = _bbox_union_many([sample.tight_bbox for sample in analysis.samples])
- current.em_height = (
- analysis.line.effective_height
- if preserve_legacy_typography
- else max(0.1, bottom - top)
- )
+ current.em_height = analysis.line.effective_height if preserve_legacy_typography else max(0.1, bottom - top)
current.state = "repair_xy" if current.state == "repair_x" else "trim_y"
current.confidence = min(analysis.geometry_coverage, analysis.dominant_share)
current.y_intrusion_ratio = analysis.intrusion_ratio
@@ -1496,11 +1649,7 @@ def apply_line_geometry_repairs(
line.geometry_confidence = repair.confidence
line.split_y_candidate = repair.split_y_candidate
line.bbox = layout
- line.em_height = (
- style_scale
- if style_scale is not None
- else repair.em_height
- )
+ line.em_height = style_scale if style_scale is not None else repair.em_height
if allow_y_trim and repair.state in {"trim_y", "repair_xy"}:
line.effective_height = repair.em_height
diff --git a/tests/unittest/test_flash_pdf_char_geometry.py b/tests/unittest/test_flash_pdf_char_geometry.py
index afa51df0..ef072c9e 100644
--- a/tests/unittest/test_flash_pdf_char_geometry.py
+++ b/tests/unittest/test_flash_pdf_char_geometry.py
@@ -12,6 +12,7 @@ from mineru.model.flash.pdf.char_geometry import (
)
from mineru.model.flash.pdf.document import PDFPageTextGeometry
from mineru.model.flash.pdf.models import _LineItem
+from mineru.model.flash.pdf.native_text import _resplit_native_visual_runs
_PDF_FIXTURE_XOR_KEY = b"MinerU flash layout fixture"
@@ -124,27 +125,12 @@ def test_two_page_repeated_loose_height_inflation_sets_canonical_em_scale() -> N
line, geometry = _line_fixture(
source_index=line_index,
baseline=baseline,
- loose_top=(
- baseline - 20.0
- if anomalous
- else baseline - 8.0
- ),
- loose_bottom=(
- baseline + 4.0
- if anomalous
- else baseline + 2.0
- ),
+ loose_top=(baseline - 20.0 if anomalous else baseline - 8.0),
+ loose_bottom=(baseline + 4.0 if anomalous else baseline + 2.0),
loose_width=12.0 if anomalous else 6.0,
- font_name=(
- "ABCDEF+Anomaly"
- if anomalous
- else "ABCDEF+Normal"
- ),
+ font_name=("ABCDEF+Anomaly" if anomalous else "ABCDEF+Normal"),
font_size=10.0,
- start_char_idx=(
- page_index * 10000
- + line_index * 100
- ),
+ start_char_idx=(page_index * 10000 + line_index * 100),
)
page_lines.append(line)
page_geometries.append(geometry)
@@ -173,8 +159,7 @@ def test_line_loose_tier_shrinks_repeated_largest_tier_to_second_tier() -> None:
"""验证同行重复最大 loose 档按次档的归一化 ascent/descent 回缩。"""
offsets = _line_loose_tier_offsets(
- [(8.0, 2.0, 7.0, 10.0)] * 6
- + [(20.0, 4.0, 7.0, 10.0)] * 4,
+ [(8.0, 2.0, 7.0, 10.0)] * 6 + [(20.0, 4.0, 7.0, 10.0)] * 4,
10.0,
)
@@ -185,8 +170,7 @@ def test_line_loose_tier_preserves_legitimate_mixed_font_sizes() -> None:
"""验证按各自 em 归一化后相同的真实混合字号不会形成异常高度档。"""
offsets = _line_loose_tier_offsets(
- [(8.0, 2.0, 7.0, 10.0)] * 4
- + [(16.0, 4.0, 14.0, 20.0)] * 4,
+ [(8.0, 2.0, 7.0, 10.0)] * 4 + [(16.0, 4.0, 14.0, 20.0)] * 4,
10.0,
)
@@ -197,8 +181,7 @@ def test_line_loose_tier_ignores_single_large_outlier() -> None:
"""验证单个 loose 高度离群字符不足以触发整行档位回缩。"""
offsets = _line_loose_tier_offsets(
- [(8.0, 2.0, 7.0, 10.0)] * 7
- + [(20.0, 4.0, 7.0, 10.0)],
+ [(8.0, 2.0, 7.0, 10.0)] * 7 + [(20.0, 4.0, 7.0, 10.0)],
10.0,
)
@@ -220,6 +203,161 @@ def test_strong_x_run_repairs_advance_and_contains_tight_bbox() -> None:
assert plan.line_repairs[(0, 0)].state == "repair_x"
+def test_sparse_trailing_punctuation_inherits_confirmed_x_repair() -> None:
+ """验证异常字体 run 尾部的稀疏标点使用同样式 donor 收紧字符和行框。"""
+
+ line, geometry = _line_fixture(
+ source_index=0,
+ baseline=20.0,
+ count=40,
+ loose_width=12.0,
+ )
+ punctuation = line.chars[-1]
+ char_idx = int(punctuation["char_idx"])
+ origin_x, origin_y = geometry.origins[char_idx]
+ inflated_bbox = (origin_x, 12.0, 400.0, 22.0)
+ punctuation["char"] = "."
+ punctuation["bbox"] = inflated_bbox
+ geometry.loose_bboxes[char_idx] = inflated_bbox
+ line.text = f"{line.text[:-1]}."
+ line.bbox = (line.bbox[0], line.bbox[1], 400.0, line.bbox[3])
+
+ plan = build_document_geometry_plan(
+ [[line]],
+ [geometry],
+ [(500.0, 100.0)],
+ )
+
+ punctuation_repair = plan.char_repairs[(0, char_idx)]
+ assert punctuation_repair.origin == (origin_x, origin_y)
+ assert punctuation_repair.layout_bbox == (244.5, 12.0, 250.0, 22.0)
+ assert punctuation_repair.layout_bbox[2] >= punctuation_repair.tight_bbox[2]
+ assert plan.line_repairs[(0, 0)].layout_bbox == (10.5, 12.0, 250.0, 22.0)
+ assert plan.line_repairs[(0, 0)].repaired_char_count == 40
+
+
+def test_sparse_punctuation_fallback_requires_inflated_matching_style() -> None:
+ """验证正常宽标点和不同字体标点不会仅因邻近异常字母而继承 donor。"""
+
+ normal, normal_geometry = _line_fixture(
+ source_index=0,
+ baseline=20.0,
+ count=40,
+ loose_width=12.0,
+ )
+ normal_punctuation = normal.chars[-1]
+ normal_idx = int(normal_punctuation["char_idx"])
+ normal_origin_x, _normal_origin_y = normal_geometry.origins[normal_idx]
+ normal_bbox = (normal_origin_x, 12.0, normal_origin_x + 6.0, 22.0)
+ normal_punctuation["char"] = "."
+ normal_punctuation["bbox"] = normal_bbox
+ normal_geometry.loose_bboxes[normal_idx] = normal_bbox
+ normal.text = f"{normal.text[:-1]}."
+ normal.bbox = (normal.bbox[0], normal.bbox[1], normal_bbox[2], normal.bbox[3])
+
+ different, different_geometry = _line_fixture(
+ source_index=1,
+ baseline=40.0,
+ count=40,
+ loose_width=12.0,
+ start_char_idx=100,
+ )
+ different_punctuation = different.chars[-1]
+ different_idx = int(different_punctuation["char_idx"])
+ different_origin_x, _different_origin_y = different_geometry.origins[different_idx]
+ different_bbox = (different_origin_x, 32.0, 400.0, 42.0)
+ different_punctuation["char"] = "."
+ different_punctuation["bbox"] = different_bbox
+ different_punctuation["font"] = {
+ "name": "ABCDEF+Different",
+ "flags": 0,
+ "size": 10.0,
+ "weight": 400,
+ }
+ different_geometry.loose_bboxes[different_idx] = different_bbox
+ different.text = f"{different.text[:-1]}."
+ different.bbox = (different.bbox[0], different.bbox[1], 400.0, different.bbox[3])
+
+ plan = build_document_geometry_plan(
+ [[normal, different]],
+ [_merge_geometries(normal_geometry, different_geometry)],
+ [(500.0, 100.0)],
+ )
+
+ assert (0, normal_idx) not in plan.char_repairs
+ assert (0, different_idx) not in plan.char_repairs
+
+
+def test_sparse_column_boundary_glyph_repair_restores_native_resplit() -> None:
+ """验证左栏末尾稀疏字符修复后恢复栏沟,使生产重切生成唯一来源序号。"""
+
+ line, geometry = _line_fixture(
+ source_index=0,
+ baseline=20.0,
+ count=80,
+ loose_width=12.0,
+ )
+ for position in range(40, 80):
+ char = line.chars[position]
+ char_idx = int(char["char_idx"])
+ source_bbox = geometry.loose_bboxes[char_idx]
+ tight_bbox = geometry.tight_bboxes[char_idx]
+ origin = geometry.origins[char_idx]
+ shifted_source = (
+ source_bbox[0] + 100.0,
+ source_bbox[1],
+ source_bbox[2] + 100.0,
+ source_bbox[3],
+ )
+ char["bbox"] = shifted_source
+ char["font"] = {
+ "name": "ABCDEF+RightColumn",
+ "flags": 0,
+ "size": 10.0,
+ "weight": 400,
+ }
+ geometry.loose_bboxes[char_idx] = shifted_source
+ geometry.tight_bboxes[char_idx] = (
+ tight_bbox[0] + 100.0,
+ tight_bbox[1],
+ tight_bbox[2] + 100.0,
+ tight_bbox[3],
+ )
+ geometry.origins[char_idx] = (origin[0] + 100.0, origin[1])
+ punctuation = line.chars[39]
+ punctuation_idx = int(punctuation["char_idx"])
+ punctuation_origin_x, _punctuation_origin_y = geometry.origins[punctuation_idx]
+ punctuation_bbox = (punctuation_origin_x, 12.0, geometry.origins[40][0], 22.0)
+ punctuation["char"] = "."
+ punctuation["bbox"] = punctuation_bbox
+ geometry.loose_bboxes[punctuation_idx] = punctuation_bbox
+ line.text = f"{line.text[:39]}.{line.text[40:]}"
+ line.bbox = (10.0, 12.0, max(bbox[2] for bbox in geometry.loose_bboxes.values()), 22.0)
+
+ plan = build_document_geometry_plan(
+ [[line]],
+ [geometry],
+ [(700.0, 100.0)],
+ )
+ apply_line_geometry_repairs(
+ [line],
+ page_index=0,
+ plan=plan,
+ allow_y_trim=False,
+ )
+ members, resplits = _resplit_native_visual_runs(
+ [line],
+ (700.0, 100.0),
+ {char_idx: repair.layout_bbox for (page_index, char_idx), repair in plan.char_repairs.items() if page_index == 0},
+ source_index_start=1,
+ )
+
+ assert (0, punctuation_idx) in plan.char_repairs
+ assert [member.source_index for member in members] == [0, 1]
+ assert [member.text for member in members] == [line.text[:40], line.text[40:]]
+ assert list(resplits) == [0]
+
+
def test_short_rows_accumulate_enough_pairs_for_strong_x_repair() -> None:
"""验证大量三字符短行仍可累计文档级 X 异常证据。"""
@@ -376,8 +514,7 @@ def test_zero_rotation_ignores_untrusted_loose_side_map_shadow() -> None:
line, geometry = _line_fixture(source_index=0, baseline=20.0, loose_width=6.0)
perturbed = {
- char_idx: (bbox[0], bbox[1] - 20.0, bbox[2] + 30.0, bbox[3] + 20.0)
- for char_idx, bbox in geometry.loose_bboxes.items()
+ char_idx: (bbox[0], bbox[1] - 20.0, bbox[2] + 30.0, bbox[3] + 20.0) for char_idx, bbox in geometry.loose_bboxes.items()
}
geometry.loose_bboxes.clear()
geometry.loose_bboxes.update(perturbed)
@@ -394,10 +531,7 @@ def test_rotated_char_rejects_implausible_side_map_x_expansion() -> None:
line, geometry = _line_fixture(source_index=0, baseline=20.0, loose_width=6.0)
for char in line.chars:
char["rotation"] = 0.2
- perturbed = {
- char_idx: (bbox[0], bbox[1], bbox[2] + 30.0, bbox[3])
- for char_idx, bbox in geometry.loose_bboxes.items()
- }
+ perturbed = {char_idx: (bbox[0], bbox[1], bbox[2] + 30.0, bbox[3]) for char_idx, bbox in geometry.loose_bboxes.items()}
geometry.loose_bboxes.clear()
geometry.loose_bboxes.update(perturbed)
@@ -556,16 +690,8 @@ def test_versioned_mixed_text_fixture_keeps_normal_geometry_identity() -> None:
(20, "4 常见问题"),
(22, "5 附录:"),
):
- section = next(
- block
- for block in pages[page_index]
- if section_text in str(block.get("content"))
- )
- security = next(
- block
- for block in pages[page_index]
- if "文档密级:秘密" in str(block.get("content"))
- )
+ section = next(block for block in pages[page_index] if section_text in str(block.get("content")))
+ security = next(block for block in pages[page_index] if "文档密级:秘密" in str(block.get("content")))
assert section["type"] == "paragraph_title"
assert security["type"] == "header"