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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
//! Scene traversal: turn selected elements into their hand-drawn counterparts.
//!
//! ## Selection (this is the whole point of the module)
//!
//! A sketch pass is **explicitly requested**, and it is *selective* on two levels:
//! 1. [`SketchOptions::kinds`] — a set of element kinds (rect / circle / path / …). A caller that
//!    only wants node boxes sketched but leaves sub-graph containers, grid lines and images
//!    untouched passes `SketchKinds::only(&[SketchKind::RoundedRect])`.
//! 2. `roughen_scene_if(..., predicate)` — an arbitrary `Fn(&SceneNode) -> bool`. A node for which
//!    the predicate returns `false` is skipped **together with its whole sub-tree**, so one can
//!    select by layer, by `name`, by z-range, …
//!
//! Nothing happens by default: a scene that never goes through one of these functions keeps its
//! exact geometry and its exact rendering.
//!
//! ## What a single element becomes
//!
//! ```text
//! Rect { fill: Solid(c), stroke: Some(s) }
//!   → Group[ Rect { fill: Sketch(c), stroke: None },      // 填充区:原几何,未抖动
//!            Path { jittered outline, stroke: s } ]       // 描边:抖动后的贝塞尔
//! ```
//!
//! Fill and outline are deliberately separated: the fill must stay inside the *original* outline
//! (a pattern fill clipped by the shape), while the outline is allowed to overshoot it — that
//! overshoot is most of the "drawn by hand" character.

use super::rough;
use super::{Rng, SketchKind, SketchOptions};
use crate::fit::{FitOptions, element_bounds, fit_scene};
use crate::geometry::{BezPath, Color, Vec2};
use crate::scene::{
    Element, Fill, FillStrokeStyle, NodeKind, Scene, SceneNode, SketchFill, Stroke,
};

/// Apply the sketch pass to the whole scene (subject to `opts.kinds`).
pub fn roughen_scene(scene: &mut Scene, opts: &SketchOptions) {
    roughen_scene_where(scene, opts, None);
}

/// Apply the sketch pass to every node accepted by `filter`.
///
/// A node rejected by the filter is skipped **with its whole sub-tree** (a Group's children are
/// not visited either), so a single predicate can exclude a whole layer or a named sub-graph.
pub fn roughen_scene_if(
    scene: &mut Scene,
    opts: &SketchOptions,
    filter: &dyn Fn(&SceneNode) -> bool,
) {
    roughen_scene_where(scene, opts, Some(filter));
}

/// [`roughen_scene`] followed by a refit — the recommended entry point for
/// content-driven canvases (diagram / chart exporters).
///
/// Jitter can push the outline a couple of units past the canvas edge; refitting with `margin`
/// pulls it back in. The refit is idempotent in the sense that an already-hugging scene only gets
/// shifted by the (tiny) overshoot — see the tests.
pub fn roughen_scene_fit(scene: &mut Scene, opts: &SketchOptions, margin: f64) {
    roughen_scene_fit_where(scene, opts, margin, None);
}

/// [`roughen_scene_fit`] restricted by a predicate (see [`roughen_scene_if`]).
pub fn roughen_scene_fit_if(
    scene: &mut Scene,
    opts: &SketchOptions,
    margin: f64,
    filter: &dyn Fn(&SceneNode) -> bool,
) {
    roughen_scene_fit_where(scene, opts, margin, Some(filter));
}

fn roughen_scene_fit_where(
    scene: &mut Scene,
    opts: &SketchOptions,
    margin: f64,
    filter: Option<&dyn Fn(&SceneNode) -> bool>,
) {
    roughen_scene_where(scene, opts, filter);
    fit_scene(
        scene,
        FitOptions::new().with_margin(if margin > 0.0 { margin } else { 0.0 }),
    );
}

fn roughen_scene_where(
    scene: &mut Scene,
    opts: &SketchOptions,
    filter: Option<&dyn Fn(&SceneNode) -> bool>,
) {
    let mut rng = match opts.seed {
        Some(seed) => Rng::new(seed),
        None => Rng::from_entropy(),
    };
    // Document order (default nodes, then visible layers bottom-to-top) so the same seed always
    // yields the same randomness per element. `root_lists_mut` is the shared root traversal.
    for nodes in scene.root_lists_mut() {
        walk(nodes, &mut rng, opts, filter);
    }
}

