Skip to content

feat(pdf): HarfBuzz/FriBidi complex-script shaping for Unicode XObjects - #36

Merged
vitormattos merged 11 commits into
mainfrom
feat/issue33-harfbuzz-shaping
Nov 24, 2026
Merged

vitormattos merged 11 commits into
mainfrom
feat/issue33-harfbuzz-shaping

Conversation

@vitormattos

@vitormattos vitormattos commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Purpose

Implements complex-script shaping for #33. Stacked on #35, which depends on SignerPHP/pdf-core#5. Keep this PR in draft until its dependencies are integrated and a downstream signed-PDF release is verified.

Design

  • Reuse HarfBuzz for GSUB/GPOS and FriBidi for bidi paragraph and visual-run handling rather than maintaining a custom Arabic Presentation Forms shaper.
  • Keep direct-glyph layout for simple scripts, and carry actual shaped glyph IDs, Unicode cluster associations, advances and offsets into positioned PDF text operators.
  • Embed full Unicode Type0/CIDFontType2 fonts with ToUnicode and logical ActualText; the feat(pdf): embed Unicode font and image objects in signed XObjects #35 bridge supplies indirect object references for the destination signed document.
  • Execute native binaries via Symfony Process (argument vector, fixed timeout, UTF-8 locale, input/run limits); use private temporary font data with deterministic cleanup.
  • Resolve missing coverage with fallback across font runs in the same line, without separating a grapheme across fonts. Preserve bidi order, shaping context, actual glyph offsets and original Unicode when switching fonts.
  • Share the glyph planning algorithm between measurement and rendering; reuse one resource plan within a document build.

Evidence

  • PHPUnit: 1,145 tests / 8,378 assertions on PHP 8.3 at 7f1f22b, combined with the latest feat(pdf): embed Unicode font and image objects in signed XObjects #35 fixes. The Unicode integration suite has 62 cases / 930 assertions (35 additional cases), including all six literal examples from Support Unicode text in PDF XObject output #33 together in one PDF.
  • Named data providers: 13 single-script cases, 9 mixed-font lines, 11 complex-script cases, 5 HTML/context input variants, 6 negative coverage cases, 3 shaped-width cases, 10 native HarfBuzz GID/position comparisons, and 3 grapheme-safe wrapping regressions. Two standalone tests cover the six-language document and ASCII compatibility.
  • 39 positive PDF cases compare logical extraction and identical nonblank monochrome rasters from the standalone exporter and an actual pdf-core signature widget serialized as an incremental PDF update, without altering the preceding revision. The released core API is exercised by explicitly invoking the resource-builder callback before attaching the appearance. This does not claim that the widget fixture is cryptographically signed.
  • Actual regression: the old automatic word breaker split decomposed Latin accents, Hebrew niqqud and Arabic vowel marks from their base. All three provider cases failed before the fix; the grapheme-based implementation passes.
  • CI on Ubuntu 26.04 exposed three additional combining-mark discrepancies against HarfBuzz. Explicit primary-script matching and inheriting the base script for combining marks correct the segmentation, independent of Script_Extensions data. Three additional unit data sets cover inherited/script-specific marks; two reproduced the bug locally. Targeted Infection for FriBidiParagraphMapper and TextLineBreaker: 199/199 killed, 100% MSI with no skipped or errored mutants.
  • PHPCS, PHPMD, Psalm, Rector and duplication checks passed locally.
  • Targeted Infection on the revised UnicodeTextResources (81/81), UnicodeFontPdfWriter (112/112), StandardFontMetrics (MSI 100%), HarfBuzzRunShaper (67/67), NativeTextCommandRunner (4/4), FriBidiParagraphMapper (100%) and TemplateDocumentBuilder (127/127).
  • Latest targeted Infection: 506 mutations, 505 killed, one errored mutant, zero survivors/uncovered; reported MSI and mutation code coverage 100%.
  • Local cross-project E2E with merged pdf-core main 1f55bbc and pdf-signer main 12031a0: synthetic SHA-256 CMS PDF signature valid in SignerPHP and Poppler pdfsig. Poppler and MuPDF independently rendered and extracted the six required languages and mixed-script lines; the test used a self-signed synthetic certificate, not production trust.
  • Documentation: Unicode fonts.

