rustyfi-backend 0.1.4

Box/glue model, typesetting context, line and page breaking for SATySFi
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
//! The drawing data model — paths, colors, and `graphics` elements; the
//! analog of upstream's `GraphicBase`/`PrePath`/`GraphicD`. Everything here is
//! already-resolved coordinates/data: no lang-side closure or deferred
//! computation crosses into this module.

use crate::hbox::PureHorzBox;
use crate::length::Length;

/// A point in graphics space (upstream `point`; matches the runtime
/// `Value::Tuple([Length, Length])` representation). Graphics space is
/// y-**up** (PDF-native); the PDF writer's `place_graphics` flips
/// page-layout's y-down convention when placing a graphics box on a line.
pub type Point = (Length, Length);

/// A dash pattern (`dashed-stroke`'s 2nd argument; upstream `graphicD.ml`'s
/// `type dash = length * length * length`, `(d1, d2, d0)` = on-length,
/// off-length, phase).
pub type Dash = (Length, Length, Length);

/// `color.satyh`'s `Gray`/`RGB`/`CMYK` after extraction by `as_color`
/// (mirrors `evalUtil.ml`'s `get_color` → `DeviceGray`/`DeviceRGB`/
/// `DeviceCMYK`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Color {
    Gray(f64),
    Rgb(f64, f64, f64),
    Cmyk(f64, f64, f64, f64),
}

/// One path element: a control-point-free straight segment, or a cubic
/// Bézier (2 control points + destination) — `graphicBase.ml`'s
/// `point path_element`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PathSeg {
    Line(Point),
    Bezier(Point, Point, Point),
}

/// How a subpath closes (`graphicBase.ml`'s `path`'s `cycleopt`): left open,
/// closed with a straight segment back to the start (`close-with-line`), or
/// closed with a cubic (`close-with-bezier` — the destination is always the
/// subpath's own `start`, so only the two control points are stored).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Closing {
    Open,
    Line,
    Bezier(Point, Point),
}

/// One `GraphicBase.GeneralPath(start, elems, closing)`.
#[derive(Clone, Debug, PartialEq)]
pub struct Subpath {
    pub start: Point,
    pub segs: Vec<PathSeg>,
    pub closing: Closing,
}

/// The `path` value = upstream `path list` (`unite-path` appends subpath
/// lists).
#[derive(Clone, Debug, PartialEq)]
pub struct Path {
    pub subpaths: Vec<Subpath>,
}

/// The `pre-path` value (`PrePath.t`): a start point plus forward-accumulated
/// segments, before a `terminate-path`/`close-with-line` fixes a closing.
/// Upstream accumulates in reverse and flips at close time; this port pushes
/// forward directly, which is unobservable.
#[derive(Clone, Debug, PartialEq)]
pub struct PrePath {
    pub start: Point,
    pub segs: Vec<PathSeg>,
}

/// One `graphics` element (`GraphicD.element`). `place_graphics`
/// (rustyfi-pdf) matches this exhaustively, without a wildcard arm.
///
/// See [`crate::hbox::PureHorzBox`] for what the `#[subast]` list means and
/// what checks it.
#[derive(Clone, Debug, PartialEq, syan::visit::Ast)]
#[subast(crate::graphics::GraphicsElem, crate::hbox::PureHorzBox)]
pub enum GraphicsElem {
    /// Filled region, even-odd rule (upstream's `op_f'`).
    Fill(Color, Path),
    /// Stroked outline at the given line width.
    Stroke(Length, Color, Path),
    /// Dashed stroked outline (`dashed-stroke`), rendered with a PDF `d`
    /// dash-array op alongside the same stroke ops as `Stroke`.
    DashedStroke(Length, Dash, Color, Path),
    /// `draw-text`: a text run anchored at `pt` (box-local, y-up; the run's
    /// leftmost baseline point). `contents` is the run laid out at NATURAL
    /// width (upstream `LineBreak.natural` = `determine_widths None`,
    /// `widperfil = 0`; here `fit_cell(boxes, natural_width)`), each box with
    /// its x offset from `pt`. `width`/`height`/`depth` are the run's
    /// `natural_metrics`, stored at construction so `graphics_bbox` needs no
    /// re-measure. Rendered by each PDF writer re-entering its own per-box
    /// emission at `pt + dx` INSIDE `place_graphics`'s box-local `cm` frame.
    Text {
        pt: Point,
        contents: Vec<(Length, PureHorzBox)>,
        width: Length,
        height: Length,
        depth: Length,
        /// The accumulated 2×2 linear transform (`linear-transform-graphics`,
        /// row-major `(a, b, c, d)` — same convention as
        /// `linear_transform_point`) applied to the run about its local
        /// origin BEFORE the `pt` translation. `None` means identity: the run
        /// is drawn upright at `pt`. `Some` appears once
        /// `rotate-graphics`/`scale-graphics` is composed onto a `draw-text`;
        /// the writer then emits the run under a `cm` carrying this matrix
        /// (upstream's lazy `LinearTrans` render-time `cm`).
        transform: Option<(f64, f64, f64, f64)>,
    },
    /// 0.1 collection node (`GraphicD.concat`, dev-0-1-0 `graphicD.ml:23`):
    /// `unite-graphics`' payload. No 0.0.6-visible primitive builds one, so it
    /// is unreachable from 0.0.6 rendering by construction.
    Group(Vec<GraphicsElem>),
    /// 0.1 clip node (`GraphicD.make_clip`, `graphicD.ml:97-98`): render
    /// `contents` clipped to `clip` (even-odd, `Op_W'` — `graphicD.ml:331`).
    /// The port's `Path` already carries N subpaths, standing in for
    /// upstream's `path list`. Never constructed by any 0.0.6 path, as `Group`.
    Clip(Path, Vec<GraphicsElem>),
    /// A DEFERRED `register-destination` call, carrying NO ink. `pt` is
    /// box-local in the same y-**up** frame as every other element's
    /// coordinates, so the existing transform pipeline carries it, and
    /// `rustyfi-lang`'s `fire_hooks` replays it once the box has a page and a
    /// placed point.
    ///
    /// It exists because this port applies an `inline-graphics` callback
    /// eagerly at construction time (`prim_inline_graphics`) rather than during
    /// page breaking as upstream does, so a `register-destination` inside one
    /// has no page and `annotation.ml:15`'s gate — faithfully — refuses it.
    ///
    /// KNOWN GAP: `math_boxes_of_inline_boxes` (rustyfi-lang) harvests a
    /// graphics box's elements into a `PureHorzBox::Math`'s `rules` and
    /// `fire_hooks` has no `Math` arm, so an anchor inside a `make_paren`
    /// delimiter closure never fires. `shift_graphics` carries the point
    /// correctly, so closing it is one arm and no arithmetic.
    Destination { key: String, pt: Point },
}

// `shift-path`/`shift-graphics`/`linear-transform-path`/
// `linear-transform-graphics` are all EAGER point remaps — no lazy
// `LinearTrans`-wrapper element: every point is rewritten up front, mirroring
// `graphicBase.ml`'s `shift_path`/`linear_transform_path` (`(x, y) ->
// (x*a + y*b, x*c + y*d)` for the 2x2 matrix `((a, b), (c, d))`).

/// `shift_path v pt` (`graphicBase.ml`'s `(+@%)`).
fn shift_point(v: Point, pt: Point) -> Point {
    (pt.0 + v.0, pt.1 + v.1)
}

/// `graphicBase.ml`'s `linear_transform_point`: `(x, y) |-> (x*a + y*b, x*c +
/// y*d)` for matrix `mat = (a, b, c, d)`.
fn linear_transform_point(mat: (f64, f64, f64, f64), pt: Point) -> Point {
    let (a, b, c, d) = mat;
    (pt.0 * a + pt.1 * b, pt.0 * c + pt.1 * d)
}

/// Map `f` over every point of `path` (subpath starts, every segment's
/// points — including Bézier control points — and any closing control
/// points), preserving structure.
fn map_path(path: &Path, f: impl Fn(Point) -> Point) -> Path {
    Path {
        subpaths: path
            .subpaths
            .iter()
            .map(|sub| Subpath {
                start: f(sub.start),
                segs: sub
                    .segs
                    .iter()
                    .map(|seg| match *seg {
                        PathSeg::Line(p) => PathSeg::Line(f(p)),
                        PathSeg::Bezier(c1, c2, p) => PathSeg::Bezier(f(c1), f(c2), f(p)),
                    })
                    .collect(),
                closing: match sub.closing {
                    Closing::Open => Closing::Open,
                    Closing::Line => Closing::Line,
                    Closing::Bezier(c1, c2) => Closing::Bezier(f(c1), f(c2)),
                },
            })
            .collect(),
    }
}

/// `shift-path : point -> path -> path` (vminst.ml:663) — translate every
/// point of `path` by `v`.
pub fn shift_path(v: Point, path: &Path) -> Path {
    map_path(path, |p| shift_point(v, p))
}

/// `linear-transform-path : float -> float -> float -> float -> path ->
/// path` (vminst.ml:678) — apply the 2x2 matrix `mat` to every point.
pub fn linear_transform_path(mat: (f64, f64, f64, f64), path: &Path) -> Path {
    map_path(path, |p| linear_transform_point(mat, p))
}

/// One block frame's own decoration, captured at its natural size so a
/// backend with no page grid can still draw it.
///
/// The graphics are BOX-LOCAL (origin at the frame's bottom-left, y up) and
/// span `width` x `height`, which is what lets a reflowable renderer scale
/// them to whatever width the reader's window gives the frame. `fire_hooks`
/// records one per `DecoId` the first time that frame fires as a SINGLE
/// fragment (`decoS`); a frame split across pages has no whole-frame drawing
/// and records nothing.
#[derive(Clone, Debug, PartialEq)]
pub struct FrameDecoration {
    pub width: crate::Length,
    pub height: crate::Length,
    /// The frame's own paddings, `(left, right, top, bottom)`.
    ///
    /// Three of the four are already visible in the flow — `indent_left`
    /// folds `left` into every contained line's x offset, and the two
    /// vertical ones arrive as `VertBox::FramePad` — so a reflowing renderer
    /// needs only `right`, which nothing else records: the content is simply
    /// laid out narrower, and once the frame is redrawn at the reader's own
    /// width a right-aligned line lands on its border.
    pub pads: (crate::Length, crate::Length, crate::Length, crate::Length),
    pub elems: Vec<GraphicsElem>,
}

/// `shift-graphics : point -> graphics -> graphics` (vminst.ml:2451) —
/// `graphicD.ml`'s `shift_element`.
pub fn shift_graphics(v: Point, elem: &GraphicsElem) -> GraphicsElem {
    match elem {
        GraphicsElem::Fill(c, p) => GraphicsElem::Fill(*c, shift_path(v, p)),
        GraphicsElem::Stroke(w, c, p) => GraphicsElem::Stroke(*w, *c, shift_path(v, p)),
        GraphicsElem::DashedStroke(w, d, c, p) => {
            GraphicsElem::DashedStroke(*w, *d, *c, shift_path(v, p))
        }
        GraphicsElem::Text { pt, contents, width, height, depth, transform } => {
            GraphicsElem::Text {
                pt: shift_point(v, *pt),
                contents: contents.clone(),
                width: *width,
                height: *height,
                depth: *depth,
                // A pure translation leaves the run's own 2×2 transform intact
                // (only `pt` moves) — the affine is `transform·l + pt`.
                transform: *transform,
            }
        }
        // `graphicD.ml:38`: `Group` maps every child; `Clip` shifts its own
        // clip path AND recurses into its contents.
        GraphicsElem::Group(gs) => {
            GraphicsElem::Group(gs.iter().map(|g| shift_graphics(v, g)).collect())
        }
        GraphicsElem::Clip(path, gs) => GraphicsElem::Clip(
            shift_path(v, path),
            gs.iter().map(|g| shift_graphics(v, g)).collect(),
        ),
        // The anchor point is an ordinary box-local coordinate: it moves with
        // the ink around it.
        GraphicsElem::Destination { key, pt } => GraphicsElem::Destination {
            key: key.clone(),
            pt: shift_point(v, *pt),
        },
    }
}

/// `linear-transform-graphics : float -> float -> float -> float ->
/// graphics -> graphics` (vminst.ml:2432) — `graphicD.ml`'s
/// `make_linear_trans`, applied eagerly.
pub fn linear_transform_graphics(mat: (f64, f64, f64, f64), elem: &GraphicsElem) -> GraphicsElem {
    match elem {
        GraphicsElem::Fill(c, p) => GraphicsElem::Fill(*c, linear_transform_path(mat, p)),
        GraphicsElem::Stroke(w, c, p) => GraphicsElem::Stroke(*w, *c, linear_transform_path(mat, p)),
        GraphicsElem::DashedStroke(w, d, c, p) => {
            GraphicsElem::DashedStroke(*w, *d, *c, linear_transform_path(mat, p))
        }
        // A `draw-text` run carries the composed 2×2 matrix so the writer can
        // rotate/scale the glyphs/image at render time (upstream's lazy
        // `LinearTrans` `cm`). The affine is `transform·l + pt`; pre-composing
        // `mat` gives `mat·(transform·l + pt) = (mat·transform)·l + mat·pt`, so
        // `transform ↦ mat·transform` and `pt ↦ mat·pt`. Matrices are row-major
        // `(a, b, c, d)` = `[[a, b], [c, d]]` (the `linear_transform_point`
        // convention), so the product below is the standard 2×2 multiply.
        GraphicsElem::Text { pt, contents, width, height, depth, transform } => {
            let (ma, mb, mc, md) = mat;
            let (ta, tb, tc, td) = transform.unwrap_or((1.0, 0.0, 0.0, 1.0));
            let composed = (
                ma * ta + mb * tc,
                ma * tb + mb * td,
                mc * ta + md * tc,
                mc * tb + md * td,
            );
            GraphicsElem::Text {
                pt: linear_transform_point(mat, *pt),
                contents: contents.clone(),
                width: *width,
                height: *height,
                depth: *depth,
                transform: Some(composed),
            }
        }
        GraphicsElem::Group(gs) => GraphicsElem::Group(
            gs.iter().map(|g| linear_transform_graphics(mat, g)).collect(),
        ),
        GraphicsElem::Clip(path, gs) => GraphicsElem::Clip(
            linear_transform_path(mat, path),
            gs.iter().map(|g| linear_transform_graphics(mat, g)).collect(),
        ),
        // As in `shift_graphics`: an ordinary box-local coordinate.
        GraphicsElem::Destination { key, pt } => GraphicsElem::Destination {
            key: key.clone(),
            pt: linear_transform_point(mat, *pt),
        },
    }
}

/// One axis (x or y) of a cubic Bézier's EXACT extrema (`graphicBase.ml:88`
/// `bezier_bbox`'s per-axis `aux`): for the cubic from `r0` (current point)
/// through controls `r1`, `r2` to `r3`, the derivative's roots give the
/// interior extrema; candidates are `{r0, r3, B(t+), B(t-)}` with `t` clamped
/// to `[0, 1]` (`bezier_point`'s convention: `t < 0` snaps to `r0`, `t > 1`
/// snaps to `r3`). Returns `(min, max)` over that candidate set.
fn bezier_axis_extent(r0: f64, r1: f64, r2: f64, r3: f64) -> (f64, f64) {
    // B(t) = (1-t)^3 r0 + 3(1-t)^2 t r1 + 3(1-t) t^2 r2 + t^3 r3
    // B'(t)/3 = a t^2 + b t + c, with:
    let a = -r0 + 3.0 * (r1 - r2) + r3;
    let b = 2.0 * (r0 - 2.0 * r1 + r2);
    let c = r1 - r0;
    let bezier_point = |t: f64| -> f64 {
        if t < 0.0 {
            r0
        } else if t > 1.0 {
            r3
        } else {
            let u = 1.0 - t;
            u * u * u * r0 + 3.0 * u * u * t * r1 + 3.0 * u * t * t * r2 + t * t * t * r3
        }
    };
    let mut candidates = vec![r0, r3];
    if a.abs() < 1e-12 {
        // Linear derivative (or degenerate): at most one root, `-c/b`.
        if b.abs() > 1e-12 {
            candidates.push(bezier_point(-c / b));
        }
    } else {
        let disc = b * b - 4.0 * a * c;
        if disc >= 0.0 {
            let sq = disc.sqrt();
            candidates.push(bezier_point((-b + sq) / (2.0 * a)));
            candidates.push(bezier_point((-b - sq) / (2.0 * a)));
        }
    }
    let min = candidates.iter().cloned().fold(f64::INFINITY, f64::min);
    let max = candidates.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
    (min, max)
}

/// `get_path_bbox`/`bezier_bbox` (`graphicBase.ml:88-127,148-171`) — the
/// EXACT bounding box of `path`: walks each subpath tracking the current
/// point (`start`; each `Line` contributes its endpoint; each
/// `Bezier(c1,c2,p)` contributes the cubic extrema of `(cur, c1, c2, p)`; a
/// `Closing::Bezier(c1,c2)` contributes the extrema of `(cur, c1, c2,
/// start)`), taking each axis's true curve extent via
/// `bezier_axis_extent` rather than the (looser) control-point hull.
pub fn path_bbox(path: &Path) -> (Point, Point) {
    fn include(bounds: &mut (f64, f64, f64, f64), p: Point) {
        bounds.0 = bounds.0.min(p.0 .0);
        bounds.1 = bounds.1.max(p.0 .0);
        bounds.2 = bounds.2.min(p.1 .0);
        bounds.3 = bounds.3.max(p.1 .0);
    }
    fn include_axis_extents(bounds: &mut (f64, f64, f64, f64), ex: (f64, f64), ey: (f64, f64)) {
        bounds.0 = bounds.0.min(ex.0);
        bounds.1 = bounds.1.max(ex.1);
        bounds.2 = bounds.2.min(ey.0);
        bounds.3 = bounds.3.max(ey.1);
    }
    // (min_x, max_x, min_y, max_y).
    let mut bounds = (f64::INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::NEG_INFINITY);
    for sub in &path.subpaths {
        include(&mut bounds, sub.start);
        let mut cur = sub.start;
        for seg in &sub.segs {
            match *seg {
                PathSeg::Line(p) => {
                    include(&mut bounds, p);
                    cur = p;
                }
                PathSeg::Bezier(c1, c2, p) => {
                    let ex = bezier_axis_extent(cur.0 .0, c1.0 .0, c2.0 .0, p.0 .0);
                    let ey = bezier_axis_extent(cur.1 .0, c1.1 .0, c2.1 .0, p.1 .0);
                    include_axis_extents(&mut bounds, ex, ey);
                    cur = p;
                }
            }
        }
        if let Closing::Bezier(c1, c2) = sub.closing {
            let ex = bezier_axis_extent(cur.0 .0, c1.0 .0, c2.0 .0, sub.start.0 .0);
            let ey = bezier_axis_extent(cur.1 .0, c1.1 .0, c2.1 .0, sub.start.1 .0);
            include_axis_extents(&mut bounds, ex, ey);
        }
    }
    let (min_x, max_x, min_y, max_y) = bounds;
    if min_x.is_infinite() {
        return ((Length::ZERO, Length::ZERO), (Length::ZERO, Length::ZERO));
    }
    (
        (Length(min_x), Length(min_y)),
        (Length(max_x), Length(max_y)),
    )
}

fn union_bbox((amin, amax): (Point, Point), (bmin, bmax): (Point, Point)) -> (Point, Point) {
    (
        (
            Length(amin.0 .0.min(bmin.0 .0)),
            Length(amin.1 .0.min(bmin.1 .0)),
        ),
        (
            Length(amax.0 .0.max(bmax.0 .0)),
            Length(amax.1 .0.max(bmax.1 .0)),
        ),
    )
}

/// `get-graphics-bbox : graphics -> point * point` (v0.0.6 vminst.ml:2466) /
/// `graphics -> option (point * point)` (dev-0-1-0 vminst.ml:2301, the
/// "version-blind fix") — `graphicD.ml`'s `get_bbox`/`get_element_bbox`,
/// ignoring stroke thickness (upstream's own documented simplification).
/// `Clip(paths, _)` returns the CLIP PATHS' own bbox, ignoring `contents`
/// (upstream `graphicD.ml:50-52` — deliberate: the clip boundary, not what is
/// inside it, bounds the visible ink). `Group` union-folds its children
/// (`graphicD.ml:61-74`); `None` for an empty `Group` or an empty top-level
/// list, which v0.0.6 could never produce.
pub fn graphics_bbox(elem: &GraphicsElem) -> Option<(Point, Point)> {
    match elem {
        GraphicsElem::Fill(_, p)
        | GraphicsElem::Stroke(_, _, p)
        | GraphicsElem::DashedStroke(_, _, _, p) => Some(path_bbox(p)),
        GraphicsElem::Text { pt, width, height, depth, transform, .. } => {
            match transform {
                // Upright run: the axis-aligned `[0,width]×[-depth, height]`
                // extent translated to `pt`.
                None => Some(((pt.0, pt.1 - *depth), (pt.0 + *width, pt.1 + *height))),
                // Rotated/scaled run: transform the four local corners, translate
                // by `pt`, take the axis-aligned hull — so a `rotate`d figbox
                // reserves the correct (rotated) inline size.
                Some(mat) => {
                    let corners = [
                        (Length::ZERO, -*depth),
                        (*width, -*depth),
                        (*width, *height),
                        (Length::ZERO, *height),
                    ];
                    let mut min = (f64::INFINITY, f64::INFINITY);
                    let mut max = (f64::NEG_INFINITY, f64::NEG_INFINITY);
                    for c in corners {
                        let t = linear_transform_point(*mat, c);
                        let (x, y) = (t.0 .0 + pt.0 .0, t.1 .0 + pt.1 .0);
                        min = (min.0.min(x), min.1.min(y));
                        max = (max.0.max(x), max.1.max(y));
                    }
                    Some((
                        (Length(min.0), Length(min.1)),
                        (Length(max.0), Length(max.1)),
                    ))
                }
            }
        }
        GraphicsElem::Clip(path, _) => Some(path_bbox(path)),
        GraphicsElem::Group(gs) => gs
            .iter()
            .filter_map(graphics_bbox)
            .reduce(union_bbox),
        // No ink: an anchor must not inflate its box's bbox (it is typically a
        // `0pt 0pt 0pt` `inline-graphics`).
        GraphicsElem::Destination { .. } => None,
    }
}

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

    fn rect(x0: f64, y0: f64, x1: f64, y1: f64) -> Path {
        Path {
            subpaths: vec![Subpath {
                start: (Length(x0), Length(y0)),
                segs: vec![
                    PathSeg::Line((Length(x1), Length(y0))),
                    PathSeg::Line((Length(x1), Length(y1))),
                    PathSeg::Line((Length(x0), Length(y1))),
                ],
                closing: Closing::Line,
            }],
        }
    }

    /// Over a `Clip`/`Group` both move the clip path AND the contents
    /// (the `graphicD.ml:38` recursing-arm contract).
    #[test]
    fn shift_and_transform_recurse_into_clip_and_group() {
        let fill = GraphicsElem::Fill(Color::Gray(0.0), rect(0.0, 0.0, 1.0, 1.0));
        let group = GraphicsElem::Group(vec![fill.clone(), fill.clone()]);
        let shifted_group = shift_graphics((Length(2.0), Length(3.0)), &group);
        match &shifted_group {
            GraphicsElem::Group(gs) => {
                assert_eq!(gs.len(), 2);
                for g in gs {
                    assert_eq!(
                        graphics_bbox(g),
                        Some(((Length(2.0), Length(3.0)), (Length(3.0), Length(4.0))))
                    );
                }
            }
            other => panic!("expected Group, got {other:?}"),
        }

        let clip = GraphicsElem::Clip(rect(0.0, 0.0, 5.0, 5.0), vec![fill.clone()]);
        let shifted_clip = shift_graphics((Length(1.0), Length(1.0)), &clip);
        match &shifted_clip {
            GraphicsElem::Clip(path, inner) => {
                assert_eq!(
                    path_bbox(path),
                    ((Length(1.0), Length(1.0)), (Length(6.0), Length(6.0)))
                );
                assert_eq!(
                    graphics_bbox(&inner[0]),
                    Some(((Length(1.0), Length(1.0)), (Length(2.0), Length(2.0))))
                );
            }
            other => panic!("expected Clip, got {other:?}"),
        }

        // `linear-transform-graphics` (scale by 2 on both axes) also
        // recurses into both the clip path AND the contents.
        let scaled_clip = linear_transform_graphics((2.0, 0.0, 0.0, 2.0), &clip);
        match &scaled_clip {
            GraphicsElem::Clip(path, inner) => {
                assert_eq!(
                    path_bbox(path),
                    ((Length(0.0), Length(0.0)), (Length(10.0), Length(10.0)))
                );
                assert_eq!(
                    graphics_bbox(&inner[0]),
                    Some(((Length(0.0), Length(0.0)), (Length(2.0), Length(2.0))))
                );
            }
            other => panic!("expected Clip, got {other:?}"),
        }
    }

    /// `get-graphics-bbox` `Option` semantics: an empty `Group` has no
    /// ink and returns `None`; a `Group` of two fills union-folds; a `Clip`
    /// returns the CLIP PATH's own bbox, ignoring `contents`.
    #[test]
    fn bbox_option_semantics() {
        assert_eq!(graphics_bbox(&GraphicsElem::Group(vec![])), None);

        let a = GraphicsElem::Fill(Color::Gray(0.0), rect(0.0, 0.0, 1.0, 1.0));
        let b = GraphicsElem::Fill(Color::Gray(0.0), rect(2.0, 2.0, 3.0, 3.0));
        let group = GraphicsElem::Group(vec![a.clone(), b.clone()]);
        assert_eq!(
            graphics_bbox(&group),
            Some(((Length(0.0), Length(0.0)), (Length(3.0), Length(3.0))))
        );

        let clip = GraphicsElem::Clip(rect(10.0, 10.0, 20.0, 20.0), vec![a]);
        assert_eq!(
            graphics_bbox(&clip),
            Some(((Length(10.0), Length(10.0)), (Length(20.0), Length(20.0))))
        );
    }
}