summaryrefslogtreecommitdiff
path: root/src/geom
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-07-29 12:21:55 +0200
committerLaurenz <laurmaedje@gmail.com>2021-07-29 12:28:54 +0200
commit7d15dc634b3be1b6e284bb6b2450e3736d3e6e8d (patch)
treef1dc308528515ccfc90e047fe3cb75bd171b3f8c /src/geom
parent853361338bd756c23992041511b1836a2cd0647f (diff)
Move font family and refactor alignment
Diffstat (limited to 'src/geom')
-rw-r--r--src/geom/align.rs62
1 files changed, 55 insertions, 7 deletions
diff --git a/src/geom/align.rs b/src/geom/align.rs
index f6e5bb2a..d83c00b0 100644
--- a/src/geom/align.rs
+++ b/src/geom/align.rs
@@ -1,6 +1,6 @@
use super::*;
-/// Where to align something along a directed axis.
+/// Where to align something along an axis.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Align {
/// Align at the start of the axis.
@@ -9,15 +9,27 @@ pub enum Align {
Center,
/// Align at the end of the axis.
End,
+ /// Align at the left side of the axis.
+ Left,
+ /// Align at the right side of the axis.
+ Right,
+ /// Align at the top side of the axis.
+ Top,
+ /// Align at the bottom side of the axis.
+ Bottom,
}
impl Align {
- /// Returns the position of this alignment in the given range.
- pub fn resolve(self, dir: Dir, range: Range<Length>) -> Length {
- match if dir.is_positive() { self } else { self.inv() } {
- Self::Start => range.start,
- Self::Center => (range.start + range.end) / 2.0,
- Self::End => range.end,
+ /// The axis this alignment belongs to if it is specific.
+ pub fn axis(self) -> Option<SpecAxis> {
+ match self {
+ Self::Start => None,
+ Self::Center => None,
+ Self::End => None,
+ Self::Left => Some(SpecAxis::Horizontal),
+ Self::Right => Some(SpecAxis::Horizontal),
+ Self::Top => Some(SpecAxis::Vertical),
+ Self::Bottom => Some(SpecAxis::Vertical),
}
}
@@ -27,6 +39,38 @@ impl Align {
Self::Start => Self::End,
Self::Center => Self::Center,
Self::End => Self::Start,
+ Self::Left => Self::Right,
+ Self::Right => Self::Left,
+ Self::Top => Self::Bottom,
+ Self::Bottom => Self::Top,
+ }
+ }
+
+ /// Returns the position of this alignment in the given range.
+ pub fn resolve(self, dir: Dir, range: Range<Length>) -> Length {
+ #[cfg(debug_assertions)]
+ if let Some(axis) = self.axis() {
+ debug_assert_eq!(axis, dir.axis())
+ }
+
+ match self {
+ Self::Start => {
+ if dir.is_positive() {
+ range.start
+ } else {
+ range.end
+ }
+ }
+ Self::Center => (range.start + range.end) / 2.0,
+ Self::End => {
+ if dir.is_positive() {
+ range.end
+ } else {
+ range.start
+ }
+ }
+ Self::Left | Self::Top => range.start,
+ Self::Right | Self::Bottom => range.end,
}
}
}
@@ -43,6 +87,10 @@ impl Display for Align {
Self::Start => "start",
Self::Center => "center",
Self::End => "end",
+ Self::Left => "left",
+ Self::Right => "right",
+ Self::Top => "top",
+ Self::Bottom => "bottom",
})
}
}