paint

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Kite Paint Package

The paint package is the final stage of the visual pipeline in Kite. It is responsible for taking the immutable physical geometry (the layout.Fragment tree) and drawing it into a 2D grid of terminal cells (the FrameBuffer).

Intent and Philosophy

  1. Dumb and Fast: The paint phase does absolutely zero layout math, size resolution, or styling inheritance. All of that is handled by the layout and style packages. The paint package simply reads coordinates and colors, and applies them to a grid.
  2. Absolute Coordinates: Layout fragments store relative offsets (e.g., "I am 5 cells below my parent"). The paint engine maintains a running transform matrix (a global X/Y offset) as it walks the tree, converting these relative positions into absolute terminal coordinates.
  3. Clipping: Terminals don't naturally support "hiding" text that overflows a box. The paint package implements software clipping regions (respecting overflow: hidden) to prevent child elements from drawing outside their parent's boundaries.

Core Data Structures

  • PaintEngine: The orchestrator of the paint walk.
  • FrameBuffer: A 2D array representing the terminal screen. It implements the Surface interface.
  • Cell: Represents a single character on the screen. It holds the rune, foreground/background colors, and visual decorators (bold, italic, underline, reverse).
  • Surface: An interface abstracting the drawing target, allowing the engine to write strings, set cells, and define clipping boundaries.

The Paint Flow

When the engine triggers the Paint Phase, it requests a new FrameBuffer from the backend and passes it, along with the root layout.Fragment, to the PaintEngine. The paint flow consists of two primary phases:

Phase 1: Recursive Fragment Paint

The PaintEngine performs a depth-first traversal of the Fragment tree. Because Fragments are immutable and fully resolved, this walk is highly cache-friendly.

1. Coordinate Translation

For every FragmentLink (which joins a parent to a child), the engine takes the parent's absolute (X, Y) position and adds the child's local offset. This absolute coordinate is passed down to the child's paint routine.

2. Layered Drawing

For every fragment, the paint engine draws layers in a specific order (similar to the CSS stacking context):

  1. Background: Fills the fragment's bounding box with the ComputedStyle.Background color.
  2. Borders: Draws box-drawing characters (lines, rounded corners) around the perimeter based on ComputedStyle.Border. Every cell drawn as a border is tagged with its BorderStyle metadata (e.g., Single, Double, Thick).
  3. Content (Text): If the fragment represents a text node, it writes the shaped runes into the buffer, applying text colors and decorators.
3. Clipping & Overflow

Before drawing a subtree, the paint engine checks the ComputedStyle.Overflow properties of the current node. If overflow is hidden or scrollable, the paint engine pushes a new Clip Rect onto the Surface. Any child fragment that attempts to draw cells outside this clip rect will have those cells silently discarded, ensuring clean UI boundaries.

Phase 2: Screen-Space Border Resolution

After the entire fragment tree has been painted, the PaintEngine performs a global post-processing pass over the FrameBuffer.

This pass scans every cell with a non-none BorderStyle. For each border cell, it inspects its four cardinal neighbors (up, down, left, right). If a neighbor also has a border, they are considered connected.

The engine uses a "Heaviest Style Wins" precedence rule: it determines the dominant style among the center cell and its neighbors (e.g., Thick > Double > Single). The engine then replaces the original border character with the correct Unicode box-drawing junction (e.g., ┬, ┴, ┼, ╬) matching that dominant style.

This approach ensures that if a thick-bordered card intersects a single-line sidebar, the resulting junction uses the thick style, maintaining visual hierarchy.

This two-phase approach allows disparate elements—like a sidebar and a header, or adjacent table cells—to seamlessly "snap" together with perfect junctions without requiring manual configuration or awareness of their neighbors during the layout phase.

5. Overlays and Z-Index

Overlays (like dropdowns or modals) bypass normal DOM flow. The engine paints the main document root first, and then explicitly invokes the PaintEngine on the Fragment tree of each overlay. Because the FrameBuffer is shared, overlays simply overwrite the cells painted by the main document, achieving standard z-index stacking.

Documentation

