Documentation
¶
Overview ¶
Package rendersvg provides efficient SVG rendering for layout trees.
This package integrates with github.com/SCKelemen/layout to render computed layout trees as SVG graphics. It supports transforms, styling, text rendering, and clipPath management.
Basic usage:
root := &layout.Node{...}
layout.Layout(root, constraints, nil)
svg := rendersvg.RenderToSVG(root, rendersvg.Options{
Width: 800,
Height: 600,
})
Index ¶
- func AreaPath(points []Point, baselineY float64) string
- func ArrowMarker(id string, color string) string
- func Circle(cx, cy, r float64, style Style) string
- func CircleMarker(id string, color string) string
- func CirclePath(cx, cy, r float64) string
- func CrossMarker(id string, color string, strokeWidth float64) string
- func DiamondMarker(id string, color string) string
- func DotMarker(id string, color string, radius float64) string
- func Ellipse(cx, cy, rx, ry float64, style Style) string
- func EllipsePath(cx, cy, rx, ry float64) string
- func GetRectFromNode(node *layout.Node) layout.Rect
- func GetTransformFromNode(node *layout.Node) string
- func GradientURL(id string) string
- func Group(content string, transform string, style Style) string
- func GroupWithClipPath(content string, clipPathID string, style Style) string
- func InterpolatedLinearGradient(id string, startColor, endColor string, angle float64, steps int, ...) (string, error)
- func InterpolatedRadialGradient(id string, centerColor, edgeColor string, steps int, ...) (string, error)
- func Line(x1, y1, x2, y2 float64, style Style) string
- func LineWithMarkers(x1, y1, x2, y2 float64, style Style, markerStart, markerEnd string) string
- func LinearGradient(def LinearGradientDef) string
- func Marker(def MarkerDef) string
- func MarkerURL(id string) string
- func OKLCHLinearGradient(id string, startColor, endColor string, angle float64, steps int) (string, error)
- func OKLCHRadialGradient(id string, centerColor, edgeColor string, steps int) (string, error)
- func Path(d string, style Style) string
- func PathWithMarkers(d string, style Style, markerStart, markerMid, markerEnd string) string
- func Polygon(points []Point, style Style) string
- func PolygonPath(points []Point) string
- func Polyline(points []Point, style Style) string
- func PolylinePath(points []Point) string
- func PolylineWithMarkers(points []Point, style Style, markerStart, markerMid, markerEnd string) string
- func RadialGradient(def RadialGradientDef) string
- func Rect(x, y, width, height float64, style Style) string
- func RectPath(x, y, width, height float64) string
- func RenderNodes(nodes []*layout.Node, opts Options) string
- func RenderToSVG(root *layout.Node, opts Options) string
- func RoundedRect(x, y, width, height, rx, ry float64, style Style) string
- func RoundedRectPath(x, y, width, height, rx, ry float64) string
- func SimpleLinearGradient(id string, startColor, endColor string, angle float64) string
- func SimpleRadialGradient(id string, centerColor, edgeColor string) string
- func SmoothAreaPath(points []Point, baselineY float64, tension float64) string
- func SmoothLinePath(points []Point, tension float64) string
- func SquareMarker(id string, color string) string
- func TSpan(content string, style Style, dx, dy float64) string
- func Text(content string, x, y float64, style Style) string
- func TextPath(content string, pathID string, style Style, startOffset string) string
- func TextWithSpans(x, y float64, style Style, spans []string) string
- func TriangleMarker(id string, color string) string
- func URL(id string) string
- func XMarker(id string, color string, strokeWidth float64) string
- type ClipPath
- type ClipPathManager
- func (m *ClipPathManager) AddCircle(cx, cy, r float64) string
- func (m *ClipPathManager) AddCustom(pathContent string) string
- func (m *ClipPathManager) AddRect(x, y, width, height float64) string
- func (m *ClipPathManager) AddRoundedRect(x, y, width, height, radius float64) string
- func (m *ClipPathManager) GenerateID() string
- func (m *ClipPathManager) ToSVGDefs() string
- type DominantBaseline
- type FontStyle
- type FontWeight
- type GradientSpreadMethod
- type GradientStop
- type GradientUnits
- type LinearGradientDef
- type MarkerDef
- type MarkerOrient
- type MarkerStyle
- type MarkerUnits
- type Options
- type PathBuilder
- func (pb *PathBuilder) ArcTo(rx, ry, xAxisRotation float64, largeArcFlag, sweepFlag int, x, y float64) *PathBuilder
- func (pb *PathBuilder) Build() string
- func (pb *PathBuilder) Close() *PathBuilder
- func (pb *PathBuilder) CurveTo(x1, y1, x2, y2, x, y float64) *PathBuilder
- func (pb *PathBuilder) HorizontalLineTo(x float64) *PathBuilder
- func (pb *PathBuilder) LineTo(x, y float64) *PathBuilder
- func (pb *PathBuilder) MoveTo(x, y float64) *PathBuilder
- func (pb *PathBuilder) QuadraticCurveTo(x1, y1, x, y float64) *PathBuilder
- func (pb *PathBuilder) Reset() *PathBuilder
- func (pb *PathBuilder) SmoothCurveTo(x2, y2, x, y float64) *PathBuilder
- func (pb *PathBuilder) SmoothQuadraticCurveTo(x, y float64) *PathBuilder
- func (pb *PathBuilder) String() string
- func (pb *PathBuilder) VerticalLineTo(y float64) *PathBuilder
- type Point
- type RadialGradientDef
- type Renderer
- type StrokeLinecap
- type StrokeLinejoin
- type Style
- type StyleRule
- type StyleSheet
- type TextAnchor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrowMarker ¶
ArrowMarker creates a simple arrow marker pointing right
func CircleMarker ¶
CircleMarker creates a circular marker
func CirclePath ¶
CirclePath creates a circular path using arcs
func CrossMarker ¶
CrossMarker creates a cross/plus marker
func DiamondMarker ¶
DiamondMarker creates a diamond marker
func EllipsePath ¶
EllipsePath creates an elliptical path using arcs
func GetRectFromNode ¶
GetRectFromNode extracts the computed rectangle from a layout node
func GetTransformFromNode ¶
GetTransformFromNode extracts SVG transform attribute from a layout node
func GradientURL ¶
GradientURL creates a url() reference to a gradient for use in fill or stroke
func GroupWithClipPath ¶
GroupWithClipPath wraps content in an SVG <g> element with clipPath
func InterpolatedLinearGradient ¶
func InterpolatedLinearGradient(id string, startColor, endColor string, angle float64, steps int, colorSpace color.GradientSpace) (string, error)
InterpolatedLinearGradient creates a linear gradient with color interpolation in the specified color space This creates smooth, natural-looking gradients by interpolating in the chosen color space and converting to sRGB for SVG compatibility. Defaults to OKLCH for perceptually uniform gradients.
colorSpace options:
- color.GradientRGB: Interpolates in RGB space (fast but not perceptually uniform)
- color.GradientHSL: Interpolates in HSL space
- color.GradientLAB: Interpolates in CIE LAB space
- color.GradientOKLAB: Interpolates in OKLAB space (perceptually uniform)
- color.GradientLCH: Interpolates in CIE LCH space
- color.GradientOKLCH: Interpolates in OKLCH space (perceptually uniform, recommended)
func InterpolatedRadialGradient ¶
func InterpolatedRadialGradient(id string, centerColor, edgeColor string, steps int, colorSpace color.GradientSpace) (string, error)
InterpolatedRadialGradient creates a radial gradient with color interpolation in the specified color space This creates smooth, natural-looking gradients by interpolating in the chosen color space and converting to sRGB for SVG compatibility.
colorSpace options: same as InterpolatedLinearGradient
func LineWithMarkers ¶
LineWithMarkers renders a line with marker references
func LinearGradient ¶
func LinearGradient(def LinearGradientDef) string
LinearGradient creates a linear gradient definition (for use in <defs>)
func OKLCHLinearGradient ¶
func OKLCHLinearGradient(id string, startColor, endColor string, angle float64, steps int) (string, error)
OKLCHLinearGradient creates a perceptually uniform linear gradient using OKLCH interpolation This is a convenience wrapper around InterpolatedLinearGradient with color.GradientOKLCH
func OKLCHRadialGradient ¶
OKLCHRadialGradient creates a perceptually uniform radial gradient using OKLCH interpolation This is a convenience wrapper around InterpolatedRadialGradient with color.GradientOKLCH
func PathWithMarkers ¶
PathWithMarkers renders a path with marker references
func PolygonPath ¶
PolygonPath creates a closed path from multiple points
func PolylinePath ¶
PolylinePath creates a path from multiple points (open path)
func PolylineWithMarkers ¶
func PolylineWithMarkers(points []Point, style Style, markerStart, markerMid, markerEnd string) string
PolylineWithMarkers renders a polyline with marker references
func RadialGradient ¶
func RadialGradient(def RadialGradientDef) string
RadialGradient creates a radial gradient definition (for use in <defs>)
func RenderNodes ¶
RenderNodes renders multiple layout nodes at their computed positions This is useful when you have a collection of already-positioned nodes
func RenderToSVG ¶
RenderToSVG renders a layout tree to an SVG string
func RoundedRect ¶
RoundedRect renders an SVG rectangle with rounded corners
func RoundedRectPath ¶
RoundedRectPath creates a rounded rectangular path
func SimpleLinearGradient ¶
SimpleLinearGradient creates a simple two-color linear gradient angle is in degrees (0 = left to right, 90 = bottom to top)
func SimpleRadialGradient ¶
SimpleRadialGradient creates a simple two-color radial gradient
func SmoothAreaPath ¶
SmoothAreaPath creates a smooth filled area path from points with a baseline
func SmoothLinePath ¶
SmoothLinePath creates a smooth curve through points using cubic Bézier curves tension controls how tight the curve is (0 = straight lines, 1 = very curved)
func SquareMarker ¶
SquareMarker creates a square marker
func TSpan ¶
TSpan renders an SVG tspan element (for use inside text elements) TSpan allows styling different parts of text independently
func TextWithSpans ¶
TextWithSpans renders an SVG text element with multiple styled spans
func TriangleMarker ¶
TriangleMarker creates a triangle marker
Types ¶
type ClipPathManager ¶
type ClipPathManager struct {
// contains filtered or unexported fields
}
ClipPathManager manages SVG clipPath definitions and generates unique IDs
func NewClipPathManager ¶
func NewClipPathManager() *ClipPathManager
NewClipPathManager creates a new clipPath manager
func (*ClipPathManager) AddCircle ¶
func (m *ClipPathManager) AddCircle(cx, cy, r float64) string
AddCircle adds a circle clipPath and returns its ID
func (*ClipPathManager) AddCustom ¶
func (m *ClipPathManager) AddCustom(pathContent string) string
AddCustom adds a custom clipPath and returns its ID
func (*ClipPathManager) AddRect ¶
func (m *ClipPathManager) AddRect(x, y, width, height float64) string
AddRect adds a rectangle clipPath and returns its ID
func (*ClipPathManager) AddRoundedRect ¶
func (m *ClipPathManager) AddRoundedRect(x, y, width, height, radius float64) string
AddRoundedRect adds a rounded rectangle clipPath and returns its ID
func (*ClipPathManager) GenerateID ¶
func (m *ClipPathManager) GenerateID() string
GenerateID generates a unique clipPath ID
func (*ClipPathManager) ToSVGDefs ¶
func (m *ClipPathManager) ToSVGDefs() string
ToSVGDefs converts all clipPaths to SVG <defs> content
type DominantBaseline ¶
type DominantBaseline string
DominantBaseline defines vertical text alignment
const ( DominantBaselineAuto DominantBaseline = "auto" DominantBaselineMiddle DominantBaseline = "middle" DominantBaselineHanging DominantBaseline = "hanging" DominantBaselineTextTop DominantBaseline = "text-top" DominantBaselineTextBottom DominantBaseline = "text-bottom" DominantBaselineAlphabetic DominantBaseline = "alphabetic" DominantBaselineMathematical DominantBaseline = "mathematical" )
type FontWeight ¶
type FontWeight string
FontWeight defines font weight values
const ( FontWeightNormal FontWeight = "normal" FontWeightBold FontWeight = "bold" FontWeightBolder FontWeight = "bolder" FontWeightLighter FontWeight = "lighter" FontWeight100 FontWeight = "100" FontWeight200 FontWeight = "200" FontWeight300 FontWeight = "300" FontWeight400 FontWeight = "400" FontWeight500 FontWeight = "500" FontWeight600 FontWeight = "600" FontWeight700 FontWeight = "700" FontWeight800 FontWeight = "800" FontWeight900 FontWeight = "900" )
type GradientSpreadMethod ¶
type GradientSpreadMethod string
GradientSpreadMethod defines how gradient fills outside its bounds
const ( GradientSpreadPad GradientSpreadMethod = "pad" GradientSpreadReflect GradientSpreadMethod = "reflect" GradientSpreadRepeat GradientSpreadMethod = "repeat" )
type GradientStop ¶
type GradientStop struct {
Offset string // percentage or decimal (e.g., "0%", "50%", "1.0")
Color string // color value (hex, rgb, etc.)
Opacity float64
}
GradientStop represents a color stop in a gradient
type GradientUnits ¶
type GradientUnits string
GradientUnits defines the coordinate system for gradients
const ( GradientUnitsUserSpaceOnUse GradientUnits = "userSpaceOnUse" GradientUnitsObjectBoundingBox GradientUnits = "objectBoundingBox" )
type LinearGradientDef ¶
type LinearGradientDef struct {
ID string
X1, Y1 string // Start point (can be percentage or absolute)
X2, Y2 string // End point (can be percentage or absolute)
Stops []GradientStop
Units GradientUnits
SpreadMethod GradientSpreadMethod
}
LinearGradientDef represents a linear gradient definition
type MarkerDef ¶
type MarkerDef struct {
ID string
ViewBox string // e.g., "0 0 10 10"
RefX float64 // Reference point X
RefY float64 // Reference point Y
MarkerWidth float64 // Width of marker viewport
MarkerHeight float64 // Height of marker viewport
Orient MarkerOrient // auto, auto-start-reverse, or angle
MarkerUnits MarkerUnits
Content string // SVG content inside the marker
}
MarkerDef represents a marker definition
type MarkerOrient ¶
type MarkerOrient string
MarkerOrient defines the orientation of a marker
const ( MarkerOrientAuto MarkerOrient = "auto" MarkerOrientAutoStart MarkerOrient = "auto-start-reverse" )
type MarkerStyle ¶
type MarkerStyle struct {
MarkerStart string // URL reference to marker for start of path
MarkerMid string // URL reference to marker for middle vertices
MarkerEnd string // URL reference to marker for end of path
}
MarkerStyle represents marker styling that can be added to a Style struct
type MarkerUnits ¶
type MarkerUnits string
MarkerUnits defines the coordinate system for marker dimensions
const ( MarkerUnitsStrokeWidth MarkerUnits = "strokeWidth" MarkerUnitsUserSpaceOnUse MarkerUnits = "userSpaceOnUse" )
type Options ¶
type Options struct {
// Width of the output SVG
Width float64
// Height of the output SVG
Height float64
// ViewBox specifies a custom viewBox attribute (optional)
// If empty, uses "0 0 {Width} {Height}"
ViewBox string
// StyleSheet to include in the SVG (optional)
StyleSheet *StyleSheet
// IncludeXMLDeclaration includes <?xml...?> declaration
IncludeXMLDeclaration bool
// Namespace adds xmlns attribute to <svg>
Namespace bool
// PreserveAspectRatio sets the preserveAspectRatio attribute
PreserveAspectRatio string
// BackgroundColor sets a background rectangle (optional)
BackgroundColor string
// StyleFunc allows custom styling per node
// Called for each node with the node and its depth in the tree
StyleFunc func(node interface{}, depth int) Style
// RenderFunc allows custom rendering per node type
// If it returns non-empty string, that's used instead of default rendering
RenderFunc func(node interface{}, depth int) string
}
Options configures SVG rendering behavior
func WithStyleSheet ¶
func WithStyleSheet(stylesheet *StyleSheet) Options
WithStyleSheet creates options with a custom stylesheet
type PathBuilder ¶
type PathBuilder struct {
// contains filtered or unexported fields
}
PathBuilder provides a fluent API for constructing SVG path data
func (*PathBuilder) ArcTo ¶
func (pb *PathBuilder) ArcTo(rx, ry, xAxisRotation float64, largeArcFlag, sweepFlag int, x, y float64) *PathBuilder
ArcTo draws an elliptical arc rx, ry: x and y radius xAxisRotation: rotation of the ellipse in degrees largeArcFlag: 0 for small arc, 1 for large arc sweepFlag: 0 for counter-clockwise, 1 for clockwise x, y: end point
func (*PathBuilder) Build ¶
func (pb *PathBuilder) Build() string
Build returns the path data string (alias for String)
func (*PathBuilder) Close ¶
func (pb *PathBuilder) Close() *PathBuilder
Close closes the current path by drawing a line back to the first point
func (*PathBuilder) CurveTo ¶
func (pb *PathBuilder) CurveTo(x1, y1, x2, y2, x, y float64) *PathBuilder
CurveTo draws a cubic Bézier curve
func (*PathBuilder) HorizontalLineTo ¶
func (pb *PathBuilder) HorizontalLineTo(x float64) *PathBuilder
HorizontalLineTo draws a horizontal line to the specified x coordinate
func (*PathBuilder) LineTo ¶
func (pb *PathBuilder) LineTo(x, y float64) *PathBuilder
LineTo draws a line from the current point to the specified point
func (*PathBuilder) MoveTo ¶
func (pb *PathBuilder) MoveTo(x, y float64) *PathBuilder
MoveTo moves the pen to the specified point without drawing
func (*PathBuilder) QuadraticCurveTo ¶
func (pb *PathBuilder) QuadraticCurveTo(x1, y1, x, y float64) *PathBuilder
QuadraticCurveTo draws a quadratic Bézier curve
func (*PathBuilder) Reset ¶
func (pb *PathBuilder) Reset() *PathBuilder
Reset clears the path builder for reuse
func (*PathBuilder) SmoothCurveTo ¶
func (pb *PathBuilder) SmoothCurveTo(x2, y2, x, y float64) *PathBuilder
SmoothCurveTo draws a smooth cubic Bézier curve (first control point is reflection of previous)
func (*PathBuilder) SmoothQuadraticCurveTo ¶
func (pb *PathBuilder) SmoothQuadraticCurveTo(x, y float64) *PathBuilder
SmoothQuadraticCurveTo draws a smooth quadratic Bézier curve
func (*PathBuilder) String ¶
func (pb *PathBuilder) String() string
String returns the path data string
func (*PathBuilder) VerticalLineTo ¶
func (pb *PathBuilder) VerticalLineTo(y float64) *PathBuilder
VerticalLineTo draws a vertical line to the specified y coordinate
type RadialGradientDef ¶
type RadialGradientDef struct {
ID string
CX, CY string // Center point (can be percentage or absolute)
R string // Radius (can be percentage or absolute)
FX, FY string // Focal point (optional, defaults to center)
FR string // Focal radius (optional)
Stops []GradientStop
Units GradientUnits
SpreadMethod GradientSpreadMethod
}
RadialGradientDef represents a radial gradient definition
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer renders layout trees to SVG
func NewRenderer ¶
NewRenderer creates a new SVG renderer with the given options
func (*Renderer) GetClipPathManager ¶
func (r *Renderer) GetClipPathManager() *ClipPathManager
GetClipPathManager returns the clipPath manager for custom clipPath creation
func (*Renderer) SetDefaultStyle ¶
SetDefaultStyle sets the default style for rendered nodes
type StrokeLinecap ¶
type StrokeLinecap string
StrokeLinecap defines how the end of a stroke is rendered
const ( StrokeLinecapButt StrokeLinecap = "butt" StrokeLinecapRound StrokeLinecap = "round" StrokeLinecapSquare StrokeLinecap = "square" )
type StrokeLinejoin ¶
type StrokeLinejoin string
StrokeLinejoin defines how corners of a stroke are rendered
const ( StrokeLinejoinMiter StrokeLinejoin = "miter" StrokeLinejoinRound StrokeLinejoin = "round" StrokeLinejoinBevel StrokeLinejoin = "bevel" )
type Style ¶
type Style struct {
Fill string
Stroke string
StrokeWidth float64
StrokeDashArray string // Dash pattern, e.g. "5,5" or "10,5,2,5"
StrokeLinecap StrokeLinecap
StrokeLinejoin StrokeLinejoin
Opacity float64
FillOpacity float64
StrokeOpacity float64
Class string
ClipPath string
TextAnchor TextAnchor
DominantBaseline DominantBaseline
FontFamily string
FontSize units.Length // Type-safe CSS length with units
FontWeight FontWeight
FontStyle FontStyle
}
Style represents styling attributes for SVG elements
func StyleWithMarkers ¶
func StyleWithMarkers(style Style, markers MarkerStyle) Style
StyleWithMarkers extends a Style with marker references
type StyleSheet ¶
type StyleSheet struct {
Rules []StyleRule
}
StyleSheet represents a collection of CSS styles for SVG rendering
func DefaultStyleSheet ¶
func DefaultStyleSheet() *StyleSheet
DefaultStyleSheet returns a sensible default stylesheet for SVG rendering
func (*StyleSheet) AddRule ¶
func (ss *StyleSheet) AddRule(selector string, properties map[string]string)
AddRule adds a custom CSS rule to the stylesheet
func (*StyleSheet) ToSVG ¶
func (ss *StyleSheet) ToSVG() string
ToSVG converts the stylesheet to SVG <style> element
type TextAnchor ¶
type TextAnchor string
TextAnchor defines horizontal text alignment
const ( TextAnchorStart TextAnchor = "start" TextAnchorMiddle TextAnchor = "middle" TextAnchorEnd TextAnchor = "end" )