#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Twips(i32);
impl Twips {
pub const TWIPS_PER_PIXEL: f64 = 20.0;
pub const ZERO: Self = Self(0);
pub const ONE: Self = Self(Self::TWIPS_PER_PIXEL as i32);
pub fn new<T: Into<i32>>(twips: T) -> Self {
Self(twips.into())
}
pub const fn get(self) -> i32 {
self.0
}
pub fn from_pixels(pixels: f64) -> Self {
Self((pixels * Self::TWIPS_PER_PIXEL) as i32)
}
pub fn to_pixels(self) -> f64 {
f64::from(self.0) / Self::TWIPS_PER_PIXEL
}
}
impl std::ops::Add for Twips {
type Output = Self;
fn add(self, other: Self) -> Self {
Self(self.0 + other.0)
}
}
impl std::ops::AddAssign for Twips {
fn add_assign(&mut self, other: Self) {
self.0 += other.0
}
}
impl std::ops::Sub for Twips {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self(self.0 - other.0)
}
}
impl std::ops::SubAssign for Twips {
fn sub_assign(&mut self, other: Self) {
self.0 -= other.0
}
}
impl std::ops::Mul<i32> for Twips {
type Output = Self;
fn mul(self, other: i32) -> Self {
Self(self.0 * other)
}
}
impl std::ops::MulAssign<i32> for Twips {
fn mul_assign(&mut self, other: i32) {
self.0 *= other
}
}
impl std::ops::Div<i32> for Twips {
type Output = Self;
fn div(self, other: i32) -> Self {
Self(self.0 / other)
}
}
impl std::ops::DivAssign<i32> for Twips {
fn div_assign(&mut self, other: i32) {
self.0 /= other
}
}
impl std::fmt::Display for Twips {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.to_pixels())
}
}