fn walk(
    nodes: &mut [SceneNode],
    rng: &mut Rng,
    opts: &SketchOptions,
    filter: Option<&dyn Fn(&SceneNode) -> bool>,
) {
    for node in nodes.iter_mut() {
        if !node.visible {
            continue;
        }
        if let Some(f) = filter
            && !f(node)
        {
            continue;
        }
        // Containers: recurse into the group's children (a container is never itself sketched).
        // Uses the shared container accessor instead of matching the node kind directly.
        if let Some(children) = node.children_mut() {
            walk(children, rng, opts, filter);
            continue;
        }
        // The remaining nodes are leaves; a container was handled above.
        let Some(element) = node.element() else {
            continue;
        };
        let Some(kind) = SketchKind::of(element) else {
            continue;
        };
        if !opts.kinds.contains(kind) {
            continue;
        }
        if let Some(kind) = sketch_element(element, rng, opts) {
            node.kind = kind;
        }
    }
}

/// Sketch a single element: returns the replacement (fill first, outline second) or `None` when
/// there is nothing to draw by hand (text, image, an element without fill *and* stroke, …).
///
/// Exposed for callers that build their own pipeline; the result is a `Vec` because a filled and
/// stroked shape becomes two elements.
pub fn roughen_element(el: &Element, rng: &mut Rng, opts: &SketchOptions) -> Vec<Element> {
    let mut out = Vec::with_capacity(2);
    let (fill, outline) = sketch_parts(el, rng, opts);
    if let Some(fill) = fill {
        out.push(fill);
    }
    if let Some(outline) = outline {
        out.push(outline);
    }
    out
}

fn sketch_element(el: &Element, rng: &mut Rng, opts: &SketchOptions) -> Option<NodeKind> {
    let (fill, outline) = sketch_parts(el, rng, opts);
    match (fill, outline) {
        (None, None) => None,
        (Some(e), None) | (None, Some(e)) => Some(NodeKind::Draw(e)),
        // Fill under outline — same as how a single filled+stroked shape is painted.
        (Some(f), Some(o)) => Some(NodeKind::Group(vec![SceneNode::new(f), SceneNode::new(o)])),
    }
}

/// Fill element + outline element for one input element.
///
/// The outline only drops the fill when the fill moved into its own (sketch) layer; otherwise the
/// original fill is carried over onto the outline path — otherwise "sketch outlines only"
/// (`opts.fill = None`) would silently delete every fill in the scene.
fn sketch_parts(
    el: &Element,
    rng: &mut Rng,
    opts: &SketchOptions,
) -> (Option<Element>, Option<Element>) {
    let fill = fill_element(el, opts);
    let outline = outline_element(el, fill.is_some(), rng, opts);
    (fill, outline)
}

/// The fill region: the **original** geometry with the fill swapped for [`Fill::Sketch`].
///
/// `None` when there is nothing to convert: no style requested, the element has no fill, the fill
/// is already a sketch fill, or the fill region is smaller than
/// [`SketchOptions::min_fill_area`] (tiny filled shapes — arrowheads, label backgrounds — read
/// much better with their solid ink preserved than with a 3-line hatch).
///
/// Gradients have no single colour, so they degrade deterministically to their *last* stop (the
/// outer ring of a radial gradient, the end of a linear one).
fn fill_element(el: &Element, opts: &SketchOptions) -> Option<Element> {
    let style = opts.fill?;
    if let Some(min) = opts.min_fill_area
        && let Some(b) = element_bounds(el)
        && b.width() * b.height() < min
    {
        return None;
    }
    let color = fill_color(el)?;
    let sf = SketchFill::new(color)
        .with_style(style)
        .with_angle(opts.fill_angle)
        .with_gap(floored_gap(el, opts))
        .with_fill_weight(opts.fill_weight);
    match el {
        // `GradientPath` carries a gradient instead of a `FillStrokeStyle`.
        Element::GradientPath { path, .. } => Some(Element::Path {
            path: path.clone(),
            style: FillStrokeStyle {
                fill: Some(Fill::Sketch(sf)),
                stroke: None,
            },
            closed: true,
        }),
        _ => {
            let mut out = el.clone();
            let st = style_mut(&mut out)?;
            st.fill = Some(Fill::Sketch(sf));
            st.stroke = None;
            Some(out)
        }
    }
}

