use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
#[cfg(doc)]
use {
crate::pdf::bitmap::PdfBitmap, crate::pdf::document::page::PdfPage,
crate::pdf::document::PdfDocument,
};
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub struct PdfPoints {
pub value: f32,
}
impl PdfPoints {
pub const ZERO: PdfPoints = PdfPoints::zero();
pub const MAX: PdfPoints = PdfPoints::max();
pub const MIN: PdfPoints = PdfPoints::min();
#[inline]
pub const fn new(value: f32) -> Self {
Self { value }
}
#[inline]
pub const fn zero() -> Self {
Self::new(0.0)
}
#[inline]
pub const fn max() -> Self {
Self::new(2_000_000_000.0)
}
#[inline]
pub const fn min() -> Self {
Self::new(-2_000_000_000.0)
}
#[inline]
pub fn from_inches(inches: f32) -> Self {
Self::new(inches * 72.0)
}
#[inline]
pub fn from_cm(cm: f32) -> Self {
Self::from_inches(cm / 2.54)
}
#[inline]
pub fn from_mm(mm: f32) -> Self {
Self::from_cm(mm / 10.0)
}
#[inline]
pub fn to_inches(&self) -> f32 {
self.value / 72.0
}
#[inline]
pub fn to_cm(&self) -> f32 {
self.to_inches() * 2.54
}
#[inline]
pub fn to_mm(&self) -> f32 {
self.to_cm() * 10.0
}
#[inline]
pub fn abs(&self) -> PdfPoints {
PdfPoints::new(self.value.abs())
}
}
impl Add<PdfPoints> for PdfPoints {
type Output = PdfPoints;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
PdfPoints::new(self.value + rhs.value)
}
}
impl AddAssign<PdfPoints> for PdfPoints {
#[inline]
fn add_assign(&mut self, rhs: PdfPoints) {
self.value += rhs.value;
}
}
impl Sub<PdfPoints> for PdfPoints {
type Output = PdfPoints;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
PdfPoints::new(self.value - rhs.value)
}
}
impl SubAssign<PdfPoints> for PdfPoints {
#[inline]
fn sub_assign(&mut self, rhs: PdfPoints) {
self.value -= rhs.value;
}
}
impl Mul<f32> for PdfPoints {
type Output = PdfPoints;
#[inline]
fn mul(self, rhs: f32) -> Self::Output {
PdfPoints::new(self.value * rhs)
}
}
impl Div<f32> for PdfPoints {
type Output = PdfPoints;
#[inline]
fn div(self, rhs: f32) -> Self::Output {
PdfPoints::new(self.value / rhs)
}
}
impl Neg for PdfPoints {
type Output = PdfPoints;
#[inline]
fn neg(self) -> Self::Output {
PdfPoints::new(-self.value)
}
}
impl Eq for PdfPoints {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for PdfPoints {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.value
.partial_cmp(&other.value)
.unwrap_or(Ordering::Equal)
}
}
impl Display for PdfPoints {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("PdfPoints({})", self.value))
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn test_points_ordering() {
assert!(PdfPoints::new(1.0) > PdfPoints::ZERO);
assert_eq!(PdfPoints::ZERO, -PdfPoints::ZERO);
assert!(PdfPoints::ZERO > PdfPoints::new(-1.0));
}
}