exprim 0.4.1

A concurrent, memory-bounded PDF reader and renderer: parse, extract text, and rasterize pages on an immutable document snapshot
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Pure imposition maths: geometry in, geometry out.
//!
//! No I/O, no OS, no renderer. This is where every bug that produces a
//! wrong-looking printout lives, and it is the only part that can be
//! exhaustively tested without a printer.
//!
//! # The frames
//!
//! * *Source points* — the coordinate system of one source page: origin at
//!   its bottom-left corner, `y` up, and already sized as
//!   [`SourcePage::width`]/[`SourcePage::height`], i.e. after the page's
//!   `/Rotate` has been applied.
//! * *Sheet points* — one physical side of one sheet: origin at its
//!   bottom-left corner, `y` up, extent [`Sheet::bounds`].
//!
//! [`Placement::transform`] maps the first onto the second, and
//! [`Placement::clip`] is the cell it may paint into, already intersected
//! with the sheet's imageable area.
//!
//! # Decisions worth knowing about
//!
//! **Copies and collation are not applied here.** [`impose`] emits the sheet
//! sequence for exactly *one* copy. Copies are a spooler concern — every
//! target spooler takes a copy count natively, a print preview wants one copy
//! rather than five identical ones, and [`PrintOptions::is_pass_through_capable`]
//! already treats copies and collation as options that do not force
//! composition. A caller that must expand them itself (a `file` backend
//! writing one PNG per side, say) calls [`expand_copies`].
//!
//! **Margins are read in the oriented sheet's frame.** Both the user margins
//! and the hardware margins passed to [`impose`] name the left/right/top/
//! bottom edges of the sheet *as it prints*. A driver that reports its
//! unprintable border against portrait media should rotate it with
//! [`Margins::rotated_quarter_turns`] before handing it over. The two combine
//! with [`Margins::max_with`]: a user margin narrower than the hardware
//! border is silently widened to it, because the alternative is content that
//! vanishes on paper.
//!
//! **A quarter turn is applied once, at the level that can absorb it.**
//! Turning the sheet and turning the page are the same printout, so doing
//! both is either a no-op or a double negative. [`Orientation::Auto`]
//! therefore turns the *sheet* — once per physical sheet, scored against
//! every page that lands on it — and leaves the pages upright in their cells,
//! which is what makes `Auto` mean anything at all. A pinned
//! [`Orientation::Portrait`] or [`Orientation::Landscape`] fixes the sheet,
//! so the pages take the quarter turn instead when it fits more of them on
//! the paper — the usual "auto-rotate and centre". [`fit_page`] exposes both
//! behaviours directly to callers that want to choose.
//!
//! **Booklets ignore the N-up grid and order.** A saddle-stitched sheet is
//! folded down its vertical centre line, so it always carries exactly two
//! cells side by side and always mirrors its back side horizontally,
//! whatever [`PrintOptions::duplex`] and [`PrintOptions::n_up_order`] say.
//! The field names of [`booklet_slots`] — left and right — are that
//! constraint written down.

use core::cmp::Ordering;

use crate::print::paper::{Margins, Orientation, Rect};
use crate::print::{
    Duplex, Matrix, NUp, NUpOrder, PrintError, PrintJob, PrintOptions, Scaling, SourcePage,
};

/// Lengths closer than this are the same length. Imposition works in points,
/// where a printer's addressable dot is ~0.06pt, so this is pure float slop.
const EPSILON: f64 = 1e-9;

/// Which side of a sheet a placement is on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
    /// The front of a sheet. Every side of a one-sided job.
    Front,
    /// The back of a sheet in a two-sided job.
    Back,
}

/// One source page placed on one sheet.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Placement {
    /// Zero-based index of the source page.
    pub source_page: u32,
    /// Source page points -> sheet points.
    pub transform: Matrix,
    /// The cell on the sheet this placement may paint into, in sheet points.
    pub clip: Rect,
}

impl Placement {
    /// The source page's bounding box after [`Placement::transform`], in
    /// sheet points. Content outside [`Placement::clip`] is not printed, so
    /// this may extend past the cell under the cropping scalings.
    #[must_use]
    pub fn transformed_bounds(&self, page: SourcePage) -> Rect {
        let corners = [
            self.transform.apply(0.0, 0.0),
            self.transform.apply(page.width, 0.0),
            self.transform.apply(0.0, page.height),
            self.transform.apply(page.width, page.height),
        ];
        let mut bounds = Rect::new(f64::MAX, f64::MAX, f64::MIN, f64::MIN);
        for (x, y) in corners {
            bounds.x0 = bounds.x0.min(x);
            bounds.y0 = bounds.y0.min(y);
            bounds.x1 = bounds.x1.max(x);
            bounds.y1 = bounds.y1.max(y);
        }
        bounds
    }

    /// What actually reaches the paper: the transformed page clipped to its
    /// cell.
    #[must_use]
    pub fn painted_bounds(&self, page: SourcePage) -> Rect {
        self.transformed_bounds(page).intersect(self.clip)
    }
}

/// One physical side of one sheet of paper.
#[derive(Debug, Clone, PartialEq)]
pub struct Sheet {
    /// Zero-based position in the emitted sequence.
    pub index: u32,
    /// The sheet rectangle, origin at (0, 0), already oriented.
    pub bounds: Rect,
    /// Paper minus hardware and user margins.
    pub imageable: Rect,
    /// Front or back.
    pub side: Side,
    /// The pages on this side, each with its transform and clip.
    pub placements: Vec<Placement>,
}

impl Sheet {
    /// Whether the sheet is wider than it is tall.
    #[must_use]
    pub fn is_landscape(&self) -> bool {
        self.bounds.width() > self.bounds.height()
    }
}

/// Impose `job` onto sheets for a device with `hardware_margins`.
///
/// Emits one [`Sheet`] per printed *side*, in the order they leave the
/// printer: for a duplex or booklet job the sides alternate
/// [`Side::Front`], [`Side::Back`], and the run is padded with a blank back
/// side so a whole number of physical sheets is used.
///
/// Copies and collation are deliberately not expanded here; see the module
/// documentation and [`expand_copies`].
///
/// # Errors
///
/// [`PrintError::InvalidOptions`] if the options are not printable,
/// [`PrintError::EmptyRange`] if the page range selects nothing, and
/// [`PrintError::EmptyImageableArea`] if the paper is smaller than its
/// margins.
pub fn impose(job: &PrintJob, hardware_margins: Margins) -> Result<Vec<Sheet>, PrintError> {
    let options = &job.options;
    options.validate()?;
    hardware_margins.validate()?;

    let pages = selected_pages(job)?;

    let booklet = options.n_up == NUp::Booklet;
    let sides = if booklet {
        booklet_sides(&pages)
    } else {
        grid_sides(&pages, options)
    };
    build_sheets(
        &sides,
        booklet || options.duplex.is_duplex(),
        options,
        hardware_margins,
    )
}

/// Impose with one orientation for the entire job, as required by a spooler
/// that uses a single device configuration. Hardware margins are supplied in
/// each orientation's own frame, measured for the requested paper size.
///
/// In auto mode, score all selected source pages together, then fix the sheet
/// orientation and allow individual pages to turn inside their cells. Explicit
/// orientations are preserved. Copies are left to the spooler, as in [`impose`].
pub fn impose_uniform(
    job: &PrintJob,
    portrait_hardware: Margins,
    landscape_hardware: Margins,
) -> Result<Vec<Sheet>, PrintError> {
    job.options.validate()?;
    portrait_hardware.validate()?;
    landscape_hardware.validate()?;
    let pages = selected_pages(job)?;
    let hardware = |orientation| match orientation {
        Orientation::Landscape => landscape_hardware,
        _ => portrait_hardware,
    };
    let orientation = choose_orientation_with_hardware(&pages, &job.options, hardware)?;
    let mut uniform = job.clone();
    uniform.options.orientation = orientation;
    impose(&uniform, hardware(orientation))
}

