lievisual 0.3.3

Declarative visual scene IR + pluggable rendering backends (SVG / vello_cpu / GPU).
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
//! Content-bounds computation and "fit the canvas to the content" helpers.
//!
//! Two complementary pieces:
//!
//! - **Bounds**: [`element_bounds`] / [`node_bounds`] / [`nodes_bounds`] / [`scene_bounds`]
//!   compute the geometric bounding box of IR data. These belong in lievisual because the
//!   traversal must know every [`Element`] variant, how [`Transform`] composes, and how a
//!   [`TextLayout`](crate::text::TextLayout) is measured — knowledge that only exists here.
//! - **Fit**: [`fit_scene`] shrinks / moves an already-built [`Scene`] so the canvas hugs its
//!   content, optionally constrained by a maximum size. This is what "content-driven canvas"
//!   renderers (diagram / chart exporters) need, and it used to be re-implemented downstream.
//!
//! # What counts as content
//!
//! - Strokes: bounds are inflated by half the stroke width, so a thick outline is not clipped.
//! - Node / layer transforms: applied when walking the tree, so the result is in the same
//!   coordinate space the backends draw in.
//! - `visible = false` subtrees are skipped (they draw nothing, so they should not reserve space).
//! - Clips are **not** applied: a clip only *removes* pixels, it never adds any, so ignoring it
//!   keeps the box conservative (never too small).
//!
//! # Example
//!
//! ```
//! use lievisual::{Color, Point, Rect, Scene};
//! use lievisual::fit::{fit_scene, FitOptions};
//! use lievisual::scene::{Element, FillStrokeStyle};
//!
//! let mut scene = Scene::new(0.0, 0.0);
//! scene.push(Element::rect(
//!     Rect::new(20.0, 30.0, 120.0, 80.0),
//!     FillStrokeStyle::fill(Color::BLACK),
//! ));
//! // Hug the content with a 8pt margin, never upscaling.
//! fit_scene(&mut scene, FitOptions::new().with_margin(8.0));
//! assert_eq!(scene.width, 116.0);
//! assert_eq!(scene.height, 66.0);
//! ```

use kurbo::Shape;

use crate::geometry::{Point, Rect, Size, Transform, Vec2};
use crate::scene::{Element, NodeKind, Scene, SceneNode, Stroke};

/// Bounding box of a single primitive, in the element's own coordinate space.
///
/// Returns `None` for primitives that occupy no area (an empty polyline / path, a degenerate
/// rect, ...). Node-level attributes (`transform` / `clip` / `visible`) are **not** considered
/// here; use [`node_bounds`] for that.
pub fn element_bounds(element: &Element) -> Option<Rect> {
    let box_ = match element {
        Element::Rect { rect, style } => {
            let mut r = normalized(*rect);
            if let Some(pad) = stroke_inset(style.stroke.as_ref()) {
                r = r.inflate(pad, pad);
            }
            Some(r)
        }
        Element::RoundedRect {
            rect,
            radii: _,
            style,
        } => {
            let mut r = normalized(*rect);
            if let Some(pad) = stroke_inset(style.stroke.as_ref()) {
                r = r.inflate(pad, pad);
            }
            Some(r)
        }
        Element::Circle {
            center,
            radius,
            style,
        } => circle_box(*center, *radius, style.stroke.as_ref()),
        Element::Ellipse {
            center,
            radii,
            rotation,
            style,
        } => {
            // kurbo's bounding_box handles the rotation exactly (via the derivative), so a
            // rotated ellipse does not get the over-large "axis-aligned radii" box.
            let e = kurbo::Ellipse::new(
                (center.x, center.y),
                (radii.x.abs(), radii.y.abs()),
                *rotation,
            );
            inflate_opt(e.bounding_box(), style.stroke.as_ref())
        }
        Element::Line { start, end, style } => {
            let r = Rect::from_points(*start, *end);
            inflate_opt(r, Some(style))
        }
        Element::Polyline { points, style } => points_box(points).map(|r| inflate(r, Some(style))),
        Element::Polygon { points, style } => {
            points_box(points).map(|r| inflate(r, style.stroke.as_ref()))
        }
        Element::Arc {
            center,
            radii,
            start_angle,
            sweep_angle,
            style,
        } => inflate_opt(
            arc_box(*center, *radii, *start_angle, *sweep_angle),
            Some(style),
        ),
        Element::Pie {
            center,
            radius,
            start_angle: _,
            sweep_angle: _,
            style,
        } => circle_box(*center, *radius, style.stroke.as_ref()),
        Element::Path { path, style, .. } => {
            inflate_opt(path.bounding_box(), style.stroke.as_ref())
        }
        Element::GradientPath {
            path,
            gradient: _,
            stroke,
        } => inflate_opt(path.bounding_box(), stroke.as_ref()),
        Element::Image {
            image,
            frame,
            opacity: _,
        } => {
            // `Contain` / `Cover` / `Fill` all stay inside `frame` (`Cover` is clipped to it);
            // `None` places the bitmap at its **original pixel size** from the frame's top-left,
            // which may overflow the frame — union both to stay conservative.
            let mut r = normalized(*frame);
            if image.object_fit == crate::scene::ObjectFit::None {
                r = r.union(Rect::from_origin_size(
                    Point::new(r.min_x(), r.min_y()),
                    Size::new(image.width() as f64, image.height() as f64),
                ));
            }
            Some(r)
        }
        Element::Text {
            spans,
            position,
            style,
            layout,
        } => text_box(spans, *position, style, layout.as_deref()),
    };
    // Drop degenerate / non-finite boxes so a single pathological primitive cannot poison the
    // whole canvas transform.
    box_.filter(|r| r.is_finite() && r.width() >= 0.0 && r.height() >= 0.0)
}

