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, bounds: Bounds<Pixels>, rem_size: Pixels) -> Corners<Pixels> {
437        let max = bounds.size.width.max(bounds.size.height) / 2.;
438        Corners {
439            top_left: self.top_left.to_pixels(rem_size).min(max),
440            top_right: self.top_right.to_pixels(rem_size).min(max),
441            bottom_right: self.bottom_right.to_pixels(rem_size).min(max),
442            bottom_left: self.bottom_left.to_pixels(rem_size).min(max),
443        }
444    }
445}
446
447impl Corners<Pixels> {
448    pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> {
449        Corners {
450            top_left: self.top_left.scale(factor),
451            top_right: self.top_right.scale(factor),
452            bottom_right: self.bottom_right.scale(factor),
453            bottom_left: self.bottom_left.scale(factor),
454        }
455    }
456}
457
458impl<T: Clone + Debug> Corners<T> {
459    pub fn map<U: Clone + Debug, F: Fn(&T) -> U>(&self, f: F) -> Corners<U> {
460        Corners {
461            top_left: f(&self.top_left),
462            top_right: f(&self.top_right),
463            bottom_right: f(&self.bottom_right),
464            bottom_left: f(&self.bottom_left),
465        }
466    }
467}
468
469impl<T: Clone + Debug + Mul<Output = T>> Mul for Corners<T> {
470    type Output = Self;
471
472    fn mul(self, rhs: Self) -> Self::Output {
473        Self {
474            top_left: self.top_left.clone() * rhs.top_left,
475            top_right: self.top_right.clone() * rhs.top_right,
476            bottom_right: self.bottom_right.clone() * rhs.bottom_right,
477            bottom_left: self.bottom_left.clone() * rhs.bottom_left,
478        }
479    }
480}
481
482impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Corners<T> {
483    fn mul_assign(&mut self, rhs: S) {
484        self.top_left = self.top_left.clone() * rhs.clone();
485        self.top_right = self.top_right.clone() * rhs.clone();
486        self.bottom_right = self.bottom_right.clone() * rhs.clone();
487        self.bottom_left = self.bottom_left.clone() * rhs;
488    }
489}
490
491impl<T: Clone + Debug + Copy> Copy for Corners<T> {}
492
493#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
494#[repr(transparent)]
495pub struct Pixels(pub(crate) f32);
496
497impl Mul<f32> for Pixels {
498    type Output = Pixels;
499
500    fn mul(self, other: f32) -> Pixels {
501        Pixels(self.0 * other)
502    }
503}
504
505impl Mul<Pixels> for f32 {
506    type Output = Pixels;
507
508    fn mul(self, rhs: Pixels) -> Self::Output {
509        Pixels(self * rhs.0)
510    }
511}
512
513impl MulAssign<f32> for Pixels {
514    fn mul_assign(&mut self, other: f32) {
515        self.0 *= other;
516    }
517}
518
519impl Pixels {
520    pub fn round(&self) -> Self {
521        Self(self.0.round())
522    }
523
524    pub fn scale(&self, factor: f32) -> ScaledPixels {
525        ScaledPixels(self.0 * factor)
526    }
527}
528
529impl Mul<Pixels> for Pixels {
530    type Output = Pixels;
531
532    fn mul(self, rhs: Pixels) -> Self::Output {
533        Pixels(self.0 * rhs.0)
534    }
535}
536
537impl Eq for Pixels {}
538
539impl Ord for Pixels {
540    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
541        self.0.partial_cmp(&other.0).unwrap()
542    }
543}
544
545impl std::hash::Hash for Pixels {
546    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
547        self.0.to_bits().hash(state);
548    }
549}
550
551impl From<f64> for Pixels {
552    fn from(pixels: f64) -> Self {
553        Pixels(pixels as f32)
554    }
555}
556
557impl From<f32> for Pixels {
558    fn from(pixels: f32) -> Self {
559        Pixels(pixels)
560    }
561}
562
563impl Debug for Pixels {
564    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
565        write!(f, "{} px", self.0)
566    }
567}
568
569impl From<Pixels> for f32 {
570    fn from(pixels: Pixels) -> Self {
571        pixels.0
572    }
573}
574
575impl From<&Pixels> for f32 {
576    fn from(pixels: &Pixels) -> Self {
577        pixels.0
578    }
579}
580
581impl From<Pixels> for f64 {
582    fn from(pixels: Pixels) -> Self {
583        pixels.0 as f64
584    }
585}
586
587#[derive(
588    Add, AddAssign, Clone, Copy, Default, Div, Eq, Hash, Ord, PartialEq, PartialOrd, Sub, SubAssign,
589)]
590#[repr(transparent)]
591pub struct DevicePixels(pub(crate) i32);
592
593impl DevicePixels {
594    pub fn to_bytes(&self, bytes_per_pixel: u8) -> u32 {
595        self.0 as u32 * bytes_per_pixel as u32
596    }
597}
598
599impl std::fmt::Debug for DevicePixels {
600    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
601        write!(f, "{} px (device)", self.0)
602    }
603}
604
605impl From<DevicePixels> for i32 {
606    fn from(device_pixels: DevicePixels) -> Self {
607        device_pixels.0
608    }
609}
610
611impl From<i32> for DevicePixels {
612    fn from(device_pixels: i32) -> Self {
613        DevicePixels(device_pixels)
614    }
615}
616
617impl From<u32> for DevicePixels {
618    fn from(device_pixels: u32) -> Self {
619        DevicePixels(device_pixels as i32)
620    }
621}
622
623impl From<DevicePixels> for u32 {
624    fn from(device_pixels: DevicePixels) -> Self {
625        device_pixels.0 as u32
626    }
627}
628
629impl From<DevicePixels> for u64 {
630    fn from(device_pixels: DevicePixels) -> Self {
631        device_pixels.0 as u64
632    }
633}
634
635impl From<u64> for DevicePixels {
636    fn from(device_pixels: u64) -> Self {
637        DevicePixels(device_pixels as i32)
638    }
639}
640
641#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
642#[repr(transparent)]
643pub struct ScaledPixels(pub(crate) f32);
644
645impl ScaledPixels {
646    pub fn floor(&self) -> Self {
647        Self(self.0.floor())
648    }
649
650    pub fn ceil(&self) -> Self {
651        Self(self.0.ceil())
652    }
653}
654
655impl Eq for ScaledPixels {}
656
657impl Debug for ScaledPixels {
658    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
659        write!(f, "{} px (scaled)", self.0)
660    }
661}
662
663impl From<ScaledPixels> for DevicePixels {
664    fn from(scaled: ScaledPixels) -> Self {
665        DevicePixels(scaled.0.ceil() as i32)
666    }
667}
668
669impl From<DevicePixels> for ScaledPixels {
670    fn from(device: DevicePixels) -> Self {
671        ScaledPixels(device.0 as f32)
672    }
673}
674
675#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
676pub struct Rems(f32);
677
678impl Mul<Pixels> for Rems {
679    type Output = Pixels;
680
681    fn mul(self, other: Pixels) -> Pixels {
682        Pixels(self.0 * other.0)
683    }
684}
685
686impl Debug for Rems {
687    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
688        write!(f, "{} rem", self.0)
689    }
690}
691
692#[derive(Clone, Copy, Debug)]
693pub enum AbsoluteLength {
694    Pixels(Pixels),
695    Rems(Rems),
696}
697
698impl AbsoluteLength {
699    pub fn is_zero(&self) -> bool {
700        match self {
701            AbsoluteLength::Pixels(px) => px.0 == 0.,
702            AbsoluteLength::Rems(rems) => rems.0 == 0.,
703        }
704    }
705}
706
707impl From<Pixels> for AbsoluteLength {
708    fn from(pixels: Pixels) -> Self {
709        AbsoluteLength::Pixels(pixels)
710    }
711}
712
713impl From<Rems> for AbsoluteLength {
714    fn from(rems: Rems) -> Self {
715        AbsoluteLength::Rems(rems)
716    }
717}
718
719impl AbsoluteLength {
720    pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
721        match self {
722            AbsoluteLength::Pixels(pixels) => *pixels,
723            AbsoluteLength::Rems(rems) => *rems * rem_size,
724        }
725    }
726}
727
728impl Default for AbsoluteLength {
729    fn default() -> Self {
730        px(0.).into()
731    }
732}
733
734/// A non-auto length that can be defined in pixels, rems, or percent of parent.
735#[derive(Clone, Copy)]
736pub enum DefiniteLength {
737    Absolute(AbsoluteLength),
738    /// A fraction of the parent's size between 0 and 1.
739    Fraction(f32),
740}
741
742impl DefiniteLength {
743    pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
744        match self {
745            DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
746            DefiniteLength::Fraction(fraction) => match base_size {
747                AbsoluteLength::Pixels(px) => px * *fraction,
748                AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
749            },
750        }
751    }
752}
753
754impl Debug for DefiniteLength {
755    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
756        match self {
757            DefiniteLength::Absolute(length) => Debug::fmt(length, f),
758            DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
759        }
760    }
761}
762
763impl From<Pixels> for DefiniteLength {
764    fn from(pixels: Pixels) -> Self {
765        Self::Absolute(pixels.into())
766    }
767}
768
769impl From<Rems> for DefiniteLength {
770    fn from(rems: Rems) -> Self {
771        Self::Absolute(rems.into())
772    }
773}
774
775impl From<AbsoluteLength> for DefiniteLength {
776    fn from(length: AbsoluteLength) -> Self {
777        Self::Absolute(length)
778    }
779}
780
781impl Default for DefiniteLength {
782    fn default() -> Self {
783        Self::Absolute(AbsoluteLength::default())
784    }
785}
786
787/// A length that can be defined in pixels, rems, percent of parent, or auto.
788#[derive(Clone, Copy)]
789pub enum Length {
790    Definite(DefiniteLength),
791    Auto,
792}
793
794impl Debug for Length {
795    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
796        match self {
797            Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
798            Length::Auto => write!(f, "auto"),
799        }
800    }
801}
802
803pub fn relative(fraction: f32) -> DefiniteLength {
804    DefiniteLength::Fraction(fraction).into()
805}
806
807/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
808pub fn phi() -> DefiniteLength {
809    relative(1.61803398875)
810}
811
812pub fn rems(rems: f32) -> Rems {
813    Rems(rems)
814}
815
816pub fn px(pixels: f32) -> Pixels {
817    Pixels(pixels)
818}
819
820pub fn auto() -> Length {
821    Length::Auto
822}
823
824impl From<Pixels> for Length {
825    fn from(pixels: Pixels) -> Self {
826        Self::Definite(pixels.into())
827    }
828}
829
830impl From<Rems> for Length {
831    fn from(rems: Rems) -> Self {
832        Self::Definite(rems.into())
833    }
834}
835
836impl From<DefiniteLength> for Length {
837    fn from(length: DefiniteLength) -> Self {
838        Self::Definite(length)
839    }
840}
841
842impl From<AbsoluteLength> for Length {
843    fn from(length: AbsoluteLength) -> Self {
844        Self::Definite(length.into())
845    }
846}
847
848impl Default for Length {
849    fn default() -> Self {
850        Self::Definite(DefiniteLength::default())
851    }
852}
853
854impl From<()> for Length {
855    fn from(_: ()) -> Self {
856        Self::Definite(DefiniteLength::default())
857    }
858}
859
860pub trait IsZero {
861    fn is_zero(&self) -> bool;
862}
863
864impl IsZero for DevicePixels {
865    fn is_zero(&self) -> bool {
866        self.0 == 0
867    }
868}
869
870impl IsZero for ScaledPixels {
871    fn is_zero(&self) -> bool {
872        self.0 == 0.
873    }
874}
875
876impl IsZero for Pixels {
877    fn is_zero(&self) -> bool {
878        self.0 == 0.
879    }
880}
881
882impl IsZero for Rems {
883    fn is_zero(&self) -> bool {
884        self.0 == 0.
885    }
886}
887
888impl IsZero for AbsoluteLength {
889    fn is_zero(&self) -> bool {
890        match self {
891            AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
892            AbsoluteLength::Rems(rems) => rems.is_zero(),
893        }
894    }
895}
896
897impl IsZero for DefiniteLength {
898    fn is_zero(&self) -> bool {
899        match self {
900            DefiniteLength::Absolute(length) => length.is_zero(),
901            DefiniteLength::Fraction(fraction) => *fraction == 0.,
902        }
903    }
904}
905
906impl IsZero for Length {
907    fn is_zero(&self) -> bool {
908        match self {
909            Length::Definite(length) => length.is_zero(),
910            Length::Auto => false,
911        }
912    }
913}
914
915impl<T: IsZero + Debug + Clone> IsZero for Point<T> {
916    fn is_zero(&self) -> bool {
917        self.x.is_zero() && self.y.is_zero()
918    }
919}
920
921impl<T: IsZero + Debug + Clone> IsZero for Size<T> {
922    fn is_zero(&self) -> bool {
923        self.width.is_zero() || self.height.is_zero()
924    }
925}
926
927impl<T: IsZero + Debug + Clone> IsZero for Bounds<T> {
928    fn is_zero(&self) -> bool {
929        self.size.is_zero()
930    }
931}
932
933impl<T: IsZero + Debug + Clone> IsZero for Corners<T> {
934    fn is_zero(&self) -> bool {
935        self.top_left.is_zero()
936            && self.top_right.is_zero()
937            && self.bottom_right.is_zero()
938            && self.bottom_left.is_zero()
939    }
940}