fn selected_pages(job: &PrintJob) -> Result<Vec<SourcePage>, PrintError> {
    let page_count = u32::try_from(job.pages.len()).unwrap_or(u32::MAX);
    let mut order = job.options.range.resolve(page_count);
    if job.options.reverse {
        order.reverse();
    }
    let pages: Vec<_> = order
        .into_iter()
        .filter_map(|index| job.pages.get(usize::try_from(index).ok()?).copied())
        .collect();
    if pages.is_empty() {
        Err(PrintError::EmptyRange)
    } else {
        Ok(pages)
    }
}

/// Repeat an imposed sheet sequence for `copies` copies.
///
/// Collated output repeats the whole run — 1,2,3, 1,2,3. Uncollated output
/// repeats each *physical* sheet in place — 1,1, 2,2, 3,3 — where a physical
/// sheet is a [`Side::Front`] together with the [`Side::Back`] that follows
/// it, so duplex pairs are never split. Every sheet is re-indexed.
#[must_use]
pub fn expand_copies(sheets: &[Sheet], copies: u16, collate: bool) -> Vec<Sheet> {
    let copies = usize::from(copies.max(1));
    let mut out: Vec<Sheet> = Vec::with_capacity(sheets.len().saturating_mul(copies));
    if collate {
        for _ in 0..copies {
            out.extend(sheets.iter().cloned());
        }
    } else {
        for group in physical_sheets(sheets) {
            for _ in 0..copies {
                out.extend(group.iter().cloned());
            }
        }
    }
    for (position, sheet) in out.iter_mut().enumerate() {
        sheet.index = u32::try_from(position).unwrap_or(u32::MAX);
    }
    out
}

/// Split a side sequence into physical sheets: each front, plus any backs
/// that follow before the next front.
fn physical_sheets(sheets: &[Sheet]) -> Vec<&[Sheet]> {
    let mut groups = Vec::new();
    let mut start = 0usize;
    while start < sheets.len() {
        let mut end = start + 1;
        while sheets.get(end).is_some_and(|s| s.side == Side::Back) {
            end += 1;
        }
        if let Some(group) = sheets.get(start..end) {
            groups.push(group);
        }
        start = end;
    }
    groups
}

/// Fit `page` into `cell` under `scaling`, honouring `orientation`.
///
/// `orientation` is the rotation of the *page inside the cell*, not the sheet
/// rotation: [`Orientation::Portrait`] places the page upright,
/// [`Orientation::Landscape`] turns it a quarter turn counter-clockwise, and
/// [`Orientation::Auto`] takes whichever of the two puts more of the page on
/// the paper. The page is centred in the cell either way, and
/// [`Placement::clip`] is set to the cell so the cropping scalings
/// ([`Scaling::ActualSize`], [`Scaling::FillPage`], [`Scaling::Percent`])
/// overflow into a clip rather than onto a neighbour.
///
/// [`Scaling::Percent`] is a *factor*, `1.0` meaning 1:1 —
/// [`PrintOptions::validate`] accepts `0 < p <= 100`, so `2.0` is 200%.
#[must_use]
pub fn fit_page(
    page: SourcePage,
    cell: Rect,
    scaling: Scaling,
    orientation: Orientation,
) -> Placement {
    fit(page, cell, scaling, orientation).placement
}