/// The jittered outline, stroked with the element's own stroke (or a synthesised one).
///
/// `fill_taken` says the fill moved into its own sketch layer; otherwise the outline path
/// inherits the element's original fill (solid or gradient), so an element whose fill was *not*
/// converted stays filled.
fn outline_element(
    el: &Element,
    fill_taken: bool,
    rng: &mut Rng,
    opts: &SketchOptions,
) -> Option<Element> {
    let stroke = match stroke_of(el) {
        Some(s) => Some(s),
        // A fill-only shape (bars, pie sectors, …) would otherwise keep crisp edges under a
        // hand-drawn fill — synthesise a thin outline in the fill colour, like a pen.
        None if opts.outline_when_unstroked => {
            Some(Stroke::new(fill_color(el)?, opts.outline_width))
        }
        None => None,
    }?;
    let path = outline_path(el, rng, opts)?;
    Some(Element::Path {
        path,
        style: FillStrokeStyle {
            fill: if fill_taken { None } else { original_fill(el) },
            stroke: Some(stroke),
        },
        closed: false,
    })
}

/// The element's current fill, for carrying over onto the outline path.
fn original_fill(el: &Element) -> Option<Fill> {
    match el {
        Element::GradientPath { gradient, .. } => Some(Fill::LinearGradient(gradient.clone())),
        _ => style_of(el)?.fill.clone(),
    }
}

fn outline_path(el: &Element, rng: &mut Rng, opts: &SketchOptions) -> Option<BezPath> {
    let mut p = BezPath::new();
    match el {
        Element::Rect { rect, .. } => {
            rough::polyline(rng, &mut p, &rough::rect_points(*rect), true, opts)
        }
        Element::RoundedRect { rect, radii, .. } => {
            let pts = rough::rounded_rect_points(*rect, *radii, opts.curve_step);
            rough::polyline(rng, &mut p, &pts, true, opts);
        }
        Element::Circle { center, radius, .. } => rough::ellipse(
            rng,
            &mut p,
            *center,
            Vec2::new(radius.abs(), radius.abs()),
            0.0,
            opts,
        ),
        Element::Ellipse {
            center,
            radii,
            rotation,
            ..
        } => rough::ellipse(rng, &mut p, *center, *radii, *rotation, opts),
        Element::Line { start, end, .. } => {
            rough::polyline(rng, &mut p, &[*start, *end], false, opts)
        }
        Element::Polyline { points, .. } => rough::polyline(rng, &mut p, points, false, opts),
        Element::Polygon { points, .. } => rough::polyline(rng, &mut p, points, true, opts),
        Element::Arc {
            center,
            radii,
            start_angle,
            sweep_angle,
            ..
        } => rough::arc(
            rng,
            &mut p,
            *center,
            *radii,
            *start_angle,
            *sweep_angle,
            opts,
        ),
        Element::Pie {
            center,
            radius,
            start_angle,
            sweep_angle,
            ..
        } => {
            let mut pts = vec![*center];
            pts.extend(rough::sample_arc(
                *center,
                Vec2::new(radius.abs(), radius.abs()),
                0.0,
                *start_angle,
                *sweep_angle,
                opts.curve_step,
            ));
            rough::polyline(rng, &mut p, &pts, true, opts);
        }
        Element::Path { path, closed, .. } => rough::path(rng, &mut p, path, *closed, opts),
        Element::GradientPath { path, .. } => rough::path(rng, &mut p, path, true, opts),
        // Text / Image / Group are never sketched (see module docs).
        _ => return None,
    }
    (!p.elements().is_empty()).then_some(p)
}

