layout

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package layout lays the box tree out on pages.

A Paginator lays out one page at a time (the WeasyPrint model): Next lays out from a BreakToken, returns the display list of the page and keeps the token of the next one. Block layout follows CSS 2.1 (margin collapsing, auto margins, percentages, box sizing); inline layout shapes text with internal/text, breaks it into lines with UAX #14 and overflow-wrap, and aligns it. Fragmentation happens between blocks, between lines, and inside top and bottom decorations that do not fit a page; the decorations of a fragmented box are sliced (design §3.2).

Index

Constants

View Source
const (
	MinScale = 0.1
	MaxScale = 2
)

Scale bounds, as validated by the public options.

Variables

This section is empty.

Functions

func Dump

func Dump(w io.Writer, pg *Page) error

Dump writes a text form of the page for golden tests: the page geometry, then (with Config.KeepTree) one line per block fragment and per line box, with their rectangles in layout px rounded to 0.01.

func TextLost

func TextLost(root *box.Box, pages []*Page) string

TextLost reports, sorted and capped, the text boxes of the tree that no page draws; "" when nothing is lost. It compares box by box, so copies of a repeated header or a fixed box cannot stand for text drawn nowhere. In-flow text must reach the page area, positioned and rotated text must be emitted wherever it is, the first text of each clip with an area must be drawn, and font size 0 is not checked. For tests and fuzzing: pages hold every page of the render, laid out with KeepTree.

Types

type Config

type Config struct {
	// Page gives the page size and margins; Pages, when set, gives those of the
	// pages with a name (@page name), the default page for the empty name.
	Page  *style.PageStyle
	Pages func(name string) *style.PageStyle
	// Scale multiplies the content of the page area; 0 means 1, other values are
	// clamped to [MinScale, MaxScale]. The page area is laid out at 1/Scale of
	// its size, so more fits on the page.
	Scale float64
	// Fonts is the font set of Shaper: layout shapes a space on a scratch budget
	// to find the primary font of a style, which must not count in MaxGlyphs. When
	// nil, Shaper does it and each style costs one glyph.
	Fonts  *text.FontSet
	Shaper *text.Shaper
	Budget *budget.Budget
	// KeepTree keeps the fragment tree of each page for Dump. The tree holds
	// the glyphs of the page: leave it off in production.
	KeepTree bool
}

Config is the input of a Paginator.

type Page

type Page struct {
	// Number is the 1-based page number.
	Number int
	// Name is the used page name of the page ("" for none).
	Name string
	// HasBoxes is true when the page has a margin box to draw.
	HasBoxes bool
	// Width and Height are the page size in CSS px.
	Width, Height float64
	// Area is the page area, inside the margins, in page px.
	Area display.Rect
	// Scale is the factor applied to the content: Items are in layout space,
	// relative to the origin of Area, and paint multiplies them by Scale.
	Scale float64
	Items display.List
	// contains filtered or unexported fields
}

Page is one laid out page.

type Paginator

type Paginator struct {
	// contains filtered or unexported fields
}

Paginator lays a box tree out page by page. It is not safe for concurrent use.

func NewPaginator

func NewPaginator(root *box.Box, cfg Config) *Paginator

NewPaginator returns a Paginator for the box tree rooted at root, which must be a block box (the html element).

func (*Paginator) MarginBoxes

func (p *Paginator) MarginBoxes(pg *Page, total int) (display.List, error)

MarginBoxes lays out the margin boxes of page pg, once total, the page count, is known. The items are in page coordinates (origin at the top-left corner of the page, scale 1): paint them with a Geometry whose Area is the whole page. The result is nil when the page has no margin box.

Widths follow CSS Paged Media 3 §5.3 for auto widths: the width of the row is shared in proportion to the max-content widths. When the center box exists, the outer boxes weigh as much as the wider of them, so that it stays centered. The text of a margin box stays on one line and is clipped to its box when it holds a page counter (a deviation: wrapping would shape the whole text again on every page), starting at the box start when it overflows; a text without counter wraps in its box and is laid out once per render. Each string is shaped once per render; only the page counters are shaped again on every page. pg.Number is the page number.

func (*Paginator) Next

func (p *Paginator) Next() (*Page, error)

Next lays out the next page. The first page always exists, even for an empty document; io.EOF follows the last page. A page is charged to the Pages limit.

Jump to

Keyboard shortcuts

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