fn inflate_opt(r: Rect, stroke: Option<&Stroke>) -> Option<Rect> {
    Some(inflate(r, stroke))
}

/// Inflate by half the stroke width (strokes are centred on the geometry).
fn inflate(r: Rect, stroke: Option<&Stroke>) -> Rect {
    match stroke_inset(stroke) {
        Some(pad) => r.inflate(pad, pad),
        None => r,
    }
}

/// Half the stroke width, or `None` when there is nothing to pad for.
fn stroke_inset(stroke: Option<&Stroke>) -> Option<f64> {
    let w = stroke.map(|s| s.width).unwrap_or(0.0);
    if w.is_finite() && w > 0.0 {
        Some(w / 2.0)
    } else {
        None
    }
}

/// A `Rect` may be given with `min > max`; normalize so the box is never empty.
fn normalized(r: Rect) -> Rect {
    Rect::new(
        r.min_x().min(r.max_x()),
        r.min_y().min(r.max_y()),
        r.max_x().max(r.min_x()),
        r.max_y().max(r.min_y()),
    )
}

fn circle_box(center: Point, radius: f64, stroke: Option<&Stroke>) -> Option<Rect> {
    let r = radius.abs();
    if !r.is_finite() {
        return None;
    }
    inflate_opt(
        Rect::new(center.x - r, center.y - r, center.x + r, center.y + r),
        stroke,
    )
}

/// Union of an iterator of boxes; `None` when empty.
fn union_all(boxes: impl IntoIterator<Item = Rect>) -> Option<Rect> {
    boxes.into_iter().reduce(|a, b| a.union(b))
}

fn points_box(points: &[Point]) -> Option<Rect> {
    union_all(
        points
            .iter()
            .filter(|p| p.is_finite())
            .map(|p| Rect::from_points(*p, *p)),
    )
}

/// Union of a single point expressed as a zero-size box.
fn pt_box(p: Point) -> Rect {
    Rect::from_points(p, p)
}

/// Exact bounding box of an (elliptical) arc: endpoints plus any on-curve extrema.
///
/// The conservative "full ellipse" box is correct but wasteful — for a pie chart's 30° slice it
/// would reserve the whole circle and leave a huge empty canvas. `x` extrema sit where
/// `cos θ = ±1` (θ = 0 / π) and `y` extrema where `sin θ = ±1` (θ = ±π/2), so it suffices to
/// collect the two endpoints plus whichever of those four angles fall inside the swept range.
fn arc_box(center: Point, radii: Vec2, start_angle: f64, sweep_angle: f64) -> Rect {
    let rx = radii.x.abs();
    let ry = radii.y.abs();
    let at = |theta: f64| Point::new(center.x + rx * theta.cos(), center.y + ry * theta.sin());
    let (lo, hi) = if sweep_angle >= 0.0 {
        (start_angle, start_angle + sweep_angle)
    } else {
        (start_angle + sweep_angle, start_angle)
    };
    let mut pts = vec![at(start_angle), at(start_angle + sweep_angle)];
    if lo.is_finite() && hi.is_finite() {
        for k in [
            0.0,
            std::f64::consts::PI,
            std::f64::consts::FRAC_PI_2,
            -std::f64::consts::FRAC_PI_2,
        ] {
            // Smallest θ ≡ k (mod 2π) that is ≥ lo; keep it only if it is still ≤ hi.
            let n = ((lo - k) / std::f64::consts::TAU).ceil();
            let theta = k + n * std::f64::consts::TAU;
            if theta <= hi {
                pts.push(at(theta));
            }
        }
    }
    union_all(pts.into_iter().filter(|p| p.is_finite()).map(pt_box))
        .unwrap_or_else(|| pt_box(center))
}

