textshape

package
v0.5.57 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 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

Package textshape provides the pure-Go shaping foundation for MeUI.

It turns a UTF-8 string into visual runs with bidi levels, script segmentation, glyph IDs, advances and clusters. It deliberately has no renderer or widget dependency: the toolkit can use it for measurement and selection today, and renderer backends can grow a glyph-ID draw path later.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LineBreakOffsets added in v0.5.41

func LineBreakOffsets(text string) []int

LineBreakOffsets returns byte offsets where UAX #14 permits a line break, excluding the end of the string. It covers scripts without spaces as well as punctuation rules that a naive "break every rune" wrapper violates.

Types

type Direction

type Direction uint8

Direction is the paragraph's base direction.

const (
	LTR Direction = iota
	RTL
)

type Glyph

type Glyph struct {
	GlyphID  uint16  `json:"glyph_id"`
	Cluster  int     `json:"cluster"`
	Runes    int     `json:"runes"`
	Advance  float32 `json:"advance"`
	XOffset  float32 `json:"x_offset"`
	YOffset  float32 `json:"y_offset"`
	XBearing float32 `json:"x_bearing"`
	YBearing float32 `json:"y_bearing"`
	Width    float32 `json:"width"`
	Height   float32 `json:"height"`
}

Glyph is one shaped glyph. Cluster is a rune index into the original string (global across runs), not a byte offset.

type Run

type Run struct {
	Text      string    `json:"text"`
	Start     int       `json:"start"` // rune index into the input
	End       int       `json:"end"`   // exclusive rune index
	Direction Direction `json:"direction"`
	Level     uint8     `json:"level"`
	Script    string    `json:"script"`
	Advance   float32   `json:"advance"`
	Glyphs    []Glyph   `json:"glyphs"`
}

Run is a visually contiguous run with one direction and one script.

func VisualRuns added in v0.5.18

func VisualRuns(runs []Run) []Run

VisualRuns reorders logical runs into left-to-right visual slots using the UAX #9 L2 reversal pass. Levels come from the bidi run output. A renderer then lays each run out in its slot; RTL glyphs inside a run are already in visual order.

type Shaper

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

Shaper owns a parsed font and reusable shaping/bidi state. It is safe for reuse, but callers should not use one Shaper concurrently.

func New

func New(data []byte) (*Shaper, error)

New parses a TTF and returns a Shaper. OTF/CFF support follows the typesetting parser; MeUI's core font loader still accepts TTF only.

func (*Shaper) Face

func (s *Shaper) Face() *font.Face

Face exposes the parsed face for future fallback/font-map work.

func (*Shaper) Shape

func (s *Shaper) Shape(text string, size float32, base Direction) ([]Run, error)

Shape shapes text at size pixels. base is the paragraph direction; bidi overrides it when the text contains strong directional characters.

Runs are returned in logical order (bidi levels are preserved on each run). A renderer laying out a line reorders runs by level for visual placement. Glyph clusters remain indices into the original text, so selection and cursor mapping do not need a second rune-to-glyph table.

Jump to

Keyboard shortcuts

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