text

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Bidirectional resolution (UAX #9) for every text path in the family (stubbedev/lys-ui#68): mixed Hebrew/Arabic + Latin lines resolve into directional runs that the shaper lays out visually correct, and the editable widgets of lys-ui map caret motion through. This package is the single source of truth: render shapes the runs it returns here, and those widgets route their Direction through it, so no consumer forks reorder logic.

Positions stay rune indexes. BidiRuns returns the runs in visual left-to-right order while the spans it carries stay logical, and VisualOrder flattens the runs into the caret boundary sequence arrow keys step through. The resolver runs the full algorithm (explicit formatting characters and bracket pairs included), and the reorder here is exact for natural text — parity runs are then level runs — and approximate where explicit embeddings nest same-parity levels, which no label-level consumer relies on.

Resolution is cached by (text, direction) in the same spirit as the render shaping caches: entries are immutable and never invalidated, only evicted, so a label re-resolving per frame would be the only cost UAX #9 could add to the paint path.

Grapheme cluster segmentation (UAX #29) for lys-ui's editable text widgets: the unit Backspace, Delete, and caret motion move (stubbedev/lys-ui#57). A cluster is one user-perceived character — an emoji ZWJ family (👨‍👩‍👧, five runes), a flag (two regional indicators), a base rune with its combining marks or skin-tone modifier — and every position these primitives return is a cluster boundary, so a caret can never sit inside a character and a delete can never cut one in half.

Positions stay rune indexes; segmentation is a mapping on top. The shaping caret table, the IME byte offsets, the wrap row cache, and the undo snapshots are all rune-indexed, and ClusterRun maps between the two views in O(log n) over the boundary table. The primitives snap defensively in both directions, so even a position placed mid-cluster (public SetCursor, a wrap row edge) degrades to the enclosing cluster instead of corrupting text.

Package text segments text into words for word-wise editing, as lys-ui's Entry and TextArea do: ctrl+arrow motion, ctrl+backspace and ctrl+delete deletion, double-click selection, and the typing-run coalescing in the undo history.

Word segmentation is a rune-class rule, not UAX #29. A word is a maximal run of word runes — letters, digits, and underscore (IsWordRune) — and every other run (whitespace, punctuation) is a gap the word keys step over. The existing segmenters were checked first and rejected on that pinned rule: rivo/uniseg and go-text/typesetting's segmenter.WordIterator both implement UAX #29 word boundaries, whose rules glue mid-word punctuation to the word (can't, 3.14) and cut punctuation runs into segments of their own, so a word-left across "server.hostname.example" would park on the dots instead of skipping them. The class rule keeps the caret on real words, keeps double-click selection and word motion on one definition of a word, and matches the undo history's typing-run breaks, which already used it.

Since stubbedev/lys-ui#57 the unit under the class rule is the grapheme cluster, not the rune: uniseg's UAX #29 cluster segmentation (grapheme.go) splits rs into clusters, each cluster classified by its first rune, and words are maximal runs of same-class clusters. A combining mark, a skin-tone modifier, or a ZWJ continuation therefore extends the word it follows instead of reading as a boundary — "cafe\u0301" steps, deletes, and double-clicks as one unit. The exported functions still take and return rune indexes, so call sites are unchanged; clusters refine the unit under the same signatures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClusterRun

func ClusterRun(rs []rune, at int) (start, end int)

ClusterRun returns the [start, end) rune span of the grapheme cluster containing at, with at clamped into [0, len(rs)]. The empty slice yields (0, 0).

func ContinuesCluster

func ContinuesCluster(prev, next rune) bool

ContinuesCluster reports whether typing next directly after prev extends prev's grapheme cluster rather than starting a new one — a combining mark completing a base rune, a skin-tone modifier, a ZWJ join. It classifies the undo history's typing-run coalescing (lys-ui's widget/undo.go), where a mark typed after a word rune must not read as a run break. The two-rune test is an approximation at the edges: a second regional indicator after a complete flag pair reads as continuing, which only coarsens undo grouping, never text.

func IsWordRune

func IsWordRune(r rune) bool

IsWordRune reports whether r counts as a word character: a letter, a digit, or an underscore. It is the single word-class rule behind word motion, word deletion, double-click selection, and typing-run coalescing. The class applies to a whole grapheme cluster through its first rune (wordCluster below); the continuation runes of a cluster never carry a class of their own.

func LineSegments

func LineSegments(s string) []string

LineSegments splits s at its line-break opportunities (UAX #14), each segment a word with the spaces after it: "a/b c" gives "a/", "b ", "c". Invalid UTF-8 is one segment.

func NextCluster

func NextCluster(rs []rune, at int) int

NextCluster returns the caret-right and Delete target for position at: the boundary after the cluster containing it, stepping over the cluster whole. The slice end comes back unchanged, so a caret at the end stays put.

func PrevCluster

func PrevCluster(rs []rune, at int) int

PrevCluster returns the caret-left and Backspace target for position at: the start of the cluster before it, stepping out of the cluster containing at-1 whole. The slice start comes back unchanged, so a caret at 0 stays put.

func RTL

func RTL(s string, d Direction) bool

RTL reports whether s lays out right to left under d: DirectionRTL always, DirectionLTR never, DirectionAuto from the paragraph's first strong character. Neutrals-only text (digits, punctuation) resolves left to right, so a direction never flips content without a strong character asking for it. Start and end alignment mirror off this.

func SnapCluster

func SnapCluster(rs []rune, at int) int

SnapCluster returns at snapped down to the boundary of the cluster containing it — the leftmost caret position at or before at.

func SnapClusterForward

func SnapClusterForward(rs []rune, at int) int

SnapClusterForward returns at snapped up to the nearest cluster boundary: at itself when it already sits on one, otherwise the end of the cluster containing it. This is the rightmost caret position that does not skip past at — splice targets use it to park the caret after what was just inserted without eating into the following cluster.

func VisualOrder

func VisualOrder(rs []rune, runs []Run) []int

VisualOrder returns the caret boundaries of rs in visual left-to-right order under runs — the sequence the arrow keys step through. Elements are cluster-snapped rune indexes in [0, len(rs)], each appearing once, leftmost first; the logical order of the text never changes. Editing stays logical: callers move within this sequence and keep the rune indexes it hands back.

func VisualStep

func VisualStep(rs []rune, order []int, at, delta int) int

VisualStep returns the boundary one visual step from at: visual left for negative delta, visual right for positive, over the visual boundary order. at snaps to its cluster first, so a caret parked mid-cluster joins the sequence; the ends come back unchanged, so a caret at an edge stays put.

func VisualWordStep

func VisualWordStep(rs []rune, order []int, at, delta int) int

VisualWordStep returns the boundary one visual word away from at: over the shared word segmentation (words.go) walked in visual order, so ctrl+arrows land on the word edge the eye sees — word ends moving visually right, word starts moving visually left, gaps skipped whole, exactly the edges WordEnd and WordStart land on in logical order. Word classes come from rs; order carries the visual sequence. Like VisualStep the ends come back unchanged.

func WordBoundaries

func WordBoundaries(rs []rune) []bool

WordBoundaries reports, for every gap between the runes of rs, whether a word boundary sits there: element i covers the gap before rune i, element len(rs) the gap after the last rune. Boundaries sit between grapheme clusters only — never inside one — and where the word class changes: the edges of the word-cluster runs.

func WordEnd

func WordEnd(rs []rune, at int) int

WordEnd returns the column word-right motion lands on: the end of the word at or after at, stepping over any gap in between. Clusters are atomic: at snaps to its cluster first, and the walk steps cluster-wise. at is clamped into [0, len(rs)]; the slice end is returned unchanged, so a caret at the end stays put.

func WordRun

func WordRun(rs []rune, at int) (start, end int)

WordRun returns the span of the same-class run containing at: a word's [start, end) when at sits on a word cluster, the punctuation or whitespace run otherwise. Runs are cluster runs — a mark or modifier never splits one — and at is clamped into the slice; the empty slice yields (0, 0). Double-click selection uses this, so what it grabs is exactly what the ctrl+arrows steps land on.

func WordStart

func WordStart(rs []rune, at int) int

WordStart returns the column word-left motion lands on: the start of the word at or before at, stepping back over any gap (whitespace, punctuation) in between. Clusters are atomic: at snaps to its cluster first, and the walk steps cluster-wise. at is clamped into [0, len(rs)]; the slice start is returned unchanged, so a caret at 0 stays put.

Types

type Direction

type Direction uint8

Direction names the base paragraph direction text resolves and lays out with.

const (
	// DirectionAuto resolves the base from the paragraph's first strong
	// character (UAX #9 rules P2-P3). It is the zero value, so existing
	// LTR content is unchanged by default.
	DirectionAuto Direction = iota
	// DirectionLTR forces a left-to-right base.
	DirectionLTR
	// DirectionRTL forces a right-to-left base.
	DirectionRTL
)

type Run

type Run struct {
	Start, End int
	Level      int
	RTL        bool
}

Run is one directional run of a resolved paragraph: the [Start, End) rune span, the embedding level UAX #9 resolved it to, and whether it lays out right to left (odd level). Runs come back in visual left-to-right order; the spans they carry stay logical.

func BidiRuns

func BidiRuns(s string, d Direction) []Run

BidiRuns returns the directional runs of s under base direction d, in visual left-to-right order — the order the shaper lays pieces out in. Cached by (s, d): the second resolution of the same paragraph is a lookup, shared with every reader of the text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL