RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(deepdoc): preserve complete PDF figures (#21390)
jay77721fd675ebde21a
diff --git a/internal/deepdoc/parser/pdf/figure_bounds_integration_test.go b/internal/deepdoc/parser/pdf/figure_bounds_integration_test.gonew file mode 100644index 0000000000..5273402cb7--- /dev/null+++ b/internal/deepdoc/parser/pdf/figure_bounds_integration_test.go@@ -0,0 +1,54 @@+//go:build cgo && integration++package pdf++import (+ "fmt"+ "os"+ "testing"++ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++// TestAN0590FigureBounds exercises complete DLA figure bounds on the real PDF.+// AN0590_PDF points to an0590scv140.pdf; MODEL_DIR supplies DeepDOC models.+func TestAN0590FigureBounds(t *testing.T) {+ path := os.Getenv("AN0590_PDF")+ if path == "" {+ t.Skip("AN0590_PDF is required")+ }+ analyzer := mustConnectInProcessAnalyzer(t)+ data, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ cfg := pdf.DefaultParserConfig()+ result, err := NewParser(cfg).Parse(t.Context(), data, analyzer)+ if err != nil {+ t.Fatal(err)+ }+ defer result.Close()+ for _, tc := range []struct {+ number, page int+ minWidth, minHeight float64+ }{+ {2, 1, 240, 50}, {3, 3, 350, 100},+ } {+ t.Run(fmt.Sprintf("figure%d", tc.number), func(t *testing.T) {+ var position *pdf.Position+ for _, section := range result.Figures() {+ for i := range section.Positions {+ pos := §ion.Positions[i]+ if len(pos.PageNumbers) == 1 && pos.PageNumbers[0] == tc.page &&+ pos.Right-pos.Left >= tc.minWidth && pos.Bottom-pos.Top >= tc.minHeight {+ position = pos+ break+ }+ }+ }+ if position == nil {+ t.Fatalf("figure %d missing", tc.number)+ }+ })+ }+}diff --git a/internal/deepdoc/parser/pdf/figure_bounds_test.go b/internal/deepdoc/parser/pdf/figure_bounds_test.gonew file mode 100644index 0000000000..faaaa604ba--- /dev/null+++ b/internal/deepdoc/parser/pdf/figure_bounds_test.go@@ -0,0 +1,46 @@+package pdf++import (+ "image"+ "testing"++ tbl "ragflow/internal/deepdoc/parser/pdf/table"+ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++func TestEnrichFigureBoundsUseRenderScale(t *testing.T) {+ analyzer := &MockDocAnalyzer{Healthy: true, DLARegions: []pdf.DLARegion{+ {X0: 90, Y0: 180, X1: 810, Y1: 540, Label: "figure", Confidence: 0.95},+ }}+ boxes := []pdf.TextBox{{Text: "SDA", X0: 5, X1: 25, Top: 30, Bottom: 35}}+ p := NewParser(pdf.DefaultParserConfig())+ annotated, _, _ := p.enrichOnePageWithDeepDoc(t.Context(), image.NewRGBA(image.Rect(0, 0, 900, 900)), boxes, 4, nil, analyzer, NewTableBuilderFor(analyzer), 9)+ if len(annotated) != 2 || annotated[0].X0 != 5 || annotated[0].X1 != 25 {+ t.Fatalf("annotation changed native text geometry: %+v", annotated)+ }+ figures := tbl.ConsolidateFigures(annotated)+ if len(figures) != 1 || figures[0].PageNumber != 4 || figures[0].X0 != 10 || figures[0].X1 != 90 || figures[0].Top != 20 || figures[0].Bottom != 60 {+ t.Fatalf("figure did not retain retry-render scale or page: %+v", figures)+ }+}++func TestParsePreservesFigureWithoutText(t *testing.T) {+ engine := &MockEngine{NumPages: 4, RenderW: 900, RenderH: 900}+ analyzer := &MockDocAnalyzer{Healthy: true, DLARegions: []pdf.DLARegion{+ {X0: 90, Y0: 180, X1: 810, Y1: 540, Label: "figure", Confidence: 0.95},+ }}+ cfg := pdf.DefaultParserConfig()+ cfg.Pages = [][]int{{4, 4}}+ result, err := NewParser(cfg).ParseRaw(t.Context(), engine, analyzer)+ if err != nil {+ t.Fatal(err)+ }+ figures := result.Figures()+ if len(figures) != 1 || figures[0].Text != "" {+ t.Fatalf("textless figure lost or fabricated text: %+v", result.Sections)+ }+ pos := figures[0].Positions[0]+ if len(pos.PageNumbers) != 1 || pos.PageNumbers[0] != 3 || pos.Left != 30 || pos.Right != 270 || pos.Top != 60 || pos.Bottom != 180 {+ t.Fatalf("textless figure geometry lost: %+v", pos)+ }+}diff --git a/internal/deepdoc/parser/pdf/layout/boxes_sections.go b/internal/deepdoc/parser/pdf/layout/boxes_sections.goindex bfc5dc3082..cdbe865cc6 100644--- a/internal/deepdoc/parser/pdf/layout/boxes_sections.go+++ b/internal/deepdoc/parser/pdf/layout/boxes_sections.go@@ -55,7 +55,7 @@ func BoxesToSections(boxes []pdf.TextBox, pageHeights map[int]float64) []pdf.Secsections := make([]pdf.Section, 0, len(boxes))for _, b := range boxes {t := strings.TrimSpace(b.Text)- if t == "" {+ if t == "" && b.LayoutType != pdf.LayoutTypeFigure {continue}diff --git a/internal/deepdoc/parser/pdf/layout/layout.go b/internal/deepdoc/parser/pdf/layout/layout.goindex 9708d228bc..79946f5cd2 100644--- a/internal/deepdoc/parser/pdf/layout/layout.go+++ b/internal/deepdoc/parser/pdf/layout/layout.go@@ -554,6 +554,9 @@ func groupBoxesByPage(boxes []pdf.TextBox) (map[int][]int, []int) {// shouldMergeBoxes determines whether two boxes should be mergedfunc shouldMergeBoxes(prev, curr *pdf.TextBox, mh, mw float64, isEnglish bool) bool {+ if prev.LayoutType == pdf.LayoutTypeFigure || curr.LayoutType == pdf.LayoutTypeFigure {+ return false+ }// Check layout numberif prev.LayoutNo != curr.LayoutNo {common.Debug("vm reject", zap.String("reason", "layoutNo"),@@ -674,6 +677,10 @@ func mergeColumnBoxes(sortedBoxes []pdf.TextBox, mh, mw float64, isEnglish bool)out := make([]pdf.TextBox, 0, len(sortedBoxes))for i := 0; i < len(sortedBoxes); i++ {curr := sortedBoxes[i]+ if curr.LayoutType == pdf.LayoutTypeFigure {+ out = append(out, curr)+ continue+ }// Skip cross-page suffixes (like previous page number)if i > 0 && sortedBoxes[i-1].PageNumber < curr.PageNumber && pageNumSuffixPattern.MatchString(sortedBoxes[i-1].Text) {diff --git a/internal/deepdoc/parser/pdf/table/merge_captions.go b/internal/deepdoc/parser/pdf/table/merge_captions.goindex eb3ead582e..f4264aa8b9 100644--- a/internal/deepdoc/parser/pdf/table/merge_captions.go+++ b/internal/deepdoc/parser/pdf/table/merge_captions.go@@ -84,16 +84,13 @@ func MergeCaptions(sections []pdf.Section) []pdf.Section {captions = append(captions, i)continue}- // No merge target. A FIGURE caption is kept as its own section: a pure- // image figure has no text section (BoxesToSections skips empty figure- // boxes), so removing it would drop caption text that Python keeps- // (07_mixed_content 'Figure 1/2'). A TABLE caption with neither a table- // nor a figure parent — findNearestParent already fell back to figures,- // mirroring Python's nearest(tables)/nearest(figures) double search — is- // a DLA mislabel (e.g. rotate_270's rotated text labeled "table"); keep- // the historical removal so rotated-page text is not duplicated. Python- // likewise drops a caption only when BOTH searches fail, so the removal- // branch now fires only when no parent exists at all.+ // No merge target. Keep a FIGURE caption as its own section when no+ // nearby figure is available. Position-only sections let captions for+ // pure-image figures attach to their image before reaching this branch.+ // A TABLE caption with no reachable table or figure parent is a DLA+ // mislabel (e.g. rotate_270's rotated text labeled "table").+ // findNearestParent searches both kinds, matching Python's fallback.+ // Keep the historical removal so rotated-page text is not duplicated.if captionType != pdf.LayoutTypeFigure {captions = append(captions, i)}diff --git a/internal/deepdoc/parser/pdf/table/table_annotate.go b/internal/deepdoc/parser/pdf/table/table_annotate.goindex 73770b4712..dcb50998fe 100644--- a/internal/deepdoc/parser/pdf/table/table_annotate.go+++ b/internal/deepdoc/parser/pdf/table/table_annotate.go@@ -37,6 +37,12 @@ func regionOverlapsBox(region pdf.DLARegion, box pdf.TextBox, scale float64) boo// Each table region is matched if at least one box overlaps it (>40% of box// area) or if there are no boxes at all (image-only PDF), matching Python's// _table_transformer_job which processes every table DLA region.+//+// Figure-layout boxes are skipped: AnnotateBoxLayouts appends one full-extent+// geometry box per figure/equation region, and a nested/double-detected+// table+figure pair would otherwise pull the whole figure extent into the+// table's BoxIdx (and from there into Positions, boxInCrop and R/C+// annotations, causing HTML cross-region and NoMerge mislabels).func MatchTableRegions(boxes []pdf.TextBox, regions []pdf.DLARegion, scale float64) []TableMatch {var matches []TableMatchfor _, r := range regions {@@ -45,6 +51,9 @@ func MatchTableRegions(boxes []pdf.TextBox, regions []pdf.DLARegion, scale float}var matched []intfor i, b := range boxes {+ if b.LayoutType == pdf.LayoutTypeFigure {+ continue+ }if regionOverlapsBox(r, b, scale) {matched = append(matched, i)}@@ -65,7 +74,6 @@ type annRegion struct {x0, y0, x1, y1 float64label stringscore float64- visited booltypeIndex int}@@ -280,11 +288,11 @@ func nmsIoU(a, b pdf.DLARegion) float64 {// AnnotateBoxLayouts assigns LayoutType and LayoutNo to boxes based on DLA// regions. Returns the filtered slice (Python pops CID-garbled boxes and// garbage-layout boxes at wrong positions - Go mirrors with compact).-// Also creates synthetic figure boxes for unmatched figure/equation regions.+// Also creates a complete geometry box for every figure/equation region.//// Before annotation, regions are de-duplicated (layouts_cleanup) and sorted-// top-to-bottom (sort_Y_firstly) to match Python, and unmatched figure and-// equation regions receive SEPARATE synthetic namespaces (figure-N /+// top-to-bottom (sort_Y_firstly), and figure and equation regions receive+// separate geometry namespaces (figure-N /// equation-N) so they never collide.// FilteredDLARegions returns the DLA regions after per-class NMS, the// confidence filter, Y-sort, and cleanup — i.e. the exact set fed to@@ -413,7 +421,6 @@ func AnnotateBoxLayouts(boxes []pdf.TextBox, regions []pdf.DLARegion, scale floadropped[i] = truecontinue}- cands[bestJ].visited = true// Python: equation mapped to "figure" for layout_typeif ty == pdf.LayoutTypeEquation {boxes[i].LayoutType = pdf.LayoutTypeFigure@@ -446,28 +453,21 @@ func AnnotateBoxLayouts(boxes []pdf.TextBox, regions []pdf.DLARegion, scale floa}boxes = compacted- // Synthetic figure boxes for unmatched figure/equation regions (Python:- // layout_recognizer.py:145-155). Python numbers each unmatched region with- // its index WITHIN the per-type list that also includes already-visited- // regions (enumerate([lt for lt in lts if lt["type"] == ty])), so we reuse- // the per-type typeIndex computed above rather than a separate- // unvisited-only counter. Python keeps figure-N / equation-N in SEPARATE- // namespaces, so the typeIndex is keyed by the original type label.+ // Keep one geometry box per figure/equation, independently of its text.+ // ConsolidateFigures combines it with matching labels after text cleanup.for j := range cands {- if cands[j].visited {- continue- }if cands[j].label != pdf.LayoutTypeFigure && cands[j].label != pdf.LayoutTypeEquation {continue}boxes = append(boxes, pdf.TextBox{- X0: cands[j].x0,- X1: cands[j].x1,- Top: cands[j].y0,- Bottom: cands[j].y1,- Text: "",- LayoutType: pdf.LayoutTypeFigure,- LayoutNo: fmt.Sprintf("%s-%d", cands[j].label, cands[j].typeIndex),+ X0: cands[j].x0,+ X1: cands[j].x1,+ Top: cands[j].y0,+ Bottom: cands[j].y1,+ Text: "",+ LayoutType: pdf.LayoutTypeFigure,+ LayoutNo: fmt.Sprintf("%s-%d", cands[j].label, cands[j].typeIndex),+ FigureBounds: &pdf.Position{Left: cands[j].x0, Top: cands[j].y0, Right: cands[j].x1, Bottom: cands[j].y1},})}diff --git a/internal/deepdoc/parser/pdf/table/table_annotate_parity_test.go b/internal/deepdoc/parser/pdf/table/table_annotate_parity_test.goindex 23cb815e1a..d1664c086e 100644--- a/internal/deepdoc/parser/pdf/table/table_annotate_parity_test.go+++ b/internal/deepdoc/parser/pdf/table/table_annotate_parity_test.go@@ -1,24 +1,5 @@-// Tests pinning the Python-equivalent behavior of AnnotateBoxLayouts.-//-// These assert the layout-annotation semantics that Python's-// LayoutRecognizer.__call__ (deepdoc/vision/layout_recognizer.py:68) produces,-// and that the Go implementation now replicates (GREEN):-//-// - #1 layouts_cleanup: Python de-dupes overlapping same-type regions-// (recognizer.py:124-160, called at layout_recognizer.py:100) BEFORE-// annotation. Go matches via cleanupLayouts, so no extra synthetic-// figure/equation boxes are emitted.-// - #2 sort_Y_firstly: Python sorts regions top-to-bottom (recognizer.py:54,-// layout_recognizer.py:99) before numbering them, so layoutno indices-// follow reading order. Go matches via sortYFirstly.-// - #3 synthetic namespace: Python numbers unmatched figure/equation regions-// in SEPARATE counters -> "figure-N" / "equation-N"-// (layout_recognizer.py:145-155). Go matches via a per-type typeIndex-// keyed by the original type label.-//-// These are pure-Go unit tests (no model server, no external service): they-// feed crafted pdf.DLARegion slices and assert the annotated pdf.TextBox-// output. Coordinates use scale=1 so region pixels == PDF space.+// Tests for layout filtering, reading-order numbering, and figure identity.+// Text annotations and complete image regions must share the same identifiers.package table@@ -40,8 +21,7 @@ func countFigureBoxes(boxes []pdf.TextBox) int {}// countSyntheticFigures returns the number of figure boxes with no text-// (the synthetic placeholders AnnotateBoxLayouts appends for unvisited-// figure/equation regions).+// (the geometry boxes emitted for figure/equation regions).func countSyntheticFigures(boxes []pdf.TextBox) int {n := 0for _, b := range boxes {@@ -52,12 +32,7 @@ func countSyntheticFigures(boxes []pdf.TextBox) int {return n}-// TestAnnotateBoxLayouts_DuplicateFigureRegions_Merged pins #1: two heavily-// overlapping same-type (figure) regions must be treated as ONE, so only one-// figure box (the annotated text box) results and NO synthetic figure box is-// produced. Python merges via layouts_cleanup; Go now mirrors that by fusing-// the overlapping pair into one region, so the duplicate synthetic figure is-// avoided.+// Duplicate DLA detections must assemble into one complete figure.func TestAnnotateBoxLayouts_DuplicateFigureRegions_Merged(t *testing.T) {box := pdf.TextBox{X0: 0, X1: 100, Top: 0, Bottom: 50, Text: "Some caption text", PageNumber: 0}regions := []pdf.DLARegion{@@ -65,7 +40,7 @@ func TestAnnotateBoxLayouts_DuplicateFigureRegions_Merged(t *testing.T) {{X0: 0, Y0: 0, X1: 100, Y1: 50, Confidence: 0.8, Label: pdf.LayoutTypeFigure}, // identical -> IoU 1.0}- out := AnnotateBoxLayouts([]pdf.TextBox{box}, regions, 1.0, 100.0)+ out := ConsolidateFigures(AnnotateBoxLayouts([]pdf.TextBox{box}, regions, 1.0, 100.0))if got := countFigureBoxes(out); got != 1 {t.Errorf("#1 layouts_cleanup: expected 1 figure box after de-duplicating overlapping figure regions, got %d", got)@@ -242,7 +217,7 @@ func TestAnnotateBoxLayouts_CleanupEqualScoreKeepsLater(t *testing.T) {{X0: 0, Y0: 0, X1: 400, Y1: 400, Confidence: 0.9, Label: pdf.LayoutTypeFigure}, // B: large, on top (Y0=0){X0: 100, Y0: 200, X1: 200, Y1: 300, Confidence: 0.9, Label: pdf.LayoutTypeFigure}, // A: small, inside B, lower (Y0=200)}- // No text box overlaps -> both unvisited -> cleanup collapses to one synthetic.+ // Cleanup must retain only one geometry box for these overlapping regions.out := AnnotateBoxLayouts([]pdf.TextBox{}, regions, 1.0, 100.0)if got := countFigureBoxes(out); got != 1 {t.Fatalf("expected 1 figure box after cleanup merge, got %d", got)@@ -264,7 +239,7 @@ func TestAnnotateBoxLayouts_SyntheticVisitedInterleaved(t *testing.T) {{X0: 0, X1: 100, Top: 0, Bottom: 50, Text: "caption for A"},}regions := []pdf.DLARegion{- {X0: 0, Y0: 0, X1: 100, Y1: 50, Confidence: 0.9, Label: pdf.LayoutTypeFigure}, // A: matched to text box -> visited+ {X0: 0, Y0: 0, X1: 100, Y1: 50, Confidence: 0.9, Label: pdf.LayoutTypeFigure}, // A: matched to text box -> annotated{X0: 200, Y0: 0, X1: 300, Y1: 50, Confidence: 0.9, Label: pdf.LayoutTypeFigure}, // B: unmatched -> synthetic}out := AnnotateBoxLayouts(boxes, regions, 1.0, 100.0)@@ -279,15 +254,15 @@ func TestAnnotateBoxLayouts_SyntheticVisitedInterleaved(t *testing.T) {}}if annotated == nil {- t.Fatal("visited figure A: text box was not annotated as figure")+ t.Fatal("annotated figure A: text box was not annotated as figure")}if annotated.LayoutNo != "figure-0" {- t.Errorf("visited figure A: expected LayoutNo figure-0, got %q", annotated.LayoutNo)+ t.Errorf("annotated figure A: expected LayoutNo figure-0, got %q", annotated.LayoutNo)}if synthetic == nil {t.Fatal("expected a synthetic figure box for unmatched B")}- // B is the 2nd figure in Y order (after visited A), so Python numbers it figure-1.+ // B is the 2nd figure in Y order (after annotated A), so Python numbers it figure-1.if synthetic.LayoutNo != "figure-1" {t.Errorf("unmatched figure B: expected figure-1 (index in full per-type list), got %q", synthetic.LayoutNo)}diff --git a/internal/deepdoc/parser/pdf/table/table_annotate_test.go b/internal/deepdoc/parser/pdf/table/table_annotate_test.goindex 4138ac8140..0b440681a4 100644--- a/internal/deepdoc/parser/pdf/table/table_annotate_test.go+++ b/internal/deepdoc/parser/pdf/table/table_annotate_test.go@@ -349,9 +349,9 @@ func TestAnnotateBoxLayouts_EquationMappedToFigure(t *testing.T) {regions := []pdf.DLARegion{{X0: 0, Y0: 0, X1: 300, Y1: 60, Label: "equation", Confidence: 0.9},}- boxes = AnnotateBoxLayouts(boxes, regions, 3.0, 0)+ boxes = ConsolidateFigures(AnnotateBoxLayouts(boxes, regions, 3.0, 0))if len(boxes) != 1 {- t.Fatalf("expected 1 box, got %d", len(boxes))+ t.Fatalf("unexpected assembled box count: %d", len(boxes))}if boxes[0].LayoutType != "figure" {t.Errorf("equation → LayoutType: got %q, want 'figure'", boxes[0].LayoutType)@@ -374,9 +374,9 @@ func TestAnnotateBoxLayouts_MixedTypesLayoutNo(t *testing.T) {{X0: 0, Y0: 600, X1: 150, Y1: 660, Label: "text", Confidence: 0.9}, // text-1{X0: 600, Y0: 0, X1: 900, Y1: 60, Label: "figure", Confidence: 0.9}, // figure-0 (PDF: x0=200, x1=300)}- boxes = AnnotateBoxLayouts(boxes, regions, 3.0, 0)+ boxes = ConsolidateFigures(AnnotateBoxLayouts(boxes, regions, 3.0, 0))if len(boxes) != 3 {- t.Fatalf("expected 3 boxes, got %d", len(boxes))+ t.Fatalf("unexpected assembled box count: %d", len(boxes))}// Check that text and figure indices are independentif boxes[0].LayoutNo != "text-0" {@@ -481,6 +481,26 @@ func TestMatchTableRegions_BelowThreshold(t *testing.T) {}}+func TestMatchTableRegions_SkipsFigureBoxes(t *testing.T) {+ // Nested/double-detected table+figure: the full-extent figure geometry+ // box must not enter the table's BoxIdx, otherwise its extent leaks+ // into TableItem.Positions (HTML cross-region, NoMerge mislabel).+ boxes := []pdf.TextBox{+ {X0: 0, X1: 100, Top: 0, Bottom: 50, LayoutType: pdf.LayoutTypeTable},+ {X0: 0, X1: 100, Top: 0, Bottom: 50, LayoutType: pdf.LayoutTypeFigure},+ }+ regions := []pdf.DLARegion{+ {X0: 0, Y0: 0, X1: 300, Y1: 150, Label: "table"},+ }+ matches := MatchTableRegions(boxes, regions, 3.0)+ if len(matches) != 1 {+ t.Fatalf("expected 1 match, got %d", len(matches))+ }+ if len(matches[0].BoxIdx) != 1 || matches[0].BoxIdx[0] != 0 {+ t.Errorf("figure box leaked into table match, got %v", matches[0].BoxIdx)+ }+}+// MockTableBuilder is a test-only pdf.TableBuilder with a configurable GroupCells.type MockTableBuilder struct {GroupCellsFn func(cells []pdf.TSRCell) [][]pdf.TSRCelldiff --git a/internal/deepdoc/parser/pdf/table/table_post.go b/internal/deepdoc/parser/pdf/table/table_post.goindex 22eb98bfef..36aff2ed2a 100644--- a/internal/deepdoc/parser/pdf/table/table_post.go+++ b/internal/deepdoc/parser/pdf/table/table_post.go@@ -589,7 +589,15 @@ func mergeFigureGroups(boxes []pdf.TextBox, groups map[figKey][]int, removeSet manchor := indices[0]for _, idx := range indices[1:] {b := boxes[idx]- boxes[anchor].Text += "\n" + b.Text+ if b.Text != "" {+ if boxes[anchor].Text != "" {+ boxes[anchor].Text += "\n"+ }+ boxes[anchor].Text += b.Text+ }+ if boxes[anchor].FigureBounds == nil {+ boxes[anchor].FigureBounds = b.FigureBounds+ }boxes[anchor].X0 = math.Min(boxes[anchor].X0, b.X0)boxes[anchor].X1 = math.Max(boxes[anchor].X1, b.X1)boxes[anchor].Top = math.Min(boxes[anchor].Top, b.Top)@@ -600,14 +608,14 @@ func mergeFigureGroups(boxes []pdf.TextBox, groups map[figKey][]int, removeSet m}// ConsolidateFigures merges figure boxes that share the same LayoutNo-// (i.e., belong to the same DLA figure region) into a single pdf.TextBox.-// Matches Python's _extract_table_figure + insert_table_figures which pops-// individual figure boxes and re-inserts one consolidated figure block-// per DLA region with combined text.+// into one block, using the matched DLA region for the complete image bounds.//// Figure boxes whose text matches the data-source discard pattern-// (r"(数据|资料|图表)*来源[:: ]") are removed entirely — matching Python's-// _extract_table_figure discard behavior (pdf_parser.py:1050-1052).+// (r"(数据|资料|图表)*来源[:: ]") have that text removed, matching Python's+// _extract_table_figure discard behavior (pdf_parser.py:1050-1052). The+// region's geometry box survives, so the figure image is preserved even when+// the attribution line was its only text — a deliberate divergence from+// Python, which drops the whole region once the box is popped.func ConsolidateFigures(boxes []pdf.TextBox) []pdf.TextBox {removeSet := markDataSourceBoxesForRemoval(boxes)groups := groupFigureBoxes(boxes, removeSet)@@ -616,9 +624,28 @@ func ConsolidateFigures(boxes []pdf.TextBox) []pdf.TextBox {mergeFigureGroups(boxes, groups, removeSet)}+ applyFigureBounds(boxes)return FilterBoxesByRemoveSet(boxes, removeSet)}+// applyFigureBounds pins every figure box to its matched DLA region.+//+// The DLA region is the crop contract: a figure section's geometry must be+// the region the layout model detected, not the union of the text boxes inside+// it. Merging unions the member boxes, and NaiveVerticalMerge's empty-box+// branch can stretch a standalone geometry box (prev.Bottom = curr.Bottom), so+// the region is re-applied after merging and for single-member groups too.+func applyFigureBounds(boxes []pdf.TextBox) {+ for i := range boxes {+ region := boxes[i].FigureBounds+ if region == nil {+ continue+ }+ boxes[i].X0, boxes[i].X1 = region.Left, region.Right+ boxes[i].Top, boxes[i].Bottom = region.Top, region.Bottom+ }+}+// boxOverlapsPositionPage reports whether a pdf.TextBox overlaps a// pdf.Position, additionally requiring the box to live on a page the position// spans. Table positions and table-layout boxes are both stored in page-localdiff --git a/internal/deepdoc/parser/pdf/table/table_post_test.go b/internal/deepdoc/parser/pdf/table/table_post_test.goindex ddf95af8ba..9fc8d74e51 100644--- a/internal/deepdoc/parser/pdf/table/table_post_test.go+++ b/internal/deepdoc/parser/pdf/table/table_post_test.go@@ -749,6 +749,48 @@ func TestConsolidateFigures_OnlyDataSource(t *testing.T) {}}+// TestConsolidateFigures_AppliesFigureBounds pins the crop contract: every+// surviving figure box is pinned to its DLA region, including a single-member+// group whose bounds the layout stages stretched (NaiveVerticalMerge's+// empty-box branch sets prev.Bottom = curr.Bottom) and a merged group whose+// union differs from the region.+func TestConsolidateFigures_AppliesFigureBounds(t *testing.T) {+ boxes := []pdf.TextBox{+ // Single-member geometry group stretched past its region's bottom.+ {+ LayoutType: pdf.LayoutTypeFigure, PageNumber: 0, LayoutNo: "figure-0",+ X0: 10, X1: 200, Top: 10, Bottom: 55,+ FigureBounds: &pdf.Position{Left: 10, Right: 200, Top: 10, Bottom: 50},+ },+ // Merged group: the caption's union is narrower than the region.+ {+ Text: "caption", LayoutType: pdf.LayoutTypeFigure, PageNumber: 0, LayoutNo: "figure-1",+ X0: 10, X1: 100, Top: 100, Bottom: 120,+ },+ {+ LayoutType: pdf.LayoutTypeFigure, PageNumber: 0, LayoutNo: "figure-1",+ X0: 10, X1: 200, Top: 100, Bottom: 160,+ FigureBounds: &pdf.Position{Left: 10, Right: 200, Top: 100, Bottom: 160},+ },+ }++ result := ConsolidateFigures(boxes)+ if len(result) != 2 {+ t.Fatalf("expected 2 figures, got %d: %+v", len(result), result)+ }+ if got := result[0]; got.X0 != 10 || got.X1 != 200 || got.Top != 10 || got.Bottom != 50 {+ t.Errorf("single-member figure not pinned to its region: got (%v,%v,%v,%v)",+ got.X0, got.Top, got.X1, got.Bottom)+ }+ if got := result[1]; got.X0 != 10 || got.X1 != 200 || got.Top != 100 || got.Bottom != 160 {+ t.Errorf("merged figure not pinned to its region: got (%v,%v,%v,%v)",+ got.X0, got.Top, got.X1, got.Bottom)+ }+ if result[1].Text != "caption" {+ t.Errorf("merged figure lost its text: %q", result[1].Text)+ }+}+func TestExtractTableAndReplace_MergeTablesAcrossPages(t *testing.T) {// Regression test: ExtractTableAndReplace must correctly replace an// ALREADY-merged cross-page table (merged upstream by Parser.buildLayout atdiff --git a/internal/deepdoc/parser/pdf/table_extract.go b/internal/deepdoc/parser/pdf/table_extract.goindex c7c2d7e9e6..c3fe0cb014 100644--- a/internal/deepdoc/parser/pdf/table_extract.go+++ b/internal/deepdoc/parser/pdf/table_extract.go@@ -62,7 +62,7 @@ const (//// Returns:// - annotated: page boxes after DLA/TSR annotation write-back (LayoutType,-// LayoutNo, R/C/H/SP fields) — same length as input pageBoxes.+// LayoutNo, R/C/H/SP fields), plus image geometry.// - tables: table candidates detected on this page.// - dlaRegions: page-local DLA regions payload.func (p *Parser) enrichOnePageWithDeepDoc(ctx context.Context,@@ -89,6 +89,10 @@ func (p *Parser) enrichOnePageWithDeepDoc(ctx context.Context,// caller should use downstream for layout/text-merge.annotated = append([]pdf.TextBox(nil), pageBoxes...)annotated = tbl.AnnotateBoxLayouts(annotated, regions, scale, float64(pageImg.Bounds().Dy()))+ for i := range annotated {+ annotated[i].PageNumber = pg+ annotated[i].HasPageNumber = true+ }tableMatches := tbl.MatchTableRegions(annotated, regions, scale)var items []pdf.TableItemdiff --git a/internal/deepdoc/parser/type/types.go b/internal/deepdoc/parser/type/types.goindex cd87566e23..f590d854ec 100644--- a/internal/deepdoc/parser/type/types.go+++ b/internal/deepdoc/parser/type/types.go@@ -117,8 +117,10 @@ type TextBox struct {Pages []intLayoutType stringLayoutNo string- ColID int- R int+ // FigureBounds retains DLA geometry in PDF points until figure consolidation.+ FigureBounds *Position+ ColID int+ R int// IsOCR marks a box produced by an OCR pass (ocrDetectAndRecognize /// ocrMergeChars), as opposed to one built from embedded PDF chars// (CharsToBoxes). It scopes OCR-only post-processing (see layout.Dedup*)diff --git a/internal/parser/parser/pdf_parser_common_test.go b/internal/parser/parser/pdf_parser_common_test.goindex 9dccb609dc..66cf76e611 100644--- a/internal/parser/parser/pdf_parser_common_test.go+++ b/internal/parser/parser/pdf_parser_common_test.go@@ -475,7 +475,7 @@ func TestPDFParseResultToJSON_NoInlineRetainsPositions(t *testing.T) {PageHeight: map[int]float64{0: 100},Sections: []deepdoctype.Section{{- Text: "Figure caption",+ Text: "",LayoutType: deepdoctype.LayoutTypeFigure,PositionTag: "@@0\t10\t50\t10\t50##",Positions: []deepdoctype.Position{@@ -512,12 +512,19 @@ func TestPDFParseResultToJSON_NoInlineRetainsPositions(t *testing.T) {t.Fatalf("JSON len = %d, want 3", len(res.JSON))}- // Figure must retain positions but NOT inline an image.- figPos, ok := res.JSON[0].ToMap()["_pdf_positions"].([][]any)+ // A textless figure must remain an image crop item without inlining an image.+ figure := res.JSON[0].ToMap()+ if got, want := figure["doc_type_kwd"], "image"; got != want {+ t.Fatalf("textless figure doc_type_kwd = %v, want %v", got, want)+ }+ figPos, ok := figure["_pdf_positions"].([][]any)if !ok || len(figPos) == 0 {- t.Fatalf("Figure should retain _pdf_positions, got %v", res.JSON[0].ToMap()["_pdf_positions"])+ t.Fatalf("textless figure should retain _pdf_positions, got %v", figure["_pdf_positions"])+ }+ if !HasPDFCropRegion(figure) {+ t.Fatal("textless figure should carry a crop region for VLM and chunker")}- if img, _ := res.JSON[0].ToMap()["image"].(string); img != "" {+ if img, _ := figure["image"].(string); img != "" {t.Fatalf("Figure should not inline image under cgo, got %q", img)}