laminapdf-model 0.9.0

Typed views of a PDF document in Rust: pages, outline, document info, page labels, name trees and the structure tree, read tolerantly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//! Coordinates and units ([ADR 0027]): page space in typed points ([`Pt`], [`PointPt`],
//! [`RectPt`]: the top-left of the displayed `CropBox`, y down, `/UserUnit` applied), raw user
//! space only in `UserSpace…` types ([`UserSpacePoint`], [`UserSpaceRect`]), and the
//! [`PageFrame`] that converts between the two.
//!
//! [ADR 0027]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0027-coordinates-units-and-page-indexing.md

use std::ops::{Add, Div, Mul, Neg, Sub};

use crate::graphics::Matrix;
use crate::page::Rotation;

/// A length in points: 1/72 inch, physical (`/UserUnit` applied). Page-space geometry is in
/// points; [`Pt::from_mm`] and [`Pt::from_in`] convert other units.
///
/// ```
/// use laminapdf_model::Pt;
///
/// assert_eq!(Pt::from_in(1.0), Pt::new(72.0));
/// assert!((Pt::from_mm(210.0).get() - 595.2756).abs() < 1e-4); // A4's width
/// ```
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
pub struct Pt(f64);

impl Pt {
    /// Zero points.
    ///
    /// Added in 0.2.0.
    pub const ZERO: Self = Self(0.0);

    /// `pt` points.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new(pt: f64) -> Self {
        Self(pt)
    }

    /// `mm` millimetres (`mm * 72 / 25.4`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn from_mm(mm: f64) -> Self {
        Self(mm * 72.0 / 25.4)
    }

    /// `inches` inches (`inches * 72`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn from_in(inches: f64) -> Self {
        Self(inches * 72.0)
    }

    /// The value in points.
    ///
    /// Added in 0.2.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)
    }
}

/// A point in page space: from the top-left of the displayed `CropBox`, x right, y down, in
/// points.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct PointPt {
    /// Distance from the left edge.
    pub x: Pt,
    /// Distance from the top edge (down is positive).
    pub y: Pt,
}

impl PointPt {
    /// The point `(x, y)`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new(x: Pt, y: Pt) -> Self {
        Self { x, y }
    }

    /// The point `(x, y)` given in points.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn from_points(x: f64, y: f64) -> Self {
        Self { x: Pt(x), y: Pt(y) }
    }
}

/// A rectangle in page space, corners normalised: `x0 <= x1`, `y0 <= y1`, and `(x0, y0)` is
/// the top-left corner as displayed.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RectPt {
    /// Left.
    pub x0: Pt,
    /// Top.
    pub y0: Pt,
    /// Right.
    pub x1: Pt,
    /// Bottom.
    pub y1: Pt,
}

impl RectPt {
    /// The rectangle with corners `(x0, y0)` and `(x1, y1)`, in any order. A NaN or
    /// infinite coordinate is kept (never replaced by the other corner), so
    /// [`RectPt::is_finite`] reports it.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn new(x0: Pt, y0: Pt, x1: Pt, y1: Pt) -> Self {
        // Not f64::min/max: they return the other operand for NaN.
        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 }
    }

    /// Whether every coordinate is finite.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn is_finite(&self) -> bool {
        [self.x0, self.y0, self.x1, self.y1].iter().all(|v| v.0.is_finite())
    }

    /// Whether it lies wholly outside a page `width` × `height` in page space (ADR 0027: the
    /// page from `(0, 0)` to `(width, height)`): touching an edge from outside counts as outside.
    ///
    /// Added in 0.7.0.
    #[must_use]
    pub fn outside_page(&self, width: Pt, height: Pt) -> bool {
        self.x1.0 <= 0.0 || self.y1.0 <= 0.0 || self.x0.0 >= width.0 || self.y0.0 >= height.0
    }

    /// The smallest rectangle that holds both.
    ///
    /// Added in 0.2.0.
    #[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)),
        }
    }

    /// The rectangle whose top-left corner is `(x, y)`, `width` wide and `height` tall
    /// (negative sizes extend left or up).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn from_xywh(x: Pt, y: Pt, width: Pt, height: Pt) -> Self {
        Self::new(x, y, x + width, y + height)
    }

    /// [`RectPt::from_xywh`] with every value given in points: the rectangle whose top-left
    /// corner is `(x, y)`, `width` wide and `height` tall.
    ///
    /// ```
    /// use laminapdf_model::{Pt, RectPt};
    ///
    /// let r = RectPt::from_points(72.0, 72.0, 200.0, 50.0);
    /// assert_eq!((r.width(), r.height()), (Pt::new(200.0), Pt::new(50.0)));
    /// ```
    ///
    /// Added in 0.2.0.
    #[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))
    }

    /// `x1 - x0`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn width(&self) -> Pt {
        self.x1 - self.x0
    }

    /// `y1 - y0`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn height(&self) -> Pt {
        self.y1 - self.y0
    }
}

