---
title: "Get Unicode bidi levels and visual order in C#"
url: https://recursiveai.org/onefont/cookbook/resolve-bidi-levels-for-mixed-text/
description: "Get Unicode bidirectional embedding levels and visual order for mixed Hebrew, Latin, and digits in C#. No font or glyph shaping is needed."
version: "1.4.0"
---

# Get bidi levels and visual order 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/resolve-bidi-levels-for-mixed-text/resolve-bidi-levels-for-mixed-text.zip)

Order mixed-direction text without a font.

```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 \u05D0\u05D1 12";
int[] points = text.EnumerateRunes()
    .Select(r => r.Value).ToArray();
var levels = new byte[points.Length];
int paragraph = TextShaping.ResolveBidiLevels(
    points, TextDirection.Auto, levels);
TextShaping.ResetTrailingWhitespace(
    points, levels, paragraph);
int[] order = TextShaping.GetVisualOrder(levels);
Console.WriteLine($"Base: {paragraph}");
Console.WriteLine(string.Join(" ", levels));
Console.WriteLine(string.Join(" ", order));
```

Unzip [the example](https://recursiveai.org/onefont/cookbook/resolve-bidi-levels-for-mixed-text/resolve-bidi-levels-for-mixed-text.zip), then `dotnet run`.

For Latin `A`, Hebrew alef and bet, and the digits `12`, this prints the paragraph’s base level, then the levels in input order, then the input indices in left-to-right visual order.

```text
Base: 0
0 0 1 1 1 2 2
0 1 5 6 4 3 2
```

`ResolveBidiLevels` throws `ArgumentException` if the buffer has fewer entries than the input has code points.

## Variations

### Force a right-to-left paragraph

Replace the direction argument in the call in the snippet at the top. Use this when the paragraph’s direction comes from the surrounding layout rather than its first strong character.

```csharp
int paragraph = TextShaping.ResolveBidiLevels(
    points, TextDirection.RightToLeft, levels);
```

```text
Base: 1
2 1 1 1 1 2 2
5 6 4 3 2 1 0
```

## Related recipes

- [Lay out right-to-left text](https://recursiveai.org/onefont/cookbook/right-to-left-text/)
- [Shape code points with custom clusters](https://recursiveai.org/onefont/cookbook/shape-code-points-with-custom-clusters/)
- [Find Unicode line break opportunities](https://recursiveai.org/onefont/cookbook/break-text-into-allowed-line-wrap-points/)

---

## The short answer

Pass the paragraph’s Unicode code points to `TextShaping.ResolveBidiLevels`. It gives you the base level and one embedding level per input code point. Take the levels and code points for a line, call `ResetTrailingWhitespace` on that line, then pass its levels to `GetVisualOrder`. The resulting indices tell you which input positions appear from left to right on that line. They aren’t glyph IDs or UTF-16 offsets.

## How does the Unicode bidirectional algorithm order mixed text?

[Unicode Standard Annex #9](https://www.unicode.org/reports/tr9/tr9-51.html) defines how text in logical order (the order stored in the string) is arranged for display. The paragraph in the example starts with Latin `A`, so automatic direction chooses base level 0. Odd levels run right to left; even levels run left to right. The Hebrew letters at indices `2` and `3` are level 1. The digits at `5` and `6` are level 2: they move as part of the right-to-left span, but `1` still appears before `2`. Reversing a substring by hand wouldn’t preserve that distinction.

`EnumerateRunes()` keeps supplementary code points together instead of assigning a level to each half of a surrogate pair. If your editor stores UTF-16 positions, keep a mapping from each code point’s index to its UTF-16 offset. And keep the original string in logical order; use the visual order to drive layout rather than saving reordered text.

### Why reset trailing whitespace before getting visual order?

Resolve the paragraph once so surrounding text can determine the levels of neutral characters such as spaces. Once you know where each line wraps, take its segment of the paragraph’s levels and apply the whitespace reset to that line. This is rule L1: among other resets, trailing whitespace returns to the base level. Then reorder that line with rule L2. Reordering the entire paragraph and cutting it into lines afterward doesn’t apply those rules at your actual line ends.

## What happens to Unicode bidi control characters?

Some characters affect the resolution without appearing in visual order. The [OneFont shaping guide](https://recursiveai.org/onefont/docs/shaping/) says that rule X9 removals, including the embedding and override controls, get `TextShaping.BidiRemoved` (`255`). `GetVisualOrder` skips those entries, so its output can be shorter than the input. Keep the levels if you need to relate the output back to source positions; don’t treat `255` as an odd level that runs right to left.

This recipe stops at code-point order. A renderer still needs to choose a font, shape text into glyphs, and handle display details such as mirrored punctuation and combining marks. The index list alone isn’t a shaped line and shouldn’t be turned into a string for a text control that already runs the bidi algorithm.
