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 + Mul<S, Output = T>, S: Clone> MulAssign<S> for Bounds<T> {
163    fn mul_assign(&mut self, rhs: S) {
164        self.origin *= rhs.clone();
165        self.size *= rhs;
166    }
167}
168
169impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
170    pub fn upper_right(&self) -> Point<T> {
171        Point {
172            x: self.origin.x.clone() + self.size.width.clone(),
173            y: self.origin.y.clone(),
174        }
175    }
176
177    pub fn lower_right(&self) -> Point<T> {
178        Point {
179            x: self.origin.x.clone() + self.size.width.clone(),
180            y: self.origin.y.clone() + self.size.height.clone(),
181        }
182    }
183}
184
185impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
186    pub fn contains_point(&self, point: Point<T>) -> bool {
187        point.x >= self.origin.x
188            && point.x <= self.origin.x.clone() + self.size.width.clone()
189            && point.y >= self.origin.y
190            && point.y <= self.origin.y.clone() + self.size.height.clone()
191    }
192}
193
194impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
195
196#[derive(Refineable, Clone, Default, Debug)]
197#[refineable(debug)]
198#[repr(C)]
199pub struct Edges<T: Clone + Debug> {
200    pub top: T,
201    pub right: T,
202    pub bottom: T,
203    pub left: T,
204}
205
206impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Edges<T> {
207    fn mul_assign(&mut self, rhs: S) {
208        self.top = self.top.clone() * rhs.clone();
209        self.right = self.right.clone() * rhs.clone();
210        self.bottom = self.bottom.clone() * rhs.clone();
211        self.left = self.left.clone() * rhs.clone();
212    }
213}
214
215impl<T: Clone + Debug + Copy> Copy for Edges<T> {}
216
217unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Edges<T> {}
218
219unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Edges<T> {}
220
221impl Edges<Length> {
222    pub fn auto() -> Self {
223        Self {
224            top: Length::Auto,
225            right: Length::Auto,
226            bottom: Length::Auto,
227            left: Length::Auto,
228        }
229    }
230
231    pub fn zero() -> Self {
232        Self {
233            top: px(0.).into(),
234            right: px(0.).into(),
235            bottom: px(0.).into(),
236            left: px(0.).into(),
237        }
238    }
239}
240
241impl Edges<DefiniteLength> {
242    pub fn zero() -> Self {
243        Self {
244            top: px(0.).into(),
245            right: px(0.).into(),
246            bottom: px(0.).into(),
247            left: px(0.).into(),
248        }
249    }
250}
251
252impl Edges<AbsoluteLength> {
253    pub fn zero() -> Self {
254        Self {
255            top: px(0.).into(),
256            right: px(0.).into(),
257            bottom: px(0.).into(),
258            left: px(0.).into(),
259        }
260    }
261
262    pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
263        Edges {
264            top: self.top.to_pixels(rem_size),
265            right: self.right.to_pixels(rem_size),
266            bottom: self.bottom.to_pixels(rem_size),
267            left: self.left.to_pixels(rem_size),
268        }
269    }
270}
271
272impl Edges<Pixels> {
273    pub fn is_empty(&self) -> bool {
274        self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.)
275    }
276}
277
278#[derive(Refineable, Clone, Default, Debug)]
279#[refineable(debug)]
280#[repr(C)]
281pub struct Corners<T: Clone + Debug> {
282    pub top_left: T,
283    pub top_right: T,
284    pub bottom_right: T,
285    pub bottom_left: T,
286}
287
288impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
289    fn mul_assign(&mut self, rhs: S) {
290        self.top_left = self.top_left.clone() * rhs.clone();
291        self.top_right = self.top_right.clone() * rhs.clone();
292        self.bottom_right = self.bottom_right.clone() * rhs.clone();
293        self.bottom_left = self.bottom_left.clone() * rhs;
294    }
295}
296
297impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
298
299unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Corners<T> {}
300
301unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Corners<T> {}
302
303#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
304#[repr(transparent)]
305pub struct Pixels(pub(crate) f32);
306
307impl Mul<f32> for Pixels {
308    type Output = Pixels;
309
310    fn mul(self, other: f32) -> Pixels {
311        Pixels(self.0 * other)
312    }
313}
314
315impl Pixels {
316    pub fn round(&self) -> Self {
317        Self(self.0.round())
318    }
319
320    pub fn to_device_pixels(&self, scale: f32) -> DevicePixels {
321        DevicePixels((self.0 * scale).ceil() as u32)
322    }
323}
324
325impl Mul<Pixels> for Pixels {
326    type Output = Pixels;
327
328    fn mul(self, rhs: Pixels) -> Self::Output {
329        Pixels(self.0 * rhs.0)
330    }
331}
332
333impl Eq for Pixels {}
334
335impl Ord for Pixels {
336    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
337        self.0.partial_cmp(&other.0).unwrap()
338    }
339}
340
341impl std::hash::Hash for Pixels {
342    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
343        self.0.to_bits().hash(state);
344    }
345}
346
347impl From<f64> for Pixels {
348    fn from(val: f64) -> Self {
349        Pixels(val as f32)
350    }
351}
352
353impl From<f32> for Pixels {
354    fn from(val: f32) -> Self {
355        Pixels(val)
356    }
357}
358
359unsafe impl bytemuck::Pod for Pixels {}
360unsafe impl bytemuck::Zeroable for Pixels {}
361
362impl Debug for Pixels {
363    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
364        write!(f, "{} px", self.0)
365    }
366}
367
368impl From<Pixels> for f32 {
369    fn from(pixels: Pixels) -> Self {
370        pixels.0
371    }
372}
373
374impl From<&Pixels> for f32 {
375    fn from(pixels: &Pixels) -> Self {
376        pixels.0
377    }
378}
379
380impl From<Pixels> for f64 {
381    fn from(pixels: Pixels) -> Self {
382        pixels.0 as f64
383    }
384}
385
386#[derive(
387    Clone, Copy, Debug, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd,
388)]
389#[repr(transparent)]
390pub struct DevicePixels(pub(crate) u32);
391
392unsafe impl bytemuck::Pod for DevicePixels {}
393unsafe impl bytemuck::Zeroable for DevicePixels {}
394
395impl From<DevicePixels> for u32 {
396    fn from(device_pixels: DevicePixels) -> Self {
397        device_pixels.0
398    }
399}
400
401impl From<u32> for DevicePixels {
402    fn from(val: u32) -> Self {
403        DevicePixels(val)
404    }
405}
406
407#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
408pub struct Rems(f32);
409
410impl Mul<Pixels> for Rems {
411    type Output = Pixels;
412
413    fn mul(self, other: Pixels) -> Pixels {
414        Pixels(self.0 * other.0)
415    }
416}
417
418impl Debug for Rems {
419    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
420        write!(f, "{} rem", self.0)
421    }
422}
423
424#[derive(Clone, Copy, Debug)]
425pub enum AbsoluteLength {
426    Pixels(Pixels),
427    Rems(Rems),
428}
429
430impl From<Pixels> for AbsoluteLength {
431    fn from(pixels: Pixels) -> Self {
432        AbsoluteLength::Pixels(pixels)
433    }
434}
435
436impl From<Rems> for AbsoluteLength {
437    fn from(rems: Rems) -> Self {
438        AbsoluteLength::Rems(rems)
439    }
440}
441
442impl AbsoluteLength {
443    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
444        match self {
445            AbsoluteLength::Pixels(pixels) => *pixels,
446            AbsoluteLength::Rems(rems) => *rems * rem_size,
447        }
448    }
449}
450
451impl Default for AbsoluteLength {
452    fn default() -> Self {
453        px(0.).into()
454    }
455}
456
457/// A non-auto length that can be defined in pixels, rems, or percent of parent.
458#[derive(Clone, Copy)]
459pub enum DefiniteLength {
460    Absolute(AbsoluteLength),
461    /// A fraction of the parent's size between 0 and 1.
462    Fraction(f32),
463}
464
465impl Debug for DefiniteLength {
466    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
467        match self {
468            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
469            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
470        }
471    }
472}
473
474impl From<Pixels> for DefiniteLength {
475    fn from(pixels: Pixels) -> Self {
476        Self::Absolute(pixels.into())
477    }
478}
479
480impl From<Rems> for DefiniteLength {
481    fn from(rems: Rems) -> Self {
482        Self::Absolute(rems.into())
483    }
484}
485
486impl From<AbsoluteLength> for DefiniteLength {
487    fn from(length: AbsoluteLength) -> Self {
488        Self::Absolute(length)
489    }
490}
491
492impl Default for DefiniteLength {
493    fn default() -> Self {
494        Self::Absolute(AbsoluteLength::default())
495    }
496}
497
498/// A length that can be defined in pixels, rems, percent of parent, or auto.
499#[derive(Clone, Copy)]
500pub enum Length {
501    Definite(DefiniteLength),
502    Auto,
503}
504
505impl Debug for Length {
506    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
507        match self {
508            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
509            Length::Auto => write!(f, "auto"),
510        }
511    }
512}
513
514pub fn relative(fraction: f32) -> DefiniteLength {
515    DefiniteLength::Fraction(fraction).into()
516}
517
518pub fn rems(rems: f32) -> Rems {
519    Rems(rems)
520}
521
522pub fn px(pixels: f32) -> Pixels {
523    Pixels(pixels)
524}
525
526pub fn auto() -> Length {
527    Length::Auto
528}
529
530impl From<Pixels> for Length {
531    fn from(pixels: Pixels) -> Self {
532        Self::Definite(pixels.into())
533    }
534}
535
536impl From<Rems> for Length {
537    fn from(rems: Rems) -> Self {
538        Self::Definite(rems.into())
539    }
540}
541
542impl From<DefiniteLength> for Length {
543    fn from(length: DefiniteLength) -> Self {
544        Self::Definite(length)
545    }
546}
547
548impl From<AbsoluteLength> for Length {
549    fn from(length: AbsoluteLength) -> Self {
550        Self::Definite(length.into())
551    }
552}
553
554impl Default for Length {
555    fn default() -> Self {
556        Self::Definite(DefiniteLength::default())
557    }
558}
559
560impl From<()> for Length {
561    fn from(_: ()) -> Self {
562        Self::Definite(DefiniteLength::default())
563    }
564}