---
title: "Split mixed RTL and LTR text into bidi runs in Go"
url: https://recursiveai.org/onefont/go/cookbook/resolve-bidirectional-text-runs/
description: "Use Unicode bidirectional resolution in Go to split mixed Hebrew and Latin text into runs in display order and inspect each run’s direction."
version: "1.1.1"
---

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

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onefont) · [GitHub](https://github.com/recursive-ai-oss/onefont-go) · [Docs](https://recursiveai.org/onefont/go/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/go/cookbook/resolve-bidirectional-text-runs/resolve-bidirectional-text-runs.zip)

Call `ResolveBidi`, then read `Runs` in display order.

```shell
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](https://recursiveai.org/onefont/go/cookbook/resolve-bidirectional-text-runs/resolve-bidirectional-text-runs.zip) 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
```

## Related recipes

- [Measure text width in Go with a font](https://recursiveai.org/onefont/go/cookbook/measure-shaped-text-width/)
- [Go font and shaping quickstart](https://recursiveai.org/onefont/go/docs/getting-started/)

## 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](https://www.unicode.org/reports/tr9/) 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.