Open-source comparison

Merge / issue closure

  1. feat(appearance): materialize indirect XObject resources in destination PDF SignerPHP/pdf-core#5 is merged; release the API (latest published version at review time: v0.1.2).
  2. Validate and merge feat(pdf): embed Unicode font and image objects in signed XObjects #35, then this stacked PR after its current CI and review pass.
  3. Verify a consuming LibreSign/SignerPHP application with the published pdf-core version, including PDF signature validity and rendered/copyable Arabic, Hebrew and CJK.
  4. Do not close Support Unicode text in PDF XObject output #33 until dependencies and downstream tests pass.

Known boundaries

  • Configured TrueType fonts may now cover different runs in one line. Each grapheme must be covered by a single font; unsupported glyphs still fail explicitly. The implementation is not a general-purpose browser layout engine.
  • Native HarfBuzz/FriBidi tools are necessary for complex text; embedding full fonts increases output size.
  • PDF extractors differ in how they present RTL strings. Regression tests verify semantic ActualText and normalize Poppler's visual-order bidi presentation for the extraction check.

No merge or issue closure requested by this PR.

Add isolated native shaping, bidirectional run segmentation, GPOS glyph
placement and logical text extraction support for Arabic, Hebrew and
combining marks. Preserve the existing direct Unicode path and reject
unsafe native output or excessive shaping workloads.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Validate real PDF round trips for Arabic, Hebrew, ligatures, combining
marks and mixed-script names. Add negative/data-provider coverage for
untrusted native output, unsafe inputs, timeouts, temporary font files
and glyph cluster boundaries.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Install HarfBuzz and FriBidi tools for integration, coverage and
mutation CI jobs without modifying runtime deployment settings.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Reject invalid explicit glyph identifiers before emitting PDF objects,
use scalar-only font selection and eliminate duplicate shaping when
building the content stream and resource dictionary. Preserve glyph
placement and existing public builder methods.

Refs: #33

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

Cover absolute glyph offsets and equal-cluster order, shared shaping
per document, invalid shaped glyph IDs, and Unicode widths. Preserve
existing script round trips with targeted regressions.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Describe trusted font configuration, HarfBuzz/FriBidi deployment,
validating signed PDFs, full font embedding and interoperability
limitations without changing README usage examples.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Compile the exact Western, Greek, Cyrillic, Arabic, Hebrew and Japanese
examples with fallback fonts and assert two embedded Unicode resources,
logical extraction and six preserved lines in one exported PDF.

Refs: #33

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Select fonts without splitting grapheme clusters, preserve bidi order and shaping context, and use a common glyph plan for measurement and rendering. Cover mixed-script rendering, fallback boundaries, resource mappings and logical text extraction.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Exercise Unicode input, supplementary scalars, mixed-font fallback, unsupported coverage and native shaping through named data providers. Verify the destination PDF signature appearance with the actual core object allocator, independent Poppler extraction and matching raster output.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Split automatic hyphenation candidates at extended grapheme boundaries rather than individual Unicode scalars. Regressions cover Latin accents, Hebrew niqqud and Arabic vowel marks with actual embedded-font metrics.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Use explicit primary script properties and keep combining marks attached to their base across PCRE Unicode database versions. Add run segmentation regressions. Return the initialized grapheme match list directly to remove an equivalent mutation while preserving invalid UTF-8 behavior.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Base automatically changed from feat/issue33-signerphp-resource-bridge to main November 24, 2026 02:37
@vitormattos
vitormattos marked this pull request as ready for review November 24, 2026 02:39
@vitormattos
vitormattos merged commit da2d1d8 into main Nov 24, 2026
24 checks passed
@vitormattos
vitormattos deleted the feat/issue33-harfbuzz-shaping branch November 24, 2026 02:39
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.

Support Unicode text in PDF XObject output

1 participant