#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct Twips(i32);
impl Twips {
pub const TWIPS_PER_PIXEL: i32 = 20;
pub const ZERO: Self = Self(0);
pub const ONE_PX: Self = Self(Self::TWIPS_PER_PIXEL);
pub const HALF_PX: Self = Self(Self::TWIPS_PER_PIXEL / 2);
#[inline]
pub const fn new(twips: i32) -> Self {
Self(twips)
}
#[inline]
pub const fn get(self) -> i32 {
self.0
}
#[inline]
pub fn from_pixels(pixels: f64) -> Self {
Self((pixels * Self::TWIPS_PER_PIXEL as f64) as i32)
}
#[inline]
pub const fn from_pixels_i32(pixels: i32) -> Self {
Self(pixels * Self::TWIPS_PER_PIXEL)
}
#[inline]
pub fn to_pixels(self) -> f64 {
f64::from(self.0) / Self::TWIPS_PER_PIXEL as f64
}
#[inline]
pub fn trunc_to_pixel(self) -> Self {
Self::new(self.0 / Twips::TWIPS_PER_PIXEL * Twips::TWIPS_PER_PIXEL)
}
#[inline]
pub fn round_to_pixel_ties_even(self) -> Self {
Self::from_pixels(self.to_pixels().round_ties_even())
}
}
impl std::ops::Add for Twips {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self(self.0 + other.0)
}
}
impl std::ops::AddAssign for Twips {
#[inline]
fn add_assign(&mut self, other: Self) {
self.0 += other.0
}
}
impl std::ops::Sub for Twips {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self(self.0 - other.0)
}
}
impl std::ops::SubAssign for Twips {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.0 -= other.0
}
}
impl std::ops::Mul<i32> for Twips {
type Output = Self;
#[inline]
fn mul(self, other: i32) -> Self {
Self(self.0 * other)
}
}
impl std::ops::MulAssign<i32> for Twips {
#[inline]
fn mul_assign(&mut self, other: i32) {
self.0 *= other
}
}
impl std::ops::Div<i32> for Twips {
type Output = Self;
#[inline]
fn div(self, other: i32) -> Self {
Self(self.0 / other)
}
}
impl std::ops::DivAssign<i32> for Twips {
#[inline]
fn div_assign(&mut self, other: i32) {
self.0 /= other
}
}
impl std::ops::Neg for Twips {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Twips(-self.0)
}
}
impl std::fmt::Display for Twips {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.to_pixels(), f)
}
}