/// Bounding box of a text element, honouring `align` / `baseline` / `rotation` / `baseline_shift`.
fn text_box(
    spans: &[crate::text::RichSpan],
    position: Point,
    style: &crate::text::TextStyle,
    layout: Option<&crate::text::TextLayout>,
) -> Option<Rect> {
    // Prefer a pre-laid-out result; otherwise measure (only when fitting, so the cost is paid
    // once per export rather than once per frame).
    //
    // Both backends position the block vertically via `TextBaseline::anchor_offset` against
    // the **real** first-line metrics (alphabetic baseline, em-box center, ink height for
    // Middle/Bottom). Reuse the same metric here instead of font-size proportions — the old
    // `h * 0.8` estimate drifted from the rendered position for multi-line text or fonts with
    // large descenders, biasing the fit margin on one side.
    let m = match layout {
        Some(l) => crate::text::layout_metrics(l),
        None => crate::text::measure_text(spans, style.max_width).metrics,
    };
    let (w, h) = (m.width, m.height);
    if !w.is_finite() || !h.is_finite() || w <= 0.0 || h <= 0.0 {
        return None;
    }
    let (x0, x1) = match style.align {
        crate::text::TextAlign::Left | crate::text::TextAlign::Justify => {
            (position.x, position.x + w)
        }
        crate::text::TextAlign::Center => (position.x - w / 2.0, position.x + w / 2.0),
        crate::text::TextAlign::Right => (position.x - w, position.x),
    };
    // Vertical: block top sits `anchor_offset` above the anchor, and the block extends
    // `h` downward (y-down). `baseline_shift` shifts the whole block along y.
    let shift = style.baseline_shift;
    let block_top = position.y - style.baseline.anchor_offset(&m);
    let (y0, y1) = (block_top + shift, block_top + h + shift);
    let mut r = Rect::new(x0, y0, x1, y1);
    // Rotation is about `position`: rotate the four corners and re-fit an axis-aligned box.
    if style.rotation != 0.0 && style.rotation.is_finite() {
        let rot = Transform::rotate(style.rotation);
        r = transform_box_about(&rot, r, position)?;
    }
    Some(r)
}

/// Bounding box of a node, with its own `transform` applied.
///
/// `visible = false` yields `None` (nothing is drawn). `clip` is ignored on purpose — it can only
/// shrink the visible area, so skipping it keeps the result conservative.
pub fn node_bounds(node: &SceneNode) -> Option<Rect> {
    if !node.visible {
        return None;
    }
    // A container's box is the union of its children (each child recurses through this same
    // function, so `visible = false` subtrees are skipped); a leaf delegates to `element_bounds`.
    let local = match &node.kind {
        NodeKind::Draw(element) => element_bounds(element)?,
        NodeKind::Group(children) => nodes_bounds(children)?,
    };
    Some(match node.transform.as_ref() {
        Some(t) => transform_box(t, local),
        None => local,
    })
}

/// Bounding box of a node slice (all boxes unioned).
pub fn nodes_bounds(nodes: &[SceneNode]) -> Option<Rect> {
    union_all(nodes.iter().filter_map(node_bounds))
}

/// Bounding box of a whole scene: the default `nodes` layer **and** every visible named layer.
///
/// Uses [`Scene::root_lists`] so the default list and layers share one traversal (a layer's
/// `transform` is applied to its union).
pub fn scene_bounds(scene: &Scene) -> Option<Rect> {
    union_all(scene.root_lists().filter_map(|(nodes, t)| {
        let b = nodes_bounds(nodes)?;
        Some(match t {
            Some(t) => transform_box(&t, b),
            None => b,
        })
    }))
}

