Documentation
¶
Overview ¶
Package layout provides a backend-agnostic graphical layout framework.
The package models layout data as a Document composed of Pages and Elements, and renders them via pluggable backends.
Supported element primitives include:
- Box
- Line
- Text
- Image
Reusable layout structures are supported via:
- Group
- Repeat
- TemplateInstance (with {{key}} parameter substitution)
Template defaults and required parameters are supported through:
- Template.WithDefaults(...)
- Template.WithRequired(...)
Render via Render or RenderWithOptions using a backend renderer, for example html, svg, canvas, pdf, or png implementations in backend/*.
Example:
output, err := layout.RenderWithOptions(doc, renderer, layout.RenderOptions{DeterministicIDs: true})
_ = output
_ = err
Index ¶
- Variables
- func ApplyParameters(text string, parameters Parameters) string
- func Render(doc Document, renderer Renderer) ([]byte, error)
- func RenderWithOptions(doc Document, renderer Renderer, options RenderOptions) ([]byte, error)
- func ResourceKey(image Image) string
- func Supports(renderer Renderer, feature Feature) bool
- type Backend
- type Box
- type BoxStyle
- type CapabilityProvider
- type Color
- type Document
- type Element
- type ElementType
- type ExpandOptions
- type Feature
- type FillStyle
- type Group
- type HorizontalAlign
- type Image
- type ImageStyle
- type Insets
- type Line
- type LineCap
- type LineJoin
- type LineStyle
- type Page
- type Pagination
- type Parameters
- type Point
- type Rect
- type RenderOptions
- type Renderer
- type Repeat
- type Size
- type StringParameters
- type StrokeStyle
- type Template
- type TemplateInstance
- type Text
- type TextAlign
- type TextStyle
- type VerticalAlign
Constants ¶
This section is empty.
Variables ¶
var ( // ColorBlack is opaque black. ColorBlack = Color{R: 0, G: 0, B: 0, A: 255} // ColorWhite is opaque white. ColorWhite = Color{R: 255, G: 255, B: 255, A: 255} // ColorRed is opaque red. ColorRed = Color{R: 255, G: 0, B: 0, A: 255} // ColorGreen is opaque green. ColorGreen = Color{R: 0, G: 255, B: 0, A: 255} // ColorBlue is opaque blue. ColorBlue = Color{R: 0, G: 0, B: 255, A: 255} )
Functions ¶
func ApplyParameters ¶
func ApplyParameters(text string, parameters Parameters) string
ApplyParameters replaces placeholders in text with parameter values.
Example:
text := layout.ApplyParameters("Hello {{name}}", layout.Parameters{"name": "Ada"})
// text == "Hello Ada"
func Render ¶
Render validates and renders a document with the given renderer.
It is equivalent to RenderWithOptions(doc, renderer, RenderOptions{}).
func RenderWithOptions ¶
func RenderWithOptions(doc Document, renderer Renderer, options RenderOptions) ([]byte, error)
RenderWithOptions validates and renders a document with explicit render options.
Reusable elements are expanded before backend rendering. Header/footer/pagination sections are merged into the page element stream during expansion.
func ResourceKey ¶
ResourceKey returns a stable key for image/resource deduplication.
If image.ID is set, the key is "<id>:<mime>". Otherwise the key is based on a SHA-1 hash of image bytes and MIME type.
Example:
key := layout.ResourceKey(image)
Types ¶
type Box ¶
Box represents a rectangular layout primitive.
func NewBox ¶
NewBox creates a Box with DefaultBoxStyle.
Example:
box := layout.NewBox("hero", layout.Rect{X: 20, Y: 20, Width: 300, Height: 160})
box.Style.CornerRadius = 8
type BoxStyle ¶
type BoxStyle struct {
Fill FillStyle
Stroke StrokeStyle
Insets Insets
CornerRadius float64
}
BoxStyle configures visual style for a Box.
func DefaultBoxStyle ¶
func DefaultBoxStyle() BoxStyle
DefaultBoxStyle returns the default style for boxes.
Defaults: white fill, black 1px stroke.
type CapabilityProvider ¶
type CapabilityProvider interface {
Capabilities() []Feature
}
CapabilityProvider can declare supported backend features.
type Document ¶
type Document struct {
Pages []Page
}
Document is a collection of pages.
func NewDocument ¶
func NewDocument() Document
NewDocument returns an empty document.
Example:
doc := layout.NewDocument()
doc.AddPage(layout.Page{Size: layout.Size{Width: 600, Height: 400}})
type Element ¶
type Element interface {
Type() ElementType
Validate() error
}
Element is the backend-agnostic layout element interface.
Implementations include Box, Line, Text, Image and reusable types in reusable.go.
func ExpandElements ¶
ExpandElements flattens reusable elements into concrete primitives.
Group, Repeat, and TemplateInstance are recursively expanded.
func ExpandElementsWithOptions ¶
func ExpandElementsWithOptions(elements []Element, options ExpandOptions) ([]Element, error)
ExpandElementsWithOptions flattens reusable elements with explicit expansion options.
type ElementType ¶
type ElementType string
ElementType identifies a concrete layout element kind.
const ( ElementTypeBox ElementType = "box" ElementTypeLine ElementType = "line" ElementTypeText ElementType = "text" ElementTypeImage ElementType = "image" )
const ElementTypeGroup ElementType = "group"
ElementTypeGroup groups child elements and applies an offset.
const ElementTypeRepeat ElementType = "repeat"
ElementTypeRepeat repeats child elements at multiple positions.
const ElementTypeTemplateInstance ElementType = "template-instance"
ElementTypeTemplateInstance renders a template with parameter substitution.
type ExpandOptions ¶
type ExpandOptions struct {
DeterministicIDs bool
Parameters Parameters
}
ExpandOptions controls how reusable elements are flattened.
Parameters are applied at the top-level before nested template merges.
type Feature ¶
type Feature string
Feature identifies optional rendering capabilities.
const ( FeatureBox Feature = "box" FeatureLine Feature = "line" FeatureText Feature = "text" FeatureImage Feature = "image" FeatureInsets Feature = "insets" FeatureBorder Feature = "border" FeatureDash Feature = "dash" FeatureCornerRadius Feature = "corner-radius" FeatureTemplates Feature = "templates" FeatureDeterministicIDs Feature = "deterministic-ids" )
type Group ¶
Group is a reusable element container with a coordinate offset.
Example:
group := layout.NewGroup("card", layout.Point{X: 40, Y: 40}, layout.NewBox("b", layout.Rect{X: 0, Y: 0, Width: 100, Height: 60}))
type HorizontalAlign ¶
type HorizontalAlign string
HorizontalAlign describes horizontal placement in a container rectangle.
const ( AlignLeft HorizontalAlign = "left" AlignCenter HorizontalAlign = "center" AlignRight HorizontalAlign = "right" )
type Image ¶
type Image struct {
ID string
Rect Rect
Data []byte
MIMEType string
Style ImageStyle
}
Image represents an image drawn into a destination rectangle.
func NewImage ¶
NewImage creates an Image element.
Example:
img := layout.NewImage("logo", layout.Rect{X: 20, Y: 20, Width: 64, Height: 64}, data, "image/png")
img.Style.Border.Enabled = true
type ImageStyle ¶
type ImageStyle struct {
Insets Insets
Border StrokeStyle
}
ImageStyle configures visual style for Image.
func DefaultImageStyle ¶
func DefaultImageStyle() ImageStyle
DefaultImageStyle returns the default style for images.
Defaults: no border and no insets.
type Insets ¶
Insets represents top/right/bottom/left spacing values.
Example:
box.Style.Insets = layout.Insets{Top: 4, Right: 8, Bottom: 4, Left: 8}
func (Insets) Horizontal ¶
Horizontal returns the combined left+right inset.
type Line ¶
Line represents a line segment between two points.
type LineStyle ¶
type LineStyle struct {
Stroke StrokeStyle
}
LineStyle configures visual style for a Line.
func DefaultLineStyle ¶
func DefaultLineStyle() LineStyle
DefaultLineStyle returns the default style for lines.
Defaults: black 1px stroke.
type Page ¶
type Page struct {
Size Size
Header []Element
Elements []Element
Pagination Pagination
}
Page is a single drawable surface within a document.
Header and Footer elements are rendered in addition to Elements (body). If Pagination.Enabled is true, a pagination Text element is generated per page.
func (*Page) AddElement ¶
AddElement appends an element to the page body.
Example:
page.AddElement(layout.NewBox("card", layout.Rect{X: 20, Y: 20, Width: 200, Height: 120}))
func (*Page) AddFooterElement ¶
AddFooterElement appends an element to the page footer section.
Example:
page.AddFooterElement(layout.NewText("ftr", layout.Point{X: 20, Y: 390}, "Confidential"))
func (*Page) AddHeaderElement ¶
AddHeaderElement appends an element to the page header section.
Example:
page.AddHeaderElement(layout.NewText("hdr", layout.Point{X: 20, Y: 24}, "Report"))
type Pagination ¶
Pagination configures automatic page number text rendering.
Template supports the placeholders {{page}} and {{total}}.
Example:
page.Pagination = layout.DefaultPagination()
page.Pagination.Enabled = true
page.Pagination.Position = layout.Point{X: 580, Y: 390}
page.Pagination.Style.Align = layout.TextAlignRight
func DefaultPagination ¶
func DefaultPagination() Pagination
DefaultPagination returns the default pagination configuration.
func (Pagination) BuildText ¶
func (p Pagination) BuildText(content string) Text
BuildText returns a Text element for the given resolved pagination content.
type Parameters ¶
Parameters is a key/value set for template substitution.
Placeholders use the format {{key}}.
Example:
params := layout.Parameters{"name": "Ada Lovelace", "city": "London"}
func MergeParameters ¶
func MergeParameters(base Parameters, override Parameters) Parameters
MergeParameters merges base and override where override wins.
func (Parameters) Clone ¶
func (p Parameters) Clone() Parameters
Clone returns a shallow copy of the parameter map.
type Point ¶
Point represents a 2D coordinate.
Example:
p := layout.Point{X: 10, Y: 20}
func Align ¶
func Align(container Rect, content Size, horizontal HorizontalAlign, vertical VerticalAlign) Point
Align returns a top-left point that places content inside container.
Example:
origin := layout.Align(container, layout.Size{Width: 120, Height: 40}, layout.AlignRight, layout.AlignBottom)
func AlignCenterInRect ¶
AlignCenterInRect centers content in a container rectangle.
Example:
center := layout.AlignCenterInRect(container, layout.Size{Width: 160, Height: 80})
func GridPositions ¶
GridPositions returns row-major positions for a rectangular grid.
It returns an empty slice if columns <= 0 or rows <= 0.
Example:
positions := layout.GridPositions(layout.Point{X: 40, Y: 40}, 2, 2, 240, 140)
type Rect ¶
Rect represents a rectangle by top-left origin and size.
Example:
r := layout.Rect{X: 20, Y: 40, Width: 200, Height: 120}
type RenderOptions ¶
type RenderOptions struct {
DeterministicIDs bool
}
RenderOptions controls rendering behavior shared across backends.
type Renderer ¶
Renderer renders a document into a backend-specific representation.
Renderers are provided by backend packages (for example backend/html, backend/pdf).
type Repeat ¶
Repeat replicates child elements at each listed position.
Example:
repeat := layout.NewRepeat("labels", positions, group)
type Size ¶
Size represents a 2D width and height.
Example:
s := layout.Size{Width: 640, Height: 480}
type StringParameters ¶
StringParameters is kept for compatibility with string-only parameter maps. Deprecated: use Parameters.
func (StringParameters) ToParameters ¶
func (p StringParameters) ToParameters() Parameters
ToParameters converts StringParameters to Parameters. Deprecated: use Parameters directly.
type StrokeStyle ¶
type StrokeStyle struct {
Enabled bool
Color Color
Width float64
Dash []float64
LineCap LineCap
LineJoin LineJoin
}
StrokeStyle configures a stroked outline.
type Template ¶
type Template struct {
ID string
Elements []Element
DefaultParameters Parameters
RequiredParameters []string
}
Template is a reusable, parameterizable element tree.
Templates are instantiated through TemplateInstance.
func NewTemplate ¶
NewTemplate creates a reusable template.
Example:
tpl := layout.NewTemplate("label", layout.NewText("name", layout.Point{X: 10, Y: 24}, "{{name}}"))
func (Template) WithDefaults ¶
func (t Template) WithDefaults(defaultParameters Parameters) Template
WithDefaults returns a copy of the template with default parameters.
func (Template) WithRequired ¶
WithRequired returns a copy of the template with required parameter keys.
type TemplateInstance ¶
type TemplateInstance struct {
ID string
Template Template
Offset Point
Parameters Parameters
}
TemplateInstance renders a template with offset and parameters.
func NewTemplateInstance ¶
func NewTemplateInstance( id string, template Template, offset Point, parameters Parameters, ) TemplateInstance
NewTemplateInstance creates a template instance element.
func (TemplateInstance) Type ¶
func (t TemplateInstance) Type() ElementType
Type returns the element type.
func (TemplateInstance) Validate ¶
func (t TemplateInstance) Validate() error
Validate validates the template instance and its template.
type Text ¶
Text represents a text label drawn at a position.
type TextAlign ¶
type TextAlign string
TextAlign controls horizontal alignment relative to the text anchor position.
type TextStyle ¶
type TextStyle struct {
Color Color
FontSize float64
FontFamily string
Bold bool
Italic bool
Underline bool
Align TextAlign
}
TextStyle configures visual style for Text.
Example:
text.Style.Bold = true text.Style.Italic = true text.Style.Underline = true text.Style.Align = layout.TextAlignCenter
func DefaultTextStyle ¶
func DefaultTextStyle() TextStyle
DefaultTextStyle returns the default style for text.
Defaults: black 12px Helvetica, left-aligned, no bold/italic/underline.
type VerticalAlign ¶
type VerticalAlign string
VerticalAlign describes vertical placement in a container rectangle.
const ( AlignTop VerticalAlign = "top" AlignMiddle VerticalAlign = "middle" AlignBottom VerticalAlign = "bottom" )