layout

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package layout provides grid-based layout and rendering algorithms.

This package is the Rendering Domain - a peer to the Application Domain in internal/domain/. While internal/domain/ contains business logic and entities (Diagram, Node, Edge), this package handles the computational details of arranging nodes on a grid and routing edges (paths).

Key types:

  • Config: Grid/layout configuration (spacing, margins, dimensions)
  • LayoutNode: A node positioned on the grid (distinct from domain.Node)
  • LayoutPath: An edge's route through the grid (distinct from domain.Edge.Path)
  • VertexMap: Grid vertex tracking for pathfinding

Layout algorithms:

  • LayoutFlowSquare: Arrange nodes in a square grid pattern
  • LayoutTopologicalSort: Single-row topological ordering
  • LayoutTarjan: Multi-row layout using Tarjan's algorithm
  • LayoutAbsolute: Use explicit positions from config
  • LayoutRandomShortestSquare: Multiple attempts to minimize path length

Internal adapters (internal/adapters/) convert between the Application Domain (internal/domain/) and this Rendering Domain.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsoluteFromSVG

func AbsoluteFromSVG(svg string, output OutputFunc) error

AbsoluteFromSVG parses a string of an SVG and turns it in to a Layli configuration with with absolute layout that can represent the same SVG

func GetHeuristics added in v0.0.16

func GetHeuristics() []string

GetHeuristics returns all available heuristic function names