/// `&FillStrokeStyle` of every variant that owns one.
fn style_of(el: &Element) -> Option<&FillStrokeStyle> {
    match el {
        Element::Rect { style, .. }
        | Element::Circle { style, .. }
        | Element::Ellipse { style, .. }
        | Element::RoundedRect { style, .. }
        | Element::Polygon { style, .. }
        | Element::Pie { style, .. }
        | Element::Path { style, .. } => Some(style),
        _ => None,
    }
}

fn style_mut(el: &mut Element) -> Option<&mut FillStrokeStyle> {
    match el {
        Element::Rect { style, .. }
        | Element::Circle { style, .. }
        | Element::Ellipse { style, .. }
        | Element::RoundedRect { style, .. }
        | Element::Polygon { style, .. }
        | Element::Pie { style, .. }
        | Element::Path { style, .. } => Some(style),
        _ => None,
    }
}

/// The stroke an element is drawn with (stroke-only variants carry one directly).
fn stroke_of(el: &Element) -> Option<Stroke> {
    match el {
        Element::Line { style, .. }
        | Element::Polyline { style, .. }
        | Element::Arc { style, .. } => Some(style.clone()),
        Element::GradientPath { stroke, .. } => stroke.clone(),
        _ => style_of(el)?.stroke.clone(),
    }
}

/// The dominant colour of an element's fill; `None` when there is no fill (or it is already a
/// sketch fill, which must not be re-sketched).
fn fill_color(el: &Element) -> Option<Color> {
    match el {
        Element::GradientPath { gradient, .. } => last_stop(&gradient.stops),
        _ => match style_of(el)?.fill.as_ref()? {
            Fill::Solid(c) => Some(*c),
            Fill::LinearGradient(g) => last_stop(&g.stops),
            Fill::RadialGradient(g) => last_stop(&g.stops),
            Fill::SweepGradient(g) => last_stop(&g.stops),
            // 图片填充没有"单一颜色"可降级;保持原样(描边仍会被手绘化)。
            Fill::Image(_) | Fill::Sketch(_) => None,
        },
    }
}

/// Floor the hachure gap so a single element can never ask for more strokes than
/// [`SketchOptions::max_fill_strokes`] — regardless of how large the shape is.
///
/// The protection is baked into the IR here (rather than left to the backends, or to every
/// caller remembering to switch hachure off for a heat map): the element knows its own size, and
/// the resulting `SketchFill.gap` is what both backends then honour.
fn floored_gap(el: &Element, opts: &SketchOptions) -> f64 {
    let gap = if opts.fill_gap.is_finite() && opts.fill_gap > 0.05 {
        opts.fill_gap
    } else {
        4.0
    };
    let Some(bbox) = element_bounds(el) else {
        return gap;
    };
    let t = opts.fill_angle.to_radians();
    let along = (bbox.width() * t.sin()).abs() + (bbox.height() * t.cos()).abs();
    // Cross-hatch adds an orthogonal family, so size for the worst case.
    let across = (bbox.width() * t.cos()).abs() + (bbox.height() * t.sin()).abs();
    gap.max(along.max(across) / opts.max_fill_strokes.max(1) as f64)
}

