text

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 5 Imported by: 0 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

Package text shapes strings into grapheme clusters with terminal-cell widths and line-break opportunities for inline layout. Shaper caches shaping results.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BreakClass

type BreakClass int

BreakClass describes the line-break opportunity BEFORE a grapheme cluster. Values mirror the simplified UAX.

const (
	// BreakNone means there is no line-break opportunity before this cluster.
	BreakNone BreakClass = iota

	// BreakSoft is a discretionary break: at word boundaries (between a
	// whitespace cluster and the following non-whitespace cluster) and at
	// U+00AD (SOFT HYPHEN). When a soft-hyphen break is taken, a "-" is
	// rendered at the end of the line.
	BreakSoft

	// BreakMandatory is an unconditional break: LF, CR, CRLF, FF, U+0085
	// (NEL), U+2028 (LINE SEPARATOR), U+2029 (PARAGRAPH SEPARATOR).
	BreakMandatory

	// BreakAnywhere marks every cluster boundary as a break opportunity.
	// Assigned to CJK Unified Ideographs, Hangul, Hiragana, Katakana, and
	// emoji clusters.
	BreakAnywhere
)

type Cluster

type Cluster struct {
	Bytes      []byte     // UTF-8 grapheme bytes (no copy; refs source)
	CellWidth  int        // 0, 1, or 2 per East Asian Width + emoji presentation
	BreakClass BreakClass // line-break opportunity before this cluster
}

Cluster is a shaped extended grapheme cluster the atomic unit of text rendering and line-breaking in kite.

Bytes references the source string's backing memory without copying. Callers must not modify the slice and must ensure the source string outlives the returned clusters.

func Shape

func Shape(text string) []Cluster

Shape segments text into extended grapheme clusters, assigns East-Asian-Width cell widths, and classifies line-break opportunities.

The Bytes field of each returned Cluster aliases text's backing memory without copying. text must outlive the returned slice.

Returns nil for an empty string.

type Shaper

type Shaper struct {
	// contains filtered or unexported fields
}

Shaper shapes text into grapheme clusters and caches results by (text, fontSubsetID) under a configurable byte-budget LRU policy. fontSubsetID is always 0 in kitex v2 (single font subset).

Shaper is not safe for concurrent use.

func NewShaper

func NewShaper(budgetBytes int) *Shaper

NewShaper creates a Shaper with the given byte budget. Pass 0 to use the default 256 KiB budget.

func (*Shaper) MeasureRun

func (s *Shaper) MeasureRun(text string) int

MeasureRun returns the total display cell width of text, reusing the shaping cache.

func (*Shaper) Shape

func (s *Shaper) Shape(text string) []Cluster

Shape returns the grapheme clusters for text, using fontSubsetID 0. The same []Cluster pointer is returned on every cache hit.

Jump to

Keyboard shortcuts

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