phppdf Docs

Knuth-Liang TeX hyphenation for phppdf

Source ↗

Documentation

Getting started

Installation

composer require phppdf/tex-hyphenator

Your first hyphenation

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-hyphenator/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

LocaleFile
af-ZAresources/hyphenation/af-ZA.tex
en-GBresources/hyphenation/en-GB.tex
en-USresources/hyphenation/en-US.tex
en-ZAresources/hyphenation/en-ZA.tex
es-ESresources/hyphenation/es-ES.tex
fr-FRresources/hyphenation/fr-FR.tex
nl-NLresources/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));