textrender

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 25 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 textrender lays out and draws text for giode's text elements.

Gio draws text as vector paths, which its GPU renderer rasterizes again on every frame - for a screen of text, most of the frame. This package draws each line of text as an image instead: glyphs are rasterized once on the CPU (golang.org/x/image/vector, from the same font outlines Gio uses) into a glyph cache, composed into line images cached by their glyphs and color, and drawn as textured rectangles, the way Skia and the platform text stacks do. Lines whose glyphs it can't rasterize (color emoji, fallback system fonts) are drawn as Gio's paths, so output never goes missing.

Layouts are cached too: a text laid out with the same string and parameters as before isn't shaped or iterated again, and long text is laid out per paragraph, so appending to it (a streamed reply) re-lays out only its last paragraph.

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultBackground is the background without a box's: the
	// theme's.
	DefaultBackground = func() color.NRGBA { return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff} }
)

Backgrounds text is drawn over: boxes push theirs (Box with a Background) while laying out their content.

Functions

func Background

func Background() color.NRGBA

Background returns the background text is drawn over now, opaque.

func CacheStats

func CacheStats() (runs, runBytes, plain, rich int)

CacheStats reports the size of the text caches: line images (count and bytes) and laid out plain and rich texts.

func DrawRun

func DrawRun(ops *op.Ops, sh *text.Shaper, glyphs []text.Glyph, col color.NRGBA)

DrawRun draws a line of glyphs in col. The glyphs are positioned as Gio's shaper placed them, in the coordinates of the current transform.

func FrameDone

func FrameDone()

FrameDone marks the end of a frame's layout: until the next Tick, the idle sweeper may clear the caches.

func HitRune

func HitRune(lines []StopLine, p image.Point) int

HitRune returns the rune offset of the caret stop nearest p: on the line p is on, or the nearest line.

func OnNextIdle

func OnNextIdle(key any, fn func())

OnNextIdle has fn run, on the UI goroutine, at the next idle sweep - a component's way to let go of state it keeps only for frames it draws (a list's off-screen rows). Call it every frame the component is laid out, with the same key: the sweep forgets it after running it, so a component no longer laid out isn't kept.

func PushBackground

func PushBackground(c color.NRGBA) (pop func())

PushBackground makes c the background of the text drawn until the returned function runs. A translucent c is composited over the current background; a transparent one changes nothing.

func RangeRects

func RangeRects(lines []StopLine, a, b int) []image.Rectangle

RangeRects returns the rectangles covering runes [a, b), a line each.

func SetIdleSweep

func SetIdleSweep(d time.Duration) time.Duration

SetIdleSweep sets how long a window draws nothing before the idle sweep, for tests; it returns the previous delay.

func SetUIRunner

func SetUIRunner(run func(func()))

SetUIRunner gives the idle sweep a way to run on the UI goroutine before a frame (App.Post), so components can sweep their own state (OnNextIdle). Without one - a custom host - the idle sweep clears only the text caches, from its own goroutine, between frames.

func SetVectorText

func SetVectorText(on bool)

SetVectorText draws all text as Gio's vector paths instead of cached images, when on.

func Tick

func Tick()

Tick advances the frame counter and drops cache entries no frame has used for a while. The app calls it once per frame, before laying it out, and FrameDone after.

Types

type Laid

type Laid struct {
	// Glyphs are the visible glyphs in order; Lines index into them.
	Glyphs []text.Glyph
	// Lines are the glyph ranges of each line, Glyphs[Lines[i][0]:Lines[i][1]].
	Lines [][2]int
	// Dims are the text's dimensions, constrained, with its baseline.
	Dims layout.Dimensions
	// Viewport is the area drawing is clipped to: the constraints,
	// grown by how far glyphs overhang.
	Viewport image.Rectangle
	// Truncated is the number of runes cut off by MaxLines.
	Truncated int
	// contains filtered or unexported fields
}

Laid is a laid out text: its glyphs in lines, positioned as Gio's widget.Label positions them, and its dimensions.

func Layout

func Layout(sh *text.Shaper, p text.Parameters, cs layout.Constraints, str string) *Laid

Layout lays out str as widget.Label would with the same parameters and constraints, from cache when it's been laid out before.

func (*Laid) Draw

func (l *Laid) Draw(ops *op.Ops, sh *text.Shaper, col color.NRGBA)

Draw paints the text in col, clipped as widget.Label clips it.

func (*Laid) EachLine

func (l *Laid) EachLine(fn func(glyphs []text.Glyph, dy int))

EachLine calls fn with the glyphs of each line, in order. dy moves the line's glyphs down from their Y.

func (*Laid) Runes

func (l *Laid) Runes() int

Runes returns the number of runes each glyph cluster ends, for mapping glyphs to text: Gio puts a cluster's rune count on its last glyph.

func (*Laid) StopLines

func (l *Laid) StopLines() []StopLine

StopLines returns the text's caret stops by line, in the text's coordinates. Rune counts are from the start of the text.

type RichLaid

type RichLaid struct {
	Segments []Segment
	Dims     layout.Dimensions
	// contains filtered or unexported fields
}

RichLaid is laid out rich text.

func LayoutRich

func LayoutRich(sh *text.Shaper, gtx layout.Context, p RichParams, spans []Span) *RichLaid

LayoutRich lays out spans as styledtext.TextStyle would, from cache when the spans, settings and constraints are the same as before.

func (*RichLaid) Draw

func (l *RichLaid) Draw(ops *op.Ops, sh *text.Shaper, colorOf func(span int) color.NRGBA)

Draw paints the segments, each in the color colorOf returns for its span.

func (*RichLaid) StopLines

func (l *RichLaid) StopLines(spanStart []int) []StopLine

StopLines returns the rich text's caret stops by line. spanStart[i] is the rune offset of span i in the whole text.

type RichParams

type RichParams struct {
	Alignment       text.Alignment
	LineHeight      unit.Sp
	LineHeightScale float32
	// MaxLines, when above zero, stops after that many lines; with
	// Ellipsis, the last ends in "…" when text was left out.
	MaxLines int
	Ellipsis bool
}

RichParams are the paragraph-wide settings of rich text.

type Segment

type Segment struct {
	// Span indexes the spans the text was laid out from.
	Span int
	// Offset is the segment's top left corner in the text; Size its
	// size; Ascent its baseline from its top.
	Offset image.Point
	Size   image.Point
	Ascent int
	// Glyphs are the segment's glyphs, relative to Offset.
	Glyphs []text.Glyph
	// Runes are the span's runes the segment covers, from its start.
	RuneStart, RuneEnd int
	// Box marks an inline box's segment, which has no glyphs.
	Box bool
}

Segment is a piece of one span on one line.

type Span

type Span struct {
	Font    font.Font
	Size    unit.Sp
	Content string
	Box     image.Point
	Ascent  int
}

Span is a run of text with its own style, or, with Box set, an inline box: something drawn in the line (a formula), Box wide and tall, its baseline Ascent from its top. A box stands for Content, its text when selected, and wraps as a word does.

func (Span) IsBox

func (s Span) IsBox() bool

IsBox reports whether the span is an inline box.

type Stop

type Stop struct {
	Rune int
	X    int
}

Stop is a caret position: the number of runes before it and its x.

type StopLine

type StopLine struct {
	Top, Bottom int
	Stops       []Stop
}

StopLine is a line of caret stops with its vertical extent.

Jump to

Keyboard shortcuts

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