Recursive AI

Split mixed RTL and LTR text into bidi runs in Go

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

Call ResolveBidi, then read Runs in display order.

Terminal
go get recursiveai.org/go/onefont

Requires Go 1.24+. No font needed.

GO
package main
 
import (
	"fmt"
 
	"recursiveai.org/go/onefont"
)
 
func main() {
	text := "abc אבג 123"
	bidi := onefont.ResolveBidi(
		text, onefont.DirectionAuto,
	)
	for run := range bidi.Runs(0, len(text)) {
		fmt.Printf("%q RTL=%t\n",
			text[run.Start:run.End], run.RTL())
	}
}
Text
"abc " RTL=false
"123" RTL=false
"אבג " RTL=true

The string is stored as abc, then Hebrew, then 123; the browser reorders it on screen.

The number run appears before the Hebrew run in visual order, and it still reads left to right. The printed Hebrew substring itself is unchanged.

Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onefont, then go run .; or unzip the example and go run ..

Variations

Use a right-to-left paragraph base. Replace the ResolveBidi call. This moves abc to the rightmost run; the digits still form a left-to-right run.

GO
bidi := onefont.ResolveBidi(
    text, onefont.DirectionRTL,
)
Text
"123" RTL=false
" אבג " RTL=true
"abc" RTL=false

The short answer

Call ResolveBidi with the original string and a base direction. Iterate bidi.Runs(0, len(text)) for one line in visual order, from its left edge to its right edge. run.Start and run.End are byte offsets in the original Go string, so text[run.Start:run.End] gives you the original substring. Call run.RTL() to decide how that piece reads.

What is bidirectional text?

Bidirectional text combines material that reads right to left, such as Hebrew or Arabic, with material that reads left to right, such as Latin words or numbers. A split at spaces or at every script change doesn’t resolve the direction of spaces, punctuation, or digits. The Unicode Bidirectional Algorithm assigns embedding levels to resolve those cases. With DirectionAuto, the first strong directional character sets the paragraph base for this example. Specify DirectionLTR or DirectionRTL when a document already gives you the base direction.

What is the difference between logical and visual order in bidi text?

Logical order is the order in which characters are stored; visual order is how they are placed on a line. The sample prints its runs in visual order, but each slice is still in logical order. That’s why the Hebrew substring isn’t printed as reversed text. Hand that original slice and its direction to the shaper instead of reversing bytes or runes yourself. Reversing bytes can break UTF-8, and reversing runes can separate a combining mark from its base.

How do you handle bidi runs when text wraps?

Resolve the paragraph as a whole, then pass each actual line’s start and end byte offsets to Runs. The line routine applies the bidi rules for those bounds, so the same paragraph can produce different runs depending on where it wraps. ResolveBidi doesn’t choose line breaks or turn characters into glyphs. If the next step is to shape a line with font fallback, ShapeText combines bidi resolution with font selection and shaping for the text you pass it.

All onefont Go recipes