layout

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: GPL-3.0 Imports: 6 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 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 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
}

Options provide logical viewport size and an optional text shaping engine.

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 }

Jump to

Keyboard shortcuts

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