flexbox

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package flexbox is waffle's own flexbox layout engine — the replacement for react-pdf's Yoga binding. It computes box positions and sizes for a tree of nodes following CSS flexbox semantics (the subset react-pdf documents).

Owning the engine (rather than binding an external Yoga port) keeps the dependency graph clean and gives the pagination step the re-layout control it needs. The core implementation covers flex-direction row/column, grow/shrink, justify-content, align-items/self (including stretch), the full box model (margin/padding/border), gap, percentages, aspect-ratio, absolute positioning, flex-wrap (wrap/wrap-reverse) with align-content, and min/max width/height. Reversed directions are layered on in later passes.

All coordinates in Layout are border-box and relative to the parent's content-box origin, matching react-pdf's node model.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Calculate

func Calculate(root *Node, width, height float64)

Calculate lays out the tree rooted at root within the given border-box size (typically the page dimensions). It sets Layout on root and every descendant.

Types

type Align

type Align int

Align is a cross-axis alignment (align-items / align-self / align-content). AlignAuto on a child defers to the container's align-items; AlignAuto on a container means stretch for align-items and flex-start for align-content. AlignSpaceBetween and AlignSpaceAround are meaningful only for align-content; elsewhere they fall back to flex-start.

const (
	AlignAuto Align = iota
	AlignFlexStart
	AlignCenter
	AlignFlexEnd
	AlignStretch
	AlignSpaceBetween
	AlignSpaceAround
)

type Dim

type Dim struct {
	Value float64
	Kind  DimKind
}

Dim is a dimension that may be a definite point value, a percentage, or auto. Its zero value is auto, so an unset Width/Height/Basis behaves like CSS "auto" rather than a definite 0.

func Auto

func Auto() Dim

func Percent

func Percent(v float64) Dim

func Pt

func Pt(v float64) Dim

Pt returns a definite point dimension; Percent returns a percentage dimension; Auto returns the auto dimension (the zero value).

func (Dim) IsAuto

func (d Dim) IsAuto() bool

IsAuto reports whether the dimension is auto (undefined).

type DimKind

type DimKind uint8

DimKind distinguishes the kinds of a dimension value.

const (
	DimAuto    DimKind = iota // the zero value: unset / "auto"
	DimPoint                  // a definite point value
	DimPercent                // a percentage of the parent's content size
)

type Direction

type Direction int

Direction is the flex main-axis direction. The zero value is Column, matching react-pdf's default.

const (
	Column Direction = iota
	Row
)

type Justify

type Justify int

Justify is the main-axis distribution (justify-content).

const (
	JustifyFlexStart Justify = iota
	JustifyCenter
	JustifyFlexEnd
	JustifySpaceBetween
	JustifySpaceAround
	JustifySpaceEvenly
)

type Layout

type Layout struct {
	Left, Top, Width, Height float64
}

Layout is a node's computed border-box, relative to its parent's content-box origin.

type MeasureFunc

type MeasureFunc func(availW, availH float64) Size

MeasureFunc measures a leaf node (e.g. text or an image) given the available space. Returned sizes are content sizes (the engine adds nothing to them).

type Node

type Node struct {
	Style    Style
	Children []*Node
	Measure  MeasureFunc

	Layout Layout
}

Node is a layout node. Set Style, Children, and (for leaves) Measure; after Calculate, read Layout.

type Position

type Position int

Position is a node's positioning scheme. Absolute takes the node out of flow and positions it via Top/Right/Bottom/Left against the parent's content box. Relative is currently treated as Static (in-flow) for layout.

const (
	PositionStatic Position = iota
	PositionRelative
	PositionAbsolute
)

type Size

type Size struct{ W, H float64 }

Size is a measured width/height.

type Style

type Style struct {
	Direction    Direction
	Wrap         Wrap // flex-wrap; the zero value (NoWrap) keeps all children on one line
	Justify      Justify
	AlignItems   Align // container cross alignment; AlignAuto ⇒ stretch
	AlignSelf    Align // per-child override; AlignAuto ⇒ use parent's AlignItems
	AlignContent Align // cross-axis distribution of wrapped lines; AlignAuto ⇒ flex-start

	Grow   float64
	Shrink float64
	Basis  Dim // flex-basis; Auto ⇒ use the main-dimension size

	Width, Height        Dim
	MinWidth, MaxWidth   Dim // min/max constraints; DimAuto ⇒ unconstrained
	MinHeight, MaxHeight Dim
	AspectRatio          float64 // width/height; when one dimension is auto, derive the other

	Position                 Position
	Top, Right, Bottom, Left Dim // insets for absolute positioning

	MarginTop, MarginRight, MarginBottom, MarginLeft     float64
	PaddingTop, PaddingRight, PaddingBottom, PaddingLeft float64
	BorderTop, BorderRight, BorderBottom, BorderLeft     float64

	Gap float64 // space between children along the main axis and between wrapped lines along the cross axis
}

Style holds the resolved layout inputs for a node. Lengths are in points; percentages are resolved by the caller before layout.

type Wrap

type Wrap int

Wrap controls whether flow children are laid out on a single main-axis line or may break onto multiple lines (flex-wrap). The zero value is NoWrap, matching react-pdf's default; WrapReverse wraps and reverses the order of the lines along the cross axis.

const (
	WrapNoWrap Wrap = iota
	WrapWrap
	WrapReverse
)

Jump to

Keyboard shortcuts

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