/// Map an axis-aligned box through an affine transform: transform the four corners, then re-fit.
fn transform_box(t: &Transform, r: Rect) -> Rect {
    // `apply_point` can yield NaN for a degenerate matrix; fall back to the untouched box so one
    // pathological node cannot poison the whole fit.
    transform_box_about(t, r, Point::ZERO).unwrap_or(r)
}

/// Same as [`transform_box`], but the transform is applied about `pivot` rather than the origin.
fn transform_box_about(t: &Transform, r: Rect, pivot: Point) -> Option<Rect> {
    let corners = [
        Point::new(r.min_x(), r.min_y()),
        Point::new(r.max_x(), r.min_y()),
        Point::new(r.max_x(), r.max_y()),
        Point::new(r.min_x(), r.max_y()),
    ]
    .into_iter()
    .map(|c| {
        let p = t.apply_point(Point::new(c.x - pivot.x, c.y - pivot.y));
        Point::new(p.x + pivot.x, p.y + pivot.y)
    });
    union_all(corners.filter(|p| p.is_finite()).map(pt_box))
}

/// Options for [`fit_scene`].
#[derive(Debug, Clone)]
pub struct FitOptions {
    /// Maximum canvas width, **including** the margin. `None` = unconstrained on this axis.
    pub max_width: Option<f64>,
    /// Maximum canvas height, **including** the margin. `None` = unconstrained on this axis.
    pub max_height: Option<f64>,
    /// Padding kept on all four sides of the content.
    pub margin: f64,
    /// When `false` (default) the constraints are an **upper bound** and content is never
    /// enlarged — matching mermaid's "canvas hugs the content" output. Set `true` when the
    /// target is a bitmap that needs a minimum pixel size.
    pub upscale: bool,
    /// Canvas size used when the scene has no measurable content (empty, or all coordinates
    /// non-finite). `None` leaves `scene.width` / `scene.height` untouched.
    pub empty_size: Option<Size>,
}

impl Default for FitOptions {
    fn default() -> Self {
        Self {
            max_width: None,
            max_height: None,
            margin: 0.0,
            upscale: false,
            empty_size: None,
        }
    }
}

impl FitOptions {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Maximum canvas width (including margin).
    #[must_use]
    pub fn with_max_width(mut self, w: f64) -> Self {
        self.max_width = Some(w);
        self
    }

    /// Maximum canvas height (including margin).
    #[must_use]
    pub fn with_max_height(mut self, h: f64) -> Self {
        self.max_height = Some(h);
        self
    }

    /// Padding on all four sides.
    #[must_use]
    pub fn with_margin(mut self, margin: f64) -> Self {
        self.margin = margin;
        self
    }

    /// Allow enlarging the content to fill the constraint.
    #[must_use]
    pub fn with_upscale(mut self, upscale: bool) -> Self {
        self.upscale = upscale;
        self
    }

    /// Canvas size for an empty scene.
    #[must_use]
    pub fn with_empty_size(mut self, size: Size) -> Self {
        self.empty_size = Some(size);
        self
    }
}

