//! `FontSize` type for text rendering #[derive(Default, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct FontSize(pub u16); impl FontSize { /// Create a new `FontSize` from a u16 value #[must_use] pub const fn new(size: u16) -> Self { Self(size) } /// Get the inner u16 value #[must_use] pub const fn value(self) -> u16 { self.0 } } impl From for FontSize { fn from(size: u16) -> Self { Self(size) } } impl From for FontSize { fn from(f: f32) -> Self { Self(f.max(1.0) as u16) } } impl From for FontSize { fn from(i: i32) -> Self { Self(i.max(1) as u16) } } impl From for FontSize { fn from(i: i16) -> Self { Self(i.max(1) as u16) } } impl From for FontSize { fn from(f: f64) -> Self { Self(f.max(1.0) as u16) } } impl From for FontSize { fn from(size: usize) -> Self { Self(u16::try_from(size).unwrap_or(u16::MAX).max(1)) } } #[cfg(test)] mod tests { use super::*; #[test] fn font_size_conversions() { assert_eq!(FontSize::from(16_u16).value(), 16); assert_eq!(FontSize::from(24_i32).value(), 24); assert_eq!(FontSize::from(18_i16).value(), 18); assert_eq!(FontSize::from(32_usize).value(), 32); assert_eq!(FontSize::from(20.5_f32).value(), 20); assert_eq!(FontSize::from(28.0_f64).value(), 28); } }