/// Booklet page order: the source-page slots for sheet `sheet` of a booklet
/// with `sheets * 4` slots, as `[front_left, front_right, back_left,
/// back_right]`, `None` meaning a blank.
///
/// `total_slots` is the number of pages actually being printed; the booklet
/// itself is `total_slots` rounded up to a multiple of four, and the padding
/// comes back as `None`. Sheet *n* of an *N*-slot booklet carries the
/// one-based reading positions `(N-2n, 2n+1)` on its front and
/// `(2n+2, N-2n-1)` on its back; the returned values are those positions
/// converted to **zero-based** indices into the reading order, which is the
/// same convention as [`PageRange::resolve`](crate::print::PageRange::resolve).
///
/// Folding the emitted stack in half reproduces the reading order, which is
/// the property the round-trip test asserts.
#[must_use]
pub fn booklet_slots(sheet: u32, total_slots: u32) -> [Option<u32>; 4] {
    let padded = total_slots.div_ceil(4).saturating_mul(4);
    let taken = sheet.saturating_mul(2);
    // One-based reading positions; 0 and anything past the real page count is
    // a blank leaf of the padding.
    let pick = |position: u32| -> Option<u32> {
        (position >= 1 && position <= total_slots).then(|| position - 1)
    };
    [
        pick(padded.saturating_sub(taken)),
        pick(taken.saturating_add(1)),
        pick(taken.saturating_add(2)),
        pick(padded.saturating_sub(taken.saturating_add(1))),
    ]
}

// ---------------------------------------------------------------------------
// Side planning
// ---------------------------------------------------------------------------

/// The pages that land on one side, in cell order. `None` is an empty cell.
type SidePlan = Vec<Option<SourcePage>>;

/// Chunk the selected pages into N-up sides, padding the last side and, for
/// duplex, padding the run to a whole number of physical sheets.
fn grid_sides(pages: &[SourcePage], options: &PrintOptions) -> Vec<SidePlan> {
    let per_side = usize::try_from(options.n_up.per_side()).unwrap_or(1).max(1);
    let mut sides: Vec<SidePlan> = pages
        .chunks(per_side)
        .map(|chunk| {
            let mut slots: SidePlan = chunk.iter().copied().map(Some).collect();
            slots.resize(per_side, None);
            slots
        })
        .collect();
    if options.duplex.is_duplex() && sides.len() % 2 == 1 {
        sides.push(vec![None; per_side]);
    }
    sides
}

/// Saddle-stitch order: two slots a side, front and back of every sheet.
fn booklet_sides(pages: &[SourcePage]) -> Vec<SidePlan> {
    let total = u32::try_from(pages.len()).unwrap_or(u32::MAX);
    let sheets = total.div_ceil(4);
    let mut sides = Vec::new();
    for sheet in 0..sheets {
        let slots = booklet_slots(sheet, total);
        let at = |slot: Option<u32>| -> Option<SourcePage> {
            let position = usize::try_from(slot?).ok()?;
            pages.get(position).copied()
        };
        sides.push(vec![at(slots[0]), at(slots[1])]);
        sides.push(vec![at(slots[2]), at(slots[3])]);
    }
    sides
}

// ---------------------------------------------------------------------------
// Sheet construction
// ---------------------------------------------------------------------------

