---
title: "Find Unicode line break opportunities in C#"
url: https://recursiveai.org/onefont/cookbook/break-text-into-allowed-line-wrap-points/
description: "Find where Unicode text may or must wrap in C# with UAX #14. Get UTF-16 offsets for legal line breaks before measuring text to fit a width."
version: "1.4.0"
---

# Find Unicode line break opportunities in C#

**onefont** is a free, open-source font and text-shaping library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneFont) · [GitHub](https://github.com/recursive-ai-oss/onefont-csharp) · [Docs](https://recursiveai.org/onefont/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/cookbook/break-text-into-allowed-line-wrap-points/break-text-into-allowed-line-wrap-points.zip)

```shell
dotnet add package RecursiveAI.OneFont
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onefont/cookbook/break-text-into-allowed-line-wrap-points/break-text-into-allowed-line-wrap-points.zip), then `dotnet run`.

Prints each allowed or mandatory break as the UTF-16 offset just after the break.

```text
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.

```csharp
const string crlf = "a\r\nb";
var flags = new byte[crlf.Length + 1];
TextSegmentation.FillLineBreakOpportunities(
    crlf, flags);
Console.WriteLine(string.Join(" ", flags));
```

```text
0 0 0 2 1
```

## Related recipes

- [Measure and wrap text](https://recursiveai.org/onefont/cookbook/measure-and-wrap-text/)
- [Find Unicode grapheme boundaries](https://recursiveai.org/onefont/cookbook/find-unicode-grapheme-boundaries/)
- [Find Unicode word boundaries](https://recursiveai.org/onefont/cookbook/find-unicode-word-boundaries/)

---

## 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](https://www.unicode.org/reports/tr14/tr14-55.html) 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](https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) 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](https://www.unicode.org/reports/tr29/#Word_Boundaries) 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](https://recursiveai.org/onefont/docs/shaping/) 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.
