Documentation
¶
Overview ¶
Package layout provides layout engines that calculate the position and size of document nodes within the available space. The package converts the abstract document tree into a flat list of placed nodes that a renderer can draw.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ResolvePageNumbers ¶
func ResolvePageNumbers(pages []PageLayout)
ResolvePageNumbers walks all pages and replaces page-number placeholder strings in text nodes with the actual page number and total page count.
Types ¶
type BlockLayout ¶
type BlockLayout struct{}
BlockLayout arranges children vertically from top to bottom, similar to CSS block-level layout. Each child occupies the full available width and is placed below the previous child. When the available height is exhausted, remaining content is returned as overflow for placement on the next page.
func NewBlockLayout ¶
func NewBlockLayout() *BlockLayout
NewBlockLayout creates a new BlockLayout engine.
func (*BlockLayout) Layout ¶
func (bl *BlockLayout) Layout(node document.DocumentNode, constraints Constraints) Result
Layout places the given node's children vertically within the constraints. It resolves margins, padding, and border widths from the node's style, then positions each child sequentially.
type Constraints ¶
type Constraints struct {
// AvailableWidth is the maximum horizontal space in points.
AvailableWidth float64
// AvailableHeight is the maximum vertical space in points.
AvailableHeight float64
// FontResolver provides font metrics and text measurement.
FontResolver FontResolver
}
Constraints define the available space and resources for layout.
type Engine ¶
type Engine interface {
// Layout calculates the position and size of the given node and its
// children, subject to the provided constraints. If the content does
// not fit, the Overflow field in the Result carries the remaining
// content for placement on subsequent pages.
Layout(node document.DocumentNode, constraints Constraints) Result
}
Engine performs layout calculation on a document tree, converting logical nodes into positioned, sized outputs within the given constraints.
type FlowLayout ¶
type FlowLayout struct{}
FlowLayout handles inline text layout with word wrapping. It breaks text content into lines that fit within the available width and calculates the vertical extent of the resulting text block.
func (*FlowLayout) LayoutRichText ¶
func (fl *FlowLayout) LayoutRichText(rt *document.RichText, constraints Constraints) Result
LayoutRichText lays out a RichText node, placing multiple styled fragments inline with word-wrapping. The resulting PlacedNode tree has one PlacedNode per line, each containing child PlacedNodes for the individual text runs.
func (*FlowLayout) LayoutText ¶
func (fl *FlowLayout) LayoutText(text string, style document.Style, constraints Constraints) Result
LayoutText breaks the given text into wrapped lines and returns a Result describing the bounds and per-line placed nodes. If the text does not fit vertically, the remaining text is returned as overflow.
type FontMetrics ¶
type FontMetrics struct {
// Ascender is the distance from the baseline to the top of tall
// characters, in points per unit of font size.
Ascender float64
// Descender is the distance from the baseline to the bottom of
// descending characters (typically negative), in points per unit.
Descender float64
// LineHeight is the recommended distance between baselines, in
// points per unit of font size.
LineHeight float64
// CapHeight is the height of capital letters, in points per unit.
CapHeight float64
}
FontMetrics holds typographic measurements for a resolved font, expressed as proportions or absolute values that can be scaled by the desired font size.
type FontResolver ¶
type FontResolver interface {
// Resolve looks up a font by family name, weight, and italic flag,
// returning a resolved font handle suitable for measurement.
Resolve(family string, weight document.FontWeight, italic bool) ResolvedFont
// MeasureString returns the width in points of the given text at the
// specified font size.
MeasureString(font ResolvedFont, text string, size float64) float64
// LineBreak splits text into lines that fit within maxWidth at the
// given font size.
LineBreak(font ResolvedFont, text string, size float64, maxWidth float64) []string
}
FontResolver resolves font families and weights to concrete font metrics, and provides text measurement and line-breaking services. This abstraction keeps the layout package independent of the low-level pdf/font package.
type PageLayout ¶
type PageLayout struct {
// Size is the page's physical dimensions.
Size document.Size
// Children holds the positioned nodes on this page.
Children []PlacedNode
}
PageLayout contains the final placed nodes for a single page along with the page dimensions.
type Paginator ¶
type Paginator struct {
// contains filtered or unexported fields
}
Paginator splits a document into individually laid-out pages. It uses a BlockLayout engine to place content within each page's content area and carries overflow content to subsequent pages automatically.
func NewPaginator ¶
func NewPaginator(pageSize document.Size, margins document.Edges, fontResolver FontResolver) *Paginator
NewPaginator creates a Paginator with the specified page size, margins, and font resolver.
func (*Paginator) Paginate ¶
func (p *Paginator) Paginate(doc *document.Document) []PageLayout
Paginate processes the document's pages and their content, producing a list of PageLayout values. Each input page is laid out within its content area (page size minus margins). Content that overflows is automatically continued on a new page with the same size and margins.
func (*Paginator) SetHeaderFooter ¶
func (p *Paginator) SetHeaderFooter(header, footer []document.DocumentNode)
SetHeaderFooter registers document nodes to be placed as header and footer on every page. These are laid out once to measure their height, which is subtracted from the available body height on each page.
type PlacedNode ¶
type PlacedNode struct {
// Node is the original document node.
Node document.DocumentNode
// Position is the top-left corner of the node in layout coordinates.
Position document.Point
// Size is the calculated width and height of the node.
Size document.Size
// Children holds the placed children of this node.
Children []PlacedNode
}
PlacedNode is a document node together with its calculated position, size, and recursively placed children.
type ResolvedFont ¶
type ResolvedFont struct {
// ID is a unique identifier for this font variant (e.g., "Helvetica-Bold").
ID string
// Metrics holds the font's typographic measurements.
Metrics FontMetrics
}
ResolvedFont is a handle to a concrete font, carrying an identifier and pre-resolved metrics.
type Result ¶
type Result struct {
// Bounds is the rectangle occupied by the laid-out node.
Bounds document.Rectangle
// Children holds the positioned child nodes.
Children []PlacedNode
// Overflow holds content that did not fit within the constraints.
// A nil value indicates that all content was placed.
Overflow document.DocumentNode
}
Result is the output of a layout calculation.