Tables

Tables

The table model is similar to that of HTML. You combine cells into rows which form a table to be typeset. In HTML for example you can create a simple table with this code:

<table>
  <tr>
    <td>Hello,</td>
    <td>world</td>
  </tr>
  <tr>
    <td>Hello,</td>
    <td>user</td>
  </tr>
</table>

So a table is built row by row. In boxes and glue, the table consists of table rows (TableRow) and cells (TableCell). The contents of each cell is a sequence of Paragraph structs and the contents of a row is a sequence of cells.

Table cell

type TableCell struct {
	BackgroundColor   *color.Color
	BorderTopColor    *color.Color
	BorderBottomColor *color.Color
	BorderLeftColor   *color.Color
	BorderRightColor  *color.Color
	Contents []any
	BorderTopWidth    bag.ScaledPoint
	BorderBottomWidth bag.ScaledPoint
	BorderLeftWidth   bag.ScaledPoint
	BorderRightWidth  bag.ScaledPoint
	CalculatedWidth   bag.ScaledPoint
	CalculatedHeight  bag.ScaledPoint
	HAlign            HorizontalAlignment
	VAlign            VerticalAlignment
	ExtraColspan      int
	ExtraRowspan      int
	PaddingTop        bag.ScaledPoint
	PaddingBottom     bag.ScaledPoint
	PaddingLeft       bag.ScaledPoint
	PaddingRight      bag.ScaledPoint
	IsHeader bool // true for &amp;lt;th&amp;gt; cells
}

Table row

type TableRow struct {
	Cells            []*TableCell
	CalculatedHeight bag.ScaledPoint
	VAlign           VerticalAlignment
}
type Table struct {
	FontFamily *FontFamily
	Rows    TableRows
	ColSpec []ColSpec
	MaxWidth bag.ScaledPoint
	FontSize bag.ScaledPoint
	Leading  bag.ScaledPoint
	Stretch    bool
	HeaderRows int // number of initial rows that are header rows (from &amp;lt;thead&amp;gt;)
}

HeaderRows specifies how many of the initial rows are header rows (typically from <thead>). When a table spans multiple pages, these rows are automatically repeated at the top of each continuation page. The header repetition is handled by the page breaker (e.g. htmlbag’s outputGroupNodes): BuildTable stores a closure in the VList’s attributes that can rebuild the header rows fresh for each new page.

Stretch controls whether the table expands to fill MaxWidth. When false, the table is only as wide as its content requires.

func (fe *Document) BuildTable(tbl *Table) ([]*node.VList, error)

BuildTable returns a single VList containing all table rows. If HeaderRows is set, the VList carries metadata that enables the page breaker to split the table across pages and repeat headers automatically. The caller does not need to handle splitting — it is done during page output.