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.
- PHP 8.4+
ext-mbstring
composer require phppdf/tex-hyphenationTeXHyphenator 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.
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);| 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 PhpPdf\Builder\TextBox;
use PhpPdf\Text\TeXHyphenator;
$box = TextBox::create($text, $metrics, 12, 200.0, hyphenator: new TeXHyphenator($patterns));MIT