Skip to content

PdfHyphenator class

View as Markdown

Namespace: PdfNetStack.Drawing
Assembly: PdfNetStack.dll
Package: PdfNetStack

Finds where words may break with a hyphen, by Liang’s pattern algorithm as TeX defines it (Liang 1983, and The TeXbook, appendix H). Give one to Hyphenator to hyphenate a paragraph, or use InsertSoftHyphens on text of your own. The core library has the algorithm and no patterns: the PdfNetStack.Hyphenation package has them for many languages, and Create takes any others.

public sealed record PdfHyphenator : IEquatable<PdfHyphenator>

Inheritance

object ← PdfHyphenator

Implements

IEquatable<PdfHyphenator>

// Your own patterns, in TeX's format, and exceptions in \hyphenation's.
PdfHyphenator h = PdfHyphenator.Create(patterns, ["ta-ble", "project"]) with { MinLeft = 2, MinRight = 3 };
string text = h.InsertSoftHyphens("Hyphenation");   // "Hy\u00ADphen\u00ADation"

A hyphenator is immutable and safe to share between threads. Letters fold to lower case by invariant rules, so results do not depend on the current culture. These are left alone: words with a soft hyphen (U+00AD), whose breaks are the author’s; words joined to a digit, or holding a capital past the first letter; words of all capitals, unless HyphenateAllCaps; anything that looks like a web address, e-mail address or file path; words with letters outside the patterns’ alphabet; and words over 64 letters, as in TeX.

Whether words of all capitals are hyphenated too. Default false: most are acronyms and names.

public bool HyphenateAllCaps { get; init; }

bool

The fewest letters before a hyphen (TeX’s \lefthyphenmin). Default 2.

public int MinLeft { get; init; }

int

ArgumentOutOfRangeException

The value is less than 1.

The fewest letters after a hyphen (TeX’s \righthyphenmin). Default 3.

public int MinRight { get; init; }

int

ArgumentOutOfRangeException

The value is less than 1.

The fewest letters a word needs to be hyphenated at all. Default 5, as CSS hyphens: auto does.

public int MinWordLength { get; init; }

int

ArgumentOutOfRangeException

The value is less than 1.

Create(IEnumerable<string>, IEnumerable<string>?)

Section titled “ Create(IEnumerable<string>, IEnumerable<string>?)”

A hyphenator from patterns in TeX’s format, such as hy3ph and .ach4, and exceptions.

public static PdfHyphenator Create(IEnumerable<string> patterns, IEnumerable<string>? exceptions = null)

patterns IEnumerable<string>

Letters with a digit in any gap; a dot at either end marks the edge of a word.

exceptions IEnumerable<string>?

Words with their only breaks marked, such as ta-ble; a word without a hyphen never breaks.

PdfHyphenator

ArgumentException

A pattern or exception is malformed.

Where text may break with a hyphen: indexes of the characters that would start the next line, in order.

public IReadOnlyList<int> FindBreaks(string text)

text string

IReadOnlyList<int>

The text with a soft hyphen (U+00AD) at every place it may break. Layouts, viewers and browsers honor those without a hyphenator.

public string InsertSoftHyphens(string text)

text string

string

A copy of this hyphenator that also knows these words, which take precedence over the patterns and over earlier exceptions. MinLeft and MinRight still apply to them, as in TeX, and letters new to the alphabet join it.

public PdfHyphenator WithExceptions(IEnumerable<string> words)

words IEnumerable<string>

Words with their only breaks marked, such as ta-ble; a word without a hyphen never breaks.

PdfHyphenator

ArgumentException

A word is empty or holds a digit, a dot or two hyphens in a row.