fn build_sheets(
    sides: &[SidePlan],
    two_sided: bool,
    options: &PrintOptions,
    hardware: Margins,
) -> Result<Vec<Sheet>, PrintError> {
    let booklet = options.n_up == NUp::Booklet;
    let per_physical_sheet = if two_sided { 2 } else { 1 };
    let user = options.margins;
    let page_rotation = page_rotation(options.orientation);
    let order = if booklet {
        NUpOrder::RightThenDown
    } else {
        options.n_up_order
    };

    let mut out: Vec<Sheet> = Vec::with_capacity(sides.len());
    let mut index: u32 = 0;
    for chunk in sides.chunks(per_physical_sheet) {
        // One rotation for the whole physical sheet: the front and the back
        // are the same piece of paper.
        let on_sheet: Vec<SourcePage> = chunk.iter().flatten().flatten().copied().collect();
        let orientation = choose_orientation(&on_sheet, options, hardware)?;
        let bounds = options.paper.rect(orientation);
        let landscape = orientation == Orientation::Landscape;
        if booklet && options.duplex.is_duplex() {
            let required = if landscape {
                Duplex::ShortEdge
            } else {
                Duplex::LongEdge
            };
            if options.duplex != required {
                return Err(PrintError::InvalidOptions(format!(
                    "a {orientation:?} booklet requires {required:?} duplex; use simplex for manual duplex"
                )));
            }
        }
        let (columns, rows) = cell_grid(options.n_up, landscape);

        for (position, slots) in chunk.iter().enumerate() {
            let side = if position == 0 {
                Side::Front
            } else {
                Side::Back
            };
            let margins = if side == Side::Back {
                back_margins(user, options.duplex, booklet, landscape)
            } else {
                user
            }
            .max_with(hardware);
            let imageable = inset(bounds, margins);
            if imageable.is_empty() {
                return Err(PrintError::EmptyImageableArea {
                    paper_w: bounds.width(),
                    paper_h: bounds.height(),
                });
            }
            let cells = cell_rects(imageable, columns, rows, order);
            let mut placements = Vec::new();
            for (slot, cell) in slots.iter().zip(cells) {
                if let Some(page) = *slot {
                    let mut placement = fit_page(page, cell, options.scaling, page_rotation);
                    placement.clip = placement.clip.intersect(imageable);
                    placements.push(placement);
                }
            }
            out.push(Sheet {
                index,
                bounds,
                imageable,
                side,
                placements,
            });
            index = index.saturating_add(1);
        }
    }
    Ok(out)
}

/// The back side's margins, so that the binding margin stays on the binding
/// side once the sheet is turned over.
///
/// Long-edge binding flips the sheet about its long axis, short-edge about
/// its short one, so on a portrait sheet long-edge is a left/right mirror and
/// short-edge a top/bottom one — and the other way round on a landscape
/// sheet. A booklet always folds down the vertical centre line, so its back
/// is always a left/right mirror.
fn back_margins(front: Margins, duplex: Duplex, booklet: bool, landscape: bool) -> Margins {
    if booklet {
        return front.mirrored_horizontally();
    }
    match duplex {
        Duplex::None => front,
        Duplex::LongEdge => {
            if landscape {
                front.mirrored_vertically()
            } else {
                front.mirrored_horizontally()
            }
        }
        Duplex::ShortEdge => {
            if landscape {
                front.mirrored_horizontally()
            } else {
                front.mirrored_vertically()
            }
        }
    }
}

/// Resolve [`Orientation::Auto`] against the pages that land on one physical
/// sheet: lay them out both ways and keep the rotation that puts more of them
/// on the paper.
fn choose_orientation(
    pages: &[SourcePage],
    options: &PrintOptions,
    hardware: Margins,
) -> Result<Orientation, PrintError> {
    choose_orientation_with_hardware(pages, options, |_| hardware)
}

fn choose_orientation_with_hardware(
    pages: &[SourcePage],
    options: &PrintOptions,
    hardware: impl Fn(Orientation) -> Margins,
) -> Result<Orientation, PrintError> {
    let candidates: &[Orientation] = match options.orientation {
        Orientation::Portrait => &[Orientation::Portrait],
        Orientation::Landscape => &[Orientation::Landscape],
        Orientation::Auto => &[Orientation::Portrait, Orientation::Landscape],
    };
    let page_rotation = page_rotation(options.orientation);

    let mut best: Option<(Orientation, FitScore)> = None;
    for &candidate in candidates {
        let margins = options.margins.max_with(hardware(candidate));
        let bounds = options.paper.rect(candidate);
        let imageable = inset(bounds, margins);
        if imageable.is_empty() {
            continue;
        }
        let landscape = candidate == Orientation::Landscape;
        let (columns, rows) = cell_grid(options.n_up, landscape);
        let cells = cell_rects(imageable, columns, rows, NUpOrder::RightThenDown);
        let mut score = FitScore::ZERO;
        for (position, page) in pages.iter().enumerate() {
            // A duplex group carries more pages than one side has cells, so
            // wrap: page k of the group would sit in cell k mod cells.
            if let Some(cell) = cells.get(position % cells.len().max(1)) {
                score = score.plus(fit(*page, *cell, options.scaling, page_rotation).score);
            }
        }
        let better = match best {
            None => true,
            Some((_, incumbent)) => score.better_than(incumbent),
        };
        if better {
            best = Some((candidate, score));
        }
    }

    match best {
        Some((orientation, _)) => Ok(orientation),
        None => {
            let bounds = options.paper.rect(options.orientation);
            Err(PrintError::EmptyImageableArea {
                paper_w: bounds.width(),
                paper_h: bounds.height(),
            })
        }
    }
}

