svg

package
v0.5.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: 9 Imported by: 0

Documentation

Overview

Package svg provides SVG rendering utilities for PIDL protocols.

Index

Constants

This section is empty.

Variables

View Source
var BuiltinTemplates = []string{"default", "minimal", "sketch", "blueprint", "dark", "high-contrast"}

BuiltinTemplates lists available built-in template names.

Functions

func BoundaryStrokeDashArray

func BoundaryStrokeDashArray(style BoundaryStyle) string

BoundaryStrokeDashArray returns the stroke dash array for a boundary style.

func BoundaryStyleColors

func BoundaryStyleColors(style BoundaryStyle) (fill, stroke string)

BoundaryStyleColors returns the default colors for a boundary style.

func GenerateAnimationCSS

func GenerateAnimationCSS(flows []FlowAnimationStyle, pathData []string) string

GenerateAnimationCSS generates CSS for flow animations.

func GenerateCSS

func GenerateCSS(theme Theme) string

GenerateCSS generates CSS styles for the SVG diagram.

func GenerateLegacyAnimationCSS

func GenerateLegacyAnimationCSS(messageCount int) string

GenerateLegacyAnimationCSS generates backwards-compatible animation CSS. This is used when flows don't have per-message animation config.

func GenerateNetworkCSS

func GenerateNetworkCSS(theme Theme) string

GenerateNetworkCSS generates CSS for network diagrams.

func ThemeClass

func ThemeClass(theme Theme) string

ThemeClass returns the CSS class for the given theme.

Types

type AnimationConfig

type AnimationConfig struct {
	// DefaultEnabled is whether animation is enabled by default.
	DefaultEnabled bool
	// DefaultDuration is the default animation cycle duration.
	DefaultDuration string
	// DefaultDotSize is the default dot radius.
	DefaultDotSize int
	// DefaultDotColor is the default dot color.
	DefaultDotColor string
	// Stagger is the delay between message animations.
	Stagger string
	// StaggerMode controls how animations are staggered.
	StaggerMode StaggerMode
}

AnimationConfig contains global animation settings.

func DefaultAnimationConfig

func DefaultAnimationConfig() AnimationConfig

DefaultAnimationConfig returns the default animation configuration.

type Boundary

type Boundary struct {
	// ID is the boundary identifier.
	ID string
	// Name is the display name.
	Name string
	// Style is the visual style.
	Style BoundaryStyle
	// Entities are the entity IDs within this boundary.
	Entities []string
	// Description is tooltip text.
	Description string
	// Color overrides the default color.
	Color string
}

Boundary represents a resolved network boundary with its entities.

func ResolveBoundaries

func ResolveBoundaries(p *pidl.Protocol, cliOverrides map[string][]string) []Boundary

ResolveBoundaries resolves network boundaries from a protocol. It applies the resolution order: CLI overrides > metadata.networks > entity.metadata.network > default.

type BoundaryLayout

type BoundaryLayout struct {
	// ID is the boundary identifier.
	ID string
	// Name is the display name.
	Name string
	// Style is the visual style.
	Style BoundaryStyle
	// X is the left edge.
	X int
	// Y is the top edge.
	Y int
	// Width of the boundary region.
	Width int
	// Height of the boundary region.
	Height int
	// Entities contains layout for entities within this boundary.
	Entities []EntityLayout
}

BoundaryLayout contains position data for a boundary region.

type BoundaryStyle

type BoundaryStyle string

BoundaryStyle represents the visual style for a network boundary.

const (
	// BoundaryStyleTrusted is for internal/protected networks (green).
	BoundaryStyleTrusted BoundaryStyle = "trusted"
	// BoundaryStyleDMZ is for perimeter/semi-trusted networks (blue).
	BoundaryStyleDMZ BoundaryStyle = "dmz"
	// BoundaryStyleExternal is for untrusted/public networks (red).
	BoundaryStyleExternal BoundaryStyle = "external"
	// BoundaryStyleCloud is for cloud provider boundaries (gray).
	BoundaryStyleCloud BoundaryStyle = "cloud"
)