Overview

Package paint implements the paint phase of the Kite rendering pipeline.

The paint engine performs a depth-first traversal of the immutable layout.Fragment tree produced by the layout phase and rasterises each fragment into a 2-D grid of terminal Cell values held by a FrameBuffer.

Per-fragment clipping (ADR-011)

When a fragment's computed style declares OverflowX or OverflowY as any value other than OverflowVisible (i.e., Hidden, Clip, Scroll, or Auto), the engine creates a clipped sub-Surface whose drawable area equals the fragment's content box (origin inset by border + padding, size reduced by the same amounts). All descendant paint calls are routed through this clipped surface, so any cell written outside the content box is silently dropped.

The fragment's own background fill and border decoration are painted onto the unclipped surface before the clipped surface is created, ensuring that a node's border-box decoration is never eaten by its own overflow property.

For asymmetric overflow (e.g., OverflowX: Hidden, OverflowY: Visible), the clip rect spans the full surface extent on the visible axis and the content-box inset on the clipping axis. Composition for nested overflow containers is automatic: Surface.Clip intersects the new rect with the already-active clip.

Border resolution invariant

PaintEngine.ResolveBorders is invoked exactly once, on the root Surface, after the full fragment tree has been painted. It must never be called on a clipped sub-surface because the junction resolver must inspect the complete set of border cells across the entire viewport. # Scroll Translation (ADR-012)

When a fragment's node is a scroll container (computed OverflowX or OverflowY is Scroll or Auto), the engine reads the node's raw scroll offset from the DOM. Paint "clamps on read" to the current content extent: the offset is clamped between 0 and (contentSize - viewportSize).

All descendant fragments are then painted at an origin shifted by the negative of the clamped scroll offset. This shift combined with the content-box clipping (ADR-011) enables scrolling.

Storing the scroll state in the DOM while clamping at paint time ensures that author intent (e.g., "scroll to bottom") is preserved across viewport resizes or content updates even when the required clamping value changes.

Index

Constants

