scene

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package scene is a diagram laid out: a list of items, each a thing of the diagram, where it is and what it is, in the order they are drawn — but not how they look. A backend draws a scene: the svg package as SVG, others on screens of their own.

An item says what it is by its Kind, its Role, as "flowchart.node", and, if it has an outline, by its Shape, as "diam". Its colors are tokens of the theme (see the style package), settled only when it is drawn, so a scene is drawn in any theme without being laid out again.

An item also carries what a plain backend needs to draw it: its outline as Parts, paths and boxes; for an edge, the markers at its ends; a label laid out. A backend with a look of its own may draw an item its own way from its Role, Shape, Box and, for an edge, its Route instead.

Index

Constants

View Source
const MarkerSize = 8

MarkerSize is the length of an arrowhead.

Variables

This section is empty.

Functions

This section is empty.

Types

type Align

type Align uint8

Align is where a label's lines are about the point it is drawn at, across; down, the label is always centered on it.

const (
	Middle Align = iota // each line centered on the point
	Start               // each line starting at it
	End                 // each line ending at it
)

type Item

type Item struct {
	Kind  Kind
	Role  Role
	Shape Shape
	ID    string // the id the diagram's text gave it, if any
	// Class is the classes the diagram's author gave the item, by name: with class statements
	// and classDef's default, with :::name, or in a list of classes where the diagram takes
	// one, as a kanban card or a tree view's entry does. A class's style is already in Author.
	Class []string
	// Tags are what the diagram says the item is beyond its role, which Pele writes among the
	// classes of what it draws: a C4 element's kind ("person", "system_db"…) and "external",
	// a C4 relation's kind ("rel", "birel"…) and a deployment node's ("node", "nodeL",
	// "nodeR"); a gantt task's state ("done", "active", "crit", "milestone", "unscheduled");
	// the branch a gantt section or a mindmap's node or edge is in ("section-0"…); a wardley
	// component's sourcing ("build", "buy", "outsource", "market"); "reset" on an event
	// modeling frame that resets; and "clickable", which Mermaid gives what has a click.
	//
	// The svg backend writes the tags and then the classes as the element's classes, for a
	// host's CSS.
	Tags []string

	// Box is where the item is: the box of its shape, for a node or a group; for an edge, the
	// box its path lies in, the control points of its curves included, and for a mark, of
	// what it draws. A Text item's Box is where the diagram sets its label, as an edge
	// label's patch, and is empty where the diagram gives the label no box of its own;
	// LabelBox is where its label is drawn.
	Box Rect

	// Parts draw the outline; a backend that draws the shape its own way ignores them.
	Parts []Part

	// For an edge: the route it takes, as the corners of a line that runs straight between
	// them, and the markers at its ends. Its curves are in Parts. Every edge has a route; a
	// self-loop's, which no layout routes, is the corners its curve is drawn about
	// (Path.Corners).
	Route []Point
	Ends  [2]Marker

	// Label is drawn about At: centered on it, or with its lines starting or ending there,
	// as Align says, and turned by Rotate degrees about it.
	Label  *text.Label
	At     Point
	Align  Align
	Rotate float64

	// Picture is an image or an icon the item shows, as a flowchart's node may.
	Picture *Picture

	Style  style.Style // the diagram's own
	Author style.Style // what the author set with classDef and style lines

	Link    string // where the item leads, when it is a link
	Tooltip string
}

An Item is a thing of a diagram, as it is drawn: a node, a group, an edge, a label on its own or a mark.

func (*Item) LabelBox

func (it *Item) LabelBox() Rect

LabelBox is the box the item's label is drawn in, laid about At as Align says and turned by Rotate degrees about it: the box the turned label lies in. It is empty for an item with no label.

func (*Item) Move

func (it *Item) Move(dx, dy float64)

Move moves the item by dx, dy: its box, parts, route, markers, label and picture. Its parts and paths are copied before they are moved, as other items may share them.

type Kind

type Kind uint8

A Kind is the sort of thing an item is.

const (
	Node  Kind = iota + 1 // a shape, most often with a label: a flowchart's node, an actor
	Group                 // a box that holds others: a subgraph, a loop, a lane
	Edge                  // a line from one thing to another, with markers at its ends
	Text                  // a label on its own: a title, an edge's label, an axis's
	Mark                  // a shape with no label: a lifeline, a tick, a slice of a pie
)

type Marker

type Marker struct {
	Type   MarkerType
	At     Point
	DX, DY float64
}

A Marker is what an edge has at an end: its kind, where its tip is, and the way it points, as a unit vector.

