---
title: "Find Thai line break opportunities in Go"
url: https://recursiveai.org/onefont/go/cookbook/find-thai-line-breaks/
description: "Find Thai line break opportunities without splitting on spaces. Get UTF-8 byte offsets with onefont-go’s built-in dictionary before measuring lines."
version: "1.1.1"
---

# Find Thai line break opportunities 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/find-thai-line-breaks/find-thai-line-breaks.zip)

Use `AppendLineBreaks` to find Thai wrap points where there are no spaces. It finds candidates, not the lines that fit.

```shell
go get recursiveai.org/go/onefont
```

```go
package main

import (
	"fmt"

	"recursiveai.org/go/onefont"
)

func main() {
	text := "สวัสดีครับผมชื่อสมชาย"
	var segmenter onefont.Segmenter
	start := 0
	breaks := segmenter.AppendLineBreaks(nil, text)
	for _, b := range breaks {
		fmt.Printf("%d %s\n", b.Pos, text[start:b.Pos])
		start = b.Pos
	}
}
```

```text
18 สวัสดี
30 ครับ
36 ผม
48 ชื่อ
63 สมชาย
```

Each line shows a candidate end byte and the text since the previous candidate. These are **not** the lines a layout engine would draw.

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/find-thai-line-breaks/find-thai-line-breaks.zip) and `go run .`.

Requires Go 1.24 or later.

## Variations

**Compare with Unicode’s rules alone.** Replace the declaration of `segmenter` to turn the dictionary off:

```go
segmenter := onefont.Segmenter{NoDictionary: true}
```

```text
63 สวัสดีครับผมชื่อสมชาย
```

## Related recipes

- [Iterate Unicode grapheme clusters in Go](https://recursiveai.org/onefont/go/cookbook/split-string-into-grapheme-clusters/)
- [Choose an emoji fallback font in Go](https://recursiveai.org/onefont/go/cookbook/shape-text-with-emoji-fallback/)

## The short answer

Use a zero-value `onefont.Segmenter` and call `AppendLineBreaks(nil, text)`. The built-in dictionary adds break opportunities inside Thai letter runs. `Pos` is a UTF-8 byte offset that you can use to slice the original Go string. No font or target width is needed to find these opportunities.

## How do you break Thai text without spaces?

Don’t split on spaces or after a fixed number of runes. In `สวัสดีครับผมชื่อสมชาย`, the first internal opportunity is after `สวัสดี`, at byte 18. A width-independent split on spaces would find no internal breaks. [Unicode’s line-breaking algorithm](https://www.unicode.org/reports/tr14/#SA) leaves this kind of letter run to language-specific analysis. onefont-go matches against its built-in word lists for Thai, Lao, Khmer and Burmese.

The dictionary may not contain every name or term in your text. An unmatched stretch stays together until the next word the dictionary recognizes. The result is a set of allowed break positions, not a general-purpose Thai tokenizer. `NoDictionary` switches off only dictionary line breaking, not the other Unicode line-breaking rules or the grapheme iterator.

## How do you wrap Thai text to fit a width?

Find the opportunities first, then use your font and size to measure candidate lines. For a simple greedy wrap, start from the beginning of the line and keep the last legal end that fits. At a mandatory break, end the line even if there is room. `Pos` includes the point after a hard break such as a newline; separate that separator from the text you draw.

The width check needs shaped advances, not bytes or a count of dictionary words. Shape the text you intend to draw; with a fixed font, scale the resulting advance from font units to pixels using the font’s units per em. If no candidate fits the line, decide whether your application will allow overflow or use a separate emergency-break policy. A grapheme boundary won’t cut a combining sequence in half, but that does not make it a normal Thai line-break opportunity.

The last candidate is normally the end of the paragraph, not a request to insert a newline. Check `Mandatory` rather than treating every returned offset as a forced line end. If you need to keep the same segmenter for several paragraphs, reuse it in one goroutine or use a separate one for each concurrent worker.
