PdfHyphenator class
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
Implements
Examples
Section titled “Examples”// 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"
Remarks
Section titled “Remarks”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.
Properties
Section titled “Properties”HyphenateAllCaps
Section titled “ HyphenateAllCaps”Whether words of all capitals are hyphenated too. Default false: most are acronyms and names.
public bool HyphenateAllCaps { get; init; }Property Value
Section titled “Property Value”MinLeft
Section titled “ MinLeft”The fewest letters before a hyphen (TeX’s \lefthyphenmin). Default 2.
public int MinLeft { get; init; }Property Value
Section titled “Property Value”Exceptions
Section titled “Exceptions”The value is less than 1.
MinRight
Section titled “ MinRight”The fewest letters after a hyphen (TeX’s \righthyphenmin). Default 3.
public int MinRight { get; init; }Property Value
Section titled “Property Value”Exceptions
Section titled “Exceptions”The value is less than 1.
MinWordLength
Section titled “ MinWordLength”The fewest letters a word needs to be hyphenated at all. Default 5, as CSS hyphens: auto does.
public int MinWordLength { get; init; }Property Value
Section titled “Property Value”Exceptions
Section titled “Exceptions”The value is less than 1.
Methods
Section titled “Methods”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)Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”Exceptions
Section titled “Exceptions”A pattern or exception is malformed.
FindBreaks(string)
Section titled “ FindBreaks(string)”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)Parameters
Section titled “Parameters”text string
Returns
Section titled “Returns”InsertSoftHyphens(string)
Section titled “ InsertSoftHyphens(string)”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)Parameters
Section titled “Parameters”text string
Returns
Section titled “Returns”WithExceptions(IEnumerable<string>)
Section titled “ WithExceptions(IEnumerable<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)Parameters
Section titled “Parameters”words IEnumerable<string>
Words with their only breaks marked, such as ta-ble; a word without a hyphen never breaks.
Returns
Section titled “Returns”Exceptions
Section titled “Exceptions”A word is empty or holds a digit, a dot or two hyphens in a row.