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
650impl Eq for ScaledPixels {}
651
652impl Debug for ScaledPixels {
653    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
654        write!(f, "{} px (scaled)", self.0)
655    }
656}
657
658impl From<ScaledPixels> for DevicePixels {
659    fn from(scaled: ScaledPixels) -> Self {
660        DevicePixels(scaled.0.ceil() as i32)
661    }
662}
663
664impl From<DevicePixels> for ScaledPixels {
665    fn from(device: DevicePixels) -> Self {
666        ScaledPixels(device.0 as f32)
667    }
668}
669
670#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
671pub struct Rems(f32);
672
673impl Mul<Pixels> for Rems {
674    type Output = Pixels;
675
676    fn mul(self, other: Pixels) -> Pixels {
677        Pixels(self.0 * other.0)
678    }
679}
680
681impl Debug for Rems {
682    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
683        write!(f, "{} rem", self.0)
684    }
685}
686
687#[derive(Clone, Copy, Debug)]
688pub enum AbsoluteLength {
689    Pixels(Pixels),
690    Rems(Rems),
691}
692
693impl AbsoluteLength {
694    pub fn is_zero(&self) -> bool {
695        match self {
696            AbsoluteLength::Pixels(px) => px.0 == 0.,
697            AbsoluteLength::Rems(rems) => rems.0 == 0.,
698        }
699    }
700}
701
702impl From<Pixels> for AbsoluteLength {
703    fn from(pixels: Pixels) -> Self {
704        AbsoluteLength::Pixels(pixels)
705    }
706}
707
708impl From<Rems> for AbsoluteLength {
709    fn from(rems: Rems) -> Self {
710        AbsoluteLength::Rems(rems)
711    }
712}
713
714impl AbsoluteLength {
715    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
716        match self {
717            AbsoluteLength::Pixels(pixels) => *pixels,
718            AbsoluteLength::Rems(rems) => *rems * rem_size,
719        }
720    }
721}
722
723impl Default for AbsoluteLength {
724    fn default() -> Self {
725        px(0.).into()
726    }
727}
728
729/// A non-auto length that can be defined in pixels, rems, or percent of parent.
730#[derive(Clone, Copy)]
731pub enum DefiniteLength {
732    Absolute(AbsoluteLength),
733    /// A fraction of the parent's size between 0 and 1.
734    Fraction(f32),
735}
736
737impl DefiniteLength {
738    pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
739        match self {
740            DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
741            DefiniteLength::Fraction(fraction) => match base_size {
742                AbsoluteLength::Pixels(px) => px * *fraction,
743                AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
744            },
745        }
746    }
747}
748
749impl Debug for DefiniteLength {
750    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
751        match self {
752            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
753            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
754        }
755    }
756}
757
758impl From<Pixels> for DefiniteLength {
759    fn from(pixels: Pixels) -> Self {
760        Self::Absolute(pixels.into())
761    }
762}
763
764impl From<Rems> for DefiniteLength {
765    fn from(rems: Rems) -> Self {
766        Self::Absolute(rems.into())
767    }
768}
769
770impl From<AbsoluteLength> for DefiniteLength {
771    fn from(length: AbsoluteLength) -> Self {
772        Self::Absolute(length)
773    }
774}
775
776impl Default for DefiniteLength {
777    fn default() -> Self {
778        Self::Absolute(AbsoluteLength::default())
779    }
780}
781
782/// A length that can be defined in pixels, rems, percent of parent, or auto.
783#[derive(Clone, Copy)]
784pub enum Length {
785    Definite(DefiniteLength),
786    Auto,
787}
788
789impl Debug for Length {
790    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
791        match self {
792            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
793            Length::Auto => write!(f, "auto"),
794        }
795    }
796}
797
798pub fn relative(fraction: f32) -> DefiniteLength {
799    DefiniteLength::Fraction(fraction).into()
800}
801
802/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
803pub fn phi() -> DefiniteLength {
804    relative(1.61803398875)
805}
806
807pub fn rems(rems: f32) -> Rems {
808    Rems(rems)
809}
810
811pub fn px(pixels: f32) -> Pixels {
812    Pixels(pixels)
813}
814
815pub fn auto() -> Length {
816    Length::Auto
817}
818
819impl From<Pixels> for Length {
820    fn from(pixels: Pixels) -> Self {
821        Self::Definite(pixels.into())
822    }
823}
824
825impl From<Rems> for Length {
826    fn from(rems: Rems) -> Self {
827        Self::Definite(rems.into())
828    }
829}
830
831impl From<DefiniteLength> for Length {
832    fn from(length: DefiniteLength) -> Self {
833        Self::Definite(length)
834    }
835}
836
837impl From<AbsoluteLength> for Length {
838    fn from(length: AbsoluteLength) -> Self {
839        Self::Definite(length.into())
840    }
841}
842
843impl Default for Length {
844    fn default() -> Self {
845        Self::Definite(DefiniteLength::default())
846    }
847}
848
849impl From<()> for Length {
850    fn from(_: ()) -> Self {
851        Self::Definite(DefiniteLength::default())
852    }
853}
854
855pub trait IsZero {
856    fn is_zero(&self) -> bool;
857}
858
859impl IsZero for DevicePixels {
860    fn is_zero(&self) -> bool {
861        self.0 == 0
862    }
863}
864
865impl IsZero for ScaledPixels {
866    fn is_zero(&self) -> bool {
867        self.0 == 0.
868    }
869}
870
871impl IsZero for Pixels {
872    fn is_zero(&self) -> bool {
873        self.0 == 0.
874    }
875}
876
877impl IsZero for Rems {
878    fn is_zero(&self) -> bool {
879        self.0 == 0.
880    }
881}
882
883impl IsZero for AbsoluteLength {
884    fn is_zero(&self) -> bool {
885        match self {
886            AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
887            AbsoluteLength::Rems(rems) => rems.is_zero(),
888        }
889    }
890}
891
892impl IsZero for DefiniteLength {
893    fn is_zero(&self) -> bool {
894        match self {
895            DefiniteLength::Absolute(length) => length.is_zero(),
896            DefiniteLength::Fraction(fraction) => *fraction == 0.,
897        }
898    }
899}
900
901impl IsZero for Length {
902    fn is_zero(&self) -> bool {
903        match self {
904            Length::Definite(length) => length.is_zero(),
905            Length::Auto => false,
906        }
907    }
908}
909
910impl<T: IsZero + Debug + Clone> IsZero for Point<T> {
911    fn is_zero(&self) -> bool {
912        self.x.is_zero() && self.y.is_zero()
913    }
914}
915
916impl<T: IsZero + Debug + Clone> IsZero for Size<T> {
917    fn is_zero(&self) -> bool {
918        self.width.is_zero() || self.height.is_zero()
919    }
920}
921
922impl<T: IsZero + Debug + Clone> IsZero for Bounds<T> {
923    fn is_zero(&self) -> bool {
924        self.origin.is_zero() && self.size.is_zero()
925    }
926}