diff options
| author | PgBiel <9021226+PgBiel@users.noreply.github.com> | 2024-03-13 05:54:36 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-13 08:54:36 +0000 |
| commit | fd2eb0ceb25270e5ea738b76a2f5271e84234667 (patch) | |
| tree | 6b731b4a359eb5e1d415db87bae22079f1da779f | |
| parent | 3fd06136c27a4bbdfc7833526311313c4a87be71 (diff) | |
Ensure grids have at least the given amount of rows (#3644)
| -rw-r--r-- | crates/typst/src/layout/grid/layout.rs | 15 | ||||
| -rw-r--r-- | tests/ref/layout/grid-5.png | bin | 3891 -> 9301 bytes | |||
| -rw-r--r-- | tests/typ/layout/grid-5.typ | 10 |
3 files changed, 23 insertions, 2 deletions
diff --git a/crates/typst/src/layout/grid/layout.rs b/crates/typst/src/layout/grid/layout.rs index 6d89b73c..645b68b6 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<Entry>'. - // 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 Binary files differindex a57493e4..233ebb00 100644 --- a/tests/ref/layout/grid-5.png +++ b/tests/ref/layout/grid-5.png diff --git a/tests/typ/layout/grid-5.typ b/tests/typ/layout/grid-5.typ index 64385f61..66272421 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] +) |
