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, AddAssign, Mul, MulAssign, 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 + Mul<S, Output = T>, S: Clone> MulAssign<S> for Point<T> {
 35    fn mul_assign(&mut self, rhs: S) {
 36        self.x = self.x.clone() * rhs.clone();
 37        self.y = self.y.clone() * rhs;
 38    }
 39}
 40
 41impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
 42    fn sub_assign(&mut self, rhs: Size<T>) {
 43        self.x = self.x.clone() - rhs.width;
 44        self.y = self.y.clone() - rhs.height;
 45    }
 46}
 47
 48impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
 49    fn add_assign(&mut self, rhs: T) {
 50        self.x = self.x.clone() + rhs;
 51        self.y = self.y.clone() + rhs;
 52    }
 53}
 54
 55impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
 56    pub fn max(&self, other: &Self) -> Self {
 57        Point {
 58            x: if self.x >= other.x {
 59                self.x.clone()
 60            } else {
 61                other.x.clone()
 62            },
 63            y: if self.y >= other.y {
 64                self.y.clone()
 65            } else {
 66                other.y.clone()
 67            },
 68        }
 69    }
 70}
 71
 72impl<T: Clone + Debug> Clone for Point<T> {
 73    fn clone(&self) -> Self {
 74        Self {
 75            x: self.x.clone(),
 76            y: self.y.clone(),
 77        }
 78    }
 79}
 80
 81unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Point<T> {}
 82unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Point<T> {}
 83
 84#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq)]
 85#[refineable(debug)]
 86#[repr(C)]
 87pub struct Size<T: Clone + Debug> {
 88    pub width: T,
 89    pub height: T,
 90}
 91
 92unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Size<T> {}
 93unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Size<T> {}
 94
 95pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
 96    Size { width, height }
 97}
 98
 99impl<T: Clone + Debug> Size<T> {
100    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
101        Size {
102            width: f(self.width.clone()),
103            height: f(self.height.clone()),
104        }
105    }
106}
107
108impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Size<T> {
109    fn mul_assign(&mut self, rhs: S) {
110        self.width = self.width.clone() * rhs.clone();
111        self.height = self.height.clone() * rhs;
112    }
113}
114
115impl From<Size<Option<Pixels>>> for Size<Option<f32>> {
116    fn from(val: Size<Option<Pixels>>) -> Self {
117        Size {
118            width: val.width.map(|p| p.0 as f32),
119            height: val.height.map(|p| p.0 as f32),
120        }
121    }
122}
123
124impl Size<Length> {
125    pub fn full() -> Self {
126        Self {
127            width: relative(1.).into(),
128            height: relative(1.).into(),
129        }
130    }
131}
132
133impl Size<DefiniteLength> {
134    pub fn zero() -> Self {
135        Self {
136            width: px(0.).into(),
137            height: px(0.).into(),
138        }
139    }
140}
141
142impl Size<Length> {
143    pub fn auto() -> Self {
144        Self {
145            width: Length::Auto,
146            height: Length::Auto,
147        }
148    }
149}
150
151#[derive(Refineable, Clone, Default, Debug, PartialEq)]
152#[refineable(debug)]
153#[repr(C)]
154pub struct Bounds<T: Clone + Debug> {
155    pub origin: Point<T>,
156    pub size: Size<T>,
157}
158
159unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Bounds<T> {}
160unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Bounds<T> {}
161
162impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
163    pub fn upper_right(&self) -> Point<T> {
164        Point {
165            x: self.origin.x.clone() + self.size.width.clone(),
166            y: self.origin.y.clone(),
167        }
168    }
169
170    pub fn lower_right(&self) -> Point<T> {
171        Point {
172            x: self.origin.x.clone() + self.size.width.clone(),
173            y: self.origin.y.clone() + self.size.height.clone(),
174        }
175    }
176}
177
178impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
179    pub fn contains_point(&self, point: Point<T>) -> bool {
180        point.x >= self.origin.x
181            && point.x <= self.origin.x.clone() + self.size.width.clone()
182            && point.y >= self.origin.y
183            && point.y <= self.origin.y.clone() + self.size.height.clone()
184    }
185}
186
187impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
188
189#[derive(Refineable, Clone, Default, Debug)]
190#[refineable(debug)]
191#[repr(C)]
192pub struct Edges<T: Clone + Debug> {
193    pub top: T,
194    pub right: T,
195    pub bottom: T,
196    pub left: T,
197}
198
199impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Edges<T> {
200    fn mul_assign(&mut self, rhs: S) {
201        self.top = self.top.clone() * rhs.clone();
202        self.right = self.right.clone() * rhs.clone();
203        self.bottom = self.bottom.clone() * rhs.clone();
204        self.left = self.left.clone() * rhs.clone();
205    }
206}
207
208impl<T: Clone + Debug + Copy> Copy for Edges<T> {}
209
210unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Edges<T> {}
211
212unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Edges<T> {}
213
214impl Edges<Length> {
215    pub fn auto() -> Self {
216        Self {
217            top: Length::Auto,
218            right: Length::Auto,
219            bottom: Length::Auto,
220            left: Length::Auto,
221        }
222    }
223
224    pub fn zero() -> Self {
225        Self {
226            top: px(0.).into(),
227            right: px(0.).into(),
228            bottom: px(0.).into(),
229            left: px(0.).into(),
230        }
231    }
232}
233
234impl Edges<DefiniteLength> {
235    pub fn zero() -> Self {
236        Self {
237            top: px(0.).into(),
238            right: px(0.).into(),
239            bottom: px(0.).into(),
240            left: px(0.).into(),
241        }
242    }
243}
244
245impl Edges<AbsoluteLength> {
246    pub fn zero() -> Self {
247        Self {
248            top: px(0.).into(),
249            right: px(0.).into(),
250            bottom: px(0.).into(),
251            left: px(0.).into(),
252        }
253    }
254
255    pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
256        Edges {
257            top: self.top.to_pixels(rem_size),
258            right: self.right.to_pixels(rem_size),
259            bottom: self.bottom.to_pixels(rem_size),
260            left: self.left.to_pixels(rem_size),
261        }
262    }
263}
264
265impl Edges<Pixels> {
266    pub fn is_empty(&self) -> bool {
267        self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.)
268    }
269}
270
271#[derive(Refineable, Clone, Default, Debug)]
272#[refineable(debug)]
273#[repr(C)]
274pub struct Corners<T: Clone + Debug> {
275    pub top_left: T,
276    pub top_right: T,
277    pub bottom_right: T,
278    pub bottom_left: T,
279}
280
281impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
282    fn mul_assign(&mut self, rhs: S) {
283        self.top_left = self.top_left.clone() * rhs.clone();
284        self.top_right = self.top_right.clone() * rhs.clone();
285        self.bottom_right = self.bottom_right.clone() * rhs.clone();
286        self.bottom_left = self.bottom_left.clone() * rhs;
287    }
288}
289
290impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
291
292unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Corners<T> {}
293
294unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Corners<T> {}
295
296#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
297#[repr(transparent)]
298pub struct Pixels(pub(crate) f32);
299
300impl Mul<f32> for Pixels {
301    type Output = Pixels;
302
303    fn mul(self, other: f32) -> Pixels {
304        Pixels(self.0 * other)
305    }
306}
307
308impl Pixels {
309    pub fn round(&self) -> Self {
310        Self(self.0.round())
311    }
312
313    pub fn to_device_pixels(&self, scale: f32) -> DevicePixels {
314        DevicePixels((self.0 * scale).ceil() as u32)
315    }
316}
317
318impl Mul<Pixels> for Pixels {
319    type Output = Pixels;
320
321    fn mul(self, rhs: Pixels) -> Self::Output {
322        Pixels(self.0 * rhs.0)
323    }
324}
325
326impl Eq for Pixels {}
327
328impl Ord for Pixels {
329    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
330        self.0.partial_cmp(&other.0).unwrap()
331    }
332}
333
334impl std::hash::Hash for Pixels {
335    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
336        self.0.to_bits().hash(state);
337    }
338}
339
340impl From<f64> for Pixels {
341    fn from(val: f64) -> Self {
342        Pixels(val as f32)
343    }
344}
345
346impl From<f32> for Pixels {
347    fn from(val: f32) -> Self {
348        Pixels(val)
349    }
350}
351
352unsafe impl bytemuck::Pod for Pixels {}
353unsafe impl bytemuck::Zeroable for Pixels {}
354
355impl Debug for Pixels {
356    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
357        write!(f, "{} px", self.0)
358    }
359}
360
361impl From<Pixels> for f32 {
362    fn from(pixels: Pixels) -> Self {
363        pixels.0
364    }
365}
366
367impl From<&Pixels> for f32 {
368    fn from(pixels: &Pixels) -> Self {
369        pixels.0
370    }
371}
372
373impl From<Pixels> for f64 {
374    fn from(pixels: Pixels) -> Self {
375        pixels.0 as f64
376    }
377}
378
379#[derive(
380    Clone, Copy, Debug, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd,
381)]
382#[repr(transparent)]
383pub struct DevicePixels(pub(crate) u32);
384
385unsafe impl bytemuck::Pod for DevicePixels {}
386unsafe impl bytemuck::Zeroable for DevicePixels {}
387
388impl From<DevicePixels> for u32 {
389    fn from(device_pixels: DevicePixels) -> Self {
390        device_pixels.0
391    }
392}
393
394impl From<u32> for DevicePixels {
395    fn from(val: u32) -> Self {
396        DevicePixels(val)
397    }
398}
399
400#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
401pub struct Rems(f32);
402
403impl Mul<Pixels> for Rems {
404    type Output = Pixels;
405
406    fn mul(self, other: Pixels) -> Pixels {
407        Pixels(self.0 * other.0)
408    }
409}
410
411impl Debug for Rems {
412    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413        write!(f, "{} rem", self.0)
414    }
415}
416
417#[derive(Clone, Copy, Debug)]
418pub enum AbsoluteLength {
419    Pixels(Pixels),
420    Rems(Rems),
421}
422
423impl From<Pixels> for AbsoluteLength {
424    fn from(pixels: Pixels) -> Self {
425        AbsoluteLength::Pixels(pixels)
426    }
427}
428
429impl From<Rems> for AbsoluteLength {
430    fn from(rems: Rems) -> Self {
431        AbsoluteLength::Rems(rems)
432    }
433}
434
435impl AbsoluteLength {
436    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
437        match self {
438            AbsoluteLength::Pixels(pixels) => *pixels,
439            AbsoluteLength::Rems(rems) => *rems * rem_size,
440        }
441    }
442}
443
444impl Default for AbsoluteLength {
445    fn default() -> Self {
446        px(0.).into()
447    }
448}
449
450/// A non-auto length that can be defined in pixels, rems, or percent of parent.
451#[derive(Clone, Copy)]
452pub enum DefiniteLength {
453    Absolute(AbsoluteLength),
454    /// A fraction of the parent's size between 0 and 1.
455    Fraction(f32),
456}
457
458impl Debug for DefiniteLength {
459    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
460        match self {
461            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
462            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
463        }
464    }
465}
466
467impl From<Pixels> for DefiniteLength {
468    fn from(pixels: Pixels) -> Self {
469        Self::Absolute(pixels.into())
470    }
471}
472
473impl From<Rems> for DefiniteLength {
474    fn from(rems: Rems) -> Self {
475        Self::Absolute(rems.into())
476    }
477}
478
479impl From<AbsoluteLength> for DefiniteLength {
480    fn from(length: AbsoluteLength) -> Self {
481        Self::Absolute(length)
482    }
483}
484
485impl Default for DefiniteLength {
486    fn default() -> Self {
487        Self::Absolute(AbsoluteLength::default())
488    }
489}
490
491/// A length that can be defined in pixels, rems, percent of parent, or auto.
492#[derive(Clone, Copy)]
493pub enum Length {
494    Definite(DefiniteLength),
495    Auto,
496}
497
498impl Debug for Length {
499    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
500        match self {
501            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
502            Length::Auto => write!(f, "auto"),
503        }
504    }
505}
506
507pub fn relative(fraction: f32) -> DefiniteLength {
508    DefiniteLength::Fraction(fraction).into()
509}
510
511pub fn rems(rems: f32) -> Rems {
512    Rems(rems)
513}
514
515pub fn px(pixels: f32) -> Pixels {
516    Pixels(pixels)
517}
518
519pub fn auto() -> Length {
520    Length::Auto
521}
522
523impl From<Pixels> for Length {
524    fn from(pixels: Pixels) -> Self {
525        Self::Definite(pixels.into())
526    }
527}
528
529impl From<Rems> for Length {
530    fn from(rems: Rems) -> Self {
531        Self::Definite(rems.into())
532    }
533}
534
535impl From<DefiniteLength> for Length {
536    fn from(length: DefiniteLength) -> Self {
537        Self::Definite(length)
538    }
539}
540
541impl From<AbsoluteLength> for Length {
542    fn from(length: AbsoluteLength) -> Self {
543        Self::Definite(length.into())
544    }
545}
546
547impl Default for Length {
548    fn default() -> Self {
549        Self::Definite(DefiniteLength::default())
550    }
551}
552
553impl From<()> for Length {
554    fn from(_: ()) -> Self {
555        Self::Definite(DefiniteLength::default())
556    }
557}