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 ¶
- Variables
- func Background() color.NRGBA
- func CacheStats() (runs, runBytes, plain, rich int)
- func DrawRun(ops *op.Ops, sh *text.Shaper, glyphs []text.Glyph, col color.NRGBA)
- func FrameDone()
- func HitRune(lines []StopLine, p image.Point) int
- func OnNextIdle(key any, fn func())
- func PushBackground(c color.NRGBA) (pop func())
- func RangeRects(lines []StopLine, a, b int) []image.Rectangle
- func SetIdleSweep(d time.Duration) time.Duration
- func SetUIRunner(run func(func()))
- func SetVectorText(on bool)
- func Tick()
- type Laid
- type RichLaid
- type RichParams
- type Segment
- type Span
- type Stop
- type StopLine
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
RangeRects returns the rectangles covering runes [a, b), a line each.
func SetIdleSweep ¶
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.
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) EachLine ¶
EachLine calls fn with the glyphs of each line, in order. dy moves the line's glyphs down from their Y.
type RichLaid ¶
type RichLaid struct {
Segments []Segment
Dims layout.Dimensions
// contains filtered or unexported fields
}
RichLaid is laid out rich text.
func LayoutRich ¶
LayoutRich lays out spans as styledtext.TextStyle would, from cache when the spans, settings and constraints are the same as before.
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 ¶
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.