geometry.rs

  1use bytemuck::{Pod, Zeroable};
  2use core::fmt::Debug;
  3use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
  4use refineable::Refineable;
  5use std::ops::{Add, Mul, Sub, SubAssign};
  6
  7#[derive(
  8    Refineable, Default, Add, AddAssign, Sub, SubAssign, Mul, Div, Copy, Debug, PartialEq, Eq, Hash,
  9)]
 10#[refineable(debug)]
 11#[repr(C)]
 12pub struct Point<T: Clone + Debug> {
 13    pub x: T,
 14    pub y: T,
 15}
 16
 17pub fn point<T: Clone + Debug>(x: T, y: T) -> Point<T> {
 18    Point { x, y }
 19}
 20
 21impl<T: Clone + Debug> Point<T> {
 22    pub fn new(x: T, y: T) -> Self {
 23        Self { x, y }
 24    }
 25
 26    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Point<U> {
 27        Point {
 28            x: f(self.x.clone()),
 29            y: f(self.y.clone()),
 30        }
 31    }
 32}
 33
 34impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
 35    fn sub_assign(&mut self, rhs: Size<T>) {
 36        self.x = self.x.clone() - rhs.width;
 37        self.y = self.y.clone() - rhs.height;
 38    }
 39}
 40
 41impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
 42    pub fn max(&self, other: &Self) -> Self {
 43        Point {
 44            x: if self.x >= other.x {
 45                self.x.clone()
 46            } else {
 47                other.x.clone()
 48            },
 49            y: if self.y >= other.y {
 50                self.y.clone()
 51            } else {
 52                other.y.clone()
 53            },
 54        }
 55    }
 56}
 57
 58impl<T: Clone + Debug> Clone for Point<T> {
 59    fn clone(&self) -> Self {
 60        Self {
 61            x: self.x.clone(),
 62            y: self.y.clone(),
 63        }
 64    }
 65}
 66
 67unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Point<T> {}
 68
 69unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Point<T> {}
 70
 71#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq)]
 72#[refineable(debug)]
 73pub struct Size<T: Clone + Debug> {
 74    pub width: T,
 75    pub height: T,
 76}
 77
 78pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
 79    Size { width, height }
 80}
 81
 82impl Size<Length> {
 83    pub fn full() -> Self {
 84        Self {
 85            width: relative(1.),
 86            height: relative(1.),
 87        }
 88    }
 89}
 90
 91impl Size<DefiniteLength> {
 92    pub fn zero() -> Self {
 93        Self {
 94            width: px(0.).into(),
 95            height: px(0.).into(),
 96        }
 97    }
 98}
 99
