use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::graphics::Matrix;
use crate::page::Rotation;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
pub struct Pt(f64);
impl Pt {
pub const ZERO: Self = Self(0.0);
#[must_use]
pub const fn new(pt: f64) -> Self {
Self(pt)
}
#[must_use]
pub const fn from_mm(mm: f64) -> Self {
Self(mm * 72.0 / 25.4)
}
#[must_use]
pub const fn from_in(inches: f64) -> Self {
Self(inches * 72.0)
}
#[must_use]
pub const fn get(self) -> f64 {
self.0
}
}
impl Add for Pt {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl Sub for Pt {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl Mul<f64> for Pt {
type Output = Self;
fn mul(self, rhs: f64) -> Self {
Self(self.0 * rhs)
}
}
impl Div<f64> for Pt {
type Output = Self;
fn div(self, rhs: f64) -> Self {
Self(self.0 / rhs)
}
}
impl Neg for Pt {
type Output = Self;
fn neg(self) -> Self {
Self(-self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct PointPt {
pub x: Pt,
pub y: Pt,
}
impl PointPt {
#[must_use]
pub const fn new(x: Pt, y: Pt) -> Self {
Self { x, y }
}
#[must_use]
pub const fn from_points(x: f64, y: f64) -> Self {
Self { x: Pt(x), y: Pt(y) }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RectPt {
pub x0: Pt,
pub y0: Pt,
pub x1: Pt,
pub y1: Pt,
}
impl RectPt {
#[must_use]
pub fn new(x0: Pt, y0: Pt, x1: Pt, y1: Pt) -> Self {
let order = |a: Pt, b: Pt| if b.0 < a.0 { (b, a) } else { (a, b) };
let (x0, x1) = order(x0, x1);
let (y0, y1) = order(y0, y1);
Self { x0, y0, x1, y1 }
}
#[must_use]
pub fn is_finite(&self) -> bool {
[self.x0, self.y0, self.x1, self.y1].iter().all(|v| v.0.is_finite())
}
#[must_use]
pub fn union(&self, other: &Self) -> Self {
Self {
x0: Pt(self.x0.0.min(other.x0.0)),
y0: Pt(self.y0.0.min(other.y0.0)),
x1: Pt(self.x1.0.max(other.x1.0)),
y1: Pt(self.y1.0.max(other.y1.0)),
}
}
#[must_use]
pub fn from_xywh(x: Pt, y: Pt, width: Pt, height: Pt) -> Self {
Self::new(x, y, x + width, y + height)
}
#[must_use]
pub fn from_points(x: f64, y: f64, width: f64, height: f64) -> Self {
Self::from_xywh(Pt(x), Pt(y), Pt(width), Pt(height))
}
#[must_use]
pub fn width(&self) -> Pt {
self.x1 - self.x0
}
#[must_use]
pub fn height(&self) -> Pt {
self.y1 - self.y0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct UserSpacePoint {
pub x: f64,
pub y: f64,
}
impl UserSpacePoint {
#[must_use]
pub const fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UserSpaceRect {
pub x0: f64,
pub y0: f64,
pub x1: f64,
pub y1: f64,
}
impl UserSpaceRect {
#[must_use]
pub const fn from_corners(a: f64, b: f64, c: f64, d: f64) -> Self {
Self {
x0: a.min(c),
y0: b.min(d),
x1: a.max(c),
y1: b.max(d),
}
}
#[must_use]
pub fn width(&self) -> f64 {
self.x1 - self.x0
}
#[must_use]
pub fn height(&self) -> f64 {
self.y1 - self.y0
}
#[must_use]
pub fn has_area(&self) -> bool {
self.width() > 0.0 && self.height() > 0.0
}
#[must_use]
pub const fn intersect(&self, other: &Self) -> Option<Self> {
let r = Self {
x0: self.x0.max(other.x0),
y0: self.y0.max(other.y0),
x1: self.x1.min(other.x1),
y1: self.y1.min(other.y1),
};
if r.x0 <= r.x1 && r.y0 <= r.y1 { Some(r) } else { None }
}
#[must_use]
pub const fn to_array(&self) -> [f64; 4] {
[self.x0, self.y0, self.x1, self.y1]
}
#[must_use]
pub fn transformed(&self, m: &Matrix) -> Self {
let corners = [
(self.x0, self.y0),
(self.x1, self.y0),
(self.x0, self.y1),
(self.x1, self.y1),
]
.map(|(x, y)| m.apply(x, y));
let (mut x0, mut y0, mut x1, mut y1) = (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
for (x, y) in corners {
x0 = x0.min(x);
y0 = y0.min(y);
x1 = x1.max(x);
y1 = y1.max(y);
}
Self::from_corners(x0, y0, x1, y1)
}
}
pub const LETTER: UserSpaceRect = UserSpaceRect::from_corners(0.0, 0.0, 612.0, 792.0);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageFrame {
crop_box: UserSpaceRect,
rotate: Rotation,
user_unit: f64,
letter_fallback: bool,
}
impl PageFrame {
#[must_use]
pub fn new(crop_box: UserSpaceRect, rotate: Rotation, user_unit: f64) -> Self {
let user_unit = if user_unit.is_finite() && user_unit > 0.0 {
user_unit
} else {
1.0
};
Self {
crop_box,
rotate,
user_unit,
letter_fallback: false,
}
}
pub(crate) const fn with_letter_fallback(mut self) -> Self {
self.letter_fallback = true;
self
}
#[must_use]
pub const fn crop_box(&self) -> UserSpaceRect {
self.crop_box
}
#[must_use]
pub const fn rotate(&self) -> Rotation {
self.rotate
}
#[must_use]
pub const fn user_unit(&self) -> f64 {
self.user_unit
}
#[must_use]
pub const fn is_letter_fallback(&self) -> bool {
self.letter_fallback
}
#[must_use]
pub fn width(&self) -> Pt {
let b = &self.crop_box;
let side = if self.quarter_turned() { b.height() } else { b.width() };
Pt(side * self.user_unit)
}
#[must_use]
pub fn height(&self) -> Pt {
let b = &self.crop_box;
let side = if self.quarter_turned() { b.width() } else { b.height() };
Pt(side * self.user_unit)
}
const fn quarter_turned(&self) -> bool {
matches!(self.rotate, Rotation::Clockwise90 | Rotation::Clockwise270)
}
#[must_use]
pub fn to_page(&self, p: UserSpacePoint) -> PointPt {
let UserSpaceRect { x0, y0, x1, y1 } = self.crop_box;
let u = self.user_unit;
let (x, y) = match self.rotate {
Rotation::None => (p.x - x0, y1 - p.y),
Rotation::Clockwise90 => (p.y - y0, p.x - x0),
Rotation::Half => (x1 - p.x, p.y - y0),
Rotation::Clockwise270 => (y1 - p.y, x1 - p.x),
};
PointPt::new(Pt(x * u), Pt(y * u))
}
#[must_use]
pub fn to_user_space(&self, p: PointPt) -> UserSpacePoint {
let (x, y) = self.to_user_space_matrix().apply(p.x.get(), p.y.get());
UserSpacePoint::new(x, y)
}
#[must_use]
pub fn to_user_space_unset(&self, x: Option<Pt>, y: Option<Pt>) -> (Option<f64>, Option<f64>) {
let p = self.to_user_space(PointPt::new(x.unwrap_or(Pt::ZERO), y.unwrap_or(Pt::ZERO)));
let (along_x, along_y) = if self.rotate.degrees().is_multiple_of(180) {
(x, y)
} else {
(y, x)
};
(along_x.map(|_| p.x), along_y.map(|_| p.y))
}
#[must_use]
pub fn to_user_space_matrix(&self) -> Matrix {
let UserSpaceRect { x0, y0, x1, y1 } = self.crop_box;
let s = 1.0 / self.user_unit;
Matrix::new(match self.rotate {
Rotation::None => [s, 0.0, 0.0, -s, x0, y1],
Rotation::Clockwise90 => [0.0, s, s, 0.0, x0, y0],
Rotation::Half => [-s, 0.0, 0.0, s, x1, y0],
Rotation::Clockwise270 => [0.0, -s, -s, 0.0, x1, y1],
})
}
#[must_use]
pub fn rect_to_user_space(&self, r: &RectPt) -> UserSpaceRect {
let a = self.to_user_space(PointPt::new(r.x0, r.y0));
let b = self.to_user_space(PointPt::new(r.x1, r.y1));
UserSpaceRect::from_corners(a.x, a.y, b.x, b.y)
}
#[must_use]
pub fn rect_to_page(&self, r: &UserSpaceRect) -> RectPt {
let a = self.to_page(UserSpacePoint::new(r.x0, r.y0));
let b = self.to_page(UserSpacePoint::new(r.x1, r.y1));
RectPt::new(a.x, a.y, b.x, b.y)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rect_from_points_is_from_xywh_in_points() {
let pt = Pt::new;
assert_eq!(
RectPt::from_points(10.0, 20.0, 30.0, 40.0),
RectPt::from_xywh(pt(10.0), pt(20.0), pt(30.0), pt(40.0))
);
}
#[test]
fn rect_from_points_with_negative_sizes_extends_left_and_up() {
let pt = Pt::new;
assert_eq!(
RectPt::from_points(10.0, 20.0, -5.0, -5.0),
RectPt::new(pt(5.0), pt(15.0), pt(10.0), pt(20.0))
);
}
const LETTER_RECT: UserSpaceRect = UserSpaceRect::from_corners(72.0, 692.0, 272.0, 720.0);
fn frame(rotate: Rotation) -> PageFrame {
PageFrame::new(LETTER, rotate, 1.0)
}
fn rect(x0: f64, y0: f64, x1: f64, y1: f64) -> RectPt {
RectPt::new(Pt(x0), Pt(y0), Pt(x1), Pt(y1))
}
#[test]
fn rotate_0_puts_the_heading_one_inch_from_the_top_left() {
assert_eq!(
frame(Rotation::None).rect_to_page(&LETTER_RECT),
rect(72.0, 72.0, 272.0, 100.0)
);
}
#[test]
fn rotate_90_maps_user_space_bottom_left_to_the_displayed_top_left() {
let f = frame(Rotation::Clockwise90);
assert_eq!(f.rect_to_page(&LETTER_RECT), rect(692.0, 72.0, 720.0, 272.0));
assert_eq!(
f.to_page(UserSpacePoint::new(0.0, 0.0)),
PointPt::new(Pt::ZERO, Pt::ZERO)
);
}
#[test]
fn rotate_180_maps_user_space_bottom_right_to_the_displayed_top_left() {
let f = frame(Rotation::Half);
assert_eq!(
f.to_page(UserSpacePoint::new(612.0, 0.0)),
PointPt::new(Pt::ZERO, Pt::ZERO)
);
assert_eq!(f.rect_to_page(&LETTER_RECT), rect(340.0, 692.0, 540.0, 720.0));
}
#[test]
fn rotate_270_maps_user_space_top_right_to_the_displayed_top_left() {
let f = frame(Rotation::Clockwise270);
assert_eq!(
f.to_page(UserSpacePoint::new(612.0, 792.0)),
PointPt::new(Pt::ZERO, Pt::ZERO)
);
assert_eq!(f.rect_to_page(&LETTER_RECT), rect(72.0, 340.0, 100.0, 540.0));
assert_eq!((f.width(), f.height()), (Pt(792.0), Pt(612.0)));
}
#[test]
fn to_user_space_inverts_to_page_for_every_rotation_and_offset_box() {
let crop = UserSpaceRect::from_corners(10.0, 20.0, 310.0, 420.0);
for rotate in [
Rotation::None,
Rotation::Clockwise90,
Rotation::Half,
Rotation::Clockwise270,
] {
let f = PageFrame::new(crop, rotate, 2.0);
let p = UserSpacePoint::new(55.0, 77.0);
assert_eq!(f.to_user_space(f.to_page(p)), p, "{rotate:?}");
}
}
#[test]
fn user_unit_scales_page_space_to_physical_points() {
let f = PageFrame::new(UserSpaceRect::from_corners(0.0, 0.0, 100.0, 50.0), Rotation::None, 10.0);
assert_eq!((f.width(), f.height()), (Pt(1000.0), Pt(500.0)));
assert_eq!(
f.to_page(UserSpacePoint::new(10.0, 50.0)),
PointPt::new(Pt(100.0), Pt::ZERO)
);
}
#[test]
fn an_invalid_user_unit_counts_as_one() {
assert_eq!(PageFrame::new(LETTER, Rotation::None, -3.0).user_unit(), 1.0);
}
#[test]
fn rect_pt_keeps_a_nan_coordinate() {
let r = RectPt::new(Pt(f64::NAN), Pt(0.0), Pt(10.0), Pt(10.0));
assert!(!r.is_finite());
}
#[test]
fn rect_pt_normalises_its_corners() {
let r = RectPt::from_xywh(Pt(10.0), Pt(10.0), Pt(-5.0), Pt(20.0));
assert_eq!(r, rect(5.0, 10.0, 10.0, 30.0));
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageSize {
pub width: Pt,
pub height: Pt,
}
impl PageSize {
pub const A0: Self = Self::mm(841.0, 1189.0);
pub const A1: Self = Self::mm(594.0, 841.0);
pub const A2: Self = Self::mm(420.0, 594.0);
pub const A3: Self = Self::mm(297.0, 420.0);
pub const A4: Self = Self::mm(210.0, 297.0);
pub const A5: Self = Self::mm(148.0, 210.0);
pub const A6: Self = Self::mm(105.0, 148.0);
pub const LETTER: Self = Self::new(Pt::new(612.0), Pt::new(792.0));
pub const LEGAL: Self = Self::new(Pt::new(612.0), Pt::new(1008.0));
pub const TABLOID: Self = Self::new(Pt::new(792.0), Pt::new(1224.0));
#[must_use]
pub const fn new(width: Pt, height: Pt) -> Self {
Self { width, height }
}
const fn mm(width: f64, height: f64) -> Self {
Self::new(Pt::from_mm(width), Pt::from_mm(height))
}
#[must_use]
pub const fn landscape(self) -> Self {
Self::new(self.height, self.width)
}
}