summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/layout/grid.rs
diff options
context:
space:
mode:
authorSébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com>2023-11-06 21:37:50 +0100
committerGitHub <noreply@github.com>2023-11-06 21:37:50 +0100
commitc0f6d2004afebfa9412ba0c2d598ef8287197c42 (patch)
tree4bb034ca671e7d1982a306f5aecfc4f78a01841d /crates/typst-library/src/layout/grid.rs
parent8fd546760c7c425398f0114997c8085a481d8d2a (diff)
Content rework 2 - Electric Boogaloo (#2504)
Diffstat (limited to 'crates/typst-library/src/layout/grid.rs')
-rw-r--r--crates/typst-library/src/layout/grid.rs24
1 files changed, 17 insertions, 7 deletions
diff --git a/crates/typst-library/src/layout/grid.rs b/crates/typst-library/src/layout/grid.rs
index 8d015782..b29adeb9 100644
--- a/crates/typst-library/src/layout/grid.rs
+++ b/crates/typst-library/src/layout/grid.rs
@@ -1,3 +1,5 @@
+use smallvec::{smallvec, SmallVec};
+
use crate::prelude::*;
use crate::text::TextElem;
@@ -66,12 +68,14 @@ pub struct GridElem {
/// with that many `{auto}`-sized columns. Note that opposed to rows and
/// gutters, providing a single track size will only ever create a single
/// column.
+ #[borrowed]
pub columns: TrackSizings,
/// The row sizes.
///
/// If there are more cells than fit the defined rows, the last row is
/// repeated until there are no more cells.
+ #[borrowed]
pub rows: TrackSizings,
/// The gaps between rows & columns.
@@ -85,10 +89,12 @@ pub struct GridElem {
let gutter = args.named("gutter")?;
args.named("column-gutter")?.or_else(|| gutter.clone())
)]
+ #[borrowed]
pub column_gutter: TrackSizings,
/// The gaps between rows. Takes precedence over `gutter`.
#[parse(args.named("row-gutter")?.or_else(|| gutter.clone()))]
+ #[borrowed]
pub row_gutter: TrackSizings,
/// The contents of the grid cells.
@@ -106,12 +112,16 @@ impl Layout for GridElem {
styles: StyleChain,
regions: Regions,
) -> SourceResult<Fragment> {
+ let columns = self.columns(styles);
+ let rows = self.rows(styles);
+ let column_gutter = self.column_gutter(styles);
+ let row_gutter = self.row_gutter(styles);
+
// Prepare grid layout by unifying content and gutter tracks.
- let cells = self.children();
let layouter = GridLayouter::new(
- Axes::new(&self.columns(styles).0, &self.rows(styles).0),
- Axes::new(&self.column_gutter(styles).0, &self.row_gutter(styles).0),
- &cells,
+ Axes::new(&columns.0, &rows.0),
+ Axes::new(&column_gutter.0, &row_gutter.0),
+ &self.children,
regions,
styles,
self.span(),
@@ -124,13 +134,13 @@ impl Layout for GridElem {
/// Track sizing definitions.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
-pub struct TrackSizings(pub Vec<Sizing>);
+pub struct TrackSizings(pub SmallVec<[Sizing; 4]>);
cast! {
TrackSizings,
self => self.0.into_value(),
- sizing: Sizing => Self(vec![sizing]),
- count: NonZeroUsize => Self(vec![Sizing::Auto; count.get()]),
+ sizing: Sizing => Self(smallvec![sizing]),
+ count: NonZeroUsize => Self(smallvec![Sizing::Auto; count.get()]),
values: Array => Self(values.into_iter().map(Value::cast).collect::<StrResult<_>>()?),
}