View Source
const (
	// AttrBold makes the cell's text bold.
	AttrBold = backend.CellBold
	// AttrItalic makes the cell's text italic.
	AttrItalic = backend.CellItalic
	// AttrUnderline underlines the cell's text.
	AttrUnderline = backend.CellUnderline
	// AttrInverse swaps the foreground and background colors.
	AttrInverse = backend.CellReverse
	// AttrStrikethrough strikes through the cell's text.
	AttrStrikethrough = backend.CellStrikethrough
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BorderStyle

type BorderStyle uint8

BorderStyle selects the pre-defined glyph set for a border.

const (
	// BorderNone draws no border.
	BorderNone BorderStyle = 0
	// BorderASCII draws an ASCII border.
	BorderASCII BorderStyle = 1
	// BorderRounded draws a rounded border.
	BorderRounded BorderStyle = 2
	// BorderSingle draws a single-line border.
	BorderSingle BorderStyle = 3
	// BorderDouble draws a double-line border.
	BorderDouble BorderStyle = 4
	// BorderThick draws a heavy-line border.
	BorderThick BorderStyle = 5
)

type Cell

type Cell struct {
	backend.Cell
	// Content is the string to be rendered in this cell. It may be empty, but
	// must not be nil. The string may contain multiple Unicode code points, but
	// must not contain any combining characters. The width of the cell is
	// determined by the number of columns needed to render Content, which may
	// be zero for an empty string.
	// BorderStyle indicates the type of border glyph in this cell.
	BorderStyle BorderStyle
}

Cell represents the content and styling of a single terminal cell.

type CellAttrs

type CellAttrs = backend.CellStyle

CellAttrs holds the text-attribute bitmask for a terminal cell.

type Context

type Context struct {
	Tracer    *trace.Tracer
	Selection []SelectionRect
}

Context carries state and utilities through the paint tree.

func (*Context) Begin

func (c *Context) Begin(name string) func()

Begin is a helper to start a trace span.

type FrameBuffer

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

FrameBuffer is the concrete Surface implementation used by the paint engine. It holds a 2-D grid of Cells indexed by (x, y), a monotonically-increasing frame version counter, and a dirty rectangle that grows as cells are written.

FrameBuffer is not safe for concurrent use.

func NewFrameBuffer

func NewFrameBuffer(ox, oy, width, height int) *FrameBuffer

NewFrameBuffer creates a FrameBuffer positioned at origin (ox, oy) with the given width and height. All cells are initialised to zero values.

func (*FrameBuffer) Bounds

func (fb *FrameBuffer) Bounds() geom.Rect

Bounds returns the full drawable area of the buffer.

func (*FrameBuffer) BumpVersion

func (fb *FrameBuffer) BumpVersion()

BumpVersion increments the frame version, signalling the start of a new frame. The dirty rect is also reset.

func (*FrameBuffer) CellAt

func (fb *FrameBuffer) CellAt(x, y int) Cell

CellAt returns the cell at absolute position (x, y). If the position is out of bounds an empty Cell is returned.

func (*FrameBuffer) Clip

func (fb *FrameBuffer) Clip(r geom.Rect) Surface

Clip returns a clippedSurface that restricts writes to the intersection of fb's bounds and r. The returned Surface still accepts absolute coordinates.

func (*FrameBuffer) DirtyRect

func (fb *FrameBuffer) DirtyRect() (r geom.Rect, ok bool)

DirtyRect returns the bounding rectangle of all cells written since the last BumpVersion call. If no cell has been written, ok is false.

func (*FrameBuffer) Reset

func (fb *FrameBuffer) Reset()

Reset clears the buffer content and resets the dirty rect.

func (*FrameBuffer) Set

func (fb *FrameBuffer) Set(x, y int, c Cell)

Set writes cell c into position (x, y). The coordinates are absolute (same space as Bounds). Calls outside the buffer's bounds are silently ignored.

If c.BG is color.Transparent, the existing background color in the cell is preserved.

func (*FrameBuffer) Version

func (fb *FrameBuffer) Version() uint64

Version returns the current frame version counter.

type PaintEngine

type PaintEngine struct {
	DebugXRay bool
	// contains filtered or unexported fields
}

PaintEngine handles the paint phase of the pipeline.

func NewPaintEngine

func NewPaintEngine() *PaintEngine

NewPaintEngine creates a new PaintEngine.

func (*PaintEngine) ApplySelection

func (p *PaintEngine) ApplySelection(surface Surface, selection []SelectionRect)

func (*PaintEngine) Paint

func (p *PaintEngine) Paint(ctx *Context, frag *layout.Fragment, surface Surface)

Paint draws the immutable fragment tree onto the surface and resolves borders.

func (*PaintEngine) PaintFragment

func (p *PaintEngine) PaintFragment(ctx *Context, frag *layout.Fragment, origin geom.Point, surface Surface)

PaintFragment draws the immutable fragment tree onto the surface at the given origin.

func (*PaintEngine) ResolveBorders

func (p *PaintEngine) ResolveBorders(ctx *Context, surface Surface)

ResolveBorders resolves border junctions across the entire surface.

type SelectionRect

type SelectionRect struct {
	Rect geom.Rect
	Fg   color.Color
	Bg   color.Color
}

SelectionRect represents a physical rectangle of selected content.

type Surface

type Surface interface {
	// Set writes cell c into position (x, y).
	Set(x, y int, c Cell)

	// CellAt returns the cell at absolute position (x, y).
	CellAt(x, y int) Cell

	// Bounds returns the total drawable area of this surface in absolute
	// terminal-cell coordinates.
	Bounds() geom.Rect

	// Clip returns a new Surface whose drawable area is restricted to the
	// intersection of this surface's bounds and r. Coordinates passed to
	// Set on the returned surface are still in absolute terms so that the
	// clip can be composed without coordinate translations.
	Clip(r geom.Rect) Surface
}

Surface is the drawing target provided by the backend. All paint operations are expressed in absolute terminal-cell coordinates.

Implementations must be safe to call from the main goroutine only; no concurrent access is expected.

Jump to

Keyboard shortcuts

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