layout

Standalone flexbox layout using pinned Yoga

Garnet layout

Standalone, headless flexbox layout over pinned MIT-licensed Yoga. No renderer, font provider, ECS or window is required. The vendored source revision is in native/UPSTREAM; shards install builds the static native library using a C++20 compiler and Make.

require "garnet-layout"
alias L = Garnet::Layout

left = L::Node.new(L::Style.new(width: 80)) # or L::Length.points(80), L::Length.percent(25)
right = L::Node.new(L::Style.new(grow: 1))
root = L::Node.new(L::Style.new(direction: :row, gap: 12), [left, right])
boxes = L.calculate(root, 320_f32, 100_f32)
puts boxes[right] # parent-relative x/y and border-box width/height

Style takes any number for spacing, flex factors and positions, and a number of points or a Length for sizes.

Layout::Rect and Layout::Size stay Y-down immutable records (unlike Garnet::Math::Rect, whose top/bottom are Y-up). Rect#position, #extent, #center and #contains? use Garnet::Math::Vec2; #to_math converts.

Styles are copied values and node child arrays are copied. Results remain valid after later calculations. Each calculation creates and frees its native nodes and config in an ensure block; no native tree or measurement callback survives the call. Measured nodes are leaves and receive Constraints (nil means unbounded, *_exact distinguishes exact from maximum constraints). Callbacks must be pure. Exceptions are caught inside the C callback and re-raised after Yoga returns. Callback contexts remain rooted through native execution, including collection triggered by a callback.

The initial profile supports row/column and reversed directions, wrap, grow, shrink, basis, point/percentage dimensions and min/max constraints, item alignment, justification, uniform gap/padding/margin/border, absolute x/y placement and display-none. Defaults are explicit: column, stretch, start, shrink 1, grow 0, relative placement and no spacing. Calculation is LTR, uses border boxes, disables pixel rounding and sets Yoga errata to none. This is not a complete CSS interface: per-edge spacing, auto margins, RTL layout, baselines, grid and all intrinsic sizing keywords remain outside this slice.

Run make -C native -j6 then CRYSTAL_CACHE_DIR=/tmp/garnet-layout crystal spec. The tree is limited to 10,000 nodes and depth 64; cycles and shared children raise. Native validation has been performed on macOS arm64; other platforms need their own execution evidence.

Repository

layout

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 1
  • about 9 hours ago
  • November 30, 2026
License

MIT License

Links
Synced at

Wed, 02 Dec 2026 02:09:48 GMT

Languages