Recursive AI

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.

Order mixed-direction text without a font.

Terminal
dotnet add package RecursiveAI.OneFont

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, 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.

C#
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

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

All onefont recipes