fix: merge per-glyph CID text and extract Form XObject content#76
Merged
Conversation
wkhtmltopdf emits one Tj per glyph with explicit Td kern moves, producing one TextElement per character. Callers concatenating elements with a space separator produce 'D O M I C I L I O' instead of 'DOMICILIO ESTANDAR'. Changes: - Add mergeAdjacentElements(): collapses same-line, positionally-adjacent TextElements into word-level runs using glyphMergeGapFactor=1.5x threshold - Add canMerge(): predicate encoding same-font, same-line, gap constraints - Add AssembleText(): spatial word-gap detection replaces naive 'elem + space' - Apply mergeAdjacentElements at end of ExtractFromPage - Update document.go ExtractTextFromPage and page.go ExtractText to use AssembleText instead of naive concatenation with trailing space - Add Form XObject (Do operator) support: processFormXObject, resolveXObject, getXObjectResources with 8-level recursion guard - Add test PDFs: andreani_shipping_label.pdf, correo_argentino_label.pdf - Add glyph_merge_test.go: 28 tests for canMerge, mergeAdjacentElements, AssembleText, and integration tests against real shipping label PDF - Add form_xobject_test.go: 14 tests for Form XObject extraction
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up fix for #74 — two additional issues discovered by testing with real-world shipping label PDFs from @iv7dev:
Tjper glyph withTdrepositioning, producing"D O M I C I L I O". NewmergeAdjacentElements()collapses single-glyph TextElements into words, andAssembleText()replaces naive concatenation with spatial gap detection.Dooperator. NewprocessFormXObject()with resource stack push/pop and 8-level recursion guard extracts text from nested XObjects.Fixes #74
Test plan
go fmt ./...cleango vet ./...cleango test ./...— 23 packages, ALL pass (0 FAIL)golangci-lint run— no new issues"DOMICILIO\nESTANDAR\nN° de seguimiento: 360002865586280..."(was"D O M I C I L I O")