type ConnectionLayout

type ConnectionLayout struct {
	// FromID is the source entity ID.
	FromID string
	// ToID is the target entity ID.
	ToID string
	// Label is the connection label (aggregated from flows).
	Label string
	// FromX, FromY is the start point.
	FromX, FromY int
	// ToX, ToY is the end point.
	ToX, ToY int
	// CrossesBoundary indicates if the connection crosses a boundary.
	CrossesBoundary bool
}

ConnectionLayout contains position data for a connection between entities.

func AggregateConnections

func AggregateConnections(p *pidl.Protocol, entityToBoundary map[string]string) []ConnectionLayout

AggregateConnections aggregates flows between entities into connections.

type EntityLayout

type EntityLayout struct {
	// ID is the entity ID.
	ID string
	// Name is the display name.
	Name string
	// X is the center X coordinate.
	X int
	// Y is the center Y coordinate.
	Y int
	// Width of the entity box.
	Width int
	// Height of the entity box.
	Height int
}

EntityLayout contains position data for an entity within a boundary.

type FlowAnimationStyle

type FlowAnimationStyle struct {
	// Enabled is whether animation is enabled.
	Enabled bool
	// Duration is the animation cycle duration.
	Duration string
	// Delay is the animation start delay.
	Delay string
	// DotColor is the dot fill color.
	DotColor string
	// DotSize is the dot radius.
	DotSize int
	// Pulse adds a pulsing effect.
	Pulse bool
	// Glow adds a glow effect for highlights.
	Glow bool
	// Easing is the CSS easing function.
	Easing string
}

FlowAnimationStyle contains resolved animation properties for a flow.

func ResolveFlowAnimation

func ResolveFlowAnimation(flow *pidl.FlowAnimation, index int, config AnimationConfig) FlowAnimationStyle

ResolveFlowAnimation resolves the animation style for a flow.

type Layout

type Layout struct {
	// Width is the total SVG width.
	Width int
	// Height is the total SVG height.
	Height int
	// Participants contains position data for each participant.
	Participants []ParticipantLayout
	// Messages contains position data for each message.
	Messages []MessageLayout
}

Layout contains calculated positions for SVG diagram elements.

func CalculateLayout

func CalculateLayout(participantCount, messageCount int, config LayoutConfig) Layout

CalculateLayout computes positions for all diagram elements.

func (*Layout) ParticipantCenterX

func (l *Layout) ParticipantCenterX(index int) int

ParticipantCenterX returns the center X coordinate for a participant by index.

func (*Layout) SetMessageEndpoints

func (l *Layout) SetMessageEndpoints(msgIndex, fromParticipantIndex, toParticipantIndex int)

SetMessageEndpoints sets the from/to X coordinates for a message.

type LayoutConfig

type LayoutConfig struct {
	// Padding around the entire diagram.
	Padding int
	// ParticipantBoxWidth is the width of participant boxes.
	ParticipantBoxWidth int
	// ParticipantBoxHeight is the height of participant boxes.
	ParticipantBoxHeight int
	// ParticipantSpacing is the horizontal space between participants.
	ParticipantSpacing int
	// MessageSpacing is the vertical space between messages.
	MessageSpacing int
	// MessageStartY is where messages begin (below participant boxes).
	MessageStartY int
	// LifelineEndPadding is padding below the last message.
	LifelineEndPadding int
}

LayoutConfig contains configuration for layout calculations.

func DefaultLayoutConfig

func DefaultLayoutConfig() LayoutConfig

DefaultLayoutConfig returns the default layout configuration.

type MessageLayout

type MessageLayout struct {
	// Step is the message number (1-based).
	Step int
	// Label is the message text.
	Label string
	// FromX is the start X coordinate.
	FromX int
	// ToX is the end X coordinate.
	ToX int
	// Y is the Y coordinate of the message line.
	Y int
	// IsDashed indicates if the line should be dashed (for responses).
	IsDashed bool
	// IsReverse indicates if the arrow points left (to < from).
	IsReverse bool
	// PathD is the SVG path data for the message line.
	PathD string
	// HasAlternatives indicates this flow has alternative paths.
	HasAlternatives bool
	// AlternativeCount is the number of alternatives.
	AlternativeCount int
	// AlternativeConditions lists the conditions for each alternative.
	AlternativeConditions []string
	// HasNote indicates this flow has a note.
	HasNote bool
	// Note is the note text for this flow.
	Note string
}

