typst_layout/grid/
mod.rs

1mod layouter;
2mod lines;
3mod repeated;
4mod rowspans;
5
6pub use self::layouter::GridLayouter;
7
8use typst_library::diag::SourceResult;
9use typst_library::engine::Engine;
10use typst_library::foundations::{Packed, StyleChain};
11use typst_library::introspection::Locator;
12use typst_library::layout::grid::resolve::{grid_to_cellgrid, table_to_cellgrid, Cell};
13use typst_library::layout::{Fragment, GridElem, Regions};
14use typst_library::model::TableElem;
15
16use self::layouter::RowPiece;
17use self::lines::{
18    generate_line_segments, hline_stroke_at_column, vline_stroke_at_row, LineSegment,
19};
20use self::rowspans::{Rowspan, UnbreakableRowGroup};
21
22/// Layout the cell into the given regions.
23///
24/// The `disambiguator` indicates which instance of this cell this should be
25/// layouted as. For normal cells, it is always `0`, but for headers and
26/// footers, it indicates the index of the header/footer among all. See the
27/// [`Locator`] docs for more details on the concepts behind this.
28pub fn layout_cell(
29    cell: &Cell,
30    engine: &mut Engine,
31    disambiguator: usize,
32    styles: StyleChain,
33    regions: Regions,
34) -> SourceResult<Fragment> {
35    let mut locator = cell.locator.relayout();
36    if disambiguator > 0 {
37        locator = locator.split().next_inner(disambiguator as u128);
38    }
39    crate::layout_fragment(engine, &cell.body, locator, styles, regions)
40}
41
42/// Layout the grid.
43#[typst_macros::time(span = elem.span())]
44pub fn layout_grid(
45    elem: &Packed<GridElem>,
46    engine: &mut Engine,
47    locator: Locator,
48    styles: StyleChain,
49    regions: Regions,
50) -> SourceResult<Fragment> {
51    let grid = grid_to_cellgrid(elem, engine, locator, styles)?;
52    GridLayouter::new(&grid, regions, styles, elem.span()).layout(engine)
53}
54
55/// Layout the table.
56#[typst_macros::time(span = elem.span())]
57pub fn layout_table(
58    elem: &Packed<TableElem>,
59    engine: &mut Engine,
60    locator: Locator,
61    styles: StyleChain,
62    regions: Regions,
63) -> SourceResult<Fragment> {
64    let grid = table_to_cellgrid(elem, engine, locator, styles)?;
65    GridLayouter::new(&grid, regions, styles, elem.span()).layout(engine)
66}