Class LineBreaks
Finds where lines may break: Unicode's line breaking algorithm (UAX #14, Unicode 17.0) from Egami's own tables, with CSS
Text 3's word-break and line-break switches. No shaping is involved, and there are no dictionaries: Thai, Lao,
Khmer and Myanmar break only at spaces and zero width spaces.
public static class LineBreaks
- Inheritance
-
LineBreaks
- Inherited Members
Examples
// Where lines may end: after spaces and hyphens, and always at the end; a line must end after a newline.
List<int> breaks = [];
foreach (LineBreak b in LineBreaks.Enumerate("A well-known fact.\nNext"))
{
breaks.Add(b.Index); // 2, 7, 13, 19 (mandatory), 23
}
// CSS switches: break-all lets letters break like ideographs.
LineBreakOptions breakAll = new(WordBreak: WordBreak.BreakAll);
int count = 0;
foreach (LineBreak b in LineBreaks.Enumerate("word", breakAll))
{
count++; // 4: after every letter
}
Remarks
With Strict, which is UAX #14's own rules, every break Unicode's LineBreakTest.txt lists
is found; the default (CSS's auto) also breaks before small kana. An object replacement character (U+FFFC, as
placeholders use) breaks before and after. The enumerator walks the text once and allocates nothing.
Methods
Enumerate(ReadOnlySpan<char>, LineBreakOptions)
Enumerates the break opportunities of a text, in order; the last is always at the end of the text.
public static LineBreakEnumerator Enumerate(ReadOnlySpan<char> text, LineBreakOptions options = default)
Parameters
textReadOnlySpan<char>The text.
optionsLineBreakOptionsThe CSS switches; the default is CSS's.
Returns
- LineBreakEnumerator
An enumerator of the breaks. An empty text has none.
Examples
// Where lines may end: after spaces and hyphens, and always at the end; a line must end after a newline.
List<int> breaks = [];
foreach (LineBreak b in LineBreaks.Enumerate("A well-known fact.\nNext"))
{
breaks.Add(b.Index); // 2, 7, 13, 19 (mandatory), 23
}
// CSS switches: break-all lets letters break like ideographs.
LineBreakOptions breakAll = new(WordBreak: WordBreak.BreakAll);
int count = 0;
foreach (LineBreak b in LineBreaks.Enumerate("word", breakAll))
{
count++; // 4: after every letter
}