Skip to main content

rustium_pdf/
geom.rs

1//! Points, rectangles and 2×3 affine matrices in PDF's row-vector convention.
2
3/// A point in PDF user space (bottom-left origin, y-up) unless a function says otherwise.
4#[derive(Debug, Clone, Copy, PartialEq, Default)]
5pub struct Point {
6    pub x: f32,
7    pub y: f32,
8}
9
10impl Point {
11    pub const fn new(x: f32, y: f32) -> Self {
12        Self { x, y }
13    }
14}
15
16/// An axis-aligned rectangle with `x0 <= x1` and `y0 <= y1`.
17#[derive(Debug, Clone, Copy, PartialEq, Default)]
18pub struct Rect {
19    pub x0: f32,
20    pub y0: f32,
21    pub x1: f32,
22    pub y1: f32,
23}
24
25impl Rect {
26    pub fn from_corners(ax: f32, ay: f32, bx: f32, by: f32) -> Self {
27        Self {
28            x0: ax.min(bx),
29            y0: ay.min(by),
30            x1: ax.max(bx),
31            y1: ay.max(by),
32        }
33    }
34
35    pub const EMPTY: Rect = Rect {
36        x0: 0.0,
37        y0: 0.0,
38        x1: 0.0,
39        y1: 0.0,
40    };
41
42    pub fn width(&self) -> f32 {
43        self.x1 - self.x0
44    }
45
46    pub fn height(&self) -> f32 {
47        self.y1 - self.y0
48    }
49
50    pub fn is_empty(&self) -> bool {
51        self.width() <= 0.0 || self.height() <= 0.0
52    }
53
54    pub fn union(&self, other: &Rect) -> Rect {
55        if self.is_empty() && other.is_empty() {
56            return *self;
57        }
58        if self.is_empty() {
59            return *other;
60        }
61        if other.is_empty() {
62            return *self;
63        }
64        Rect {
65            x0: self.x0.min(other.x0),
66            y0: self.y0.min(other.y0),
67            x1: self.x1.max(other.x1),
68            y1: self.y1.max(other.y1),
69        }
70    }
71
72    pub fn intersect(&self, other: &Rect) -> Rect {
73        Rect {
74            x0: self.x0.max(other.x0),
75            y0: self.y0.max(other.y0),
76            x1: self.x1.min(other.x1),
77            y1: self.y1.min(other.y1),
78        }
79    }
80}
81
82/// An affine transform `[a b c d e f]`, applied to row vectors: `(x, y, 1) · M`.
83///
84/// This is the convention the PDF specification uses for the CTM and text matrices, so
85/// composition reads the same way here as in the spec: transforming by `A` then `B` is
86/// `A.concat(B)`.
87#[derive(Debug, Clone, Copy, PartialEq)]
88pub struct Matrix {
89    pub a: f32,
90    pub b: f32,
91    pub c: f32,
92    pub d: f32,
93    pub e: f32,
94    pub f: f32,
95}
96
97impl Default for Matrix {
98    fn default() -> Self {
99        Self::IDENTITY
100    }
101}
102
103impl Matrix {
104    pub const IDENTITY: Matrix = Matrix {
105        a: 1.0,
106        b: 0.0,
107        c: 0.0,
108        d: 1.0,
109        e: 0.0,
110        f: 0.0,
111    };
112
113    pub const fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
114        Self { a, b, c, d, e, f }
115    }
116
117    pub fn translate(tx: f32, ty: f32) -> Self {
118        Self::new(1.0, 0.0, 0.0, 1.0, tx, ty)
119    }
120
121    pub fn scale(sx: f32, sy: f32) -> Self {
122        Self::new(sx, 0.0, 0.0, sy, 0.0, 0.0)
123    }
124
125    /// `self` then `other`: the matrix that applies `self` first.
126    pub fn concat(&self, other: &Matrix) -> Matrix {
127        Matrix {
128            a: self.a * other.a + self.b * other.c,
129            b: self.a * other.b + self.b * other.d,
130            c: self.c * other.a + self.d * other.c,
131            d: self.c * other.b + self.d * other.d,
132            e: self.e * other.a + self.f * other.c + other.e,
133            f: self.e * other.b + self.f * other.d + other.f,
134        }
135    }
136
137    pub fn apply(&self, p: Point) -> Point {
138        Point {
139            x: self.a * p.x + self.c * p.y + self.e,
140            y: self.b * p.x + self.d * p.y + self.f,
141        }
142    }
143
144    /// Transforms only the vector part (no translation).
145    pub fn apply_vector(&self, x: f32, y: f32) -> (f32, f32) {
146        (self.a * x + self.c * y, self.b * x + self.d * y)
147    }
148
149    /// The axis-aligned bounding box of the four transformed corners.
150    pub fn apply_rect(&self, r: &Rect) -> Rect {
151        let p0 = self.apply(Point::new(r.x0, r.y0));
152        let p1 = self.apply(Point::new(r.x1, r.y0));
153        let p2 = self.apply(Point::new(r.x0, r.y1));
154        let p3 = self.apply(Point::new(r.x1, r.y1));
155        Rect {
156            x0: p0.x.min(p1.x).min(p2.x).min(p3.x),
157            y0: p0.y.min(p1.y).min(p2.y).min(p3.y),
158            x1: p0.x.max(p1.x).max(p2.x).max(p3.x),
159            y1: p0.y.max(p1.y).max(p2.y).max(p3.y),
160        }
161    }
162
163    /// Rotation, in radians, of the transformed x-axis.
164    pub fn rotation(&self) -> f32 {
165        self.b.atan2(self.a)
166    }
167
168    /// Length of the transformed unit y vector: the factor a font size is scaled by.
169    pub fn y_scale(&self) -> f32 {
170        (self.c * self.c + self.d * self.d).sqrt()
171    }
172
173    pub fn x_scale(&self) -> f32 {
174        (self.a * self.a + self.b * self.b).sqrt()
175    }
176
177    pub fn invert(&self) -> Option<Matrix> {
178        let det = self.a * self.d - self.b * self.c;
179        if det.abs() < 1e-12 {
180            return None;
181        }
182        let inv = 1.0 / det;
183        Some(Matrix {
184            a: self.d * inv,
185            b: -self.b * inv,
186            c: -self.c * inv,
187            d: self.a * inv,
188            e: (self.c * self.f - self.d * self.e) * inv,
189            f: (self.b * self.e - self.a * self.f) * inv,
190        })
191    }
192}
193
194#[cfg(test)]
195mod tests {
196    use super::*;
197
198    #[test]
199    fn concat_applies_left_first() {
200        let scale = Matrix::scale(2.0, 2.0);
201        let shift = Matrix::translate(10.0, 0.0);
202        let m = scale.concat(&shift);
203        let p = m.apply(Point::new(1.0, 1.0));
204        assert_eq!((p.x, p.y), (12.0, 2.0));
205    }
206
207    #[test]
208    fn invert_round_trips() {
209        let m = Matrix::new(2.0, 1.0, -1.0, 3.0, 5.0, -2.0);
210        let inv = m.invert().unwrap();
211        let p = inv.apply(m.apply(Point::new(3.0, 4.0)));
212        assert!((p.x - 3.0).abs() < 1e-4 && (p.y - 4.0).abs() < 1e-4);
213    }
214}