summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorMalo <57839069+MDLC01@users.noreply.github.com>2025-02-25 15:19:17 +0100
committerGitHub <noreply@github.com>2025-02-25 14:19:17 +0000
commitd6b0d68ffa4963459f52f7d774080f1f128841d4 (patch)
tree962bfc845f46157b3d0a62add392aa8c6fc2d0ce /crates
parent8f039dd614ba518976b8b486e0a138bd6a9c660c (diff)
Add more methods to `direction` (#5893)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst-library/src/layout/dir.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/crates/typst-library/src/layout/dir.rs b/crates/typst-library/src/layout/dir.rs
index 9a2e7710..699c8c48 100644
--- a/crates/typst-library/src/layout/dir.rs
+++ b/crates/typst-library/src/layout/dir.rs
@@ -50,6 +50,42 @@ impl Dir {
pub const TTB: Self = Self::TTB;
pub const BTT: Self = Self::BTT;
+ /// Returns a direction from a starting point.
+ ///
+ /// ```example
+ /// direction.from(left) \
+ /// direction.from(right) \
+ /// direction.from(top) \
+ /// direction.from(bottom)
+ /// ```
+ #[func]
+ pub const fn from(side: Side) -> Dir {
+ match side {
+ Side::Left => Self::LTR,
+ Side::Right => Self::RTL,
+ Side::Top => Self::TTB,
+ Side::Bottom => Self::BTT,
+ }
+ }
+
+ /// Returns a direction from an end point.
+ ///
+ /// ```example
+ /// direction.to(left) \
+ /// direction.to(right) \
+ /// direction.to(top) \
+ /// direction.to(bottom)
+ /// ```
+ #[func]
+ pub const fn to(side: Side) -> Dir {
+ match side {
+ Side::Right => Self::LTR,
+ Side::Left => Self::RTL,
+ Side::Bottom => Self::TTB,
+ Side::Top => Self::BTT,
+ }
+ }
+
/// The axis this direction belongs to, either `{"horizontal"}` or
/// `{"vertical"}`.
///
@@ -65,6 +101,22 @@ impl Dir {
}
}
+ /// The corresponding sign, for use in calculations.
+ ///
+ /// ```example
+ /// #ltr.sign() \
+ /// #rtl.sign() \
+ /// #ttb.sign() \
+ /// #btt.sign()
+ /// ```
+ #[func]
+ pub const fn sign(self) -> i64 {
+ match self {
+ Self::LTR | Self::TTB => 1,
+ Self::RTL | Self::BTT => -1,
+ }
+ }
+
/// The start point of this direction, as an alignment.
///
/// ```example