func (Marker) Outline

func (m Marker) Outline() []Part

Outline is the marker's shape, as Pele draws it: parts painted in the color of the edge's line. FillOnly fills a part in it and FillAndStroke strokes it too; StrokeOnly strokes it only, and Hollow fills it with the background and strokes it, so that it hides the line beneath. It is nil for a marker of no type, or of one the scene does not know.

type MarkerType

type MarkerType string

A MarkerType is the kind of marker at an end of an edge, by Mermaid's names: "arrow_point", "arrow_circle", "arrow_cross"… The empty one is no marker: an end Pele draws no marker at has the empty type, whatever name the diagram's model gives it ("none", or "arrow_open" for an open line). Every type but the empty one is in the scene's table of markers, with a trim and an outline.

func (MarkerType) Join

func (t MarkerType) Join() style.Join

Join is how a marker of the type turns the corners of its outline when they are stroked, over whatever the edge's style says, as Pele sets it on the marker itself; unset, the marker's corners are the edge's. Its ends are the edge's, as Pele's take theirs from the group the edges are drawn in.

func (MarkerType) Trim

func (t MarkerType) Trim() float64

Trim is how far an edge stops short of its end to leave room for a marker of the type.

type Op

type Op uint8

An Op is a step of a path.

const (
	MoveTo Op = iota // to a point
	LineTo           // to a point
	QuadTo           // through a control point to a point
	CubeTo           // through two control points to a point
	Close            // back to where the figure began
)

func (Op) Args

func (op Op) Args() int

Args is how many numbers the step takes.

type Painting

type Painting uint8

Painting is how a part is painted in its item's style.

const (
	// FillAndStroke fills the part and strokes its outline.
	FillAndStroke Painting = iota
	// StrokeOnly strokes the part only: an edge, or a detail on a shape, as the lid of a
	// cylinder.
	StrokeOnly
	// FillOnly fills the part and does not stroke it: the body of a datastore, which lines
	// close at its top and bottom only.
	FillOnly
	// Solid fills the part in the theme's color of lines, and strokes it: the dot of a final
	// state, the bar of a fork.
	Solid
	// Hollow fills the part with the theme's background, and strokes it: the ring of a final
	// state, which hides what is beneath it.
	Hollow
	// Shade fills the part with the theme's background and does not stroke it: a stripe of
	// an entity's table. When the item's author set a fill, it is the background at 60%
	// opacity, laid over the parts drawn before it, as Pele draws an entity's stripes.
	Shade
)

type Part

type Part struct {
	Path    *Path
	Box     Rect
	Rounded bool
	Paint   Painting
}

A Part is a piece of an item's outline. It is a path when Path is set, and Box and Rounded are then unused; when Path is nil, it is the box Box, its corners rounded by the style's radius when Rounded is set.

type Path

type Path struct {
	Ops  []Op
	Args []float64
}

A Path is lines and curves: the steps, and the numbers they take, in turn. Arcs are made of cubic curves when they are added, so a backend draws only the four kinds of step, as MyGo's paths do.

func ParsePath

func ParsePath(d string) (*Path, error)

ParsePath reads path data, as SVG's d attribute holds it: the commands M, L, H, V, C, S, Q, T, A and Z, each also in lower case for numbers relative to where the path is, with numbers repeated for a command given once. Diagrams use it for the outlines Pele draws from path data of its own, as the icons architecture and tree view diagrams know. Data that does not follow SVG's grammar is an error.

func (*Path) ArcTo

func (p *Path) ArcTo(rx, ry, rot float64, large, sweep bool, x, y float64) *Path

ArcTo adds an elliptical arc from where the path is to x, y, as SVG's A command draws one: radii rx and ry, the ellipse turned by rot degrees, the large or the small way round, and clockwise (on the screen) when sweep is true.

func (*Path) Bounds

func (p *Path) Bounds() Rect

Bounds is the box the path's points and control points lie in.

func (*Path) Close

func (p *Path) Close() *Path

func (*Path) Corners

func (p *Path) Corners() []Point

Corners are the path's points and the control points of its curves, in order: the line its curves are drawn about, which runs straight between them. It is the route of an edge whose layout gives it none, as a self-loop: the curve out of its node and back.

func (*Path) CubeTo

func (p *Path) CubeTo(c1x, c1y, c2x, c2y, x, y float64) *Path

func (*Path) Each

func (p *Path) Each(fn func(op Op, args []float64))

Each calls fn with each step and its numbers.

func (*Path) Ellipse

