geometry.rs

  1use core::fmt::Debug;
  2use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
  3use refineable::Refineable;
  4use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign};
  5
  6#[derive(Refineable, Default, Add, AddAssign, Sub, SubAssign, Copy, Debug, PartialEq, Eq, Hash)]
  7#[refineable(debug)]
  8#[repr(C)]
  9pub struct Point<T: Clone + Debug> {
 10    pub x: T,
 11    pub y: T,
 12}
 13
 14pub fn point<T: Clone + Debug>(x: T, y: T) -> Point<T> {
 15    Point { x, y }
 16}
 17
 18impl<T: Clone + Debug> Point<T> {
 19    pub fn new(x: T, y: T) -> Self {
 20        Self { x, y }
 21    }
 22
 23    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Point<U> {
 24        Point {
 25            x: f(self.x.clone()),
 26            y: f(self.y.clone()),
 27        }
 28    }
 29}
 30
 31impl Point<Pixels> {
 32    pub fn scale(&self, factor: f32) -> Point<ScaledPixels> {
 33        Point {
 34            x: self.x.scale(factor),
 35            y: self.y.scale(factor),
 36        }
 37    }
 38}
 39
 40impl<T, Rhs> Mul<Rhs> for Point<T>
 41where
 42    T: Mul<Rhs, Output = T> + Clone + Debug,
 43    Rhs: Clone + Debug,
 44{
 45    type Output = Point<T>;
 46
 47    fn mul(self, rhs: Rhs) -> Self::Output {
 48        Point {
 49            x: self.x * rhs.clone(),
 50            y: self.y * rhs,
 51        }
 52    }
 53}
 54
 55impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Point<T> {
 56    fn mul_assign(&mut self, rhs: S) {
 57        self.x = self.x.clone() * rhs.clone();
 58        self.y = self.y.clone() * rhs;
 59    }
 60}
 61
 62impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
 63    fn sub_assign(&mut self, rhs: Size<T>) {
 64        self.x = self.x.clone() - rhs.width;
 65        self.y = self.y.clone() - rhs.height;
 66    }
 67}
 68
 69impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
 70    fn add_assign(&mut self, rhs: T) {
 71        self.x = self.x.clone() + rhs;
 72        self.y = self.y.clone() + rhs;
 73    }
 74}
 75
 76impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Point<T> {
 77    type Output = Self;
 78
 79    fn div(self, rhs: S) -> Self::Output {
 80        Self {
 81            x: self.x / rhs.clone(),
 82            y: self.y / rhs,
 83        }
 84    }
 85}
 86
 87impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
 88    pub fn max(&self, other: &Self) -> Self {
 89        Point {
 90            x: if self.x >= other.x {
 91                self.x.clone()
 92            } else {
 93                other.x.clone()
 94            },
 95            y: if self.y >= other.y {
 96                self.y.clone()
 97            } else {
 98                other.y.clone()
 99            },
100        }
101    }
102}
103
104impl<T: Clone + Debug> Clone for Point<T> {
105    fn clone(&self) -> Self {
106        Self {
107            x: self.x.clone(),
108            y: self.y.clone(),
109        }
110    }
111}
112
113#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq, Div, Hash)]
114#[refineable(debug)]
115#[repr(C)]
116pub struct Size<T: Clone + Debug> {
117    pub width: T,
118    pub height: T,
119}
120
121pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
122    Size { width, height }
123}
124
125impl<T: Clone + Debug> Size<T> {
126    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
127        Size {
128            width: f(self.width.clone()),
129            height: f(self.height.clone()),
130        }
131    }
132}
133
134impl Size<Pixels> {
135    pub fn scale(&self, factor: f32) -> Size<ScaledPixels> {
136        Size {
137            width: self.width.scale(factor),
138            height: self.height.scale(factor),
139        }
140    }
141}
142
143impl<T: Clone + Debug + Ord> Size<T> {
144    pub fn max(&self, other: &Self) -> Self {
145        Size {
146            width: if self.width >= other.width {
147                self.width.clone()
148            } else {
149                other.width.clone()
150            },
151            height: if self.height >= other.height {
152                self.height.clone()
153            } else {
154                other.height.clone()
155            },
156        }
157    }
158}
159
160impl<T, Rhs> Mul<Rhs> for Size<T>
161where
162    T: Mul<Rhs, Output = Rhs> + Debug + Clone,
163    Rhs: Debug + Clone,
164{
165    type Output = Size<Rhs>;
166
167    fn mul(self, rhs: Rhs) -> Self::Output {
168        Size {
169            width: self.width * rhs.clone(),
170            height: self.height * rhs,
171        }
172    }
173}
174
175impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Size<T> {
176    fn mul_assign(&mut self, rhs: S) {
177        self.width = self.width.clone() * rhs.clone();
178        self.height = self.height.clone() * rhs;
179    }
180}
181
182impl<T: Eq + Debug + Clone> Eq for Size<T> {}
183
184impl From<Size<Option<Pixels>>> for Size<Option<f32>> {
185    fn from(size: Size<Option<Pixels>>) -> Self {
186        Size {
187            width: size.width.map(|p| p.0 as f32),
188            height: size.height.map(|p| p.0 as f32),
189        }
190    }
191}
192
193impl Size<Length> {
194    pub fn full() -> Self {
195        Self {
196            width: relative(1.).into(),
197            height: relative(1.).into(),
198        }
199    }
200}
201
202impl Size<DefiniteLength> {
203    pub fn zero() -> Self {
204        Self {
205            width: px(0.).into(),
206            height: px(0.).into(),
207        }
208    }
209}
210
211impl Size<Length> {
212    pub fn auto() -> Self {
213        Self {
214            width: Length::Auto,
215            height: Length::Auto,
216        }
217    }
218}
219
220#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
221#[refineable(debug)]
222#[repr(C)]
223pub struct Bounds<T: Clone + Debug> {
224    pub origin: Point<T>,
225    pub size: Size<T>,
226}
227
228impl<T: Clone + Debug + Sub<Output = T>> Bounds<T> {
229    pub fn from_corners(upper_left: Point<T>, lower_right: Point<T>) -> Self {
230        let origin = Point {
231            x: upper_left.x.clone(),
232            y: upper_left.y.clone(),
233        };
234        let size = Size {
235            width: lower_right.x - upper_left.x,
236            height: lower_right.y - upper_left.y,
237        };
238        Bounds { origin, size }
239    }
240}
241
242impl<T, Rhs> Mul<Rhs> for Bounds<T>
243where
244    T: Mul<Rhs, Output = Rhs> + Clone + Debug,
245    Point<T>: Mul<Rhs, Output = Point<Rhs>>,
246    Rhs: Clone + Debug,
247{
248    type Output = Bounds<Rhs>;
249
250    fn mul(self, rhs: Rhs) -> Self::Output {
251        Bounds {
252            origin: self.origin * rhs.clone(),
253            size: self.size * rhs,
254        }
255    }
256}
257
258impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Bounds<T> {
259    fn mul_assign(&mut self, rhs: S) {
260        self.origin *= rhs.clone();
261        self.size *= rhs;
262    }
263}
264
265impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Bounds<T>
266where
267    Size<T>: Div<S, Output = Size<T>>,
268{
269    type Output = Self;
270
271    fn div(self, rhs: S) -> Self {
272        Self {
273            origin: self.origin / rhs.clone(),
274            size: self.size / rhs,
275        }
276    }
277}
278
279impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
280    pub fn upper_right(&self) -> Point<T> {
281        Point {
282            x: self.origin.x.clone() + self.size.width.clone(),
283            y: self.origin.y.clone(),
284        }
285    }
286
287    pub fn lower_right(&self) -> Point<T> {
288        Point {
289            x: self.origin.x.clone() + self.size.width.clone(),
290            y: self.origin.y.clone() + self.size.height.clone(),
291        }
292    }
293}
294
295impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
296    pub fn contains_point(&self, point: Point<T>) -> bool {
297        point.x >= self.origin.x
298            && point.x <= self.origin.x.clone() + self.size.width.clone()
299            && point.y >= self.origin.y
300            && point.y <= self.origin.y.clone() + self.size.height.clone()
301    }
302
303    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Bounds<U> {
304        Bounds {
305            origin: self.origin.map(&f),
306            size: self.size.map(f),
307        }
308    }
309}
310
311impl Bounds<Pixels> {
312    pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> {
313        Bounds {
314            origin: self.origin.scale(factor),
315            size: self.size.scale(factor),
316        }
317    }
318}
319
320impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
321
322#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
323#[refineable(debug)]
324#[repr(C)]
325pub struct Edges<T: Clone + Debug> {
326    pub top: T,
327    pub right: T,
328    pub bottom: T,
329    pub left: T,
330}
331
332impl<T: Clone + Debug + Mul<Output = T>> Mul for Edges<T> {
333    type Output = Self;
334
335    fn mul(self, rhs: Self) -> Self::Output {
336        Self {
337            top: self.top.clone() * rhs.top,
338            right: self.right.clone() * rhs.right,
339            bottom: self.bottom.clone() * rhs.bottom,
340            left: self.left.clone() * rhs.left,
341        }
342    }
343}
344
345impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Edges<T> {
346    fn mul_assign(&mut self, rhs: S) {
347        self.top = self.top.clone() * rhs.clone();
348        self.right = self.right.clone() * rhs.clone();
349        self.bottom = self.bottom.clone() * rhs.clone();
350        self.left = self.left.clone() * rhs.clone();
351    }
352}
353
354impl<T: Clone + Debug + Copy> Copy for Edges<T> {}
355
356impl<T: Clone + Debug> Edges<T> {
357    pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Edges<U> {
358        Edges {
359            top: f(&self.top),
360            right: f(&self.right),
361            bottom: f(&self.bottom),
362            left: f(&self.left),
363        }
364    }
365
366    pub fn any<F: Fn(&T) -> bool>(&self, predicate: F) -> bool {
367        predicate(&self.top)
368            || predicate(&self.right)
369            || predicate(&self.bottom)
370            || predicate(&self.left)
371    }
372}
373
374impl Edges<Length> {
375    pub fn auto() -> Self {
376        Self {
377            top: Length::Auto,
378            right: Length::Auto,
379            bottom: Length::Auto,
380            left: Length::Auto,
381        }
382    }
383
384    pub fn zero() -> Self {
385        Self {
386            top: px(0.).into(),
387            right: px(0.).into(),
388            bottom: px(0.).into(),
389            left: px(0.).into(),
390        }
391    }
392}
393
394impl Edges<DefiniteLength> {
395    pub fn zero() -> Self {
396        Self {
397            top: px(0.).into(),
398            right: px(0.).into(),
399            bottom: px(0.).into(),
400            left: px(0.).into(),
401        }
402    }
403}
404
405impl Edges<AbsoluteLength> {
406    pub fn zero() -> Self {
407        Self {
408            top: px(0.).into(),
409            right: px(0.).into(),
410            bottom: px(0.).into(),
411            left: px(0.).into(),
412        }
413    }
414
415    pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
416        Edges {
417            top: self.top.to_pixels(rem_size),
418            right: self.right.to_pixels(rem_size),
419            bottom: self.bottom.to_pixels(rem_size),
420            left: self.left.to_pixels(rem_size),
421        }
422    }
423}
424
425#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
426#[refineable(debug)]
427#[repr(C)]
428pub struct Corners<T: Clone + Debug> {
429    pub top_left: T,
430    pub top_right: T,
431    pub bottom_right: T,
432    pub bottom_left: T,
433}
434
435impl Corners<AbsoluteLength> {
436    pub fn to_pixels(&self, rem_size: Pixels) -> Corners<Pixels> {
437        Corners {
438            top_left: self.top_left.to_pixels(rem_size),
439            top_right: self.top_right.to_pixels(rem_size),
440            bottom_right: self.bottom_right.to_pixels(rem_size),
441            bottom_left: self.bottom_left.to_pixels(rem_size),
442        }
443    }
444}
445
446impl Corners<Pixels> {
447    pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> {
448        Corners {
449            top_left: self.top_left.scale(factor),
450            top_right: self.top_right.scale(factor),
451            bottom_right: self.bottom_right.scale(factor),
452            bottom_left: self.bottom_left.scale(factor),
453        }
454    }
455}
456
457impl<T: Clone + Debug> Corners<T> {
458    pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Corners<U> {
459        Corners {
460            top_left: f(&self.top_left),
461            top_right: f(&self.top_right),
462            bottom_right: f(&self.bottom_right),
463            bottom_left: f(&self.bottom_left),
464        }
465    }
466}
467
468impl<T: Clone + Debug + Mul<Output = T>> Mul for Corners<T> {
469    type Output = Self;
470
471    fn mul(self, rhs: Self) -> Self::Output {
472        Self {
473            top_left: self.top_left.clone() * rhs.top_left,
474            top_right: self.top_right.clone() * rhs.top_right,
475            bottom_right: self.bottom_right.clone() * rhs.bottom_right,
476            bottom_left: self.bottom_left.clone() * rhs.bottom_left,
477        }
478    }
479}
480
481impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
482    fn mul_assign(&mut self, rhs: S) {
483        self.top_left = self.top_left.clone() * rhs.clone();
484        self.top_right = self.top_right.clone() * rhs.clone();
485        self.bottom_right = self.bottom_right.clone() * rhs.clone();
486        self.bottom_left = self.bottom_left.clone() * rhs;
487    }
488}
489
490impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
491
492#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
493#[repr(transparent)]
494pub struct Pixels(pub(crate) f32);
495
496impl Mul<f32> for Pixels {
497    type Output = Pixels;
498
499    fn mul(self, other: f32) -> Pixels {
500        Pixels(self.0 * other)
501    }
502}
503
504impl Mul<Pixels> for f32 {
505    type Output = Pixels;
506
507    fn mul(self, rhs: Pixels) -> Self::Output {
508        Pixels(self * rhs.0)
509    }
510}
511
512impl MulAssign<f32> for Pixels {
513    fn mul_assign(&mut self, other: f32) {
514        self.0 *= other;
515    }
516}
517
518impl Pixels {
519    pub fn round(&self) -> Self {
520        Self(self.0.round())
521    }
522
523    pub fn scale(&self, factor: f32) -> ScaledPixels {
524        ScaledPixels(self.0 * factor)
525    }
526}
527
528impl Mul<Pixels> for Pixels {
529    type Output = Pixels;
530
531    fn mul(self, rhs: Pixels) -> Self::Output {
532        Pixels(self.0 * rhs.0)
533    }
534}
535
536impl Eq for Pixels {}
537
538impl Ord for Pixels {
539    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
540        self.0.partial_cmp(&other.0).unwrap()
541    }
542}
543
544impl std::hash::Hash for Pixels {
545    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
546        self.0.to_bits().hash(state);
547    }
548}
549
550impl From<f64> for Pixels {
551    fn from(pixels: f64) -> Self {
552        Pixels(pixels as f32)
553    }
554}
555
556impl From<f32> for Pixels {
557    fn from(pixels: f32) -> Self {
558        Pixels(pixels)
559    }
560}
561
562impl Debug for Pixels {
563    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
564        write!(f, "{} px", self.0)
565    }
566}
567
568impl From<Pixels> for f32 {
569    fn from(pixels: Pixels) -> Self {
570        pixels.0
571    }
572}
573
574impl From<&Pixels> for f32 {
575    fn from(pixels: &Pixels) -> Self {
576        pixels.0
577    }
578}
579
580impl From<Pixels> for f64 {
581    fn from(pixels: Pixels) -> Self {
582        pixels.0 as f64
583    }
584}
585
586#[derive(
587    Add, AddAssign, Clone, Copy, Default, Div, Eq, Hash, Ord, PartialEq, PartialOrd, Sub, SubAssign,
588)]
589#[repr(transparent)]
590pub struct DevicePixels(pub(crate) i32);
591
592impl DevicePixels {
593    pub fn to_bytes(&self, bytes_per_pixel: u8) -> u32 {
594        self.0 as u32 * bytes_per_pixel as u32
595    }
596}
597
598impl std::fmt::Debug for DevicePixels {
599    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
600        write!(f, "{} px (device)", self.0)
601    }
602}
603
604impl From<DevicePixels> for i32 {
605    fn from(device_pixels: DevicePixels) -> Self {
606        device_pixels.0
607    }
608}
609
610impl From<i32> for DevicePixels {
611    fn from(device_pixels: i32) -> Self {
612        DevicePixels(device_pixels)
613    }
614}
615
616impl From<u32> for DevicePixels {
617    fn from(device_pixels: u32) -> Self {
618        DevicePixels(device_pixels as i32)
619    }
620}
621
622impl From<DevicePixels> for u32 {
623    fn from(device_pixels: DevicePixels) -> Self {
624        device_pixels.0 as u32
625    }
626}
627
628impl From<DevicePixels> for u64 {
629    fn from(device_pixels: DevicePixels) -> Self {
630        device_pixels.0 as u64
631    }
632}
633
634impl From<u64> for DevicePixels {
635    fn from(device_pixels: u64) -> Self {
636        DevicePixels(device_pixels as i32)
637    }
638}
639
640#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
641#[repr(transparent)]
642pub struct ScaledPixels(pub(crate) f32);
643
644impl ScaledPixels {
645    pub fn floor(&self) -> Self {
646        Self(self.0.floor())
647    }
648
649    pub fn ceil(&self) -> Self {
650        Self(self.0.ceil())
651    }
652}
653
654impl Eq for ScaledPixels {}
655
656impl Debug for ScaledPixels {
657    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
658        write!(f, "{} px (scaled)", self.0)
659    }
660}
661
662impl From<ScaledPixels> for DevicePixels {
663    fn from(scaled: ScaledPixels) -> Self {
664        DevicePixels(scaled.0.ceil() as i32)
665    }
666}
667
668impl From<DevicePixels> for ScaledPixels {
669    fn from(device: DevicePixels) -> Self {
670        ScaledPixels(device.0 as f32)
671    }
672}
673
674#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
675pub struct Rems(f32);
676
677impl Mul<Pixels> for Rems {
678    type Output = Pixels;
679
680    fn mul(self, other: Pixels) -> Pixels {
681        Pixels(self.0 * other.0)
682    }
683}
684
685impl Debug for Rems {
686    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
687        write!(f, "{} rem", self.0)
688    }
689}
690
691#[derive(Clone, Copy, Debug)]
692pub enum AbsoluteLength {
693    Pixels(Pixels),
694    Rems(Rems),
695}
696
697impl AbsoluteLength {
698    pub fn is_zero(&self) -> bool {
699        match self {
700            AbsoluteLength::Pixels(px) => px.0 == 0.,
701            AbsoluteLength::Rems(rems) => rems.0 == 0.,
702        }
703    }
704}
705
706impl From<Pixels> for AbsoluteLength {
707    fn from(pixels: Pixels) -> Self {
708        AbsoluteLength::Pixels(pixels)
709    }
710}
711
712impl From<Rems> for AbsoluteLength {
713    fn from(rems: Rems) -> Self {
714        AbsoluteLength::Rems(rems)
715    }
716}
717
718impl AbsoluteLength {
719    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
720        match self {
721            AbsoluteLength::Pixels(pixels) => *pixels,
722            AbsoluteLength::Rems(rems) => *rems * rem_size,
723        }
724    }
725}
726
727impl Default for AbsoluteLength {
728    fn default() -> Self {
729        px(0.).into()
730    }
731}
732
733/// A non-auto length that can be defined in pixels, rems, or percent of parent.
734#[derive(Clone, Copy)]
735pub enum DefiniteLength {
736    Absolute(AbsoluteLength),
737    /// A fraction of the parent's size between 0 and 1.
738    Fraction(f32),
739}
740
741impl DefiniteLength {
742    pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
743        match self {
744            DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
745            DefiniteLength::Fraction(fraction) => match base_size {
746                AbsoluteLength::Pixels(px) => px * *fraction,
747                AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
748            },
749        }
750    }
751}
752
753impl Debug for DefiniteLength {
754    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
755        match self {
756            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
757            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
758        }
759    }
760}
761
762impl From<Pixels> for DefiniteLength {
763    fn from(pixels: Pixels) -> Self {
764        Self::Absolute(pixels.into())
765    }
766}
767
768impl From<Rems> for DefiniteLength {
769    fn from(rems: Rems) -> Self {
770        Self::Absolute(rems.into())
771    }
772}
773
774impl From<AbsoluteLength> for DefiniteLength {
775    fn from(length: AbsoluteLength) -> Self {
776        Self::Absolute(length)
777    }
778}
779
780impl Default for DefiniteLength {
781    fn default() -> Self {
782        Self::Absolute(AbsoluteLength::default())
783    }
784}
785
786/// A length that can be defined in pixels, rems, percent of parent, or auto.
787#[derive(Clone, Copy)]
788pub enum Length {
789    Definite(DefiniteLength),
790    Auto,
791}
792
793impl Debug for Length {
794    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
795        match self {
796            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
797            Length::Auto => write!(f, "auto"),
798        }
799    }
800}
801
802pub fn relative(fraction: f32) -> DefiniteLength {
803    DefiniteLength::Fraction(fraction).into()
804}
805
806/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
807pub fn phi() -> DefiniteLength {
808    relative(1.61803398875)
809}
810
811pub fn rems(rems: f32) -> Rems {
812    Rems(rems)
813}
814
815pub fn px(pixels: f32) -> Pixels {
816    Pixels(pixels)
817}
818
819pub fn auto() -> Length {
820    Length::Auto
821}
822
823impl From<Pixels> for Length {
824    fn from(pixels: Pixels) -> Self {
825        Self::Definite(pixels.into())
826    }
827}
828
829impl From<Rems> for Length {
830    fn from(rems: Rems) -> Self {
831        Self::Definite(rems.into())
832    }
833}
834
835impl From<DefiniteLength> for Length {
836    fn from(length: DefiniteLength) -> Self {
837        Self::Definite(length)
838    }
839}
840
841impl From<AbsoluteLength> for Length {
842    fn from(length: AbsoluteLength) -> Self {
843        Self::Definite(length.into())
844    }
845}
846
847impl Default for Length {
848    fn default() -> Self {
849        Self::Definite(DefiniteLength::default())
850    }
851}
852
853impl From<()> for Length {
854    fn from(_: ()) -> Self {
855        Self::Definite(DefiniteLength::default())
856    }
857}
858
859pub trait IsZero {
860    fn is_zero(&self) -> bool;
861}
862
863impl IsZero for DevicePixels {
864    fn is_zero(&self) -> bool {
865        self.0 == 0
866    }
867}
868
869impl IsZero for ScaledPixels {
870    fn is_zero(&self) -> bool {
871        self.0 == 0.
872    }
873}
874
875impl IsZero for Pixels {
876    fn is_zero(&self) -> bool {
877        self.0 == 0.
878    }
879}
880
881impl IsZero for Rems {
882    fn is_zero(&self) -> bool {
883        self.0 == 0.
884    }
885}
886
887impl IsZero for AbsoluteLength {
888    fn is_zero(&self) -> bool {
889        match self {
890            AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
891            AbsoluteLength::Rems(rems) => rems.is_zero(),
892        }
893    }
894}
895
896impl IsZero for DefiniteLength {
897    fn is_zero(&self) -> bool {
898        match self {
899            DefiniteLength::Absolute(length) => length.is_zero(),
900            DefiniteLength::Fraction(fraction) => *fraction == 0.,
901        }
902    }
903}
904
905impl IsZero for Length {
906    fn is_zero(&self) -> bool {
907        match self {
908            Length::Definite(length) => length.is_zero(),
909            Length::Auto => false,
910        }
911    }
912}
913
914impl<T: IsZero + Debug + Clone> IsZero for Point<T> {
915    fn is_zero(&self) -> bool {
916        self.x.is_zero() && self.y.is_zero()
917    }
918}
919
920impl<T: IsZero + Debug + Clone> IsZero for Size<T> {
921    fn is_zero(&self) -> bool {
922        self.width.is_zero() || self.height.is_zero()
923    }
924}
925
926impl<T: IsZero + Debug + Clone> IsZero for Bounds<T> {
927    fn is_zero(&self) -> bool {
928        self.origin.is_zero() && self.size.is_zero()
929    }
930}