/// A point in raw user space (ISO 32000-2 §8.3.2.3): as the content stream sees it, y up, in
/// user-space units (not scaled by `/UserUnit`).
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct UserSpacePoint {
    /// x.
    pub x: f64,
    /// y (up is positive).
    pub y: f64,
}

impl UserSpacePoint {
    /// The point `(x, y)`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

/// A rectangle in raw user space with its corners normalised (§7.9.5): `x0 <= x1` and
/// `y0 <= y1`, so `(x0, y0)` is the lower-left corner. Page boxes and annotation `/Rect`s as
/// stored are in this space.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UserSpaceRect {
    /// Lower-left x.
    pub x0: f64,
    /// Lower-left y.
    pub y0: f64,
    /// Upper-right x.
    pub x1: f64,
    /// Upper-right y.
    pub y1: f64,
}

impl UserSpaceRect {
    /// The rectangle with corners `(a, b)` and `(c, d)`, in any order.
    ///
    /// Added in 0.2.0.
    #[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),
        }
    }

    /// The smallest rectangle that holds every one of `points`; `None` when there are none.
    ///
    /// Added in 0.9.0.
    #[must_use]
    pub fn around(points: impl IntoIterator<Item = UserSpacePoint>) -> Option<Self> {
        let mut points = points.into_iter();
        let first = points.next()?;
        let start = Self::from_corners(first.x, first.y, first.x, first.y);
        Some(points.fold(start, |r, p| Self {
            x0: r.x0.min(p.x),
            y0: r.y0.min(p.y),
            x1: r.x1.max(p.x),
            y1: r.y1.max(p.y),
        }))
    }

    /// `x1 - x0`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn width(&self) -> f64 {
        self.x1 - self.x0
    }

    /// `y1 - y0`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn height(&self) -> f64 {
        self.y1 - self.y0
    }

    /// Whether the rectangle has a positive width and height.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn has_area(&self) -> bool {
        self.width() > 0.0 && self.height() > 0.0
    }

    /// The overlap of two rectangles, `None` when they don't overlap.
    ///
    /// Added in 0.2.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 }
    }

    /// `[x0 y0 x1 y1]`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn to_array(&self) -> [f64; 4] {
        [self.x0, self.y0, self.x1, self.y1]
    }

    /// The bounding box of the rectangle after `m`: the smallest upright rectangle holding its
    /// four transformed corners.
    ///
    /// Added in 0.6.0.
    #[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)
    }
}

/// US Letter, `[0 0 612 792]`: the frame's box when a page has no usable box (ADR 0027 §3,
/// as pdf.js and PDFium do).
///
/// Added in 0.2.0.
pub const LETTER: UserSpaceRect = UserSpaceRect::from_corners(0.0, 0.0, 612.0, 792.0);

/// A page's frame ([ADR 0027] §3): the reference box (the effective `CropBox`), the rotation and
/// `/UserUnit`, which together map raw user space to page space (the top-left of the `CropBox`
/// as displayed, y down, points) and back. [`crate::Page::frame`] computes it once per page.
///
/// ```
/// use laminapdf_model::{PageFrame, Pt, PointPt, Rotation, UserSpacePoint, UserSpaceRect};
///
/// let letter = UserSpaceRect::from_corners(0.0, 0.0, 612.0, 792.0);
/// let frame = PageFrame::new(letter, Rotation::None, 1.0);
/// // 1 inch from the left and the top of the page.
/// assert_eq!(
///     frame.to_page(UserSpacePoint::new(72.0, 720.0)),
///     PointPt::new(Pt::new(72.0), Pt::new(72.0))
/// );
/// let turned = PageFrame::new(letter, Rotation::Clockwise90, 1.0);
/// assert_eq!((turned.width(), turned.height()), (Pt::new(792.0), Pt::new(612.0)));
/// ```
///
/// [ADR 0027]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0027-coordinates-units-and-page-indexing.md
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageFrame {
    crop_box: UserSpaceRect,
    rotate: Rotation,
    user_unit: f64,
    letter_fallback: bool,
}

