ori_core/layout/
size.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::{
    fmt::Display,
    hash::{Hash, Hasher},
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign},
};

use super::{Point, Vector};

/// A 2 dimensional size.
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Size {
    /// The width.
    pub width: f32,
    /// The height.
    pub height: f32,
}

impl Size {
    /// The zero size.
    pub const ZERO: Self = Self::new(0.0, 0.0);

    /// The unbounded size.
    pub const UNBOUNDED: Self = Self::new(f32::INFINITY, f32::INFINITY);

    /// The infinite size.
    pub const INFINITY: Self = Self::new(f32::INFINITY, f32::INFINITY);

    /// Alias for [`Self::INFINITY`].
    pub const FILL: Self = Self::INFINITY;

    /// Create a new size.
    pub const fn new(width: f32, height: f32) -> Self {
        Self { width, height }
    }

    /// Create a new size with the same width and height.
    pub const fn all(value: f32) -> Self {
        Self::new(value, value)
    }

    /// Get the min of self and other by element.
    pub fn min(self, other: Self) -> Self {
        Self::new(self.width.min(other.width), self.height.min(other.height))
    }

    /// Get the smallest element of self.
    pub fn min_element(self) -> f32 {
        self.width.min(self.height)
    }

    /// Get the max of self and other by element.
    pub fn max(self, other: Self) -> Self {
        Self::new(self.width.max(other.width), self.height.max(other.height))
    }

    /// Get the largest element of self.
    pub fn max_element(self) -> f32 {
        self.width.max(self.height)
    }

    /// Clamp self to the range [min, max] by element.
    pub fn clamp(self, min: Self, max: Self) -> Self {
        Self::new(
            self.width.clamp(min.width, max.width),
            self.height.clamp(min.height, max.height),
        )
    }

    /// Floor self by element.
    pub fn floor(self) -> Self {
        Self::new(self.width.floor(), self.height.floor())
    }

    /// Ceil self by element.
    pub fn ceil(self) -> Self {
        Self::new(self.width.ceil(), self.height.ceil())
    }

    /// Round self by element.
    pub fn round(self) -> Self {
        Self::new(self.width.round(), self.height.round())
    }

    /// If self is finite, return self, otherwise return zero. Applied by element.
    pub fn finite_or_zero(self) -> Self {
        let width = if self.width.is_finite() {
            self.width
        } else {
            0.0
        };

        let height = if self.height.is_finite() {
            self.height
        } else {
            0.0
        };

        Self::new(width, height)
    }

    /// Get whether the size is finite.
    pub fn is_finite(self) -> bool {
        self.width.is_finite() && self.height.is_finite()
    }

    /// Get whether the size is infinite.
    pub fn is_infinite(self) -> bool {
        self.width.is_infinite() || self.height.is_infinite()
    }

    /// Convert the size to a vector.
    pub const fn to_point(self) -> Point {
        Point::new(self.width, self.height)
    }

    /// Convert the size to a vector.
    pub const fn to_vector(self) -> Vector {
        Vector::new(self.width, self.height)
    }
}

impl From<(f32, f32)> for Size {
    fn from((width, height): (f32, f32)) -> Self {
        Self::new(width, height)
    }
}

impl From<[f32; 2]> for Size {
    fn from([width, height]: [f32; 2]) -> Self {
        Self::new(width, height)
    }
}

impl From<Point> for Size {
    fn from(vec: Point) -> Self {
        Self::new(vec.x, vec.y)
    }
}

impl From<Vector> for Size {
    fn from(vec: Vector) -> Self {
        vec.to_size()
    }
}

impl From<f32> for Size {
    fn from(value: f32) -> Self {
        Self::all(value)
    }
}

impl From<Size> for (f32, f32) {
    fn from(size: Size) -> Self {
        (size.width, size.height)
    }
}

impl From<Size> for [f32; 2] {
    fn from(size: Size) -> Self {
        [size.width, size.height]
    }
}

impl Display for Size {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{} x {}]", self.width, self.height)
    }
}

macro_rules! impl_math_op {
    ($op_trait:ident, $op_assign_trait:ident, $op_fn:ident, $op_assign_fn:ident, $op:tt) => {
        impl $op_trait for Size {
            type Output = Self;

            fn $op_fn(self, rhs: Self) -> Self::Output {
                Self::new(self.width $op rhs.width, self.height $op rhs.height)
            }
        }

        impl $op_assign_trait for Size {
            fn $op_assign_fn(&mut self, rhs: Self) {
                *self = *self $op rhs;
            }
        }

        impl $op_trait<f32> for Size {
            type Output = Self;

            fn $op_fn(self, rhs: f32) -> Self::Output {
                Self::new(self.width $op rhs, self.height $op rhs)
            }
        }

        impl $op_assign_trait<f32> for Size {
            fn $op_assign_fn(&mut self, rhs: f32) {
                *self = *self $op rhs;
            }
        }
    };
}

impl_math_op!(Add, AddAssign, add, add_assign, +);
impl_math_op!(Sub, SubAssign, sub, sub_assign, -);
impl_math_op!(Mul, MulAssign, mul, mul_assign, *);
impl_math_op!(Div, DivAssign, div, div_assign, /);
impl_math_op!(Rem, RemAssign, rem, rem_assign, %);

impl Hash for Size {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.width.to_bits().hash(state);
        self.height.to_bits().hash(state);
    }
}