phppdf / tex-hyphenation
Knuth-Liang TeX hyphenation algorithm for phppdf
Requires
- php: >=8.4
- phppdf/phppdf: @dev
Requires (Dev)
- phpbench/phpbench: ^1.6
- phppdf/coding-standard: dev-master
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.1
This package is auto-updated.
Last update: 2026-06-18 20:06:54 UTC
README
A pure PHP implementation of the Knuth-Liang TeX hyphenation algorithm. It splits a word into the fragments allowed
at valid hyphenation points, using the same pattern format as TeX (e.g. hy3ph, .ach4).
This package implements the PhpPdf\Text\Hyphenator interface from phppdf/phppdf,
so it plugs directly into TextBox and other phppdf text-layout components.
Requirements
- PHP 8.4+
ext-mbstring
Installation
composer require phppdf/tex-hyphenation
Usage
TeXHyphenator is constructed with an array of raw TeX pattern strings. Bundled pattern files for several locales
are included under resources/hyphenation/:
use PhpPdf\Text\TeXHyphenator; $patterns = file( __DIR__ . '/vendor/phppdf/tex-hyphenation/resources/hyphenation/en-US.tex', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES, ) ?: []; $hyphenator = new TeXHyphenator($patterns); $hyphenator->breakWord('hyphenation'); // ['hy', 'phen', 'a', 'tion']
The result is a list of fragments; join consecutive fragments with a hyphen wherever a line break is needed.
Minimum margins
leftMin and rightMin control how many characters must remain before the first break and after the last break,
respectively (TeX defaults: 2 and 3):
$hyphenator = new TeXHyphenator($patterns, leftMin: 3, rightMin: 3);
Bundled locales
| Locale | File |
|---|---|
af-ZA |
resources/hyphenation/af-ZA.tex |
en-GB |
resources/hyphenation/en-GB.tex |
en-US |
resources/hyphenation/en-US.tex |
en-ZA |
resources/hyphenation/en-ZA.tex |
es-ES |
resources/hyphenation/es-ES.tex |
fr-FR |
resources/hyphenation/fr-FR.tex |
nl-NL |
resources/hyphenation/nl-NL.tex |
Use with phppdf
use PhpPdf\Builder\TextBox; use PhpPdf\Text\TeXHyphenator; $box = TextBox::create($text, $metrics, 12, 200.0, hyphenator: new TeXHyphenator($patterns));
License
MIT