impl PageFrame {
    /// The frame of a page with this effective `CropBox`, rotation and `/UserUnit` (a unit that
    /// is not a positive finite number counts as 1).
    ///
    /// Added in 0.2.0.
    #[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
    }

    /// The reference box: the effective `CropBox` in user space (or [`LETTER`] when the page
    /// had no usable box, [`PageFrame::is_letter_fallback`]).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn crop_box(&self) -> UserSpaceRect {
        self.crop_box
    }

    /// The rotation applied.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn rotate(&self) -> Rotation {
        self.rotate
    }

    /// The `/UserUnit` applied.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn user_unit(&self) -> f64 {
        self.user_unit
    }

    /// Whether the page had no box with an area, so the frame is US Letter (JSON's
    /// `coordinates.box` is `"default"`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn is_letter_fallback(&self) -> bool {
        self.letter_fallback
    }

    /// The displayed width in points (the box's height for 90° and 270°).
    ///
    /// Added in 0.2.0.
    #[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)
    }

    /// The displayed height in points.
    ///
    /// Added in 0.2.0.
    #[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)
    }

    /// A user-space point in page space (ADR 0027 §3's table).
    ///
    /// Added in 0.2.0.
    #[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))
    }

    /// A page-space point in user space: the inverse of [`PageFrame::to_page`].
    ///
    /// Added in 0.2.0.
    #[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)
    }

    /// A page-space point whose coordinates may be unset, as a destination's are (§12.3.2.2: an
    /// unset one keeps the viewer's), in user space: `(left, top)` with each set where the page
    /// coordinate that sets it is. On a page turned sideways the page's x runs along user y, so
    /// `x` sets the user-space top and `y` the left.
    ///
    /// Added in 0.3.0.
    #[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))
    }

    /// [`PageFrame::to_user_space`] as a matrix (§8.3.4): page-space points to user space.
    ///
    /// Added in 0.2.0.
    #[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],
        })
    }

    /// A page-space rectangle in user space (normalised: rotation and the y flip swap
    /// corners).
    ///
    /// Added in 0.2.0.
    #[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)
    }

    /// A user-space rectangle in page space.
    ///
    /// Added in 0.2.0.
    #[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))
    }

    // ADR 0027 §3's worked example and table, one test per /Rotate value.
    #[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));
    }
}

/// A page size in points: the width and height of a `/MediaBox` before `/Rotate`, physical
/// (a builder applies `/UserUnit` when it writes the page).
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageSize {
    /// Width (before `/Rotate`).
    pub width: Pt,
    /// Height (before `/Rotate`).
    pub height: Pt,
}

impl PageSize {
    /// ISO 216 A0, 841 × 1189 mm.
    ///
    /// Added in 0.2.0.
    pub const A0: Self = Self::mm(841.0, 1189.0);
    /// ISO 216 A1, 594 × 841 mm.
    ///
    /// Added in 0.2.0.
    pub const A1: Self = Self::mm(594.0, 841.0);
    /// ISO 216 A2, 420 × 594 mm.
    ///
    /// Added in 0.2.0.
    pub const A2: Self = Self::mm(420.0, 594.0);
    /// ISO 216 A3, 297 × 420 mm.
    ///
    /// Added in 0.2.0.
    pub const A3: Self = Self::mm(297.0, 420.0);
    /// ISO 216 A4, 210 × 297 mm.
    ///
    /// Added in 0.2.0.
    pub const A4: Self = Self::mm(210.0, 297.0);
    /// ISO 216 A5, 148 × 210 mm.
    ///
    /// Added in 0.2.0.
    pub const A5: Self = Self::mm(148.0, 210.0);
    /// ISO 216 A6, 105 × 148 mm.
    ///
    /// Added in 0.2.0.
    pub const A6: Self = Self::mm(105.0, 148.0);
    /// US Letter, 8.5 × 11 in.
    ///
    /// Added in 0.2.0.
    pub const LETTER: Self = Self::new(Pt::new(612.0), Pt::new(792.0));
    /// US Legal, 8.5 × 14 in.
    ///
    /// Added in 0.2.0.
    pub const LEGAL: Self = Self::new(Pt::new(612.0), Pt::new(1008.0));
    /// Tabloid, 11 × 17 in.
    ///
    /// Added in 0.2.0.
    pub const TABLOID: Self = Self::new(Pt::new(792.0), Pt::new(1224.0));

    /// `width` × `height`.
    ///
    /// Added in 0.2.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))
    }

    /// The same size turned sideways (width and height swapped).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn landscape(self) -> Self {
        Self::new(self.height, self.width)
    }
}