func (p *Path) Ellipse(cx, cy, rx, ry float64) *Path

Ellipse adds a closed ellipse with its center at cx, cy, in four curves.

func (*Path) Flatten

func (p *Path) Flatten(tol float64) []Polyline

Flatten is the path as polylines, one for each of its figures, with its curves cut into straight lines that stray from them by no more than tol (0.1 when it is not above zero). It is for backends that draw no curves, or no dashes, and for finding what is near a point. A figure of a single point is left out.

func (*Path) Last

func (p *Path) Last() Point

Last is where the path is: the end of its last step.

func (*Path) LineTo

func (p *Path) LineTo(x, y float64) *Path

func (*Path) MoveTo

func (p *Path) MoveTo(x, y float64) *Path

func (*Path) Polygon

func (p *Path) Polygon(xy ...float64) *Path

Polygon adds a closed figure through the points, given as x, y in turn.

func (*Path) QuadTo

func (p *Path) QuadTo(cx, cy, x, y float64) *Path

func (*Path) RoundRect

func (p *Path) RoundRect(r Rect, radius float64) *Path

RoundRect adds a closed box with corners rounded by r.

func (*Path) Scaled

func (p *Path) Scaled(s, dx, dy float64) *Path

Scaled is the path scaled by s about the origin, then moved by dx, dy, as an icon drawn on a grid of its own is placed in a box.

type Picture

type Picture struct {
	Image string // the image's address, checked as the diagram's options allow
	Icon  string // or the icon's name, as "fa:fa-car", drawn in the color of text
	Box   Rect
	Fit   bool // the image keeps its proportions in the box; it fills it otherwise
}

A Picture is an image, or an icon by name, drawn in a box.

type Point

type Point struct{ X, Y float64 }

A Point is a place in a diagram, in its units, y down.

type Polyline

type Polyline struct {
	Points []Point
	Closed bool
}

A Polyline is points joined by straight lines; a closed one is joined from its last point back to its first as well.

func Dash

func Dash(lines []Polyline, pattern []float64, offset float64) []Polyline

Dash cuts polylines into dashes, as SVG's stroke-dasharray and stroke-dashoffset do: the pattern is the lengths of dashes and gaps in turn, taken twice over when it has an odd number of them, and starts anew with each polyline, offset by offset along it. A dash goes round the corners it meets; a dash of no length is a polyline of two points that are the same, which a stroke with round caps draws as a dot. A pattern with a length below zero, or none above it, leaves the polylines as they are, as SVG draws them solid.

type Rect

type Rect struct{ X, Y, W, H float64 }

A Rect is a box: its top left corner, its width and height.

func (Rect) Center

func (r Rect) Center() Point

Center is the middle of the box.

func (Rect) Contains

func (r Rect) Contains(p Point) bool

Contains reports whether p is in the box.

type Role

type Role string

A Role says which part of which diagram an item is, as "flowchart.node" or "sequence.lifeline". Styles are given by role.

type Scene

type Scene struct {
	Type          string // the kind of diagram: "flowchart", "sequence"…
	Width, Height float64
	FontSize      float64 // of text with no size of its own
	Title, Desc   string  // for assistive technology: accTitle and accDescr
	Items         []Item  // in the order they are drawn
}

A Scene is a diagram laid out. Every number in a scene limu.Layout or Diagram.Layout gives that places or sizes something is finite (see Finite): where Pele would draw NaN, they give an error instead. A diagram package's own Layout makes no such promise.

func (*Scene) Finite

func (sc *Scene) Finite() bool

Finite reports whether every number that places or sizes something in the scene is finite: its size, and the boxes, paths, routes, markers, labels and pictures of its items. A layout given numbers at the edge of what a float64 holds, as a sankey diagram's flow of 5e-324, can work out NaN where Pele does too; limu.Layout gives an error for such a scene rather than hand it to a backend.

func (*Scene) Snap

func (sc *Scene) Snap(unit float64)

Snap moves everything in the scene onto a grid of unit: the corners of boxes, the points of paths and routes, the tips of markers and the points labels are drawn about. It is for a backend that draws on whole pixels, after layout; a box's sides are snapped each on its own, so boxes that touch go on touching. A path of straight lines has each of its points snapped; a path with curves is moved as a whole, by what puts its first point on the grid, so that its curves keep their shape, as rounding each of their points would not. The scene's size is rounded up.

type Shape

type Shape string

A Shape names an item's outline, as Mermaid does: "rect", "rounded", "diam", "cyl"…

Jump to

Keyboard shortcuts

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