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(chunker): preserve whole lines in PDF text previews (#20890)
jay777218efbe73da0b0
diff --git a/internal/deepdoc/parser/pdf/util/crop.go b/internal/deepdoc/parser/pdf/util/crop.goindex 9bca119466..c79834a4a3 100644--- a/internal/deepdoc/parser/pdf/util/crop.go+++ b/internal/deepdoc/parser/pdf/util/crop.go@@ -127,7 +127,6 @@ func buildSectionRasterPlan(positions []pdf.Position, decodedImages map[int]imagentries = append(entries, positionEntry{position: bottomBand, isEdge: true})plan := §ionRasterPlan{}- maxInt := int(^uint(0) >> 1)for _, entry := range entries {pos := entry.positionleft, right := pos.Left, pos.Right@@ -170,15 +169,21 @@ func buildSectionRasterPlan(positions []pdf.Position, decodedImages map[int]imagif len(plan.segments) == 0 {return nil, false}+ return plan, measureSectionRasterPlan(plan, decodedImages)+}++func measureSectionRasterPlan(plan *sectionRasterPlan, decodedImages map[int]image.Image) bool {+ plan.width, plan.height = 0, 0+ maxInt := int(^uint(0) >> 1)for _, segment := range plan.segments {width, height := cropSize(decodedImages[segment.page], segment.x0, segment.y0, segment.x1, segment.y1)plan.width = max(plan.width, width)if height > maxInt-sectionRasterGap || plan.height > maxInt-(height+sectionRasterGap) {- return nil, false+ return false}plan.height += height + sectionRasterGap}- return plan, plan.width > 0 && plan.height > 0+ return plan.width > 0 && plan.height > 0}func (p *sectionRasterPlan) addCrop(page, x0, y0, x1, y1 int, isEdge bool) {diff --git a/internal/deepdoc/parser/pdf/util/crop_test.go b/internal/deepdoc/parser/pdf/util/crop_test.goindex 056113c981..7f0256d6cb 100644--- a/internal/deepdoc/parser/pdf/util/crop_test.go+++ b/internal/deepdoc/parser/pdf/util/crop_test.go@@ -160,6 +160,81 @@ func TestCropSectionImage_ZoomScaling(t *testing.T) {}}+func TestCropTextSectionPositionsKeepsContinuousRows(t *testing.T) {+ page := image.NewRGBA(image.Rect(0, 0, 100, 100))+ for y := 0; y < 100; y++ {+ for x := 0; x < 100; x++ {+ page.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})+ }+ }+ for _, tc := range []struct {+ name string+ firstEnd float64+ secondTop float64+ }{+ {name: "touching slices", firstEnd: 45, secondTop: 45},+ {name: "overlapping boxes", firstEnd: 60, secondTop: 40},+ } {+ t.Run(tc.name, func(t *testing.T) {+ positions := []pdf.Position{+ {PageNumbers: []int{0}, Left: 10, Right: 90, Top: 0, Bottom: tc.firstEnd},+ {PageNumbers: []int{0}, Left: 10, Right: 90, Top: tc.secondTop, Bottom: 100},+ }+ got, err := DecodeBase64PNG(CropTextSectionPositions(positions, map[int]image.Image{0: page}, 1))+ if err != nil {+ t.Fatal(err)+ }+ // The empty top context occupies a 1px placeholder plus a 6px gap.+ // Every source row must follow intact, without internal gaps or repeats.+ for y := 0; y < 100; y++ {+ want := color.RGBA{10, uint8(y), 0, 255}+ if pixel := color.RGBAModel.Convert(got.At(0, 7+y)); pixel != want {+ t.Fatalf("source row %d: got %v, want %v", y, pixel, want)+ }+ }+ })+ }+}++func TestCropTextSectionPositionsKeepsSeparateRegions(t *testing.T) {+ red := color.RGBA{255, 0, 0, 255}+ blue := color.RGBA{0, 0, 255, 255}+ page := makeTestPageImage(100, 100, red).(*image.RGBA)+ for y := 0; y < 100; y++ {+ for x := 50; x < 100; x++ {+ page.SetRGBA(x, y, blue)+ }+ }+ for _, tc := range []struct {+ name string+ second pdf.Position+ wantColor color.RGBA+ }{+ {name: "vertical gap", second: pdf.Position{PageNumbers: []int{0}, Left: 10, Right: 40, Top: 60, Bottom: 100}, wantColor: red},+ {name: "different column", second: pdf.Position{PageNumbers: []int{0}, Left: 50, Right: 80, Top: 40, Bottom: 100}, wantColor: blue},+ {name: "different page", second: pdf.Position{PageNumbers: []int{1}, Left: 10, Right: 40, Top: 40, Bottom: 100}, wantColor: blue},+ } {+ t.Run(tc.name, func(t *testing.T) {+ positions := []pdf.Position{+ {PageNumbers: []int{0}, Left: 10, Right: 40, Top: 0, Bottom: 40},+ tc.second,+ }+ got, err := DecodeBase64PNG(CropTextSectionPositions(positions, map[int]image.Image{+ 0: page, 1: makeTestPageImage(100, 100, blue),+ }, 1))+ if err != nil {+ t.Fatal(err)+ }+ if pixel := color.RGBAModel.Convert(got.At(0, 47)); pixel != (color.RGBA{245, 245, 245, 255}) {+ t.Fatalf("separate regions lost their gap: %v", pixel)+ }+ if pixel := color.RGBAModel.Convert(got.At(0, 53)); pixel != tc.wantColor {+ t.Fatalf("second region: got %v, want %v", pixel, tc.wantColor)+ }+ })+ }+}+type accessCountingImage struct {bounds image.Rectanglereads intdiff --git a/internal/deepdoc/parser/pdf/util/crop_text.go b/internal/deepdoc/parser/pdf/util/crop_text.gonew file mode 100644index 0000000000..ccd5399a12--- /dev/null+++ b/internal/deepdoc/parser/pdf/util/crop_text.go@@ -0,0 +1,112 @@+package util++import (+ "image"+ "math"++ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++// CropTextSectionPositions preserves whole rasterized lines around estimated+// text boundaries. Source positions stay unchanged; only preview pixels expand.+func CropTextSectionPositions(positions []pdf.Position, pages map[int]image.Image, zoom float64) string {+ plan, ok := buildSectionRasterPlan(positions, pages, zoom)+ if !ok {+ return ""+ }+ // Twelve PDF points cover nearby glyph edges at the render's scale, while+ // bounding searches on dense graphics or pages with no clear whitespace.+ radius := int(math.Ceil(12 * zoom))+ for i := range plan.segments {+ s := &plan.segments[i]+ if s.isEdge {+ continue+ }+ s.y0, _ = snapTextBoundary(pages[s.page], s.x0, s.x1, s.y0, -1, radius)+ s.y1, _ = snapTextBoundary(pages[s.page], s.x0, s.x1, s.y1, 1, radius)+ }+ // Expanding two cuts inside the same line may make their body crops+ // overlap. Render that line once within a preview.+ segments := make([]sectionRasterSegment, 0, len(plan.segments))+ columnTolerance := int(math.Ceil(2 * zoom))+ for _, s := range plan.segments {+ if len(segments) > 0 && !s.isEdge {+ prev := &segments[len(segments)-1]+ if !prev.isEdge && prev.page == s.page && sameTextColumn(*prev, s, columnTolerance) &&+ s.y0 <= prev.y1 && s.y1 >= prev.y0 {+ prev.x0, prev.x1 = min(prev.x0, s.x0), max(prev.x1, s.x1)+ prev.y0 = min(prev.y0, s.y0)+ prev.y1 = max(prev.y1, s.y1)+ continue+ }+ }+ segments = append(segments, s)+ }+ plan.segments = segments+ // Context meets the corrected body instead of skipping a fixed strip+ // through a neighbouring line. Its outer edges also retain whole lines.+ if len(segments) >= 3 {+ top, first := &segments[0], segments[1]+ last, bottom := segments[len(segments)-2], &segments[len(segments)-1]+ if y, ok := snapTextBoundary(pages[top.page], top.x0, top.x1, first.y0, -1, radius); ok {+ top.y1 = y+ }+ top.y0, _ = snapTextBoundary(pages[top.page], top.x0, top.x1, top.y0, -1, radius)+ if y, ok := snapTextBoundary(pages[bottom.page], bottom.x0, bottom.x1, last.y1, 1, radius); ok {+ bottom.y0 = y+ }+ bottom.y1, _ = snapTextBoundary(pages[bottom.page], bottom.x0, bottom.x1, bottom.y1, 1, radius)+ }+ if !measureSectionRasterPlan(plan, pages) {+ return ""+ }+ return encodeCroppedImage(renderSectionRaster(plan, pages))+}++// sameTextColumn allows small raster-boundary drift, while requiring horizontal+// overlap and nearly matching sides so independent columns stay separate.+func sameTextColumn(a, b sectionRasterSegment, tolerance int) bool {+ return a.x0 < b.x1 && b.x0 < a.x1 &&+ max(a.x0, b.x0)-min(a.x0, b.x0) <= tolerance &&+ max(a.x1, b.x1)-min(a.x1, b.x1) <= tolerance+}++// snapTextBoundary moves a cut outward to a nearby pair of clear rows. It+// leaves safe, out-of-page, and unresolvable boundaries alone. The boolean+// reports whether the result is a safe cut, including either page edge.+func snapTextBoundary(img image.Image, x0, x1, y, direction, radius int) (int, bool) {+ b := img.Bounds()+ x0, x1 = max(x0, b.Min.X), min(x1, b.Max.X)+ if x0 >= x1 || y <= b.Min.Y || y >= b.Max.Y {+ return y, x0 < x1 && (y == b.Min.Y || y == b.Max.Y)+ }+ blank := func(row int) bool {+ if row < b.Min.Y || row >= b.Max.Y {+ return true+ }+ for x := x0; x < x1; x++ {+ r, g, blue, a := img.At(x, row).RGBA()+ // Composite transparency over the white PDF page background.+ if r+65535-a < 245*257 || g+65535-a < 245*257 || blue+65535-a < 245*257 {+ return false+ }+ }+ return true+ }+ if blank(y-1) || blank(y) {+ return y, true+ }+ for distance := 1; distance <= radius; distance++ {+ candidate := y + direction*distance+ if candidate < b.Min.Y || candidate > b.Max.Y {+ break+ }+ if candidate == b.Min.Y || candidate == b.Max.Y {+ return candidate, true+ }+ if blank(candidate-1) && blank(candidate) {+ return candidate, true+ }+ }+ return y, false+}diff --git a/internal/deepdoc/parser/pdf/util/crop_text_test.go b/internal/deepdoc/parser/pdf/util/crop_text_test.gonew file mode 100644index 0000000000..e40a26656e--- /dev/null+++ b/internal/deepdoc/parser/pdf/util/crop_text_test.go@@ -0,0 +1,207 @@+package util++import (+ "fmt"+ "image"+ "image/color"+ "reflect"+ "testing"++ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++func TestCropTextSectionPositionsKeepsUnresolvableCrop(t *testing.T) {+ positions := []pdf.Position{{PageNumbers: []int{0}, Left: 10, Right: 90, Top: 150, Bottom: 200}}+ pages := map[int]image.Image{0: makeTestPageImage(100, 400, color.RGBA{80, 80, 80, 255})}+ if got, want := CropTextSectionPositions(positions, pages, 1), CropSectionPositions(positions, pages, 1); got != want {+ t.Fatal("a crop with no clear rows should retain its body and context boundaries")+ }+}++func TestCropTextSectionPositionsDoesNotRepeatExpandedLine(t *testing.T) {+ for _, drift := range []float64{0, 1} {+ t.Run(fmt.Sprintf("horizontal drift %.0f", drift), func(t *testing.T) {+ page := makeTestPageImage(100, 400, color.RGBA{255, 255, 255, 255}).(*image.RGBA)+ for y := 140; y < 158; y++ {+ for x := 20; x < 50; x++ {+ page.SetRGBA(x, y, color.RGBA{0, 0, 0, 255})+ }+ }+ positions := []pdf.Position{+ {PageNumbers: []int{0}, Left: 10, Right: 90, Top: 130, Bottom: 147},+ {PageNumbers: []int{0}, Left: 10 + drift, Right: 90 + drift, Top: 151, Bottom: 170},+ }+ img, err := DecodeBase64PNG(CropTextSectionPositions(positions, map[int]image.Image{0: page}, 1))+ if err != nil {+ t.Fatal(err)+ }+ rows := 0+ for y := 0; y < img.Bounds().Dy(); y++ {+ if color.RGBAModel.Convert(img.At(0, y)) == (color.RGBA{255, 255, 255, 255}) &&+ color.RGBAModel.Convert(img.At(20, y)) == (color.RGBA{0, 0, 0, 255}) {+ rows+++ }+ }+ if rows != 18 {+ t.Fatalf("expanded line has %d ink rows, want 18 without repetition", rows)+ }+ })+ }+}++func TestSnapTextBoundary(t *testing.T) {+ // Use a non-zero origin and a second column with no whitespace, proving+ // that the search stays inside both the page and the selected column.+ page := image.NewRGBA(image.Rect(10, 20, 110, 120))+ for y := 20; y < 120; y++ {+ for x := 10; x < 110; x++ {+ c := color.RGBA{255, 255, 255, 255}+ if x >= 70 || (x >= 30 && x < 50 && y >= 50 && y < 70) {+ c = color.RGBA{0, 0, 0, 255}+ }+ page.SetRGBA(x, y, c)+ }+ }+ for _, tc := range []struct {+ name string+ x0, x1, y, direction, radius, want int+ safe bool+ }{+ {"top inside line", 20, 60, 60, -1, 15, 49, true},+ {"bottom inside line", 20, 60, 60, 1, 15, 71, true},+ {"top already safe", 20, 60, 50, -1, 15, 50, true},+ {"bottom already safe", 20, 60, 70, 1, 15, 70, true},+ {"white cut", 20, 60, 40, -1, 15, 40, true},+ {"search limit", 20, 60, 60, -1, 5, 60, false},+ {"no whitespace", 70, 100, 60, -1, 15, 60, false},+ {"page top", 20, 60, 20, -1, 15, 20, true},+ {"page bottom", 20, 60, 120, 1, 15, 120, true},+ {"outside column", 120, 130, 60, -1, 15, 60, false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ got, safe := snapTextBoundary(page, tc.x0, tc.x1, tc.y, tc.direction, tc.radius)+ if got != tc.want || safe != tc.safe {+ t.Fatalf("boundary = (%d, %v), want (%d, %v)", got, safe, tc.want, tc.safe)+ }+ })+ }+}++func TestCropTextSectionPositionsKeepsWholeContextLines(t *testing.T) {+ for _, wide := range []bool{false, true} {+ t.Run(fmt.Sprintf("wider context %v", wide), func(t *testing.T) {+ page := makeTestPageImage(100, 500, color.RGBA{255, 255, 255, 255}).(*image.RGBA)+ for i, top := range []int{20, 100, 140, 240, 300, 360} {+ for y := top; y < top+20; y++ {+ for x := 20; x < 40; x++ {+ page.SetRGBA(x, y, color.RGBA{uint8(40 + 20*i), 0, 0, 255})+ }+ }+ }+ positions := []pdf.Position{{PageNumbers: []int{0}, Left: 10, Right: 90, Top: 150, Bottom: 250}}+ if wide {+ positions = []pdf.Position{+ {PageNumbers: []int{0}, Left: 10, Right: 50, Top: 150, Bottom: 210},+ {PageNumbers: []int{0}, Left: 10, Right: 90, Top: 210, Bottom: 250},+ }+ // The wider context must not leave a fragment of this adjacent+ // column's line at its inner edge.+ for y := 130; y < 145; y++ {+ for x := 70; x < 80; x++ {+ page.SetRGBA(x, y, color.RGBA{0, 0, 200, 255})+ }+ }+ }+ img, err := DecodeBase64PNG(CropTextSectionPositions(positions, map[int]image.Image{0: page}, 1))+ if err != nil {+ t.Fatal(err)+ }+ counts := make(map[uint8]int)+ for y := 0; y < img.Bounds().Dy(); y++ {+ p := color.RGBAModel.Convert(img.At(10, y)).(color.RGBA)+ if p.G == 0 && p.B == 0 && p.A == 255 {+ counts[p.R]+++ }+ if wide {+ p = color.RGBAModel.Convert(img.At(60, y)).(color.RGBA)+ if p.R == 0 && p.G == 0 && p.B > 0 {+ t.Fatal("context clips adjacent column's line")+ }+ }+ }+ // Each source line appears exactly once: four dimmed context+ // lines (factor 127/255) and two unchanged body lines.+ want := map[uint8]int{19: 20, 29: 20, 80: 20, 100: 20, 59: 20, 69: 20}+ if !reflect.DeepEqual(counts, want) {+ t.Fatalf("source line rows = %v, want %v", counts, want)+ }+ })+ }+}++func TestCropTextSectionPositionsKeepsLinesAtPageEdges(t *testing.T) {+ page := makeTestPageImage(100, 100, color.RGBA{255, 255, 255, 255}).(*image.RGBA)+ for _, top := range []int{0, 80} {+ for y := top; y < top+20; y++ {+ for x := 20; x < 40; x++ {+ page.SetRGBA(x, y, color.RGBA{0, 0, 0, 255})+ }+ }+ }+ for _, tc := range []struct {+ name string+ positions []pdf.Position+ }{+ {"page edges", []pdf.Position{{PageNumbers: []int{0}, Left: 10, Right: 90, Top: 10, Bottom: 90}}},+ {"cross page", []pdf.Position{+ {PageNumbers: []int{0}, Left: 10, Right: 90, Top: 90, Bottom: 100},+ {PageNumbers: []int{1}, Left: 10, Right: 90, Top: 0, Bottom: 10},+ }},+ } {+ t.Run(tc.name, func(t *testing.T) {+ img, err := DecodeBase64PNG(CropTextSectionPositions(tc.positions, map[int]image.Image{0: page, 1: page}, 1))+ if err != nil {+ t.Fatal(err)+ }+ rows := 0+ for y := 0; y < img.Bounds().Dy(); y++ {+ if color.RGBAModel.Convert(img.At(0, y)) == (color.RGBA{255, 255, 255, 255}) &&+ color.RGBAModel.Convert(img.At(10, y)) == (color.RGBA{0, 0, 0, 255}) {+ rows+++ }+ }+ if rows != 40 {+ t.Fatalf("whole body lines = %d rows, want 40", rows)+ }+ })+ }+}++func TestCropTextSectionPositionsKeepsColumnsSeparate(t *testing.T) {+ page := makeTestPageImage(100, 400, color.RGBA{255, 255, 255, 255}).(*image.RGBA)+ for i, left := range []int{20, 70} {+ for y := 140; y < 158; y++ {+ for x := left; x < left+10; x++ {+ page.SetRGBA(x, y, color.RGBA{uint8(40 + 40*i), 0, 0, 255})+ }+ }+ }+ positions := []pdf.Position{+ {PageNumbers: []int{0}, Left: 10, Right: 50, Top: 130, Bottom: 170},+ {PageNumbers: []int{0}, Left: 60, Right: 90, Top: 130, Bottom: 170},+ }+ img, err := DecodeBase64PNG(CropTextSectionPositions(positions, map[int]image.Image{0: page}, 1))+ if err != nil {+ t.Fatal(err)+ }+ counts := make(map[uint8]int)+ for y := 0; y < img.Bounds().Dy(); y++ {+ p := color.RGBAModel.Convert(img.At(10, y)).(color.RGBA)+ if p.G == 0 && p.B == 0 {+ counts[p.R]+++ }+ }+ if want := (map[uint8]int{40: 18, 80: 18}); !reflect.DeepEqual(counts, want) {+ t.Fatalf("column line rows = %v, want %v", counts, want)+ }+}diff --git a/internal/ingestion/component/chunker/pdfcrop_cgo.go b/internal/ingestion/component/chunker/pdfcrop_cgo.goindex 704ecc6b4c..59c2557014 100644--- a/internal/ingestion/component/chunker/pdfcrop_cgo.go+++ b/internal/ingestion/component/chunker/pdfcrop_cgo.go@@ -209,7 +209,12 @@ func cropImageChunks(ctx context.Context, engine deepdoctype.PDFEngine, chunks [if len(single) == 0 {return}- img := util.CropSectionPositions(positions, single, deepdoctype.DlaScale)+ var img string+ if cropChunkType(ck) == "text" {+ img = util.CropTextSectionPositions(positions, single, deepdoctype.DlaScale)+ } else {+ img = util.CropSectionPositions(positions, single, deepdoctype.DlaScale)+ }// Release the page bitmaps now that cropping is done. They are no// longer needed and may be evicted under the LRU cap; the image// below is encoded from single and does not depend on the pages.@@ -283,14 +288,17 @@ func cropImageChunks(ctx context.Context, engine deepdoctype.PDFEngine, chunks [// set (general/token) it takes priority, so headings (CKType "heading") stay// excluded from preview cropping.func needsCrop(ck schema.ChunkDoc) bool {- typ := ck.CKType- if typ == "" {- typ = ck.DocType- }- switch typ {+ switch cropChunkType(ck) {case "image", "table", "text":return len(ck.PDFPositions) > 0 || len(ck.Positions) > 0default:return false}}++func cropChunkType(ck schema.ChunkDoc) string {+ if ck.CKType != "" {+ return ck.CKType+ }+ return ck.DocType+}diff --git a/internal/ingestion/component/chunker/pdfcrop_manual_test.go b/internal/ingestion/component/chunker/pdfcrop_manual_test.gonew file mode 100644index 0000000000..a141905a32--- /dev/null+++ b/internal/ingestion/component/chunker/pdfcrop_manual_test.go@@ -0,0 +1,135 @@+//go:build cgo && manual++package chunker++import (+ "encoding/base64"+ "encoding/json"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "ragflow/internal/agent/runtime"+ deepdocpdf "ragflow/internal/deepdoc/parser/pdf"+ infnative "ragflow/internal/deepdoc/parser/pdf/inference/native_analyzer"+ "ragflow/internal/deepdoc/parser/pdf/util"+ deepdoctype "ragflow/internal/deepdoc/parser/type"+ "ragflow/internal/ingestion/component"+ "ragflow/internal/ingestion/component/schema"+ parser "ragflow/internal/parser/parser"+ "ragflow/internal/storage"+)++// TestHongLouMengCropPreview exports previews for visual inspection of the+// Hong Lou Meng book fixture, using its Chinese chapter-heading pattern.+// PDF_CROP_INPUT and PDF_CROP_OUTPUT are required; MODEL_DIR selects DeepDoc models.+func TestHongLouMengCropPreview(t *testing.T) {+ input, output := os.Getenv("PDF_CROP_INPUT"), os.Getenv("PDF_CROP_OUTPUT")+ if input == "" || output == "" {+ t.Skip("PDF_CROP_INPUT and PDF_CROP_OUTPUT required")+ }+ data, err := os.ReadFile(input)+ if err != nil {+ t.Fatal(err)+ }+ originalAnalyzer := deepdoctype.NativeDocAnalyzerFactory+ t.Cleanup(func() { deepdoctype.SetNativeDocAnalyzerFactory(originalAnalyzer) })+ if err := infnative.Register(os.Getenv("MODEL_DIR"), infnative.DefaultDropScore); err != nil {+ t.Fatal(err)+ }+ p := parser.NewPDFParser()+ p.ConfigureFromSetup(map[string]any{"output_format": "json"})+ p.OnPageDone = func(done, total int) { t.Logf("parsed %d/%d pages", done, total) }+ result := p.ParseWithResult(t.Context(), filepath.Base(input), data)+ if result.Err != nil {+ t.Fatal(result.Err)+ }+ inputs := map[string]any{"name": filepath.Base(input), "file_type": "pdf", "output_format": result.OutputFormat, "json": result.JSON, "file": result.File, "bucket": "crop-test", "path": "source.pdf"}+ encoded, err := json.Marshal(inputs)+ if err != nil {+ t.Fatal(err)+ }+ if err := os.MkdirAll(output, 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(output, "parser.json"), encoded, 0644); err != nil {+ t.Fatal(err)+ }+ mem := storage.NewMemoryStorage()+ if err := mem.Put(t.Context(), "crop-test", "source.pdf", data); err != nil {+ t.Fatal(err)+ }+ prev := component.SetStorageFactoryOverride+ component.SetStorageFactoryOverride = func() storage.Storage { return mem }+ t.Cleanup(func() { component.SetStorageFactoryOverride = prev })+ for _, tc := range []struct {+ name string+ new func(map[string]any) (runtime.Component, error)+ }{+ {"general", NewGeneralChunker}, {"token", NewTokenChunker},+ {"group", NewGroupTitleChunker}, {"hierarchy", NewHierarchyTitleChunker},+ } {+ t.Run(tc.name, func(t *testing.T) {+ c, err := tc.new(map[string]any{"chunk_token_size": 512, "levels": [][]string{{`^第.{1,8}回`}}, "hierarchy": 1})+ if err != nil {+ t.Fatal(err)+ }+ out, err := c.Invoke(t.Context(), nil, inputs)+ if err != nil {+ t.Fatal(err)+ }+ if msg, ok := out["_ERROR"]; ok {+ t.Fatal(msg)+ }+ docs, _, err := schema.ChunkDocsFromAny(out["chunks"])+ if err != nil {+ t.Fatal(err)+ }+ count := 0+ for i := range docs {+ if docs[i].Image == "" {+ continue+ }+ png, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(docs[i].Image, "data:image/png;base64,"))+ if err != nil {+ t.Fatal(err)+ }+ name := fmt.Sprintf("%s-%03d.png", tc.name, i)+ if err := os.WriteFile(filepath.Join(output, name), png, 0644); err != nil {+ t.Fatal(err)+ }+ docs[i].Image = name+ count+++ }+ if count == 0 {+ t.Fatalf("no previews in %d chunks", len(docs))+ }+ b, err := json.MarshalIndent(docs, "", " ")+ if err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(output, tc.name+".json"), b, 0644); err != nil {+ t.Fatal(err)+ }+ t.Logf("%d chunks, %d previews", len(docs), count)+ })+ }+ engine, err := deepdocpdf.NewEngine(data)+ if err != nil {+ t.Fatal(err)+ }+ defer engine.Close()+ page, err := deepdocpdf.RenderPageToImage(engine, 0)+ if err != nil {+ t.Fatal(err)+ }+ png, err := util.EncodePNG(page)+ if err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(output, "page-001.png"), png, 0644); err != nil {+ t.Fatal(err)+ }+}diff --git a/internal/ingestion/component/chunker/pdfcrop_text_test.go b/internal/ingestion/component/chunker/pdfcrop_text_test.gonew file mode 100644index 0000000000..d460422613--- /dev/null+++ b/internal/ingestion/component/chunker/pdfcrop_text_test.go@@ -0,0 +1,101 @@+//go:build cgo++package chunker++import (+ "image"+ "image/color"+ "reflect"+ "strings"+ "testing"++ "ragflow/internal/deepdoc/parser/pdf/util"+ "ragflow/internal/ingestion/component/schema"+)++type textCropEngine struct {+ mockCropEngine+ page image.Image+}++func (e textCropEngine) RenderPageImage(int, float64) (image.Image, error) {+ return e.page, nil+}++func TestCropImageChunksPreservesCompleteTextLines(t *testing.T) {+ page := image.NewRGBA(image.Rect(0, 0, 240, 400))+ for y := 0; y < 400; y++ {+ for x := 0; x < 240; x++ {+ page.SetRGBA(x, y, color.RGBA{255, 255, 255, 255})+ }+ }+ for _, top := range []int{100, 200} {+ for y := top; y < top+24; y++ {+ for x := 40; x < 100; x++ {+ page.SetRGBA(x, y, color.RGBA{0, 0, 0, 255})+ }+ }+ }+ pos := jsonPositions(t, []float64{1, 10, 60, 112.0 / 3, 212.0 / 3})+ for _, tc := range []struct {+ name string+ ck schema.ChunkDoc+ wantRows int+ }{+ {"text", schema.ChunkDoc{Text: "two lines", CKType: "text", PDFPositions: pos}, 48},+ {"text fallback", schema.ChunkDoc{Text: "two lines", DocType: "text", Positions: pos}, 48},+ {"image", schema.ChunkDoc{Text: "figure", CKType: "image", PDFPositions: pos}, 24},+ {"table", schema.ChunkDoc{Text: "table", CKType: "table", PDFPositions: pos}, 24},+ } {+ t.Run(tc.name, func(t *testing.T) {+ out := cropImageChunks(t.Context(), textCropEngine{page: page}, []schema.ChunkDoc{tc.ck})+ if out[0].Text != tc.ck.Text || !reflect.DeepEqual(out[0].PDFPositions, tc.ck.PDFPositions) || !reflect.DeepEqual(out[0].Positions, tc.ck.Positions) {+ t.Fatal("preview correction changed text or source coordinates")+ }+ img, err := util.DecodeBase64PNG(strings.TrimPrefix(out[0].Image, "data:image/png;base64,"))+ if err != nil {+ t.Fatal(err)+ }+ rows := 0+ for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {+ // White margin identifies the body, excluding dimmed context.+ if color.RGBAModel.Convert(img.At(0, y)) == (color.RGBA{255, 255, 255, 255}) &&+ color.RGBAModel.Convert(img.At(20, y)) == (color.RGBA{0, 0, 0, 255}) {+ rows+++ }+ }+ if rows != tc.wantRows {+ t.Fatalf("visible text rows = %d, want %d", rows, tc.wantRows)+ }+ })+ }+}++func TestCropImageChunksKeepsSeparateMediaBoxes(t *testing.T) {+ page := image.NewRGBA(image.Rect(0, 0, 240, 400))+ for y := 0; y < 400; y++ {+ for x := 0; x < 240; x++ {+ page.SetRGBA(x, y, color.RGBA{255, 255, 255, 255})+ }+ }+ for _, typ := range []string{"image", "table"} {+ t.Run(typ, func(t *testing.T) {+ pos := jsonPositions(t,+ []float64{1, 10, 60, 112.0 / 3, 162.0 / 3},+ []float64{1, 10, 60, 162.0 / 3, 212.0 / 3})+ out := cropImageChunks(t.Context(), textCropEngine{page: page}, []schema.ChunkDoc{{Text: typ, CKType: typ, PDFPositions: pos}})+ img, err := util.DecodeBase64PNG(strings.TrimPrefix(out[0].Image, "data:image/png;base64,"))+ if err != nil {+ t.Fatal(err)+ }+ // Original media layout: 94px context, two 50px boxes, 170px+ // context, and four 6px gaps. Touching media boxes remain separate.+ if img.Bounds().Dy() != 388 {+ t.Fatalf("media height = %d, want 388", img.Bounds().Dy())+ }+ if got := color.RGBAModel.Convert(img.At(0, 150)); got != (color.RGBA{245, 245, 245, 255}) {+ t.Fatalf("media box separator = %v, want original gap", got)+ }+ })+ }+}