/// How a page may turn inside its cell, given the sheet's own rotation
/// policy. See the module documentation: the quarter turn belongs to the
/// sheet when the sheet is free to take it, and to the page when it is not.
fn page_rotation(sheet: Orientation) -> Orientation {
    match sheet {
        Orientation::Auto => Orientation::Portrait,
        Orientation::Portrait | Orientation::Landscape => Orientation::Auto,
    }
}

/// The `(columns, rows)` an N-up sheet is divided into.
///
/// [`NUp::grid`] is read in the *sheet's own* frame, whichever way the sheet
/// is turned. A booklet is the exception: it is always two cells side by
/// side, because the fold is always the vertical centre line.
///
/// The tempting alternative — treating [`NUp::grid`] as portrait-relative and
/// swapping the pair for a landscape sheet — keeps a cell's shape fixed
/// relative to the paper, but it costs real paper on the two non-square
/// grids. Measured, A4 pages onto A4 paper under `FitToPage` and
/// [`Orientation::Auto`]: two-up falls from 0.707 to 0.500 and six-up from
/// 0.354 to 0.333, while every square grid is unaffected. Reading the grid in
/// the sheet's frame is also exactly the conventional CUPS arrangement — a
/// landscape sheet with two columns for two-up, three-by-two for six-up — and
/// [`Orientation::Auto`] finds those arrangements unaided, because they score
/// strictly better on the coverage metric it already uses.
fn cell_grid(n_up: NUp, _landscape: bool) -> (u32, u32) {
    if n_up == NUp::Booklet {
        return (2, 1);
    }
    n_up.grid()
}

/// The cells of `area`, in the order `order` fills them. Row 0 is the top
/// row, because reading order runs down the page while sheet coordinates run
/// up it.
fn cell_rects(area: Rect, columns: u32, rows: u32, order: NUpOrder) -> Vec<Rect> {
    let columns = columns.max(1);
    let rows = rows.max(1);
    let count = columns.saturating_mul(rows);
    let cell_width = area.width() / f64::from(columns);
    let cell_height = area.height() / f64::from(rows);

    let mut cells = Vec::with_capacity(usize::try_from(count).unwrap_or(0));
    for k in 0..count {
        let (column, row) = match order {
            NUpOrder::RightThenDown => (k % columns, k / columns),
            NUpOrder::LeftThenDown => (columns - 1 - (k % columns), k / columns),
            NUpOrder::DownThenRight => (k / rows, k % rows),
            NUpOrder::DownThenLeft => (columns - 1 - (k / rows), k % rows),
        };
        let x0 = area.x0 + f64::from(column) * cell_width;
        let y1 = area.y1 - f64::from(row) * cell_height;
        cells.push(Rect::new(x0, y1 - cell_height, x0 + cell_width, y1));
    }
    cells
}

/// The paper minus its margins. Margins are measured from the paper edge.
fn inset(bounds: Rect, margins: Margins) -> Rect {
    Rect::new(
        bounds.x0 + margins.left,
        bounds.y0 + margins.bottom,
        bounds.x1 - margins.right,
        bounds.y1 - margins.top,
    )
}

// ---------------------------------------------------------------------------
// Fitting
// ---------------------------------------------------------------------------

/// How well a candidate placement uses the paper.
///
/// `visible` is the fraction of the source page that survives the clip and
/// `coverage` the fraction of the cell it fills, both averaged over the pages
/// being scored. Comparing the pair in that order is what "wastes less paper"
/// means across every scaling: a fitting scaling always shows the whole page,
/// so `coverage` decides and the bigger scale wins; a cropping scaling shows
/// less than the whole page, so `visible` decides and the arrangement that
/// crops least wins.
#[derive(Debug, Clone, Copy, PartialEq)]
struct FitScore {
    visible: f64,
    coverage: f64,
}

impl FitScore {
    const ZERO: Self = Self {
        visible: 0.0,
        coverage: 0.0,
    };

    fn plus(self, other: Self) -> Self {
        Self {
            visible: self.visible + other.visible,
            coverage: self.coverage + other.coverage,
        }
    }

    fn better_than(self, other: Self) -> bool {
        if (self.visible - other.visible).abs() > EPSILON {
            return self.visible > other.visible;
        }
        match self.coverage.total_cmp(&other.coverage) {
            Ordering::Greater => self.coverage - other.coverage > EPSILON,
            _ => false,
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct Fit {
    placement: Placement,
    score: FitScore,
}

fn fit(page: SourcePage, cell: Rect, scaling: Scaling, orientation: Orientation) -> Fit {
    let rotations: &[i32] = match orientation {
        Orientation::Portrait => &[0],
        Orientation::Landscape => &[90],
        Orientation::Auto => &[0, 90],
    };
    let mut best: Option<Fit> = None;
    for &rotation in rotations {
        let candidate = fit_rotated(page, cell, scaling, rotation);
        let better = match best {
            None => true,
            Some(incumbent) => candidate.score.better_than(incumbent.score),
        };
        if better {
            best = Some(candidate);
        }
    }
    best.unwrap_or_else(|| fit_rotated(page, cell, scaling, 0))
}

fn fit_rotated(page: SourcePage, cell: Rect, scaling: Scaling, rotation: i32) -> Fit {
    let cell = cell.normalized();
    let page_width = page.width.max(0.0);
    let page_height = page.height.max(0.0);

    // Extent of the page once rotated, and the bottom-left corner of that
    // rotated box in unscaled rotated coordinates. A quarter turn
    // counter-clockwise sends (x, y) to (-y, x), so the box moves left.
    let (extent_x, extent_y, origin_x, origin_y) = if rotation.rem_euclid(360) == 90 {
        (page_height, page_width, -page_height, 0.0)
    } else {
        (page_width, page_height, 0.0, 0.0)
    };

    let cell_width = cell.width().max(0.0);
    let cell_height = cell.height().max(0.0);
    let scale = scale_for(scaling, extent_x, extent_y, cell_width, cell_height);
    let placed_width = extent_x * scale;
    let placed_height = extent_y * scale;

    let x0 = cell.x0 + (cell_width - placed_width) / 2.0;
    let y0 = cell.y0 + (cell_height - placed_height) / 2.0;
    let transform = Matrix::rotate_degrees(rotation)
        .then(Matrix::scale(scale, scale))
        .then(Matrix::translate(
            x0 - origin_x * scale,
            y0 - origin_y * scale,
        ));

    let placed = Rect::from_size(x0, y0, placed_width, placed_height);
    let visible = placed.intersect(cell);
    let visible_area = visible.width().max(0.0) * visible.height().max(0.0);
    let placed_area = placed_width * placed_height;
    let cell_area = cell_width * cell_height;

    Fit {
        placement: Placement {
            source_page: page.index,
            transform,
            clip: cell,
        },
        score: FitScore {
            visible: if placed_area > EPSILON {
                visible_area / placed_area
            } else {
                0.0
            },
            coverage: if cell_area > EPSILON {
                visible_area / cell_area
            } else {
                0.0
            },
        },
    }
}

fn scale_for(scaling: Scaling, width: f64, height: f64, cell_width: f64, cell_height: f64) -> f64 {
    let x = ratio(cell_width, width);
    let y = ratio(cell_height, height);
    let scale = match scaling {
        Scaling::ActualSize => 1.0,
        Scaling::FitToPage => x.min(y),
        Scaling::ShrinkToFit => x.min(y).min(1.0),
        Scaling::FillPage => x.max(y),
        Scaling::Percent(percent) => percent,
    };
    if scale.is_finite() && scale > 0.0 {
        scale
    } else {
        1.0
    }
}

/// `numerator / denominator`, with a degenerate denominator reading as "no
/// constraint from this axis" rather than a NaN.
fn ratio(numerator: f64, denominator: f64) -> f64 {
    if denominator > EPSILON {
        numerator / denominator
    } else {
        f64::INFINITY
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn booklet_slots_are_blank_past_the_page_count() {
        // N = 6 pads to 8; positions 7 and 8 do not exist.
        assert_eq!(booklet_slots(0, 6), [None, Some(0), Some(1), None]);
        assert_eq!(booklet_slots(1, 6), [Some(5), Some(2), Some(3), Some(4)]);
    }

    #[test]
    fn cells_run_left_to_right_then_down() {
        let cells = cell_rects(
            Rect::new(0.0, 0.0, 100.0, 100.0),
            2,
            2,
            NUpOrder::RightThenDown,
        );
        assert_eq!(cells[0], Rect::new(0.0, 50.0, 50.0, 100.0));
        assert_eq!(cells[1], Rect::new(50.0, 50.0, 100.0, 100.0));
        assert_eq!(cells[2], Rect::new(0.0, 0.0, 50.0, 50.0));
        assert_eq!(cells[3], Rect::new(50.0, 0.0, 100.0, 50.0));
    }

    #[test]
    fn cell_order_variants_permute_the_same_cells() {
        let area = Rect::new(0.0, 0.0, 90.0, 60.0);
        let base = cell_rects(area, 3, 2, NUpOrder::RightThenDown);
        for order in [
            NUpOrder::LeftThenDown,
            NUpOrder::DownThenRight,
            NUpOrder::DownThenLeft,
        ] {
            let cells = cell_rects(area, 3, 2, order);
            assert_eq!(cells.len(), base.len());
            for cell in &cells {
                assert!(
                    base.contains(cell),
                    "{order:?} produced a stray cell {cell:?}"
                );
            }
        }
    }

    #[test]
    fn a_quarter_turn_lands_the_page_inside_the_cell() {
        let page = SourcePage::new(0, 200.0, 100.0);
        let cell = Rect::new(10.0, 20.0, 110.0, 220.0);
        let placement = fit_page(page, cell, Scaling::FitToPage, Orientation::Landscape);
        let bounds = placement.transformed_bounds(page);
        assert!(
            bounds.contained_by(cell, 1e-6),
            "{bounds:?} escaped {cell:?}"
        );
        // Rotated, the 200x100 page is 100 wide and 200 tall: an exact fit.
        assert!((bounds.width() - 100.0).abs() < 1e-6, "{bounds:?}");
        assert!((bounds.height() - 200.0).abs() < 1e-6, "{bounds:?}");
    }

    #[test]
    fn back_margins_mirror_on_the_binding_axis() {
        let front = Margins {
            left: 40.0,
            right: 10.0,
            top: 5.0,
            bottom: 20.0,
        };
        let portrait_long = back_margins(front, Duplex::LongEdge, false, false);
        assert_eq!(portrait_long.left, 10.0);
        assert_eq!(portrait_long.right, 40.0);
        assert_eq!(portrait_long.top, 5.0);

        let portrait_short = back_margins(front, Duplex::ShortEdge, false, false);
        assert_eq!(portrait_short.left, 40.0);
        assert_eq!(portrait_short.top, 20.0);
        assert_eq!(portrait_short.bottom, 5.0);

        // A landscape sheet swaps which mirror belongs to which binding.
        assert_eq!(
            back_margins(front, Duplex::LongEdge, false, true),
            portrait_short
        );
        assert_eq!(
            back_margins(front, Duplex::ShortEdge, false, true),
            portrait_long
        );
    }
}