100impl Size<Length> {
101    pub fn auto() -> Self {
102        Self {
103            width: Length::Auto,
104            height: Length::Auto,
105        }
106    }
107}
108
109#[derive(Refineable, Clone, Default, Debug, PartialEq)]
110#[refineable(debug)]
111pub struct Bounds<T: Clone + Debug> {
112    pub origin: Point<T>,
113    pub size: Size<T>,
114}
115
116impl<T: Clone + Debug + Copy + Add<T, Output = T>> Bounds<T> {
117    pub fn upper_right(&self) -> Point<T> {
118        Point {
119            x: self.origin.x + self.size.width,
120            y: self.origin.y,
121        }
122    }
123
124    pub fn lower_right(&self) -> Point<T> {
125        Point {
126            x: self.origin.x + self.size.width,
127            y: self.origin.y + self.size.height,
128        }
129    }
130}
131
132impl<T: Clone + Debug + Copy + PartialOrd + Add<T, Output = T>> Bounds<T> {
133    pub fn contains_point(&self, point: Point<T>) -> bool {
134        point.x >= self.origin.x
135            && point.x <= self.origin.x + self.size.width
136            && point.y >= self.origin.y
137            && point.y <= self.origin.y + self.size.height
138    }
139}
140
141impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
142
143#[derive(Refineable, Clone, Default, Debug)]
144#[refineable(debug)]
145pub struct Edges<T: Clone + Debug> {
146    pub top: T,
147    pub right: T,
148    pub bottom: T,
149    pub left: T,
150}
151
152impl Edges<Length> {
153    pub fn auto() -> Self {
154        Self {
155            top: Length::Auto,
156            right: Length::Auto,
157            bottom: Length::Auto,
158            left: Length::Auto,
159        }
160    }
161
162    pub fn zero() -> Self {
163        Self {
164            top: px(0.).into(),
165            right: px(0.).into(),
166            bottom: px(0.).into(),
167            left: px(0.).into(),
168        }
169    }
170}
171
172impl Edges<DefiniteLength> {
173    pub fn zero() -> Self {
174        Self {
175            top: px(0.).into(),
176            right: px(0.).into(),
177            bottom: px(0.).into(),
178            left: px(0.).into(),
179        }
180    }
181}
182
183impl Edges<AbsoluteLength> {
184    pub fn zero() -> Self {
185        Self {
186            top: px(0.).into(),
187            right: px(0.).into(),
188            bottom: px(0.).into(),
189            left: px(0.).into(),
190        }
191    }
192
193    pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
194        Edges {
195            top: self.top.to_pixels(rem_size),
196            right: self.right.to_pixels(rem_size),
197            bottom: self.bottom.to_pixels(rem_size),
198            left: self.left.to_pixels(rem_size),
199        }
200    }
201}
202
203impl Edges<Pixels> {
204    pub fn is_empty(&self) -> bool {
205        self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.)
206    }
207}
208
209#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
210#[repr(transparent)]
211pub struct Pixels(pub(crate) f32);
212
213impl Pixels {
214    pub fn round(&self) -> Self {
215        Self(self.0.round())
216    }
217}
218
219impl Mul<f32> for Pixels {
220    type Output = Pixels;
221
222    fn mul(self, other: f32) -> Pixels {
223        Pixels(self.0 * other)
224    }
225}
226
227impl Mul<Pixels> for Pixels {
228    type Output = Pixels;
229
230    fn mul(self, rhs: Pixels) -> Self::Output {
231        Pixels(self.0 * rhs.0)
232    }
233}
234
235impl Eq for Pixels {}
236
237impl Ord for Pixels {
238    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
239        self.0.partial_cmp(&other.0).unwrap()
240    }
241}
242
243impl std::hash::Hash for Pixels {
244    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
245        self.0.to_bits().hash(state);
246    }
247}
248
249impl From<f64> for Pixels {
250    fn from(val: f64) -> Self {
251        Pixels(val as f32)
252    }
253}
254
255unsafe impl bytemuck::Pod for Pixels {}
256unsafe impl bytemuck::Zeroable for Pixels {}
257
258impl Debug for Pixels {
259    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
260        write!(f, "{} px", self.0)
261    }
262}
263
264impl From<Pixels> for f32 {
265    fn from(pixels: Pixels) -> Self {
266        pixels.0
267    }
268}
269
270impl From<&Pixels> for f32 {
271    fn from(pixels: &Pixels) -> Self {
272        pixels.0
273    }
274}
275
276impl From<Pixels> for f64 {
277    fn from(pixels: Pixels) -> Self {
278        pixels.0 as f64
279    }
280}
281
282#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
283pub struct Rems(f32);
284
285impl Mul<Pixels> for Rems {
286    type Output = Pixels;
287
288    fn mul(self, other: Pixels) -> Pixels {
289        Pixels(self.0 * other.0)
290    }
291}
292
293impl Debug for Rems {
294    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
295        write!(f, "{} rem", self.0)
296    }
297}
298
299#[derive(Clone, Copy, Debug)]
300pub enum AbsoluteLength {
301    Pixels(Pixels),
302    Rems(Rems),
303}
304
305impl From<Pixels> for AbsoluteLength {
306    fn from(pixels: Pixels) -> Self {
307        AbsoluteLength::Pixels(pixels)
308    }
309}
310
311impl From<Rems> for AbsoluteLength {
312    fn from(rems: Rems) -> Self {
313        AbsoluteLength::Rems(rems)
314    }
315}
316
317impl AbsoluteLength {
318    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
319        match self {
320            AbsoluteLength::Pixels(pixels) => *pixels,
321            AbsoluteLength::Rems(rems) => *rems * rem_size,
322        }
323    }
324}
325
326impl Default for AbsoluteLength {
327    fn default() -> Self {
328        px(0.).into()
329    }
330}
331
332/// A non-auto length that can be defined in pixels, rems, or percent of parent.
333#[derive(Clone, Copy)]
334pub enum DefiniteLength {
335    Absolute(AbsoluteLength),
336    /// A fraction of the parent's size between 0 and 1.
337    Fraction(f32),
338}
339
340impl Debug for DefiniteLength {
341    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
342        match self {
343            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
344            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
345        }
346    }
347}
348
349impl From<Pixels> for DefiniteLength {
350    fn from(pixels: Pixels) -> Self {
351        Self::Absolute(pixels.into())
352    }
353}
354
355impl From<Rems> for DefiniteLength {
356    fn from(rems: Rems) -> Self {
357        Self::Absolute(rems.into())
358    }
359}
360
361impl From<AbsoluteLength> for DefiniteLength {
362    fn from(length: AbsoluteLength) -> Self {
363        Self::Absolute(length)
364    }
365}
366
367impl Default for DefiniteLength {
368    fn default() -> Self {
369        Self::Absolute(AbsoluteLength::default())
370    }
371}
372
373/// A length that can be defined in pixels, rems, percent of parent, or auto.
374#[derive(Clone, Copy)]
375pub enum Length {
376    Definite(DefiniteLength),
377    Auto,
378}
379
380impl Debug for Length {
381    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
382        match self {
383            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
384            Length::Auto => write!(f, "auto"),
385        }
386    }
387}
388
389pub fn relative<T: From<DefiniteLength>>(fraction: f32) -> T {
390    DefiniteLength::Fraction(fraction).into()
391}
392
393pub fn rems(rems: f32) -> Rems {
394    Rems(rems)
395}
396
397pub fn px(pixels: f32) -> Pixels {
398    Pixels(pixels)
399}
400
401pub fn auto() -> Length {
402    Length::Auto
403}
404
405impl From<Pixels> for Length {
406    fn from(pixels: Pixels) -> Self {
407        Self::Definite(pixels.into())
408    }
409}
410
411impl From<Rems> for Length {
412    fn from(rems: Rems) -> Self {
413        Self::Definite(rems.into())
414    }
415}
416
417impl From<DefiniteLength> for Length {
418    fn from(length: DefiniteLength) -> Self {
419        Self::Definite(length)
420    }
421}
422
423impl From<AbsoluteLength> for Length {
424    fn from(length: AbsoluteLength) -> Self {
425        Self::Definite(length.into())
426    }
427}
428
429impl Default for Length {
430    fn default() -> Self {
431        Self::Definite(DefiniteLength::default())
432    }
433}
434
435impl From<()> for Length {
436    fn from(_: ()) -> Self {
437        Self::Definite(DefiniteLength::default())
438    }
439}