/// Resize a scene so its canvas hugs the content, then move the content to `(margin, margin)`.
///
/// The content is scaled **uniformly** by `min(avail_w / content_w, avail_h / content_h)` and
/// clamped to `1.0` unless [`FitOptions::upscale`] is set, so the canvas is
/// `content * scale + 2 * margin` on each axis.
///
/// Returns `true` when the scene was adjusted. When the resulting transform is (near) the
/// identity it is skipped entirely, leaving the node tree untouched — only `width` / `height`
/// change.
pub fn fit_scene(scene: &mut Scene, opts: FitOptions) -> bool {
    // A negative / non-finite margin has no geometric meaning; clamp to 0.
    let margin = if opts.margin.is_finite() && opts.margin > 0.0 {
        opts.margin
    } else {
        0.0
    };

    let Some(bbox) = scene_bounds(scene).filter(|r| r.is_finite()) else {
        if let Some(size) = opts.empty_size {
            scene.width = size.width;
            scene.height = size.height;
            return true;
        }
        return false;
    };

    // Guard against zero-area content: a null box would make `scale` infinite.
    let content_w = (bbox.width()).max(1.0);
    let content_h = (bbox.height()).max(1.0);

    // Available area, at least 1pt — a max size smaller than 2×margin must not produce a
    // negative or zero scale (which previously emitted `viewBox="0 0 0 -20.41"`).
    let avail_w =
        sanitize_max(opts.max_width).map_or(f64::INFINITY, |w| (w - 2.0 * margin).max(1.0));
    let avail_h =
        sanitize_max(opts.max_height).map_or(f64::INFINITY, |h| (h - 2.0 * margin).max(1.0));

    // Unconstrained on both axes → no meaningful upscale target, so keep the natural size.
    let natural = (avail_w / content_w).min(avail_h / content_h);
    let natural = if natural.is_finite() { natural } else { 1.0 };
    let max_scale = if opts.upscale { f64::MAX } else { 1.0 };
    let scale = natural.clamp(f64::MIN_POSITIVE, max_scale);

    scene.width = content_w * scale + 2.0 * margin;
    scene.height = content_h * scale + 2.0 * margin;

    let offset_x = margin - bbox.min_x() * scale;
    let offset_y = margin - bbox.min_y() * scale;

    // Sub-pixel shift and unit scale: wrapping in a Group would only add indirection.
    if offset_x.abs() < 0.5 && offset_y.abs() < 0.5 && (scale - 1.0).abs() < 1e-3 {
        return true;
    }

    let fit = Transform::translate(offset_x, offset_y).then(&Transform::scale(scale));

    // Wrap the default layer; each named layer gets the transform composed onto its own
    // (layer-local transform first, then the fit), so layers and default nodes stay aligned.
    if !scene.nodes.is_empty() {
        let children = std::mem::take(&mut scene.nodes);
        scene
            .nodes
            .push(SceneNode::group(children).with_transform(fit));
    }
    for layer in &mut scene.layers {
        layer.transform = Some(match layer.transform.take() {
            Some(t) => t.then(&fit),
            None => fit,
        });
    }
    true
}

