Make anchoring explicit and improve reusability of text pipelines

This commit is contained in:
Héctor Ramón Jiménez 2025-05-04 03:54:42 +02:00
parent d643bd5ba2
commit 6bf709e03e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
13 changed files with 423 additions and 325 deletions

View file

@ -1,3 +1,4 @@
use crate::alignment;
use crate::{Padding, Point, Radians, Size, Vector};
/// An axis-aligned rectangle.
@ -303,6 +304,34 @@ impl Rectangle<f32> {
height: self.height * zoom,
}
}
/// Returns the top-left position to render an object of the given [`Size`].
/// inside the [`Rectangle`] that is anchored to the edge or corner
/// defined by the alignment arguments.
pub fn anchor(
&self,
size: Size,
align_x: impl Into<alignment::Horizontal>,
align_y: impl Into<alignment::Vertical>,
) -> Point {
let x = match align_x.into() {
alignment::Horizontal::Left => self.x,
alignment::Horizontal::Center => {
self.x + (self.width - size.width) / 2.0
}
alignment::Horizontal::Right => self.x + self.width - size.width,
};
let y = match align_y.into() {
alignment::Vertical::Top => self.y,
alignment::Vertical::Center => {
self.y + (self.height - size.height) / 2.0
}
alignment::Vertical::Bottom => self.y + self.height - size.height,
};
Point::new(x, y)
}
}
impl std::ops::Mul<f32> for Rectangle<f32> {