NOTE: When adding a new heuristic, add it to: 1. internal/domain/diagram.go (PathfindingHeuristic constant) 2. internal/adapters/config/yaml_parser.go (validation map) 3. This function (so it's discoverable)

func GetLayoutOptions added in v0.0.16

func GetLayoutOptions() []string

GetLayoutOptions returns all available layout algorithm names

NOTE: When adding a new layout algorithm, remember to: 1. Add constant to internal/domain/diagram.go 2. Implement function in arrangements.go 3. Register in selectArrangement() in arrangements.go 4. Register in selectArranger() in internal/adapters/layout/engine.go 5. Add it to this function (so it's discoverable) See CONTRIBUTING_LAYOUTS.md for detailed steps.

func GetPathfindingAlgorithms added in v0.0.16

func GetPathfindingAlgorithms() []string

GetPathfindingAlgorithms returns all available pathfinding algorithm names

NOTE: When adding a new pathfinding algorithm, add it to: 1. internal/domain/diagram.go (PathfindingAlgorithm constant) 2. internal/adapters/config/yaml_parser.go (validation map) 3. This function (so it's discoverable)

func PythagoreanDistance

func PythagoreanDistance(from, to dijkstra.Point) int64

Types

type Arc

type Arc struct {
	From     Point
	To       Point
	Distance int
}

type Arcs

type Arcs []Arc

func (*Arcs) Add

func (all *Arcs) Add(from Point, to Point, distance int)

func (Arcs) Exists

func (all Arcs) Exists(from Point, to Point) bool

func (Arcs) Get

func (all Arcs) Get(from Point, to Point) Arc

func (Arcs) String

func (all Arcs) String() string

type Config

type Config struct {
	Layout         string      `yaml:"layout,omitempty"`
	LayoutAttempts int         `yaml:"layout-attempts,omitempty"`
	Path           ConfigPath  `yaml:"path,omitempty"`
	Nodes          ConfigNodes `yaml:"nodes"`
	Edges          ConfigEdges `yaml:"edges"`
	Spacing        int         `yaml:"-"`

	NodeWidth  int `yaml:"width"`
	NodeHeight int `yaml:"height"`
	Border     int `yaml:"border"`
	Margin     int `yaml:"margin"`

	Styles ConfigStyles `yaml:"styles,omitempty"`
}

Config holds the layout configuration for a diagram.

Note: LayoutAttempts is overloaded and has different meanings depending on the layout algorithm: - For LayoutRandomShortestSquare: the number of random permutations to try - For other algorithms: available for custom use (e.g., grid columns) See CONTRIBUTING_LAYOUTS.md for details on using this field in new layouts.

func NewConfigFromFile

func NewConfigFromFile(r io.Reader) (*Config, error)

func (Config) String

func (config Config) String() string

type ConfigEdge

type ConfigEdge struct {
	ID    string `yaml:"id,omitempty"`
	From  string `yaml:"from"`
	To    string `yaml:"to"`
	Class string `yaml:"class,omitempty"`
	Style string `yaml:"style,omitempty"`
}

type ConfigEdges

type ConfigEdges []ConfigEdge

type ConfigNode

type ConfigNode struct {
	Id       string   `yaml:"id"`
	Contents string   `yaml:"contents"`
	Position Position `yaml:"position,omitempty"`
	Class    string   `yaml:"class,omitempty"`
	Style    string   `yaml:"style,omitempty"`
}

type ConfigNodes

type ConfigNodes []ConfigNode

func (ConfigNodes) ByID

func (nodes ConfigNodes) ByID(id string) *ConfigNode

type ConfigPath

type ConfigPath struct {
	Attempts  int    `yaml:"attempts,omitempty"`
	Strategy  string `yaml:"strategy,omitempty"`
	Algorithm string `yaml:"algorithm,omitempty"`
	Heuristic string `yaml:"heuristic,omitempty"`
	Class     string `yaml:"class,omitempty"`
}

type ConfigStyles

type ConfigStyles map[string]string

type CreateFinder

type CreateFinder func(start, end dijkstra.Point) PathFinder

type Diagram

type Diagram struct {
	Output   OutputFunc
	Config   Config
	Layout   *Layout
	ShowGrid bool
}

func (*Diagram) Draw

func (d *Diagram) Draw() error

Draw turns the diagram in to an image

type Layout

type Layout struct {
	Nodes        LayoutNodes
	Paths        LayoutPaths
	CreateFinder CreateFinder
	// contains filtered or unexported fields
}

func NewLayout

func NewLayout(nodes LayoutNodes, paths LayoutPaths, nodeWidth, nodeHeight, nodeMargin, layoutBorder, pathSpacing int) *Layout

NewLayout creates a Layout with the given configuration parameters.

func NewLayoutFromConfig

func NewLayoutFromConfig(finder CreateFinder, c *Config) (*Layout, error)

func (*Layout) Draw

func (l *Layout) Draw(canvas LayoutDrawer, spacing int)

func (*Layout) FindPath

func (l *Layout) FindPath(from, to string) (*LayoutPath, error)

func (*Layout) InsideAny

func (l *Layout) InsideAny(x, y int) bool

func (*Layout) IsAnyPort

func (l *Layout) IsAnyPort(x, y int) bool

func (*Layout) LayoutHeight

func (l *Layout) LayoutHeight() int

LayoutHeight is the height in path units

func (*Layout) LayoutWidth

func (l *Layout) LayoutWidth() int

LayoutWidth is the width in path units

func (*Layout) ShowGrid

func (l *Layout) ShowGrid(canvas LayoutDrawer, spacing int)

type LayoutArrangementFunc

type LayoutArrangementFunc func(c *Config) (LayoutNodes, error)

LayoutArrangementFunc returns a slice of nodes arranged according to the algorithm implemented

type LayoutDrawer

type LayoutDrawer interface {
	Circle(x int, y int, r int, s ...string)
	Path(d string, s ...string)
	Roundrect(x int, y int, w int, h int, rx int, ry int, s ...string)
	Textspan(x int, y int, t string, s ...string)
	TextEnd()
}

type LayoutNode

type LayoutNode struct {
	Id       string
	Contents string
	// contains filtered or unexported fields
}

func NewLayoutNode

func NewLayoutNode(id, contents string, left, top, width, height int, class, style string) LayoutNode

func (*LayoutNode) Draw

func (n *LayoutNode) Draw(d LayoutDrawer, spacing, order int)

func (*LayoutNode) GetCentre

func (n *LayoutNode) GetCentre() Point

func (*LayoutNode) GetPorts

func (n *LayoutNode) GetPorts() Points

func (*LayoutNode) Height

func (n *LayoutNode) Height() int

Height returns the height of the node in grid units.

func (*LayoutNode) IsInside

func (n *LayoutNode) IsInside(x, y int) bool

func (*LayoutNode) IsPort

func (n *LayoutNode) IsPort(x, y int) bool

func (*LayoutNode) Left

func (n *LayoutNode) Left() int

Left returns the left position of the node in grid units.

func (*LayoutNode) Top

func (n *LayoutNode) Top() int

Top returns the top position of the node in grid units.

func (*LayoutNode) Width

func (n *LayoutNode) Width() int

Width returns the width of the node in grid units.

type LayoutNodes

type LayoutNodes []LayoutNode

func LayoutAbsolute

func LayoutAbsolute(c *Config) (LayoutNodes, error)

func LayoutFlowSquare

func LayoutFlowSquare(c *Config) (LayoutNodes, error)

func LayoutRandomShortestSquare

func LayoutRandomShortestSquare(config *Config) (LayoutNodes, error)

func LayoutTarjan

func LayoutTarjan(config *Config) (LayoutNodes, error)

LayoutTarjan arranges nodes in multiple rows according to Tarhan's algorithm

func LayoutTopologicalSort

func LayoutTopologicalSort(config *Config) (LayoutNodes, error)

LayoutTopologicalSort arranges nodes in a single row, sorted in topological order

func (LayoutNodes) ByID

func (nodes LayoutNodes) ByID(id string) *LayoutNode

func (LayoutNodes) ConnectionDistances

func (n LayoutNodes) ConnectionDistances(connections ConfigEdges) (float64, error)

func (LayoutNodes) String

func (nodes LayoutNodes) String() string

type LayoutPath

type LayoutPath struct {
	ID     string
	From   string
	To     string
	Points Points
	Class  string
	Style  string
}

func (*LayoutPath) Draw

func (p *LayoutPath) Draw(canvas LayoutDrawer, spacing, order int)

func (*LayoutPath) Length

func (paths *LayoutPath) Length() float64

type LayoutPaths

type LayoutPaths []LayoutPath

func (*LayoutPaths) Draw

func (paths *LayoutPaths) Draw(canvas LayoutDrawer, spacing int)

func (LayoutPaths) Length

func (paths LayoutPaths) Length() float64

type OutputFunc

type OutputFunc func(output string) error

type PathFinder

type PathFinder interface {
	AddConnection(from dijkstra.Point, cost dijkstra.CostFunction, to ...dijkstra.Point)
	BestPath() ([]dijkstra.Point, error)
}

type PathStrategy

type PathStrategy func(config Config, paths *LayoutPaths, find func(from, to string) (*LayoutPath, error)) error

type Point

type Point struct {
	X float64
	Y float64
}

func (Point) Coordinates

func (p Point) Coordinates() (float64, float64)

func (Point) Distance

func (p Point) Distance(to Point) float64

func (Point) String

func (p Point) String() string

type Points

type Points []Point

func (Points) Path

func (p Points) Path(spacing int) string

type Position

type Position struct {
	X int `yaml:"x"`
	Y int `yaml:"y"`
}

type VertexMap

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

func BuildVertexMap

func BuildVertexMap(l *Layout) VertexMap

func NewVertexMap

func NewVertexMap(x, y int) VertexMap

func (*VertexMap) CountAvailable

func (v *VertexMap) CountAvailable(available bool) int

func (*VertexMap) Get

func (v *VertexMap) Get(x, y int) bool

func (VertexMap) GetArcs

func (vm VertexMap) GetArcs() Arcs

func (VertexMap) GetVertexPoints

func (v VertexMap) GetVertexPoints() Points

func (*VertexMap) Map

func (v *VertexMap) Map(m VertexStateMapper)

func (*VertexMap) MapAnd

func (v *VertexMap) MapAnd(m VertexMapper)

func (*VertexMap) MapOr

func (v *VertexMap) MapOr(m VertexMapper)

func (*VertexMap) MapSet

func (v *VertexMap) MapSet(m VertexMapper)

func (*VertexMap) MapUnset

func (v *VertexMap) MapUnset(m VertexMapper)

func (*VertexMap) Set

func (v *VertexMap) Set(x, y int, val bool)

func (VertexMap) String

func (v VertexMap) String() string

type VertexMapper

type VertexMapper func(x, y int) bool

type VertexStateMapper

type VertexStateMapper func(x, y int, state bool) bool

Jump to

Keyboard shortcuts

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