diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-08 15:01:55 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-08 15:01:55 +0200 |
| commit | 977ac77e6a3298be2644a8231e93acbef9f7f396 (patch) | |
| tree | 9c40765b862bc583275f692113fe36924c323ccc /src/eval | |
| parent | e1d7edb7c1845e6df6f5e23e3baf7bc88159eade (diff) | |
Start & end alignment
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/layout.rs | 4 | ||||
| -rw-r--r-- | src/eval/mod.rs | 2 | ||||
| -rw-r--r-- | src/eval/ops.rs | 6 | ||||
| -rw-r--r-- | src/eval/raw.rs | 49 |
4 files changed, 56 insertions, 5 deletions
diff --git a/src/eval/layout.rs b/src/eval/layout.rs index 9bf44194..09b69253 100644 --- a/src/eval/layout.rs +++ b/src/eval/layout.rs @@ -5,7 +5,7 @@ use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; use std::sync::Arc; -use super::{Barrier, StyleChain}; +use super::{Barrier, RawAlign, StyleChain}; use crate::diag::TypResult; use crate::frame::{Element, Frame, Geometry, Shape, Stroke}; use crate::geom::{ @@ -182,7 +182,7 @@ impl LayoutNode { } /// Set alignments for this node. - pub fn aligned(self, aligns: Spec<Option<Align>>) -> Self { + pub fn aligned(self, aligns: Spec<Option<RawAlign>>) -> Self { if aligns.any(Option::is_some) { AlignNode { aligns, child: self }.pack() } else { diff --git a/src/eval/mod.rs b/src/eval/mod.rs index e83c8159..8b777a64 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -17,6 +17,7 @@ mod func; mod layout; mod module; mod ops; +mod raw; mod scope; mod show; mod str; @@ -32,6 +33,7 @@ pub use dict::*; pub use func::*; pub use layout::*; pub use module::*; +pub use raw::*; pub use scope::*; pub use show::*; pub use styles::*; diff --git a/src/eval/ops.rs b/src/eval/ops.rs index ff21d93f..0ba4320e 100644 --- a/src/eval/ops.rs +++ b/src/eval/ops.rs @@ -1,8 +1,8 @@ use std::cmp::Ordering; -use super::{Dynamic, StrExt, Value}; +use super::{Dynamic, RawAlign, StrExt, Value}; use crate::diag::StrResult; -use crate::geom::{Align, Numeric, Spec, SpecAxis}; +use crate::geom::{Numeric, Spec, SpecAxis}; use Value::*; /// Bail with a type mismatch error. @@ -94,7 +94,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> { if let (Dyn(a), Dyn(b)) = (&a, &b) { // 1D alignments can be summed into 2D alignments. if let (Some(&a), Some(&b)) = - (a.downcast::<Align>(), b.downcast::<Align>()) + (a.downcast::<RawAlign>(), b.downcast::<RawAlign>()) { return if a.axis() != b.axis() { Ok(Dyn(Dynamic::new(match a.axis() { diff --git a/src/eval/raw.rs b/src/eval/raw.rs new file mode 100644 index 00000000..337638f9 --- /dev/null +++ b/src/eval/raw.rs @@ -0,0 +1,49 @@ +use std::fmt::{self, Debug, Formatter}; + +use super::{Resolve, StyleChain}; +use crate::geom::{Align, SpecAxis}; +use crate::library::text::ParNode; + +/// The unresolved alignment representation. +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub enum RawAlign { + /// Align at the start side of the text direction. + Start, + /// Align at the end side of the text direction. + End, + /// Align at a specific alignment. + Specific(Align), +} + +impl Resolve for RawAlign { + type Output = Align; + + fn resolve(self, styles: StyleChain) -> Self::Output { + let dir = styles.get(ParNode::DIR); + match self { + Self::Start => dir.start().into(), + Self::End => dir.end().into(), + Self::Specific(align) => align, + } + } +} + +impl RawAlign { + /// The axis this alignment belongs to. + pub const fn axis(self) -> SpecAxis { + match self { + Self::Start | Self::End => SpecAxis::Horizontal, + Self::Specific(align) => align.axis(), + } + } +} + +impl Debug for RawAlign { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Self::Start => f.pad("left"), + Self::End => f.pad("center"), + Self::Specific(align) => align.fmt(f), + } + } +} |