/// Drop `None` / non-finite / non-positive max sizes.
fn sanitize_max(v: Option<f64>) -> Option<f64> {
    v.filter(|v| v.is_finite() && *v > 0.0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::Color;
    use crate::scene::{Element, FillStrokeStyle, Layer, ObjectFit, SceneImage};
    use crate::text::TextStyle;

    fn rect_node(x0: f64, y0: f64, x1: f64, y1: f64) -> SceneNode {
        SceneNode::new(Element::rect(
            Rect::new(x0, y0, x1, y1),
            FillStrokeStyle::default(),
        ))
    }

    fn scene_with(nodes: Vec<SceneNode>) -> Scene {
        let mut s = Scene::new(0.0, 0.0);
        s.nodes = nodes;
        s
    }

    // ——— bounds ———

    #[test]
    fn element_bounds_includes_stroke_half_width() {
        // 100×100 rect, 10pt stroke → the outline extends 5pt beyond the geometry.
        let el = Element::rect(
            Rect::new(0.0, 0.0, 100.0, 100.0),
            FillStrokeStyle {
                fill: None,
                stroke: Some(Stroke::new(Color::BLACK, 10.0)),
            },
        );
        let b = element_bounds(&el).unwrap();
        assert_eq!((b.min_x(), b.min_y()), (-5.0, -5.0));
        assert_eq!((b.max_x(), b.max_y()), (105.0, 105.0));
    }

    /// 缺陷回归:下游实现完全忽略 stroke 宽度,粗描边图形会被画布裁掉半个线宽。
    #[test]
    fn line_bounds_includes_stroke_half_width() {
        let el = Element::Line {
            start: Point::new(0.0, 0.0),
            end: Point::new(100.0, 0.0),
            style: Stroke::new(Color::BLACK, 20.0),
        };
        let b = element_bounds(&el).unwrap();
        assert_eq!(b.min_y(), -10.0, "水平线应上下各扩展半个线宽");
        assert_eq!(b.max_y(), 10.0);
    }

    /// 缺陷回归:下游实现忽略节点 transform,算出的是未变换坐标。
    #[test]
    fn node_bounds_applies_transform() {
        let node =
            rect_node(0.0, 0.0, 10.0, 10.0).with_transform(Transform::translate(100.0, 50.0));
        let b = node_bounds(&node).unwrap();
        assert_eq!((b.min_x(), b.min_y()), (100.0, 50.0));
        assert_eq!((b.max_x(), b.max_y()), (110.0, 60.0));
    }

    #[test]
    fn node_bounds_applies_rotation() {
        // 正方形绕原点旋转 90°:应落在 x∈[-10,0], y∈[0,10]。
        let node = rect_node(0.0, 0.0, 10.0, 10.0)
            .with_transform(Transform::rotate(std::f64::consts::FRAC_PI_2));
        let b = node_bounds(&node).unwrap();
        assert!((b.min_x() + 10.0).abs() < 1e-9, "got {b:?}");
        assert!((b.max_x()).abs() < 1e-9, "got {b:?}");
        assert!((b.min_y()).abs() < 1e-9, "got {b:?}");
        assert!((b.max_y() - 10.0).abs() < 1e-9, "got {b:?}");
    }

    #[test]
    fn invisible_nodes_are_skipped() {
        let node = rect_node(0.0, 0.0, 1000.0, 1000.0).with_visible(false);
        assert_eq!(node_bounds(&node), None);
        assert_eq!(nodes_bounds(&[node]), None);
    }

    #[test]
    fn group_bounds_unions_children() {
        let g = SceneNode::group(vec![
            rect_node(0.0, 0.0, 10.0, 10.0),
            rect_node(90.0, 40.0, 100.0, 50.0),
        ]);
        let b = node_bounds(&g).unwrap();
        assert_eq!(
            (b.min_x(), b.min_y(), b.max_x(), b.max_y()),
            (0.0, 0.0, 100.0, 50.0)
        );
    }

    /// 弧的包围盒应只覆盖扫过的部分(用极值点精确求解),而非整个椭圆。
    #[test]
    fn arc_bounds_covers_only_the_swept_range() {
        // 第一象限的四分之一圆弧:应只到 (10,10),不覆盖整个圆。
        let b = arc_box(
            Point::new(0.0, 0.0),
            Vec2::new(10.0, 10.0),
            0.0,
            std::f64::consts::FRAC_PI_2,
        );
        assert!((b.min_x() - 0.0).abs() < 1e-9, "got {b:?}");
        assert!((b.min_y() - 0.0).abs() < 1e-9, "got {b:?}");
        assert!((b.max_x() - 10.0).abs() < 1e-9, "got {b:?}");
        assert!((b.max_y() - 10.0).abs() < 1e-9, "got {b:?}");

        // 下半圆(0 → π,y 恒 ≥ 0 在 y-down 坐标系下)不应含 y = -10 的极值点。
        let half = arc_box(
            Point::new(0.0, 0.0),
            Vec2::new(10.0, 10.0),
            0.0,
            std::f64::consts::PI,
        );
        assert!((half.min_y() - 0.0).abs() < 1e-9, "got {half:?}");
        assert!((half.max_y() - 10.0).abs() < 1e-9, "got {half:?}");
    }

    /// `ObjectFit::None` 下图片按原始像素尺寸摆放,可能溢出 frame。
    #[test]
    fn image_none_fit_may_overflow_frame() {
        let img = SceneImage::from_rgba8(50, 20, vec![0u8; 50 * 20 * 4])
            .unwrap()
            .with_object_fit(ObjectFit::None);
        let el = Element::Image {
            image: img,
            frame: Rect::new(0.0, 0.0, 10.0, 10.0),
            opacity: 1.0,
        };
        let b = element_bounds(&el).unwrap();
        assert_eq!(
            (b.max_x(), b.max_y()),
            (50.0, 20.0),
            "应按原始像素尺寸溢出 frame"
        );
    }

    #[test]
    fn text_bounds_uses_real_measurement() {
        let style = TextStyle::new(Color::BLACK, 16.0, "sans-serif");
        let el = Element::Text {
            spans: vec![crate::text::RichSpan::new("Hello", style.clone())],
            position: Point::new(0.0, 0.0),
            style,
            layout: None,
        };
        let b = element_bounds(&el);
        // 无可用字体时测量可能为 0 → 返回 None;有字体时宽高应为正。
        if let Some(b) = b {
            assert!(b.width() > 0.0 && b.height() > 0.0, "got {b:?}");
        }
    }

    #[test]
    fn scene_bounds_covers_layers() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 10.0, 10.0)]);
        let mut layer = Layer::new("overlay");
        layer.push(rect_node(100.0, 100.0, 120.0, 120.0));
        s.push_layer(layer);
        let b = scene_bounds(&s).unwrap();
        assert_eq!(
            (b.min_x(), b.min_y(), b.max_x(), b.max_y()),
            (0.0, 0.0, 120.0, 120.0)
        );
    }

    #[test]
    fn empty_scene_has_no_bounds() {
        assert_eq!(scene_bounds(&scene_with(vec![])), None);
    }

    // ——— fit ———

    #[test]
    fn fit_hugs_content_with_margin() {
        let mut s = scene_with(vec![rect_node(20.0, 30.0, 120.0, 80.0)]);
        assert!(fit_scene(&mut s, FitOptions::new().with_margin(8.0)));
        // 内容 100×50 + 两侧 8pt 边距。
        assert_eq!(s.width, 116.0);
        assert_eq!(s.height, 66.0);
        // 内容被平移到 (8, 8)。
        let b = scene_bounds(&s).unwrap();
        assert!((b.min_x() - 8.0).abs() < 1e-9, "got {b:?}");
        assert!((b.min_y() - 8.0).abs() < 1e-9, "got {b:?}");
    }

    #[test]
    fn fit_never_upscales_by_default() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 100.0, 100.0)]);
        fit_scene(
            &mut s,
            FitOptions::new()
                .with_max_width(800.0)
                .with_max_height(600.0)
                .with_margin(8.0),
        );
        assert!(
            s.width < 200.0 && s.height < 200.0,
            "got {}x{}",
            s.width,
            s.height
        );
    }

    #[test]
    fn fit_upscales_when_requested() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 100.0, 100.0)]);
        fit_scene(
            &mut s,
            FitOptions::new()
                .with_max_width(800.0)
                .with_max_height(600.0)
                .with_margin(8.0)
                .with_upscale(true),
        );
        assert!(
            s.width > 500.0 && s.height > 500.0,
            "got {}x{}",
            s.width,
            s.height
        );
    }

    /// 只约束一个维度时,另一维度按内容比例同步缩放(不被隐式限制)。
    #[test]
    fn fit_single_axis_constraint_keeps_aspect() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 100.0, 100.0)]);
        fit_scene(
            &mut s,
            FitOptions::new()
                .with_max_width(500.0)
                .with_margin(8.0)
                .with_upscale(true),
        );
        assert!((s.width - 500.0).abs() < 1.0, "got {}", s.width);
        assert!((s.height - 500.0).abs() < 1.0, "got {}", s.height);

        let mut s = scene_with(vec![rect_node(0.0, 0.0, 100.0, 100.0)]);
        fit_scene(
            &mut s,
            FitOptions::new()
                .with_max_height(500.0)
                .with_margin(8.0)
                .with_upscale(true),
        );
        assert!((s.height - 500.0).abs() < 1.0, "got {}", s.height);
        assert!((s.width - 500.0).abs() < 1.0, "got {}", s.width);
    }

    /// 内容宽高比与约束不同时,取较小的比例(等比缩放,不拉伸)。
    #[test]
    fn fit_uses_uniform_scale() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 200.0, 100.0)]);
        fit_scene(
            &mut s,
            FitOptions::new()
                .with_max_width(1000.0)
                .with_max_height(300.0)
                .with_margin(0.0)
                .with_upscale(true),
        );
        // avail = (1000, 300) → natural = min(5, 3) = 3 → 600×300。
        assert!((s.width - 600.0).abs() < 1e-6, "got {}", s.width);
        assert!((s.height - 300.0).abs() < 1e-6, "got {}", s.height);
    }

    /// 约束小于 2×边距时必须保证 scale 为正,画布尺寸不能为负。
    #[test]
    fn fit_tiny_constraint_keeps_positive_canvas() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 100.0, 100.0)]);
        fit_scene(
            &mut s,
            FitOptions::new()
                .with_max_width(4.0)
                .with_max_height(4.0)
                .with_margin(8.0),
        );
        assert!(
            s.width > 0.0 && s.height > 0.0,
            "got {}x{}",
            s.width,
            s.height
        );
        assert!(s.width.is_finite() && s.height.is_finite());
    }

    #[test]
    fn fit_empty_scene_uses_empty_size() {
        let mut s = scene_with(vec![]);
        assert!(fit_scene(
            &mut s,
            FitOptions::new().with_empty_size(Size::new(800.0, 600.0))
        ));
        assert_eq!((s.width, s.height), (800.0, 600.0));

        // 未给 empty_size 时保持原状。
        let mut s = Scene::new(123.0, 456.0);
        assert!(!fit_scene(&mut s, FitOptions::new()));
        assert_eq!((s.width, s.height), (123.0, 456.0));
    }

    /// 内容已在原点附近时跳过 Group 包裹(避免无谓的树变换)。
    #[test]
    fn fit_skips_wrapping_when_already_placed() {
        let mut s = scene_with(vec![rect_node(0.0, 0.0, 100.0, 50.0)]);
        fit_scene(&mut s, FitOptions::new());
        assert_eq!(s.nodes.len(), 1, "恒等变换不应包裹 Group");
        assert!(matches!(s.nodes[0].element(), Some(Element::Rect { .. })));
        assert_eq!((s.width, s.height), (100.0, 50.0));
    }

    /// 变换后 layers 与默认节点必须一起被移动,否则两层会错位。
    #[test]
    fn fit_moves_layers_together_with_nodes() {
        let mut s = scene_with(vec![rect_node(1000.0, 1000.0, 1100.0, 1050.0)]);
        let mut layer = Layer::new("overlay");
        layer.push(rect_node(1000.0, 1000.0, 1100.0, 1050.0));
        s.push_layer(layer);

        fit_scene(&mut s, FitOptions::new().with_margin(5.0));
        // 两层都从 (1000,1000) 平移到 (5,5)。
        let b = scene_bounds(&s).unwrap();
        assert!((b.min_x() - 5.0).abs() < 1e-6, "got {b:?}");
        assert!((b.min_y() - 5.0).abs() < 1e-6, "got {b:?}");
        assert!((b.width() - 100.0).abs() < 1e-6, "got {b:?}");
        assert!((b.height() - 50.0).abs() < 1e-6, "got {b:?}");
    }

    /// 图层自带 transform 时,fit 变换应叠加在其**之后**(先图层自身,再 fit)。
    #[test]
    fn fit_composes_onto_existing_layer_transform() {
        let mut s = Scene::new(0.0, 0.0);
        let mut layer = Layer::new("shifted").with_transform(Transform::translate(1000.0, 0.0));
        layer.push(rect_node(0.0, 0.0, 100.0, 50.0));
        s.push_layer(layer);

        fit_scene(&mut s, FitOptions::new().with_margin(5.0));
        let b = scene_bounds(&s).unwrap();
        assert!((b.min_x() - 5.0).abs() < 1e-6, "got {b:?}");
        assert!((b.min_y() - 5.0).abs() < 1e-6, "got {b:?}");
    }

    /// 缺陷回归:文本块顶必须按真实 alphabetic baseline 定位,而不是 `h*0.8` 近似。
    ///
    /// 此前 `text_box` 用高度比例估算基线位置,与渲染端(SVG/vello 都用
    /// `TextBaseline::anchor_offset` 的真实首行指标)不一致,多行或深 descender 字体下
    /// `fit_scene` 画布边缘会偏。现在两者共享同一指标。
    #[test]
    fn text_box_y_uses_real_alphabetic_baseline() {
        let style = TextStyle::new(Color::BLACK, 16.0, "sans-serif");
        let m = crate::text::measure_text(
            std::slice::from_ref(&crate::text::RichSpan::new("Ag", style.clone())),
            None,
        );
        if m.size.height <= 0.0 {
            return; // 无字体环境(沙箱/精简镜像)跳过。
        }
        let pos = Point::new(10.0, 50.0);
        let el = Element::Text {
            spans: vec![crate::text::RichSpan::new("Ag", style.clone())],
            position: pos,
            style: style.clone(),
            layout: Some(m.layout.clone()),
        };
        let b = element_bounds(&el).expect("有字体时应测得包围盒");
        let expect_top = pos.y - m.metrics.alphabetic_baseline;
        assert!(
            (b.min_y() - expect_top).abs() < 1e-9,
            "块顶应为 {expect_top}, got {}",
            b.min_y()
        );
        assert!(
            (b.max_y() - (expect_top + m.size.height)).abs() < 1e-9,
            "块底应为 {}",
            expect_top + m.size.height
        );
    }
}