dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; const string text = "a-b c\u00A0d\n漢字"; var breaks = TextSegmentation .GetLineBreakOpportunities(text); for (int i = 0; i < breaks.Length; i++) if (breaks[i] != LineBreakOpportunity.None) Console.WriteLine($"{i}: {breaks[i]}");
Unzip the example, then dotnet run.
Prints each allowed or mandatory break as the UTF-16 offset just after the break.
2: Allowed 4: Allowed 8: Mandatory 9: Allowed 10: Allowed
The array-returning call sizes its own buffer. If you supply a buffer with fewer than text.Length + 1 entries, the buffer overload throws ArgumentException.
Variations
Fill a byte buffer for text containing CRLF
Append this to the snippet at the top. The byte values are 0 for no break, 1 for allowed, and 2 for mandatory.
const string crlf = "a\r\nb"; var flags = new byte[crlf.Length + 1]; TextSegmentation.FillLineBreakOpportunities( crlf, flags); Console.WriteLine(string.Join(" ", flags));
0 0 0 2 1Related recipes
The short answer
Call TextSegmentation.GetLineBreakOpportunities(text) to get one result for each UTF-16 offset, including the end of the string. Allowed means a line can end just before that offset. Mandatory means it must end there, as after a newline. None means it can’t end there under the default rules. This call doesn’t choose which allowed break to take, and it needs neither a font nor a width.
How does the Unicode line breaking algorithm find wrap points?
Unicode Standard Annex #14 defines break opportunities by context. OneFont uses the Unicode 17.0 rules. In the example, offset 2 follows the hyphen, and offset 4 follows the ordinary space. Offset 8 is mandatory because it follows \n. The two Han ideographs can go on separate lines at offset 9, even though there’s no space between them. Offset 10 is the end of the string: OneFont reports that as Allowed unless the input ends with a hard break, in which case it’s Mandatory.
For a width-based wrapper, measure candidate spans with the same font and size you intend to draw. Pick a permitted break that fits, and don’t continue across a mandatory one. If nothing fits, decide whether to allow overflow or use a separate emergency-break policy. The allowed positions are not a list of lines or widths. The result also doesn’t remove newline characters or trim spaces from the text you draw.
What does a Unicode nonbreaking space do?
A nonbreaking space (U+00A0) keeps adjacent text together. In the sample, c\u00A0d has no allowed break at either edge of the space, unlike the ordinary space before c. This is why splitting a string on whitespace doesn’t give you the same wrap points. The CRLF example makes a different distinction: it has one mandatory break after the pair, not one after each code unit.
How are line breaks different from word and grapheme boundaries?
A grapheme boundary is a place to split user-perceived characters, useful for cursor movement. There are grapheme boundaries between the letters of a word, but ordinary line wrapping doesn’t split the word there. A word boundary is useful for selection or navigation; it doesn’t tell you which edge of punctuation or whitespace is a suitable line ending. Use the line-break result for wrapping rather than substituting either of those boundary lists.
OneFont’s default line-break rules don’t use dictionaries. For Thai, Lao, Khmer, and Myanmar, they don’t infer word breaks in continuous runs of letters. Where a wrap is needed in such a run, text with an explicit zero-width space can provide a break that these rules recognize.