MessageLayout contains position data for a message arrow.

type NetworkLayout

type NetworkLayout struct {
	// Width is the total diagram width.
	Width int
	// Height is the total diagram height.
	Height int
	// Boundaries contains layout for each boundary.
	Boundaries []BoundaryLayout
	// Connections contains layout for connections.
	Connections []ConnectionLayout
}

NetworkLayout contains the complete layout for a network diagram.

func CalculateNetworkLayout

func CalculateNetworkLayout(boundaries []Boundary, connections []ConnectionLayout, config NetworkLayoutConfig) NetworkLayout

CalculateNetworkLayout computes positions for all network diagram elements.

type NetworkLayoutConfig

type NetworkLayoutConfig struct {
	// Padding around the entire diagram.
	Padding int
	// BoundaryPadding inside boundary regions.
	BoundaryPadding int
	// BoundarySpacing between boundaries.
	BoundarySpacing int
	// EntityWidth is the width of entity boxes.
	EntityWidth int
	// EntityHeight is the height of entity boxes.
	EntityHeight int
	// EntitySpacing between entities within a boundary.
	EntitySpacing int
	// Direction is horizontal or vertical layout.
	Direction string
}

NetworkLayoutConfig contains configuration for network layout calculations.

func DefaultNetworkLayoutConfig

func DefaultNetworkLayoutConfig() NetworkLayoutConfig

DefaultNetworkLayoutConfig returns the default network layout configuration.

type ParticipantLayout

type ParticipantLayout struct {
	// ID is the entity ID.
	ID string
	// Name is the display name.
	Name string
	// Index is the participant order (0-based).
	Index int
	// BoxX is the top-left X of the participant box.
	BoxX int
	// BoxY is the top-left Y of the participant box.
	BoxY int
	// BoxWidth is the participant box width.
	BoxWidth int
	// BoxHeight is the participant box height.
	BoxHeight int
	// CenterX is the X coordinate of the lifeline.
	CenterX int
	// LifelineStartY is where the lifeline starts.
	LifelineStartY int
	// LifelineEndY is where the lifeline ends.
	LifelineEndY int
	// StepType is the process step type (for process specs).
	StepType string
	// IsProcessStep indicates this participant is a process step.
	IsProcessStep bool
}

ParticipantLayout contains position data for a participant.

type StaggerMode

type StaggerMode string

StaggerMode controls how animations are staggered.

const (
	// StaggerSequential starts each flow after the previous ends.
	StaggerSequential StaggerMode = "sequential"
	// StaggerOverlap starts flows with fixed delay, may overlap.
	StaggerOverlap StaggerMode = "overlap"
	// StaggerParallel animates all flows simultaneously.
	StaggerParallel StaggerMode = "parallel"
)

type Template

type Template struct {
	// Config contains template configuration.
	Config TemplateConfig
	// CSS contains custom CSS styles.
	CSS string
	// contains filtered or unexported fields
}

Template represents a loaded SVG template.

func LoadTemplate

func LoadTemplate(name string) (*Template, error)

LoadTemplate loads a template by name from embedded templates.

func LoadTemplateFromDir

func LoadTemplateFromDir(dir string) (*Template, error)

LoadTemplateFromDir loads a template from a filesystem directory.

func (*Template) ApplyToLayoutConfig

func (t *Template) ApplyToLayoutConfig(config *LayoutConfig)

ApplyToLayoutConfig applies template layout settings to a LayoutConfig.

func (*Template) GenerateCSS

func (t *Template) GenerateCSS(baseTheme Theme) string

GenerateCSS generates CSS with template overrides.

func (*Template) GetCornerRadius

func (t *Template) GetCornerRadius() int

GetCornerRadius returns the corner radius for participant boxes.

