Recursive AI

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.

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

Terminal
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 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 สวัสดีครับผมชื่อสมชาย

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

All onefont Go recipes