From fd84d23ade16fea22e8f8413a5d29c95c2ee1d84 Mon Sep 17 00:00:00 2001 From: jassler Date: Fri, 12 May 2023 12:26:14 +0200 Subject: Support for align parameter in table to take an array (#1087) (#1149) --- library/src/layout/table.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'library/src') diff --git a/library/src/layout/table.rs b/library/src/layout/table.rs index 6e3e6330..29e2edb8 100644 --- a/library/src/layout/table.rs +++ b/library/src/layout/table.rs @@ -87,9 +87,10 @@ pub struct TableElem { /// How to align the cell's content. /// - /// This can either be a single alignment or a function that returns an - /// alignment. The function is passed the cell's column and row index, - /// starting at zero. If set to `{auto}`, the outer alignment is used. + /// This can either be a single alignment, an array of alignments + /// (corresponding to each column) or a function that returns an alignment. + /// The function is passed the cell's column and row index, starting at zero. + /// If set to `{auto}`, the outer alignment is used. /// /// ```example /// #table( @@ -236,9 +237,11 @@ pub enum Celled { Value(T), /// A closure mapping from cell coordinates to a value. Func(Func), + /// An array of alignment values corresponding to each column. + Array(Vec), } -impl Celled { +impl Celled { /// Resolve the value based on the cell position. pub fn resolve(&self, vt: &mut Vt, x: usize, y: usize) -> SourceResult { Ok(match self { @@ -247,6 +250,11 @@ impl Celled { .call_vt(vt, [Value::Int(x as i64), Value::Int(y as i64)])? .cast() .at(func.span())?, + Self::Array(array) => x + .checked_rem(array.len()) + .and_then(|i| array.get(i)) + .cloned() + .unwrap_or_default(), }) } } @@ -259,19 +267,22 @@ impl Default for Celled { impl Cast for Celled { fn is(value: &Value) -> bool { - matches!(value, Value::Func(_)) || T::is(value) + matches!(value, Value::Array(_) | Value::Func(_)) || T::is(value) } fn cast(value: Value) -> StrResult { match value { Value::Func(v) => Ok(Self::Func(v)), + Value::Array(array) => { + Ok(Self::Array(array.into_iter().map(T::cast).collect::>()?)) + } v if T::is(&v) => Ok(Self::Value(T::cast(v)?)), v => ::error(v), } } fn describe() -> CastInfo { - T::describe() + CastInfo::Type("function") + T::describe() + CastInfo::Type("array") + CastInfo::Type("function") } } @@ -280,6 +291,7 @@ impl> From> for Value { match celled { Celled::Value(value) => value.into(), Celled::Func(func) => func.into(), + Celled::Array(arr) => arr.into(), } } } -- cgit v1.2.3