type TemplateAnimation

type TemplateAnimation struct {
	// DotShape for animated dots (circle, square, diamond).
	DotShape string `json:"dot_shape,omitempty"`
	// DotSize is the default dot radius.
	DotSize int `json:"dot_size,omitempty"`
	// TrailEnabled shows a fading trail.
	TrailEnabled bool `json:"trail_enabled,omitempty"`
}

TemplateAnimation configures animation defaults.

type TemplateColors

type TemplateColors struct {
	// Primary color.
	Primary string `json:"primary,omitempty"`
	// Secondary color.
	Secondary string `json:"secondary,omitempty"`
	// Accent color.
	Accent string `json:"accent,omitempty"`
	// Background color.
	Background string `json:"background,omitempty"`
	// Text color.
	Text string `json:"text,omitempty"`
	// Line color.
	Line string `json:"line,omitempty"`
	// Lifeline color.
	Lifeline string `json:"lifeline,omitempty"`
	// ParticipantBg is participant box background.
	ParticipantBg string `json:"participant_bg,omitempty"`
	// ParticipantText is participant text color.
	ParticipantText string `json:"participant_text,omitempty"`
}

TemplateColors overrides default color scheme.

type TemplateConfig

type TemplateConfig struct {
	// Name is the template identifier.
	Name string `json:"name"`
	// Description explains what the template is for.
	Description string `json:"description"`
	// Layout contains positioning parameters.
	Layout TemplateLayout `json:"layout"`
	// Fonts specifies font families.
	Fonts TemplateFonts `json:"fonts,omitempty"`
	// LineStyle configures line rendering.
	LineStyle TemplateLineStyle `json:"line_style,omitempty"`
	// Colors overrides default color scheme.
	Colors TemplateColors `json:"colors,omitempty"`
	// Animation configures animation defaults.
	Animation TemplateAnimation `json:"animation,omitempty"`
}

TemplateConfig contains configuration for an SVG template.

type TemplateFonts

type TemplateFonts struct {
	// Primary font for text.
	Primary string `json:"primary,omitempty"`
	// Mono font for code/technical text.
	Mono string `json:"mono,omitempty"`
}

TemplateFonts specifies font families.

type TemplateLayout

type TemplateLayout struct {
	// Padding around the entire diagram.
	Padding int `json:"padding,omitempty"`
	// ParticipantSpacing is horizontal spacing between participants.
	ParticipantSpacing int `json:"participant_spacing,omitempty"`
	// MessageSpacing is vertical spacing between messages.
	MessageSpacing int `json:"message_spacing,omitempty"`
	// ParticipantBox configures participant box dimensions.
	ParticipantBox TemplateParticipantBox `json:"participant_box,omitempty"`
}

TemplateLayout contains positioning parameters.

type TemplateLineStyle

type TemplateLineStyle struct {
	// StrokeWidth for lines.
	StrokeWidth float64 `json:"stroke_width,omitempty"`
	// HandDrawn enables hand-drawn effect.
	HandDrawn bool `json:"hand_drawn,omitempty"`
	// WobbleFactor controls hand-drawn wobble amount.
	WobbleFactor float64 `json:"wobble_factor,omitempty"`
}

TemplateLineStyle configures line rendering.

type TemplateParticipantBox

type TemplateParticipantBox struct {
	// Width of participant boxes.
	Width int `json:"width,omitempty"`
	// Height of participant boxes.
	Height int `json:"height,omitempty"`
	// CornerRadius for rounded corners. Use -1 to indicate "not set".
	CornerRadius *int `json:"corner_radius,omitempty"`
}

TemplateParticipantBox configures participant box dimensions.

type Theme

type Theme string

Theme represents a color theme for SVG diagrams.

const (
	// ThemeLight is the light color theme.
	ThemeLight Theme = "light"
	// ThemeDark is the dark color theme.
	ThemeDark Theme = "dark"
	// ThemeAuto uses system preference via prefers-color-scheme.
	ThemeAuto Theme = "auto"
)

Jump to

Keyboard shortcuts

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