typst_library/layout/
rect.rs

1use crate::layout::{Point, Size};
2
3/// A rectangle in 2D.
4#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
5pub struct Rect {
6    /// The top left corner (minimum coordinate).
7    pub min: Point,
8    /// The bottom right corner (maximum coordinate).
9    pub max: Point,
10}
11
12impl Rect {
13    /// Create a new rectangle from the minimum/maximum coordinate.
14    pub fn new(min: Point, max: Point) -> Self {
15        Self { min, max }
16    }
17
18    /// Create a new rectangle from the position and size.
19    pub fn from_pos_size(pos: Point, size: Size) -> Self {
20        Self { min: pos, max: pos + size.to_point() }
21    }
22
23    /// Compute the size of the rectangle.
24    pub fn size(&self) -> Size {
25        Size::new(self.max.x - self.min.x, self.max.y - self.min.y)
26    }
27}