diff --git a/crates/typst/src/layout/grid/layout.rs b/crates/typst/src/layout/grid/layout.rs index 6d89b73c8..645b68b6e 100644 --- a/crates/typst/src/layout/grid/layout.rs +++ b/crates/typst/src/layout/grid/layout.rs @@ -847,15 +847,26 @@ impl CellGrid { } } + // If the user specified cells occupying less rows than the given rows, + // we shall expand the grid so that it has at least the given amount of + // rows. + let Some(expected_total_cells) = c.checked_mul(tracks.y.len()) else { + bail!(span, "too many rows were specified"); + }; + let missing_cells = expected_total_cells.saturating_sub(resolved_cells.len()); + // Fixup phase (final step in cell grid generation): // 1. Replace absent entries by resolved empty cells, and produce a // vector of 'Entry' from 'Option'. - // 2. If any cells were added to the header's rows after the header's + // 2. Add enough empty cells to the end of the grid such that it has at + // least the given amount of rows. + // 3. If any cells were added to the header's rows after the header's // creation, ensure the header expands enough to accommodate them // across all of their spanned rows. Same for the footer. - // 3. If any cells before the footer try to span it, error. + // 4. If any cells before the footer try to span it, error. let resolved_cells = resolved_cells .into_iter() + .chain(std::iter::repeat_with(|| None).take(missing_cells)) .enumerate() .map(|(i, cell)| { if let Some(cell) = cell { diff --git a/tests/ref/layout/grid-5.png b/tests/ref/layout/grid-5.png index a57493e49..233ebb00c 100644 Binary files a/tests/ref/layout/grid-5.png and b/tests/ref/layout/grid-5.png differ diff --git a/tests/typ/layout/grid-5.typ b/tests/typ/layout/grid-5.typ index 64385f61d..662724218 100644 --- a/tests/typ/layout/grid-5.typ +++ b/tests/typ/layout/grid-5.typ @@ -28,3 +28,13 @@ ], align(top)[B], ) + +--- +// Ensure grids expand enough for the given rows. +#grid( + columns: (2em, 2em), + rows: (2em,) * 4, + fill: red, + stroke: aqua, + [a] +)