Make anchoring explicit and improve reusability of text pipelines
This commit is contained in:
parent
d643bd5ba2
commit
6bf709e03e
13 changed files with 423 additions and 325 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue