rustyfi_backend/graphics.rs
1//! The drawing data model — paths, colors, and `graphics` elements; the
2//! analog of upstream's `GraphicBase`/`PrePath`/`GraphicD`. Everything here is
3//! already-resolved coordinates/data: no lang-side closure or deferred
4//! computation crosses into this module.
5
6use crate::hbox::PureHorzBox;
7use crate::length::Length;
8
9/// A point in graphics space (upstream `point`; matches the runtime
10/// `Value::Tuple([Length, Length])` representation). Graphics space is
11/// y-**up** (PDF-native); the PDF writer's `place_graphics` flips
12/// page-layout's y-down convention when placing a graphics box on a line.
13pub type Point = (Length, Length);
14
15/// A dash pattern (`dashed-stroke`'s 2nd argument; upstream `graphicD.ml`'s
16/// `type dash = length * length * length`, `(d1, d2, d0)` = on-length,
17/// off-length, phase).
18pub type Dash = (Length, Length, Length);
19
20/// `color.satyh`'s `Gray`/`RGB`/`CMYK` after extraction by `as_color`
21/// (mirrors `evalUtil.ml`'s `get_color` → `DeviceGray`/`DeviceRGB`/
22/// `DeviceCMYK`).
23#[derive(Clone, Copy, Debug, PartialEq)]
24pub enum Color {
25 Gray(f64),
26 Rgb(f64, f64, f64),
27 Cmyk(f64, f64, f64, f64),
28}
29
30/// One path element: a control-point-free straight segment, or a cubic
31/// Bézier (2 control points + destination) — `graphicBase.ml`'s
32/// `point path_element`.
33#[derive(Clone, Copy, Debug, PartialEq)]
34pub enum PathSeg {
35 Line(Point),
36 Bezier(Point, Point, Point),
37}
38
39/// How a subpath closes (`graphicBase.ml`'s `path`'s `cycleopt`): left open,
40/// closed with a straight segment back to the start (`close-with-line`), or
41/// closed with a cubic (`close-with-bezier` — the destination is always the
42/// subpath's own `start`, so only the two control points are stored).
43#[derive(Clone, Copy, Debug, PartialEq)]
44pub enum Closing {
45 Open,
46 Line,
47 Bezier(Point, Point),
48}
49
50/// One `GraphicBase.GeneralPath(start, elems, closing)`.
51#[derive(Clone, Debug, PartialEq)]
52pub struct Subpath {
53 pub start: Point,
54 pub segs: Vec<PathSeg>,
55 pub closing: Closing,
56}
57
58/// The `path` value = upstream `path list` (`unite-path` appends subpath
59/// lists).
60#[derive(Clone, Debug, PartialEq)]
61pub struct Path {
62 pub subpaths: Vec<Subpath>,
63}
64
65/// The `pre-path` value (`PrePath.t`): a start point plus forward-accumulated
66/// segments, before a `terminate-path`/`close-with-line` fixes a closing.
67/// Upstream accumulates in reverse and flips at close time; this port pushes
68/// forward directly, which is unobservable.
69#[derive(Clone, Debug, PartialEq)]
70pub struct PrePath {
71 pub start: Point,
72 pub segs: Vec<PathSeg>,
73}
74
75/// One `graphics` element (`GraphicD.element`). `place_graphics`
76/// (rustyfi-pdf) matches this exhaustively, without a wildcard arm.
77///
78/// See [`crate::hbox::PureHorzBox`] for what the `#[subast]` list means and
79/// what checks it.
80#[derive(Clone, Debug, PartialEq, syan::visit::Ast)]
81#[subast(crate::graphics::GraphicsElem, crate::hbox::PureHorzBox)]
82pub enum GraphicsElem {
83 /// Filled region, even-odd rule (upstream's `op_f'`).
84 Fill(Color, Path),
85 /// Stroked outline at the given line width.
86 Stroke(Length, Color, Path),
87 /// Dashed stroked outline (`dashed-stroke`), rendered with a PDF `d`
88 /// dash-array op alongside the same stroke ops as `Stroke`.
89 DashedStroke(Length, Dash, Color, Path),
90 /// `draw-text`: a text run anchored at `pt` (box-local, y-up; the run's
91 /// leftmost baseline point). `contents` is the run laid out at NATURAL
92 /// width (upstream `LineBreak.natural` = `determine_widths None`,
93 /// `widperfil = 0`; here `fit_cell(boxes, natural_width)`), each box with
94 /// its x offset from `pt`. `width`/`height`/`depth` are the run's
95 /// `natural_metrics`, stored at construction so `graphics_bbox` needs no
96 /// re-measure. Rendered by each PDF writer re-entering its own per-box
97 /// emission at `pt + dx` INSIDE `place_graphics`'s box-local `cm` frame.
98 Text {
99 pt: Point,
100 contents: Vec<(Length, PureHorzBox)>,
101 width: Length,
102 height: Length,
103 depth: Length,
104 /// The accumulated 2×2 linear transform (`linear-transform-graphics`,
105 /// row-major `(a, b, c, d)` — same convention as
106 /// `linear_transform_point`) applied to the run about its local
107 /// origin BEFORE the `pt` translation. `None` means identity: the run
108 /// is drawn upright at `pt`. `Some` appears once
109 /// `rotate-graphics`/`scale-graphics` is composed onto a `draw-text`;
110 /// the writer then emits the run under a `cm` carrying this matrix
111 /// (upstream's lazy `LinearTrans` render-time `cm`).
112 transform: Option<(f64, f64, f64, f64)>,
113 },
114 /// 0.1 collection node (`GraphicD.concat`, dev-0-1-0 `graphicD.ml:23`):
115 /// `unite-graphics`' payload. No 0.0.6-visible primitive builds one, so it
116 /// is unreachable from 0.0.6 rendering by construction.
117 Group(Vec<GraphicsElem>),
118 /// 0.1 clip node (`GraphicD.make_clip`, `graphicD.ml:97-98`): render
119 /// `contents` clipped to `clip` (even-odd, `Op_W'` — `graphicD.ml:331`).
120 /// The port's `Path` already carries N subpaths, standing in for
121 /// upstream's `path list`. Never constructed by any 0.0.6 path, as `Group`.
122 Clip(Path, Vec<GraphicsElem>),
123 /// A DEFERRED `register-destination` call, carrying NO ink. `pt` is
124 /// box-local in the same y-**up** frame as every other element's
125 /// coordinates, so the existing transform pipeline carries it, and
126 /// `rustyfi-lang`'s `fire_hooks` replays it once the box has a page and a
127 /// placed point.
128 ///
129 /// It exists because this port applies an `inline-graphics` callback
130 /// eagerly at construction time (`prim_inline_graphics`) rather than during
131 /// page breaking as upstream does, so a `register-destination` inside one
132 /// has no page and `annotation.ml:15`'s gate — faithfully — refuses it.
133 ///
134 /// KNOWN GAP: `math_boxes_of_inline_boxes` (rustyfi-lang) harvests a
135 /// graphics box's elements into a `PureHorzBox::Math`'s `rules` and
136 /// `fire_hooks` has no `Math` arm, so an anchor inside a `make_paren`
137 /// delimiter closure never fires. `shift_graphics` carries the point
138 /// correctly, so closing it is one arm and no arithmetic.
139 Destination { key: String, pt: Point },
140}
141
142// `shift-path`/`shift-graphics`/`linear-transform-path`/
143// `linear-transform-graphics` are all EAGER point remaps — no lazy
144// `LinearTrans`-wrapper element: every point is rewritten up front, mirroring
145// `graphicBase.ml`'s `shift_path`/`linear_transform_path` (`(x, y) ->
146// (x*a + y*b, x*c + y*d)` for the 2x2 matrix `((a, b), (c, d))`).
147
148/// `shift_path v pt` (`graphicBase.ml`'s `(+@%)`).
149fn shift_point(v: Point, pt: Point) -> Point {
150 (pt.0 + v.0, pt.1 + v.1)
151}
152
153/// `graphicBase.ml`'s `linear_transform_point`: `(x, y) |-> (x*a + y*b, x*c +
154/// y*d)` for matrix `mat = (a, b, c, d)`.
155fn linear_transform_point(mat: (f64, f64, f64, f64), pt: Point) -> Point {
156 let (a, b, c, d) = mat;
157 (pt.0 * a + pt.1 * b, pt.0 * c + pt.1 * d)
158}
159
160/// Map `f` over every point of `path` (subpath starts, every segment's
161/// points — including Bézier control points — and any closing control
162/// points), preserving structure.
163fn map_path(path: &Path, f: impl Fn(Point) -> Point) -> Path {
164 Path {
165 subpaths: path
166 .subpaths
167 .iter()
168 .map(|sub| Subpath {
169 start: f(sub.start),
170 segs: sub
171 .segs
172 .iter()
173 .map(|seg| match *seg {
174 PathSeg::Line(p) => PathSeg::Line(f(p)),
175 PathSeg::Bezier(c1, c2, p) => PathSeg::Bezier(f(c1), f(c2), f(p)),
176 })
177 .collect(),
178 closing: match sub.closing {
179 Closing::Open => Closing::Open,
180 Closing::Line => Closing::Line,
181 Closing::Bezier(c1, c2) => Closing::Bezier(f(c1), f(c2)),
182 },
183 })
184 .collect(),
185 }
186}
187
188/// `shift-path : point -> path -> path` (vminst.ml:663) — translate every
189/// point of `path` by `v`.
190pub fn shift_path(v: Point, path: &Path) -> Path {
191 map_path(path, |p| shift_point(v, p))
192}
193
194/// `linear-transform-path : float -> float -> float -> float -> path ->
195/// path` (vminst.ml:678) — apply the 2x2 matrix `mat` to every point.
196pub fn linear_transform_path(mat: (f64, f64, f64, f64), path: &Path) -> Path {
197 map_path(path, |p| linear_transform_point(mat, p))
198}
199
200/// `shift-graphics : point -> graphics -> graphics` (vminst.ml:2451) —
201/// `graphicD.ml`'s `shift_element`.
202pub fn shift_graphics(v: Point, elem: &GraphicsElem) -> GraphicsElem {
203 match elem {
204 GraphicsElem::Fill(c, p) => GraphicsElem::Fill(*c, shift_path(v, p)),
205 GraphicsElem::Stroke(w, c, p) => GraphicsElem::Stroke(*w, *c, shift_path(v, p)),
206 GraphicsElem::DashedStroke(w, d, c, p) => {
207 GraphicsElem::DashedStroke(*w, *d, *c, shift_path(v, p))
208 }
209 GraphicsElem::Text { pt, contents, width, height, depth, transform } => {
210 GraphicsElem::Text {
211 pt: shift_point(v, *pt),
212 contents: contents.clone(),
213 width: *width,
214 height: *height,
215 depth: *depth,
216 // A pure translation leaves the run's own 2×2 transform intact
217 // (only `pt` moves) — the affine is `transform·l + pt`.
218 transform: *transform,
219 }
220 }
221 // `graphicD.ml:38`: `Group` maps every child; `Clip` shifts its own
222 // clip path AND recurses into its contents.
223 GraphicsElem::Group(gs) => {
224 GraphicsElem::Group(gs.iter().map(|g| shift_graphics(v, g)).collect())
225 }
226 GraphicsElem::Clip(path, gs) => GraphicsElem::Clip(
227 shift_path(v, path),
228 gs.iter().map(|g| shift_graphics(v, g)).collect(),
229 ),
230 // The anchor point is an ordinary box-local coordinate: it moves with
231 // the ink around it.
232 GraphicsElem::Destination { key, pt } => GraphicsElem::Destination {
233 key: key.clone(),
234 pt: shift_point(v, *pt),
235 },
236 }
237}
238
239/// `linear-transform-graphics : float -> float -> float -> float ->
240/// graphics -> graphics` (vminst.ml:2432) — `graphicD.ml`'s
241/// `make_linear_trans`, applied eagerly.
242pub fn linear_transform_graphics(mat: (f64, f64, f64, f64), elem: &GraphicsElem) -> GraphicsElem {
243 match elem {
244 GraphicsElem::Fill(c, p) => GraphicsElem::Fill(*c, linear_transform_path(mat, p)),
245 GraphicsElem::Stroke(w, c, p) => GraphicsElem::Stroke(*w, *c, linear_transform_path(mat, p)),
246 GraphicsElem::DashedStroke(w, d, c, p) => {
247 GraphicsElem::DashedStroke(*w, *d, *c, linear_transform_path(mat, p))
248 }
249 // A `draw-text` run carries the composed 2×2 matrix so the writer can
250 // rotate/scale the glyphs/image at render time (upstream's lazy
251 // `LinearTrans` `cm`). The affine is `transform·l + pt`; pre-composing
252 // `mat` gives `mat·(transform·l + pt) = (mat·transform)·l + mat·pt`, so
253 // `transform ↦ mat·transform` and `pt ↦ mat·pt`. Matrices are row-major
254 // `(a, b, c, d)` = `[[a, b], [c, d]]` (the `linear_transform_point`
255 // convention), so the product below is the standard 2×2 multiply.
256 GraphicsElem::Text { pt, contents, width, height, depth, transform } => {
257 let (ma, mb, mc, md) = mat;
258 let (ta, tb, tc, td) = transform.unwrap_or((1.0, 0.0, 0.0, 1.0));
259 let composed = (
260 ma * ta + mb * tc,
261 ma * tb + mb * td,
262 mc * ta + md * tc,
263 mc * tb + md * td,
264 );
265 GraphicsElem::Text {
266 pt: linear_transform_point(mat, *pt),
267 contents: contents.clone(),
268 width: *width,
269 height: *height,
270 depth: *depth,
271 transform: Some(composed),
272 }
273 }
274 GraphicsElem::Group(gs) => GraphicsElem::Group(
275 gs.iter().map(|g| linear_transform_graphics(mat, g)).collect(),
276 ),
277 GraphicsElem::Clip(path, gs) => GraphicsElem::Clip(
278 linear_transform_path(mat, path),
279 gs.iter().map(|g| linear_transform_graphics(mat, g)).collect(),
280 ),
281 // As in `shift_graphics`: an ordinary box-local coordinate.
282 GraphicsElem::Destination { key, pt } => GraphicsElem::Destination {
283 key: key.clone(),
284 pt: linear_transform_point(mat, *pt),
285 },
286 }
287}
288
289/// One axis (x or y) of a cubic Bézier's EXACT extrema (`graphicBase.ml:88`
290/// `bezier_bbox`'s per-axis `aux`): for the cubic from `r0` (current point)
291/// through controls `r1`, `r2` to `r3`, the derivative's roots give the
292/// interior extrema; candidates are `{r0, r3, B(t+), B(t-)}` with `t` clamped
293/// to `[0, 1]` (`bezier_point`'s convention: `t < 0` snaps to `r0`, `t > 1`
294/// snaps to `r3`). Returns `(min, max)` over that candidate set.
295fn bezier_axis_extent(r0: f64, r1: f64, r2: f64, r3: f64) -> (f64, f64) {
296 // B(t) = (1-t)^3 r0 + 3(1-t)^2 t r1 + 3(1-t) t^2 r2 + t^3 r3
297 // B'(t)/3 = a t^2 + b t + c, with:
298 let a = -r0 + 3.0 * (r1 - r2) + r3;
299 let b = 2.0 * (r0 - 2.0 * r1 + r2);
300 let c = r1 - r0;
301 let bezier_point = |t: f64| -> f64 {
302 if t < 0.0 {
303 r0
304 } else if t > 1.0 {
305 r3
306 } else {
307 let u = 1.0 - t;
308 u * u * u * r0 + 3.0 * u * u * t * r1 + 3.0 * u * t * t * r2 + t * t * t * r3
309 }
310 };
311 let mut candidates = vec![r0, r3];
312 if a.abs() < 1e-12 {
313 // Linear derivative (or degenerate): at most one root, `-c/b`.
314 if b.abs() > 1e-12 {
315 candidates.push(bezier_point(-c / b));
316 }
317 } else {
318 let disc = b * b - 4.0 * a * c;
319 if disc >= 0.0 {
320 let sq = disc.sqrt();
321 candidates.push(bezier_point((-b + sq) / (2.0 * a)));
322 candidates.push(bezier_point((-b - sq) / (2.0 * a)));
323 }
324 }
325 let min = candidates.iter().cloned().fold(f64::INFINITY, f64::min);
326 let max = candidates.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
327 (min, max)
328}
329
330/// `get_path_bbox`/`bezier_bbox` (`graphicBase.ml:88-127,148-171`) — the
331/// EXACT bounding box of `path`: walks each subpath tracking the current
332/// point (`start`; each `Line` contributes its endpoint; each
333/// `Bezier(c1,c2,p)` contributes the cubic extrema of `(cur, c1, c2, p)`; a
334/// `Closing::Bezier(c1,c2)` contributes the extrema of `(cur, c1, c2,
335/// start)`), taking each axis's true curve extent via
336/// `bezier_axis_extent` rather than the (looser) control-point hull.
337pub fn path_bbox(path: &Path) -> (Point, Point) {
338 fn include(bounds: &mut (f64, f64, f64, f64), p: Point) {
339 bounds.0 = bounds.0.min(p.0 .0);
340 bounds.1 = bounds.1.max(p.0 .0);
341 bounds.2 = bounds.2.min(p.1 .0);
342 bounds.3 = bounds.3.max(p.1 .0);
343 }
344 fn include_axis_extents(bounds: &mut (f64, f64, f64, f64), ex: (f64, f64), ey: (f64, f64)) {
345 bounds.0 = bounds.0.min(ex.0);
346 bounds.1 = bounds.1.max(ex.1);
347 bounds.2 = bounds.2.min(ey.0);
348 bounds.3 = bounds.3.max(ey.1);
349 }
350 // (min_x, max_x, min_y, max_y).
351 let mut bounds = (f64::INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::NEG_INFINITY);
352 for sub in &path.subpaths {
353 include(&mut bounds, sub.start);
354 let mut cur = sub.start;
355 for seg in &sub.segs {
356 match *seg {
357 PathSeg::Line(p) => {
358 include(&mut bounds, p);
359 cur = p;
360 }
361 PathSeg::Bezier(c1, c2, p) => {
362 let ex = bezier_axis_extent(cur.0 .0, c1.0 .0, c2.0 .0, p.0 .0);
363 let ey = bezier_axis_extent(cur.1 .0, c1.1 .0, c2.1 .0, p.1 .0);
364 include_axis_extents(&mut bounds, ex, ey);
365 cur = p;
366 }
367 }
368 }
369 if let Closing::Bezier(c1, c2) = sub.closing {
370 let ex = bezier_axis_extent(cur.0 .0, c1.0 .0, c2.0 .0, sub.start.0 .0);
371 let ey = bezier_axis_extent(cur.1 .0, c1.1 .0, c2.1 .0, sub.start.1 .0);
372 include_axis_extents(&mut bounds, ex, ey);
373 }
374 }
375 let (min_x, max_x, min_y, max_y) = bounds;
376 if min_x.is_infinite() {
377 return ((Length::ZERO, Length::ZERO), (Length::ZERO, Length::ZERO));
378 }
379 (
380 (Length(min_x), Length(min_y)),
381 (Length(max_x), Length(max_y)),
382 )
383}
384
385fn union_bbox((amin, amax): (Point, Point), (bmin, bmax): (Point, Point)) -> (Point, Point) {
386 (
387 (
388 Length(amin.0 .0.min(bmin.0 .0)),
389 Length(amin.1 .0.min(bmin.1 .0)),
390 ),
391 (
392 Length(amax.0 .0.max(bmax.0 .0)),
393 Length(amax.1 .0.max(bmax.1 .0)),
394 ),
395 )
396}
397
398/// `get-graphics-bbox : graphics -> point * point` (v0.0.6 vminst.ml:2466) /
399/// `graphics -> option (point * point)` (dev-0-1-0 vminst.ml:2301, the
400/// "version-blind fix") — `graphicD.ml`'s `get_bbox`/`get_element_bbox`,
401/// ignoring stroke thickness (upstream's own documented simplification).
402/// `Clip(paths, _)` returns the CLIP PATHS' own bbox, ignoring `contents`
403/// (upstream `graphicD.ml:50-52` — deliberate: the clip boundary, not what is
404/// inside it, bounds the visible ink). `Group` union-folds its children
405/// (`graphicD.ml:61-74`); `None` for an empty `Group` or an empty top-level
406/// list, which v0.0.6 could never produce.
407pub fn graphics_bbox(elem: &GraphicsElem) -> Option<(Point, Point)> {
408 match elem {
409 GraphicsElem::Fill(_, p)
410 | GraphicsElem::Stroke(_, _, p)
411 | GraphicsElem::DashedStroke(_, _, _, p) => Some(path_bbox(p)),
412 GraphicsElem::Text { pt, width, height, depth, transform, .. } => {
413 match transform {
414 // Upright run: the axis-aligned `[0,width]×[-depth, height]`
415 // extent translated to `pt`.
416 None => Some(((pt.0, pt.1 - *depth), (pt.0 + *width, pt.1 + *height))),
417 // Rotated/scaled run: transform the four local corners, translate
418 // by `pt`, take the axis-aligned hull — so a `rotate`d figbox
419 // reserves the correct (rotated) inline size.
420 Some(mat) => {
421 let corners = [
422 (Length::ZERO, -*depth),
423 (*width, -*depth),
424 (*width, *height),
425 (Length::ZERO, *height),
426 ];
427 let mut min = (f64::INFINITY, f64::INFINITY);
428 let mut max = (f64::NEG_INFINITY, f64::NEG_INFINITY);
429 for c in corners {
430 let t = linear_transform_point(*mat, c);
431 let (x, y) = (t.0 .0 + pt.0 .0, t.1 .0 + pt.1 .0);
432 min = (min.0.min(x), min.1.min(y));
433 max = (max.0.max(x), max.1.max(y));
434 }
435 Some((
436 (Length(min.0), Length(min.1)),
437 (Length(max.0), Length(max.1)),
438 ))
439 }
440 }
441 }
442 GraphicsElem::Clip(path, _) => Some(path_bbox(path)),
443 GraphicsElem::Group(gs) => gs
444 .iter()
445 .filter_map(graphics_bbox)
446 .reduce(union_bbox),
447 // No ink: an anchor must not inflate its box's bbox (it is typically a
448 // `0pt 0pt 0pt` `inline-graphics`).
449 GraphicsElem::Destination { .. } => None,
450 }
451}
452
453#[cfg(test)]
454mod tests {
455 use super::*;
456
457 fn rect(x0: f64, y0: f64, x1: f64, y1: f64) -> Path {
458 Path {
459 subpaths: vec![Subpath {
460 start: (Length(x0), Length(y0)),
461 segs: vec![
462 PathSeg::Line((Length(x1), Length(y0))),
463 PathSeg::Line((Length(x1), Length(y1))),
464 PathSeg::Line((Length(x0), Length(y1))),
465 ],
466 closing: Closing::Line,
467 }],
468 }
469 }
470
471 /// Over a `Clip`/`Group` both move the clip path AND the contents
472 /// (the `graphicD.ml:38` recursing-arm contract).
473 #[test]
474 fn shift_and_transform_recurse_into_clip_and_group() {
475 let fill = GraphicsElem::Fill(Color::Gray(0.0), rect(0.0, 0.0, 1.0, 1.0));
476 let group = GraphicsElem::Group(vec![fill.clone(), fill.clone()]);
477 let shifted_group = shift_graphics((Length(2.0), Length(3.0)), &group);
478 match &shifted_group {
479 GraphicsElem::Group(gs) => {
480 assert_eq!(gs.len(), 2);
481 for g in gs {
482 assert_eq!(
483 graphics_bbox(g),
484 Some(((Length(2.0), Length(3.0)), (Length(3.0), Length(4.0))))
485 );
486 }
487 }
488 other => panic!("expected Group, got {other:?}"),
489 }
490
491 let clip = GraphicsElem::Clip(rect(0.0, 0.0, 5.0, 5.0), vec![fill.clone()]);
492 let shifted_clip = shift_graphics((Length(1.0), Length(1.0)), &clip);
493 match &shifted_clip {
494 GraphicsElem::Clip(path, inner) => {
495 assert_eq!(
496 path_bbox(path),
497 ((Length(1.0), Length(1.0)), (Length(6.0), Length(6.0)))
498 );
499 assert_eq!(
500 graphics_bbox(&inner[0]),
501 Some(((Length(1.0), Length(1.0)), (Length(2.0), Length(2.0))))
502 );
503 }
504 other => panic!("expected Clip, got {other:?}"),
505 }
506
507 // `linear-transform-graphics` (scale by 2 on both axes) also
508 // recurses into both the clip path AND the contents.
509 let scaled_clip = linear_transform_graphics((2.0, 0.0, 0.0, 2.0), &clip);
510 match &scaled_clip {
511 GraphicsElem::Clip(path, inner) => {
512 assert_eq!(
513 path_bbox(path),
514 ((Length(0.0), Length(0.0)), (Length(10.0), Length(10.0)))
515 );
516 assert_eq!(
517 graphics_bbox(&inner[0]),
518 Some(((Length(0.0), Length(0.0)), (Length(2.0), Length(2.0))))
519 );
520 }
521 other => panic!("expected Clip, got {other:?}"),
522 }
523 }
524
525 /// `get-graphics-bbox` `Option` semantics: an empty `Group` has no
526 /// ink and returns `None`; a `Group` of two fills union-folds; a `Clip`
527 /// returns the CLIP PATH's own bbox, ignoring `contents`.
528 #[test]
529 fn bbox_option_semantics() {
530 assert_eq!(graphics_bbox(&GraphicsElem::Group(vec![])), None);
531
532 let a = GraphicsElem::Fill(Color::Gray(0.0), rect(0.0, 0.0, 1.0, 1.0));
533 let b = GraphicsElem::Fill(Color::Gray(0.0), rect(2.0, 2.0, 3.0, 3.0));
534 let group = GraphicsElem::Group(vec![a.clone(), b.clone()]);
535 assert_eq!(
536 graphics_bbox(&group),
537 Some(((Length(0.0), Length(0.0)), (Length(3.0), Length(3.0))))
538 );
539
540 let clip = GraphicsElem::Clip(rect(10.0, 10.0, 20.0, 20.0), vec![a]);
541 assert_eq!(
542 graphics_bbox(&clip),
543 Some(((Length(10.0), Length(10.0)), (Length(20.0), Length(20.0))))
544 );
545 }
546}