/// Last stop of a gradient (highest offset), used when a gradient degrades to a sketch fill.
fn last_stop(stops: &[crate::scene::GradientStop]) -> Option<Color> {
    stops
        .iter()
        .max_by(|a, b| {
            a.offset
                .partial_cmp(&b.offset)
                .unwrap_or(std::cmp::Ordering::Equal)
        })
        .map(|s| s.color)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::{Point, Rect, Vec2};
    use crate::scene::{Clip, SketchFillStyle};
    use crate::sketch::SketchKinds;

    fn colored_rect() -> Element {
        Element::rect(
            Rect::new(0.0, 0.0, 100.0, 50.0),
            FillStrokeStyle {
                fill: Some(Fill::Solid(Color::rgb(0x33, 0x66, 0x99))),
                stroke: Some(Stroke::new(Color::BLACK, 2.0)),
            },
        )
    }

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

    #[test]
    fn filled_and_stroked_shape_becomes_fill_plus_outline() {
        let els = roughen_element(
            &colored_rect(),
            &mut Rng::new(1),
            &SketchOptions {
                fill: Some(SketchFillStyle::Hachure),
                ..Default::default()
            },
        );
        assert_eq!(els.len(), 2, "填充 + 描边应拆成两个图元");
        match &els[0] {
            Element::Rect { style, .. } => {
                assert!(matches!(style.fill, Some(Fill::Sketch(_))));
                assert!(style.stroke.is_none(), "填充层不应再描边");
            }
            other => panic!("期望保留原几何做填充, got {other:?}"),
        }
        match &els[1] {
            Element::Path { style, closed, .. } => {
                assert!(style.fill.is_none());
                assert!(style.stroke.is_some());
                assert!(!closed, "描边是开放路径");
            }
            other => panic!("期望抖动后的描边路径, got {other:?}"),
        }
    }

    #[test]
    fn fill_only_shape_gets_a_synthesised_outline() {
        let el = Element::rect(
            Rect::new(0.0, 0.0, 10.0, 10.0),
            FillStrokeStyle::fill(Color::rgb(0xff, 0x00, 0x00)),
        );
        let opts = SketchOptions {
            fill: Some(SketchFillStyle::Hachure),
            ..Default::default()
        };
        let els = roughen_element(&el, &mut Rng::new(2), &opts);
        assert_eq!(els.len(), 2);
        if let Element::Path { style, .. } = &els[1] {
            assert_eq!(
                style.stroke.as_ref().unwrap().color,
                Color::rgb(0xff, 0x00, 0x00),
                "合成描边应取填充色"
            );
        } else {
            panic!("期望合成描边");
        }
        // 关闭合成后只保留填充层。
        let els = roughen_element(
            &el,
            &mut Rng::new(2),
            &SketchOptions {
                outline_when_unstroked: false,
                ..opts
            },
        );
        assert_eq!(els.len(), 1);
    }

    /// `opts.fill = None`:只抖描边,但原填充必须被描边层继承(不能凭空消失)。
    #[test]
    fn fill_is_untouched_when_no_style_is_requested() {
        let els = roughen_element(&colored_rect(), &mut Rng::new(3), &SketchOptions::default());
        assert_eq!(els.len(), 1, "未开启手绘填充时只输出描边");
        match &els[0] {
            Element::Path { style, .. } => {
                assert_eq!(
                    style.fill,
                    Some(Fill::Solid(Color::rgb(0x33, 0x66, 0x99))),
                    "原填充应保留在描边路径上"
                );
                assert!(style.stroke.is_some());
            }
            other => panic!("期望抖动后的 Path, got {other:?}"),
        }
    }

    /// 小面积填充保护:低于 `min_fill_area` 的填充保持实底(继承到描边层),不排线。
    #[test]
    fn small_fills_keep_their_solid_ink() {
        let opts = SketchOptions {
            fill: Some(SketchFillStyle::Hachure),
            min_fill_area: Some(1000.0),
            ..Default::default()
        };
        // 箭头级别的小三角形(~60px²)→ 实底。
        let tiny = Element::polygon(
            vec![
                Point::new(0.0, 0.0),
                Point::new(10.0, 5.0),
                Point::new(0.0, 10.0),
            ],
            FillStrokeStyle::fill(Color::rgb(0x33, 0x33, 0x33)),
        );
        let els = roughen_element(&tiny, &mut Rng::new(6), &opts);
        assert_eq!(els.len(), 1, "小填充不拆层");
        if let Element::Path { style, .. } = &els[0] {
            assert_eq!(
                style.fill,
                Some(Fill::Solid(Color::rgb(0x33, 0x33, 0x33))),
                "实底应被继承"
            );
        } else {
            panic!("期望带实底的抖动路径");
        }
        // 大矩形(10_000px²)→ 仍拆成排线填充层 + 描边层。
        let big = Element::rect(
            Rect::new(0.0, 0.0, 100.0, 100.0),
            FillStrokeStyle::fill(Color::rgb(0x33, 0x66, 0x99)),
        );
        let els = roughen_element(&big, &mut Rng::new(6), &opts);
        assert_eq!(els.len(), 2, "大填充仍应拆层");
        assert!(matches!(els[0], Element::Rect { .. }));
    }

    /// `min_fill_area = Some(0.0)` 显式关闭保护(一切填充都排线)。
    #[test]
    fn zero_min_fill_area_disables_the_guard() {
        let opts = SketchOptions {
            fill: Some(SketchFillStyle::Hachure),
            min_fill_area: Some(0.0),
            ..Default::default()
        };
        let tiny = Element::polygon(
            vec![
                Point::new(0.0, 0.0),
                Point::new(10.0, 5.0),
                Point::new(0.0, 10.0),
            ],
            FillStrokeStyle::fill(Color::rgb(0x33, 0x33, 0x33)),
        );
        let els = roughen_element(&tiny, &mut Rng::new(6), &opts);
        assert_eq!(els.len(), 2, "关闭保护后小填充也拆层");
    }

    #[test]
    fn text_and_image_are_never_sketched() {
        let opts = SketchOptions {
            fill: Some(SketchFillStyle::Hachure),
            ..Default::default()
        };
        let text = Element::text(
            "hi",
            Point::new(0.0, 0.0),
            crate::text::TextStyle::new(Color::BLACK, 12.0, "sans-serif"),
        );
        assert!(roughen_element(&text, &mut Rng::new(4), &opts).is_empty());
    }

    /// 选择性:kinds 决定哪些图元参与。
    #[test]
    fn kinds_select_which_elements_are_sketched() {
        let mut scene = scene_with(vec![
            SceneNode::new(Element::rect(
                Rect::new(0.0, 0.0, 10.0, 10.0),
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            )),
            SceneNode::new(Element::circle(
                Point::new(20.0, 20.0),
                5.0,
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            )),
        ]);
        roughen_scene(
            &mut scene,
            &SketchOptions {
                seed: Some(1),
                kinds: SketchKinds::only(&[SketchKind::Circle]),
                ..Default::default()
            },
        );
        assert!(
            matches!(scene.nodes[0].element(), Some(Element::Rect { .. })),
            "矩形被排除"
        );
        assert!(
            matches!(scene.nodes[1].element(), Some(Element::Path { .. })),
            "圆被手绘化"
        );
    }

    /// 选择性:谓词为 false 时整棵子树跳过。
    #[test]
    fn predicate_skips_whole_subtrees() {
        let child = SceneNode::new(Element::rect(
            Rect::new(0.0, 0.0, 10.0, 10.0),
            FillStrokeStyle::stroke(Color::BLACK, 1.0),
        ))
        .with_name("keep");
        let mut scene = scene_with(vec![SceneNode::group(vec![
            child,
            SceneNode::new(Element::rect(
                Rect::new(0.0, 0.0, 10.0, 10.0),
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            ))
            .with_name("drop"),
        ])]);
        roughen_scene_if(
            &mut scene,
            &SketchOptions {
                seed: Some(1),
                ..Default::default()
            },
            &|n| n.name.as_deref() != Some("drop"),
        );
        let children = scene.nodes[0].children().expect("期望 Group");
        assert!(
            matches!(children[0].element(), Some(Element::Path { .. })),
            "keep 应被手绘化"
        );
        assert!(
            matches!(children[1].element(), Some(Element::Rect { .. })),
            "drop 应被跳过"
        );
    }

    #[test]
    fn hidden_nodes_are_left_alone() {
        let mut scene = scene_with(vec![
            SceneNode::new(Element::rect(
                Rect::new(0.0, 0.0, 10.0, 10.0),
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            ))
            .with_visible(false),
        ]);
        roughen_scene(
            &mut scene,
            &SketchOptions {
                seed: Some(1),
                ..Default::default()
            },
        );
        assert!(matches!(
            scene.nodes[0].element(),
            Some(Element::Rect { .. })
        ));
    }

    #[test]
    fn layers_are_visited_too() {
        let mut scene = scene_with(vec![]);
        let mut layer = crate::scene::Layer::new("grid");
        layer.push(Element::rect(
            Rect::new(0.0, 0.0, 10.0, 10.0),
            FillStrokeStyle::stroke(Color::BLACK, 1.0),
        ));
        scene.push_layer(layer);
        roughen_scene(
            &mut scene,
            &SketchOptions {
                seed: Some(1),
                ..Default::default()
            },
        );
        assert!(matches!(
            scene.layers[0].nodes[0].element(),
            Some(Element::Path { .. })
        ));
    }

    /// 节点级属性(transform / clip / opacity / name)必须留在原位,不被手绘化吞掉。
    #[test]
    fn node_attributes_survive_the_pass() {
        let mut scene = scene_with(vec![
            SceneNode::new(Element::rect(
                Rect::new(0.0, 0.0, 10.0, 10.0),
                FillStrokeStyle {
                    fill: Some(Fill::Solid(Color::BLACK)),
                    stroke: Some(Stroke::new(Color::BLACK, 1.0)),
                },
            ))
            .with_transform(crate::geometry::Transform::translate(5.0, 5.0))
            .with_opacity(0.5)
            .with_name("node")
            .with_clip(Clip::rect(Rect::new(0.0, 0.0, 40.0, 40.0))),
        ]);
        roughen_scene(
            &mut scene,
            &SketchOptions {
                seed: Some(1),
                fill: Some(SketchFillStyle::Hachure),
                ..Default::default()
            },
        );
        let n = &scene.nodes[0];
        assert!(n.transform.is_some() && n.clip.is_some());
        assert_eq!(n.opacity, 0.5);
        assert_eq!(n.name.as_deref(), Some("node"));
        assert!(n.is_group());
    }

    /// 端到端确定性:同种子两次处理同场景 → 完全相同的路径。
    #[test]
    fn whole_scene_is_deterministic() {
        let build = || {
            scene_with(vec![
                SceneNode::new(Element::rect(
                    Rect::new(10.0, 10.0, 110.0, 60.0),
                    FillStrokeStyle {
                        fill: Some(Fill::Solid(Color::rgb(0xee, 0xee, 0xff))),
                        stroke: Some(Stroke::new(Color::BLACK, 1.5)),
                    },
                )),
                SceneNode::new(Element::circle(
                    Point::new(200.0, 40.0),
                    18.0,
                    FillStrokeStyle::stroke(Color::BLACK, 1.0),
                )),
            ])
        };
        let opts = SketchOptions {
            seed: Some(2024),
            fill: Some(SketchFillStyle::Hachure),
            ..Default::default()
        };
        let mut a = build();
        let mut b = build();
        roughen_scene(&mut a, &opts);
        roughen_scene(&mut b, &opts);
        assert_eq!(format!("{:?}", a.nodes), format!("{:?}", b.nodes));
    }

    #[test]
    fn different_seeds_produce_different_scenes() {
        let build = || {
            scene_with(vec![SceneNode::new(Element::rect(
                Rect::new(0.0, 0.0, 100.0, 50.0),
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            ))])
        };
        let mut a = build();
        let mut b = build();
        roughen_scene(
            &mut a,
            &SketchOptions {
                seed: Some(1),
                ..Default::default()
            },
        );
        roughen_scene(
            &mut b,
            &SketchOptions {
                seed: Some(2),
                ..Default::default()
            },
        );
        assert_ne!(format!("{:?}", a.nodes), format!("{:?}", b.nodes));
    }

    /// 抖动后的包围盒只会略微变大,不会跑飞。
    #[test]
    fn roughened_bounds_stay_close_to_the_original() {
        let mut scene = scene_with(vec![SceneNode::new(Element::rect(
            Rect::new(10.0, 10.0, 110.0, 60.0),
            FillStrokeStyle::stroke(Color::BLACK, 2.0),
        ))]);
        let before = crate::fit::scene_bounds(&scene).unwrap();
        roughen_scene(
            &mut scene,
            &SketchOptions {
                seed: Some(7),
                roughness: 2.0,
                ..Default::default()
            },
        );
        let after = crate::fit::scene_bounds(&scene).unwrap();
        assert!(after.min_x() < before.min_x() && after.max_x() > before.max_x());
        assert!(
            after.width() - before.width() < 12.0,
            "抖动外扩应有界, got {}",
            after.width() - before.width()
        );
    }

    /// roughen_scene_fit:抖动溢出后被重新贴合,且二次调用是幂等的(尺寸不再变化)。
    #[test]
    fn refit_is_idempotent() {
        let mut scene = scene_with(vec![SceneNode::new(Element::rect(
            Rect::new(0.0, 0.0, 100.0, 50.0),
            FillStrokeStyle {
                fill: Some(Fill::Solid(Color::BLACK)),
                stroke: Some(Stroke::new(Color::BLACK, 3.0)),
            },
        ))]);
        let opts = SketchOptions {
            seed: Some(11),
            roughness: 2.5,
            ..Default::default()
        };
        roughen_scene_fit(&mut scene, &opts, 8.0);
        let (w, h) = (scene.width, scene.height);
        let b1 = crate::fit::scene_bounds(&scene).unwrap();
        // 再 fit 一次:内容已贴合 → 只有极小的平移,画布尺寸不放大。
        crate::fit::fit_scene(&mut scene, FitOptions::new().with_margin(8.0));
        assert!((scene.width - w).abs() < 1.0 && (scene.height - h).abs() < 1.0);
        let b2 = crate::fit::scene_bounds(&scene).unwrap();
        assert!((b1.width() - b2.width()).abs() < 1.0);
    }

    #[test]
    fn gradient_degrades_to_its_last_stop() {
        let g = crate::scene::LinearGradient::new(
            Point::new(0.0, 0.0),
            Point::new(10.0, 0.0),
            vec![
                crate::scene::GradientStop {
                    offset: 0.0,
                    color: Color::WHITE,
                },
                crate::scene::GradientStop {
                    offset: 1.0,
                    color: Color::rgb(0x12, 0x34, 0x56),
                },
            ],
        );
        let el = Element::GradientPath {
            path: BezPath::new(),
            gradient: g,
            stroke: None,
        };
        let els = roughen_element(
            &el,
            &mut Rng::new(1),
            &SketchOptions {
                fill: Some(SketchFillStyle::Hachure),
                ..Default::default()
            },
        );
        match &els[0] {
            Element::Path { style, .. } => match &style.fill {
                Some(Fill::Sketch(sf)) => {
                    assert_eq!(sf.color, Color::rgb(0x12, 0x34, 0x56), "取末端色");
                }
                other => panic!("期望 Sketch 填充, got {other:?}"),
            },
            other => panic!("期望 Path, got {other:?}"),
        }
    }

    /// 已经手绘化的填充不应被二次处理(重入保护)。
    #[test]
    fn already_sketched_fill_is_not_reprocessed() {
        let sf = SketchFill::new(Color::BLACK);
        let el = Element::rect(
            Rect::new(0.0, 0.0, 10.0, 10.0),
            FillStrokeStyle {
                fill: Some(Fill::Sketch(sf)),
                stroke: Some(Stroke::new(Color::BLACK, 1.0)),
            },
        );
        let els = roughen_element(
            &el,
            &mut Rng::new(1),
            &SketchOptions {
                fill: Some(SketchFillStyle::Hachure),
                ..Default::default()
            },
        );
        assert_eq!(els.len(), 1, "只有描边被重画,填充保持原样");
        assert!(matches!(els[0], Element::Path { .. }));
    }

    #[test]
    fn ellipse_and_pie_and_arc_are_all_sketchable() {
        let opts = SketchOptions {
            seed: Some(5),
            ..Default::default()
        };
        for el in [
            Element::ellipse(
                Point::new(10.0, 10.0),
                Vec2::new(20.0, 10.0),
                0.3,
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            ),
            Element::pie(
                Point::new(10.0, 10.0),
                20.0,
                0.0,
                1.0,
                FillStrokeStyle::stroke(Color::BLACK, 1.0),
            ),
            Element::arc(
                Point::new(10.0, 10.0),
                Vec2::new(20.0, 20.0),
                0.0,
                1.0,
                Stroke::new(Color::BLACK, 1.0),
            ),
            Element::poly(
                vec![
                    Point::new(0.0, 0.0),
                    Point::new(10.0, 5.0),
                    Point::new(4.0, 12.0),
                ],
                Stroke::new(Color::BLACK, 1.0),
            ),
        ] {
            let els = roughen_element(&el, &mut Rng::new(5), &opts);
            assert_eq!(els.len(), 1, "{el:?} 应被手绘化");
            assert!(matches!(els[0], Element::Path { .. }), "{el:?}");
        }
    }
}