Skip to content

feat(pdf): embed Unicode TrueType fonts in XObject previews - #34

Merged
vitormattos merged 11 commits into
mainfrom
fix/unicode-pdf-font-33
Nov 23, 2026
Merged

vitormattos merged 11 commits into
mainfrom
fix/unicode-pdf-font-33

Conversation

@vitormattos

@vitormattos vitormattos commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Context

Refs #33; related to LibreSign/libresign#8155 and LibreSign/libresign#8236. The previous code wrote UTF-8 bytes directly to PDF literal strings with built-in Type1 fonts. Windows-1252 is not a complete solution.

Implemented in this draft

  • Configurable embeddable TrueType font files; PDF Type0/Identity-H, CIDFontType2, explicit CIDToGIDMap and ToUnicode mapping in the standalone PDF exporter.
  • Existing ASCII behavior preserved; Unicode characters without font coverage fail explicitly.
  • Independent, smaller components for sfnt table reads, Unicode cmap mapping, scalar decoding, glyph selection and indirect PDF font-object serialization.
  • Font parser hardening: bounded font sizes and table offsets, cmap bounds, glyph counts and embedding-right restrictions.
  • Replaced incomplete bespoke Arabic presentation-form logic with a direct mapper that fails closed for scripts requiring bidi/OpenType shaping. This is an intentional release blocker, not a claim of full Unicode coverage.
  • PHPUnit data providers for Latin accents, Greek, Cyrillic and Japanese, plus rejection of Arabic, Hebrew and combining marks until proper shaping is available.
  • Regressions for malformed fonts, invalid Unicode data and embedding restrictions.
  • No README changes; no font binaries added.

Review-driven CI fixes

  • Normalized Poppler's PDF page separator in extraction assertions.
  • Added the CJK font fixture to the mutation workflow.
  • Removed repeated apt-get update from affected workflows; use package installation against runner indexes only.
  • Refactored complexity hotspots and fixed Psalm/Rector/PHPMD findings instead of suppressing checks.

Evidence (local, current code)

  • PHP 8.3 / PHPUnit 10.5: 774 tests / 6497 assertions passed with Poppler.
  • PHPMD, PHP_CodeSniffer, Psalm and Rector dry-run passed.
  • git diff --check passed.
  • Before removal of the incomplete RTL shaper, PDF extraction succeeded for representative scripts; after removal, complex scripts have explicit rejection coverage. Visual validation and end-to-end signed-PDF integration remain necessary.

Must resolve before merge/release

  1. SignerPHP integration: its current XObject resource contract does not serialize the indirect Type0 font objects and embedded streams produced by this preview exporter. Do not pass internal __TrueTypeFont resources directly to SignatureAppearanceXObjectDto until a compatible adapter and signed-PDF integration tests exist.
  2. Complex text shaping: integrate a proven engine (e.g., HarfBuzz through an appropriate supported adapter) to handle Arabic/Hebrew, contextual substitutions, bidi, combining marks and glyph positioning. A custom presentation-forms implementation was removed after review.
  3. Font subsetting / size and licensing: whole-file embedding is supported but may be large; review production constraints and font licensing.
  4. Confirm GitHub Actions on this head and verify PDF rendering/extraction and signature validity end-to-end.

Draft by design. No new release should be cut from this branch until these blockers are resolved.

Implement Type0/Identity-H font resources, ToUnicode mappings, font metrics,
and basic contextual Arabic and RTL text rendering.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Exercise font parsing, Arabic joining, PDF export and multilingual
ToUnicode extraction; document configuration and test fixtures.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
…ported shaping

Split sfnt table parsing, Unicode cmap, glyph selection and PDF font
resource serialization into focused components. Remove incomplete custom
Arabic presentation shaping, strengthen font bounds and embedding checks,
and cover unsupported scripts and malformed fonts with regressions.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Install CJK font fixtures for mutation testing and avoid repeated
apt-get update in integration, coverage, and unit workflows.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Validate individual sfnt field reads against their declared table lengths,
reject invalid glyph metric indices and malformed cmap tables, and add
focused malformed-font regression cases.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Infection executes the full PHPUnit suite before mutation testing;
Unicode extraction integration tests require pdftotext. Ensure the
mutation runner installs the same PDF utility as integration CI.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Add boundary-focused data providers for UTF-8 and UTF-16BE encoding,
binary table reads, and format 4/12 cmap lookup and validation.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Drop unrelated Unicode documentation from PR #34.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
…onts

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
@vitormattos
vitormattos marked this pull request as ready for review November 23, 2026 22:45
@vitormattos
vitormattos merged commit 7ad569d into main Nov 23, 2026
24 checks passed
@vitormattos
vitormattos deleted the fix/unicode-pdf-font-33 branch November 23, 2026 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant