layout

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package layout computes logical-pixel boxes and an ordered, serializable paint list. Input nodes and CSS styles are borrowed for one synchronous Layout call.

Index

Constants

This section is empty.

Variables

View Source
var ErrDepth = errors.New("layout: tree depth exceeds 256")

Functions

func AlignOffset

func AlignOffset(align css.Keyword, direction di.Direction, contentW, lineW float64) float64

AlignOffset is the horizontal offset of a line of width lineW inside a content box of width contentW under text-align and the line's direction.

func Hit

func Hit(r *Result, x, y float64) string

Hit returns the frontmost node ID whose border box contains the logical point. Ancestor content clips constrain descendants, but not the ancestor border itself.

func IndefiniteHeights added in v0.7.0

func IndefiniteHeights(s css.Style) css.Style

IndefiniteHeights returns s with percentage height, min-height and max-height replaced by auto (no constraint), for a root whose containing height is not known, such as a surface that does not exist yet.

func Physical

func Physical(r Rect, scale float64) (x, y, w, h int)

Physical is the explicit renderer boundary; all layout and hit testing stay logical. It rounds edges independently to avoid accumulated fractional-scale drift.

Types

type Arena added in v0.6.1

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

Arena holds the result tree and display storage of one Output, so a steady layout reuses it instead of allocating. An Output stays valid until its arena is passed to Layout again; alternate two arenas to keep the previous Output readable while building the next.

func (*Arena) Keep added in v0.6.1

func (a *Arena) Keep(display []Command)

Keep makes display, the Output.Display of the arena's last Layout after the caller appended to it, the list the arena recycles next. Without it, a list that outgrew its arena block is reallocated by the caller every frame.

type Command

type Command struct {
	Op      string
	ID      string
	Rect    Rect
	Color   css.Color
	Opacity float64
	Widths  Edges
	Colors  css.ColorSides
	Radii   [4]float64
	Shadow  *css.Shadow `json:",omitempty"`
	Text    string      `json:",omitempty"`
	Runs    []Run       `json:",omitempty"`
	Image   image.Image `json:"-"`
}

Command is a paint operation. Text line ranges refer to rune offsets in Text; image pixels are borrowed and excluded from serialized display lists.

type Edges

type Edges struct{ Top, Right, Bottom, Left float64 }

Edges are in top/right/bottom/left order.

type Glyph

type Glyph struct {
	ID            uint32
	X, Y, Advance float64
	Cluster       int
}

type Kind

type Kind string

Kind selects intrinsic and container behavior. CSS display:none suppresses all kinds; display:flex takes precedence over Kind, and row/column flex defaults are overridable.

const (
	Box    Kind = "box"
	Row    Kind = "row"
	Column Kind = "column"
	Stack  Kind = "stack"
	Scroll Kind = "scroll"
	Text   Kind = "text"
	Image  Kind = "image"
)

type Measure

type Measure func(width float64) (Size, []text.Line, error)

Measure receives the available content width (zero means unbounded), and must return finite logical dimensions and optionally shaped lines. It may return an error. TextEngine, when supplied, is used for Text nodes without an explicit callback.

type Node

type Node struct {
	ID               string
	Kind             Kind
	Style            *css.Style
	Children         []*Node
	Content          string
	ImageSize        Size
	Image            image.Image `json:"-"`
	Measure          Measure     `json:"-"`
	ScrollX, ScrollY float64
	NoWrap           bool // measure text at unbounded width; useful for single-line editors
	EditorScroll     bool // clip and clamp own text on both axes, without child scrolling
	// Rect, when HasRect is set on a direct child of a Stack, is the child's
	// border-box geometry relative to the Stack's content box. It replaces the
	// child's authored width, height and margin; it is ignored elsewhere.
	// A flex-styled Stack with a visible Rect child is rejected.
	Rect    Rect
	HasRect bool
}

Node has no parent pointers or renderer resources. ID identifies hit targets and display commands; callers must supply stable, unique IDs if they need identity. ImageSize is the decoded image's intrinsic size; image ownership remains with caller. Image is borrowed through painting. Treat it as immutable while passed to Image: pass a new image value to change pixels. Renderer cache identity requires a comparable image.Image interface value (for example, *image.RGBA).

type Options

type Options struct {
	Width, Height float64
	TextEngine    *text.Engine
	Arena         *Arena
}

Options provide logical viewport size and an optional text shaping engine. Arena, when set, supplies reusable storage for the Output.

type Output

type Output struct {
	Tree    *Result
	Display []Command
}

Output contains a JSON-safe tree and paint list in back-to-front command order.

func Layout

func Layout(root *Node, options Options) (Output, error)

Layout does not mutate inputs. The caller must not create cycles in the node tree.

type Rect

type Rect struct{ X, Y, W, H float64 }

Rect uses logical pixels, including fractional positions.

func (Rect) Contains

func (r Rect) Contains(x, y float64) bool

type Result

type Result struct {
	ID                            string
	Kind                          Kind
	Border, Content, Clip         Rect
	Margin, Padding, BorderWidths Edges
	ContentSize                   Size
	ScrollX, ScrollY              float64
	Lines                         []text.Line `json:"-"`
	Children                      []*Result
	Clipped                       bool
}

Result is a detached layout snapshot. Style is deliberately not retained.

type Run

type Run struct {
	Start, End    int
	X, Y, Advance float64
	// Face is borrowed for in-process rasterization. FaceID and Size preserve
	// the shaping identity and logical pixel size in serialized display lists.
	Face   *text.Face `json:"-"`
	FaceID string
	Size   float64
	Glyphs []Glyph
}

type Size

type Size struct{ W, H float64 }

func Natural added in v0.7.0

func Natural(root *Node, options Options) (Size, error)

Natural returns the border-box size root takes when it is not stretched: the widest and tallest envelope of its content plus its padding and border, honoring explicit width, height, min and max like Layout. Text wraps at options.Width minus the root margins, but the width may still exceed options.Width when content cannot wrap (a long word, an unwrapped text, an explicit width): callers clamp. The root's own margins are not part of the result. Percentage height, min-height and max-height of root are treated as auto, as for nested nodes (CSS: the containing height is indefinite), so options.Height has no effect.

Jump to

Keyboard shortcuts

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