GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
use super::*;
use vectorcraft_color::{Color, Gradient, GradientPaint, Paint};
use vectorcraft_doc::{Appearance, Node};
use vectorcraft_geom::shapes;

mod adjust;
mod alternate;
mod arearesize;
mod bitmapglyphs;
mod blend;
mod changed;
mod charstroke;
mod clip;
mod cmykblend;
mod colorize;
mod colrglyphs;
mod container;
mod curvedstrokeedges;
mod dither;
pub(super) mod fontchange;
mod freeform;
mod fxzoom;
mod isolation;
mod keptpath;
mod knockout;
mod layeropts;
mod longstroke;
mod memory;
mod meshwarp;
mod movedstroke;
mod objectfx;
mod paintedbrush;
mod placed_document;
mod rasterize;
mod setup;
mod strokegradient;
mod thincoverage;
mod tileedge;
mod typeedges;
mod viewraster;
mod widthzoom;

fn rect_node(d: &mut Document, r: Rect, fill: Paint, stroke: Paint, w: f64) -> Node {
    let id = d.alloc_id();
    Node::path(id, shapes::rectangle(r), Appearance::basic(fill, stroke, w))
}

fn render(d: &Document) -> Rendered {
    Renderer::new().render(d, 100, 100, Affine::IDENTITY, &RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() })
}

#[test]
fn fills_red_rect() {
    let mut d = Document::new(100.0, 100.0);
    let n = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 50.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let d = {
        let mut d2 = d.clone();
        d2.insert(Some(d.layers[0].id), 0, n).unwrap();
        d2
    };
    let r = render(&d);
    assert_eq!(r.pixel(30, 30), [255, 0, 0, 255]);
    assert_eq!(r.pixel(70, 70), [255, 255, 255, 255]);
}

#[test]
fn raster_size_limits() {
    let r = Rect::new(0.0, 0.0, 100.0, 50.0);
    assert_eq!(raster_size(r, 2.0), Ok((200, 100)));
    assert_eq!(raster_size(r, 0.001), Ok((1, 1)));
    assert_eq!(raster_size(Rect::new(0.0, 0.0, MAX_RASTER_SIDE as f64, 1.0), 1.0), Ok((MAX_RASTER_SIDE, 1)));
    assert!(raster_size(Rect::new(0.0, 0.0, MAX_RASTER_SIDE as f64 + 1.0, 1.0), 1.0).is_err());
    assert!(raster_size(Rect::new(0.0, 0.0, 20_000.0, 20_000.0), 1.0).is_err(), "400 megapixels");
    assert!(raster_size(r, f64::NAN).is_err());
    assert!(raster_size(r, f64::INFINITY).is_err());
    assert!(raster_size(r, 0.0).is_err());
}

#[test]
fn stroke_draws_outline_only() {
    let mut d = Document::new(100.0, 100.0);
    let n = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::None, Paint::solid(Color::BLACK), 4.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = render(&d);
    assert_eq!(r.pixel(10, 50), [0, 0, 0, 255]);
    assert_eq!(r.pixel(50, 50), [255, 255, 255, 255]);
}

/// #1396: a stroke wider than its ellipse is a solid blob, pixel for pixel the disc a renderer
/// with a native stroke (the PDF and SVG exports) draws, at every alignment and zoom.
#[test]
fn a_stroke_wider_than_its_ellipse_paints_solid() {
    let doc = |r: f64, fill: Paint, stroke: Paint, w: f64, align: StrokeAlign| {
        let mut d = Document::new(100.0, 100.0);
        let id = d.alloc_id();
        let mut n = Node::path(id, shapes::ellipse(Rect::new(50.0 - r, 50.0 - r, 50.0 + r, 50.0 + r)), Appearance::basic(fill, stroke, w));
        if let Some(st) = n.appearance.stroke_mut() {
            st.align = align;
        }
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        d
    };
    let black = || Paint::solid(Color::BLACK);
    let render = |d: &Document, zoom: f64| {
        let view = Affine::translate((50.0, 50.0)) * Affine::scale(zoom) * Affine::translate((-50.0, -50.0));
        Renderer::new().render(d, 100, 100, view, &RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() })
    };
    // (ellipse radius, weight, alignment, radius of the disc it paints)
    for (r, w, align, disc) in [
        (10.0, 60.0, StrokeAlign::Center, 40.0),
        (6.0, 50.0, StrokeAlign::Center, 31.0),
        (15.0, 50.0, StrokeAlign::Center, 40.0),
        (10.0, 30.0, StrokeAlign::Outside, 40.0),
        (10.0, 30.0, StrokeAlign::Inside, 10.0),
    ] {
        for zoom in [0.5, 1.0, 2.0] {
            let got = render(&doc(r, Paint::None, black(), w, align), zoom);
            if align == StrokeAlign::Outside {
                // The shape itself stays unpainted, and the stroke around it has no gap.
                assert_eq!(got.pixel(50, 50), [255, 255, 255, 255], "{r} {w} {align:?} at {zoom}");
                assert_eq!(got.pixel(50 + (12.0 * zoom) as u32, 50), [0, 0, 0, 255], "{r} {w} {align:?} at {zoom}");
                continue;
            }
            let want = render(&doc(disc, black(), Paint::None, 0.0, StrokeAlign::Center), zoom);
            let off =
                (0..100).flat_map(|y| (0..100).map(move |x| (x, y))).filter(|&(x, y)| got.pixel(x, y)[0].abs_diff(want.pixel(x, y)[0]) > 40).count();
            assert!(off <= 8, "{r} {w} {align:?} at {zoom}: {off} pixels differ from the disc");
        }
    }
}

#[test]
fn inside_stroke_stays_inside() {
    let mut d = Document::new(100.0, 100.0);
    let mut n = rect_node(&mut d, Rect::new(20.0, 20.0, 80.0, 80.0), Paint::None, Paint::solid(Color::BLACK), 10.0);
    n.appearance.stroke_mut().unwrap().align = StrokeAlign::Inside;
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = render(&d);
    assert_eq!(r.pixel(17, 50), [255, 255, 255, 255]);
    assert_eq!(r.pixel(25, 50), [0, 0, 0, 255]);
}

#[test]
fn outside_stroke_stays_outside() {
    let mut d = Document::new(100.0, 100.0);
    let mut n = rect_node(&mut d, Rect::new(20.0, 20.0, 80.0, 80.0), Paint::solid(Color::WHITE), Paint::solid(Color::BLACK), 10.0);
    n.appearance.stroke_mut().unwrap().align = StrokeAlign::Outside;
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = render(&d);
    assert_eq!(r.pixel(15, 50), [0, 0, 0, 255]);
    assert_eq!(r.pixel(23, 50), [255, 255, 255, 255]);
}

#[test]
fn opacity_blends() {
    let mut d = Document::new(100.0, 100.0);
    let mut n = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    n.opacity = 0.5;
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let p = render(&d).pixel(50, 50);
    assert!((p[0] as i32 - 128).abs() <= 2, "{p:?}");
}

#[test]
fn multiply_blend() {
    let mut d = Document::new(100.0, 100.0);
    let a = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::rgb(1.0, 1.0, 0.0)), Paint::None, 0.0);
    let mut b = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::rgb(0.0, 1.0, 1.0)), Paint::None, 0.0);
    b.blend = vectorcraft_color::BlendMode::Multiply;
    let l = d.layers[0].id;
    d.insert(Some(l), 0, a).unwrap();
    d.insert(Some(l), 1, b).unwrap();
    assert_eq!(render(&d).pixel(50, 50), [0, 255, 0, 255]);
}

#[test]
fn linear_gradient_goes_white_to_black() {
    let mut d = Document::new(100.0, 100.0);
    let n =
        rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::Gradient(Box::new(GradientPaint::new(Gradient::default()))), Paint::None, 0.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = render(&d);
    assert!(r.pixel(2, 50)[0] > 240);
    assert!(r.pixel(97, 50)[0] < 15);
    let m = r.pixel(50, 50)[0] as i32;
    assert!((m - 128).abs() < 12, "{m}");
}

#[test]
fn clip_group_clips() {
    let mut d = Document::new(100.0, 100.0);
    let mut clip = rect_node(&mut d, Rect::new(0.0, 0.0, 50.0, 100.0), Paint::None, Paint::None, 0.0);
    if let NodeKind::Path { clipping, .. } = &mut clip.kind {
        *clipping = true;
    }
    let art = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let gid = d.alloc_id();
    let g = Node::new(gid, NodeKind::Group { children: vec![Arc::new(clip), Arc::new(art)], clip: true });
    let l = d.layers[0].id;
    d.insert(Some(l), 0, g).unwrap();
    let r = render(&d);
    assert_eq!(r.pixel(25, 50), [0, 0, 0, 255]);
    assert_eq!(r.pixel(75, 50), [255, 255, 255, 255]);
}

#[test]
fn hidden_and_culled() {
    let mut d = Document::new(100.0, 100.0);
    let mut n = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    n.visible = false;
    let far = rect_node(&mut d, Rect::new(1000.0, 1000.0, 1100.0, 1100.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    d.insert(Some(l), 1, far).unwrap();
    let mut rr = Renderer::new();
    let r = rr.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(r.pixel(50, 50), [0, 0, 0, 0]);
    assert_eq!(rr.stats.culled, 1);
}

#[test]
fn a_render_leaves_out_every_object_of_a_large_hidden_set() {
    // 2,000 one-pixel-wide strips; the strips in even columns are in `hidden`.
    let mut d = Document::new(100.0, 100.0);
    let l = d.layers[0].id;
    let mut hidden = std::collections::HashSet::new();
    for row in 0..20 {
        for col in 0..100 {
            let r = Rect::new(f64::from(col), f64::from(row * 5), f64::from(col + 1), f64::from(row * 5 + 5));
            let n = rect_node(&mut d, r, Paint::solid(Color::BLACK), Paint::None, 0.0);
            let id = d.insert(Some(l), usize::MAX, n).unwrap();
            if col % 2 == 0 {
                hidden.insert(id);
            }
        }
    }
    assert_eq!(hidden.len(), 1_000);
    let opts = RenderOptions { hidden, ..Default::default() };
    let r = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
    for row in 0..20 {
        for col in 0..100 {
            let want = if col % 2 == 0 { [0, 0, 0, 0] } else { [0, 0, 0, 255] };
            assert_eq!(r.pixel(col, row * 5 + 2), want, "column {col}, row {row}");
        }
    }
}

/// A document with symbol "Far" holding a red rectangle `art`, and an instance of it moved by
/// (-50, -50): its 20 × 20 square, (-60, -60) to (-40, -40), lies outside a view of the page.
fn far_instance(art: Rect) -> Document {
    let mut d = Document::new(100.0, 100.0);
    let art = rect_node(&mut d, art, Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Far".into(), art: Arc::new(art) });
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Far".into(), xf: Affine::translate((-50.0, -50.0)) })).unwrap();
    d
}

#[test]
fn an_instance_is_culled_by_its_symbols_art() {
    // The art drawn from (350, 350) is out of view.
    let away = far_instance(Rect::new(400.0, 400.0, 440.0, 440.0));
    let mut rr = Renderer::new();
    assert_eq!(rr.render(&away, 100, 100, Affine::IDENTITY, &RenderOptions::default()).pixel(70, 70), [0, 0, 0, 0]);
    assert_eq!(rr.stats.culled, 1);
    // The symbol's art, from (100, 100), drawn from (50, 50): in view. The instance is the same
    // node, and the renderer measures it again.
    let mut near = away.clone();
    let art = rect_node(&mut near, Rect::new(100.0, 100.0, 140.0, 140.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    near.symbols[0].art = Arc::new(art);
    assert_eq!(rr.render(&near, 100, 100, Affine::IDENTITY, &RenderOptions::default()).pixel(70, 70), [255, 0, 0, 255]);
    assert_eq!(rr.stats.culled, 0);
}

#[test]
fn an_instance_is_culled_by_the_effects_in_its_art() {
    // The art, (-1, -1) to (1, 1), drawn from (-5, 20) lies left of the view, and its drop shadow,
    // 8 pt to the right, reaches into it.
    let mut d = Document::new(100.0, 100.0);
    let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
    art.appearance.effects.push(shadow);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf: Affine::translate((-5.0, 20.0)) })).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert!(img.pixel(3, 20)[3] > 0, "{:?}", img.pixel(3, 20));
}

#[test]
fn a_repeat_of_an_instance_is_culled_by_the_effects_in_its_art() {
    // The instance of the test above as the one copy of a one-by-one grid repeat.
    let mut d = Document::new(100.0, 100.0);
    let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
    art.appearance.effects.push(shadow);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
    let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Dot".into(), xf: Affine::translate((-5.0, 20.0)) });
    let grid = vectorcraft_doc::TileType::Grid;
    let kind =
        vectorcraft_doc::RepeatKind::Grid { h_spacing: 0.0, v_spacing: 0.0, rows: 1, cols: 1, grid_type: grid, flip_rows: false, flip_cols: false };
    let repeat = Node::new(d.alloc_id(), NodeKind::Repeat(vectorcraft_doc::RepeatSpec { kind, source: vec![Arc::new(instance)] }));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, repeat).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert!(img.pixel(3, 20)[3] > 0, "{:?}", img.pixel(3, 20));
}

#[test]
fn past_the_measured_placements_an_instance_is_culled_by_an_estimate_of_its_art_effects() {
    // Symbol `Spot` is a square from (-1, -1) to (1, 1) with a drop shadow 8 pt to the right, and
    // symbols `Dot` and `Late` each hold an instance of it. 4,100 instances of `Dot`, each turned by
    // another angle, lie far left of the view: one frame measures the effects in the art of at most
    // 4,096 placements of symbols that hold instances. An instance of `Late` at (-5, 20) comes
    // after them: its square lies left of the view, and the shadow reaches into it.
    let mut d = Document::new(100.0, 100.0);
    let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
    art.appearance.effects.push(shadow);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Spot".into(), art: Arc::new(art) });
    for name in ["Dot", "Late"] {
        let spot = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Spot".into(), xf: Affine::IDENTITY });
        let group = Node::group(d.alloc_id(), vec![Arc::new(spot)]);
        d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: name.into(), art: Arc::new(group) });
    }
    let l = d.layers[0].id;
    for i in 0..4100 {
        let xf = Affine::translate((-1000.0, 50.0)) * Affine::rotate(f64::from(i) * 1e-4);
        let id = d.alloc_id();
        d.insert(Some(l), i as usize, Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf })).unwrap();
    }
    let id = d.alloc_id();
    d.insert(Some(l), 4100, Node::new(id, NodeKind::SymbolInstance { symbol: "Late".into(), xf: Affine::translate((-5.0, 20.0)) })).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert!(img.pixel(3, 20)[3] > 0, "{:?}", img.pixel(3, 20));
}

/// 2,000 symbols, then two named "Mark" (a red square, then a blue one), and an instance of
/// "Mark" at (25, 25) and one with a drop shadow at (70, 70). With `second` the blue square's
/// symbol is named that instead.
fn marks(second: &str) -> Document {
    let mut d = Document::new(100.0, 100.0);
    for i in 0..2000 {
        let art = rect_node(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
        d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: format!("Filler {i}"), art: Arc::new(art) });
    }
    for (name, color) in [("Mark", Color::rgb(1.0, 0.0, 0.0)), (second, Color::rgb(0.0, 0.0, 1.0))] {
        let art = rect_node(&mut d, Rect::new(-10.0, -10.0, 10.0, 10.0), Paint::solid(color), Paint::None, 0.0);
        d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: name.into(), art: Arc::new(art) });
    }
    let l = d.layers[0].id;
    let plain = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((25.0, 25.0)) });
    let mut shadowed = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((70.0, 70.0)) });
    shadowed.appearance.effects.push(vectorcraft_doc::Effect {
        id: "stylize.dropShadow".into(),
        params: serde_json::json!({"x": 4, "y": 4, "blur": 0}),
        visible: true,
    });
    d.insert(Some(l), 0, plain).unwrap();
    d.insert(Some(l), 1, shadowed).unwrap();
    d
}

/// Instances among many symbols draw the first symbol of their name, the same pixels as when no
/// other symbol has that name.
#[test]
fn instances_draw_the_first_symbol_of_their_name() {
    let img = render(&marks("Mark"));
    assert_eq!(img.pixel(25, 25), [255, 0, 0, 255]);
    assert_eq!(img.pixel(70, 70), [255, 0, 0, 255]);
    assert!(img.pixels == render(&marks("Other")).pixels, "the pixels differ");
}

/// A frame that draws 300 instances with drop shadows, among 2,000 symbols, and an envelope around
/// instances reads the symbol list once to find their symbols.
#[test]
fn a_frame_reads_the_symbol_list_once() {
    let mut d = marks("Other");
    let l = d.layers[0].id;
    for i in 0..300 {
        let xf = Affine::translate((f64::from(i % 20) * 5.0, f64::from(i / 20) * 5.0)) * Affine::scale(0.2);
        let mut n = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf });
        n.appearance.effects.push(vectorcraft_doc::Effect {
            id: "stylize.dropShadow".into(),
            params: serde_json::json!({"x": 1, "y": 1, "blur": 0}),
            visible: true,
        });
        d.insert(Some(l), usize::MAX, n).unwrap();
    }
    let content = Arc::new(Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Mark".into(), xf: Affine::translate((50.0, 50.0)) }));
    let kind = vectorcraft_doc::live::EnvelopeKind::Warp { style: "arc".into(), bend: 30.0, h: 0.0, v: 0.0, horizontal: true, version: 0 };
    let envelope = NodeKind::Envelope {
        content: vec![content],
        kind: Box::new(kind),
        fidelity: 50.0,
        editing: false,
        options: Default::default(),
        frame: Box::new(Affine::IDENTITY),
    };
    let envelope = Node::new(d.alloc_id(), envelope);
    d.insert(Some(l), usize::MAX, envelope).unwrap();
    let before = vectorcraft_doc::symbols_read();
    render(&d);
    let read = vectorcraft_doc::symbols_read() - before;
    assert!(read <= d.symbols.len(), "{read} symbols read, {} in the document", d.symbols.len());
}

/// Symbols 0 to 29 each hold two instances of the next, sheared two ways, so that the 2^30
/// instances drawn from the first are all placed differently. Symbol 30 holds a square with a drop
/// shadow. An instance of symbol 0 lies far left of the view. With `effect`, every instance has
/// that effect of its own. The renderer must cull the instance of symbol 0 measuring symbol art at
/// most 20,000 times ([`FrameStats::symbol_art`]); measuring the art for every placement would take
/// 2^30 measurements or more. The frame runs on a thread of its own, and the test fails when it
/// has not finished after 300 s.
fn nested_instances_are_culled_in_bounded_time(effect: Option<vectorcraft_doc::Effect>) {
    let mut d = Document::new(100.0, 100.0);
    let mut art = rect_node(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 2, "y": 0, "blur": 0}), visible: true };
    art.appearance.effects.push(shadow);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S30".into(), art: Arc::new(art) });
    let instance = |d: &mut Document, symbol: String, xf: Affine| {
        let mut n = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol, xf });
        n.appearance.effects.extend(effect.clone());
        n
    };
    for i in (0..30).rev() {
        let next = format!("S{}", i + 1);
        let mut pair = vec![];
        for xf in [Affine::new([1.0, 0.0, 0.1, 1.0, 0.0, 0.0]), Affine::new([1.0, 0.1, 0.0, 1.0, 2.0, 0.0])] {
            pair.push(Arc::new(instance(&mut d, next.clone(), xf)));
        }
        let group = Node::group(d.alloc_id(), pair);
        d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: format!("S{i}"), art: Arc::new(group) });
    }
    let first = instance(&mut d, "S0".into(), Affine::translate((-100_000.0, 0.0)));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, first).unwrap();
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let mut r = Renderer::new();
        let img = r.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
        tx.send((img.pixel(50, 50), r.stats.culled, r.stats.symbol_art)).ok();
    });
    let (pixel, culled, measured) = rx.recv_timeout(std::time::Duration::from_secs(300)).expect("the frame within 300 s");
    assert_eq!(pixel, [0, 0, 0, 0]);
    assert!(culled > 0);
    assert!(measured > 0, "the frame stats report no symbol art measures");
    assert!(measured <= 20_000, "symbol art measured {measured} times");
}

#[test]
fn nested_instances_with_effects_are_culled_in_bounded_time() {
    nested_instances_are_culled_in_bounded_time(None);
}

#[test]
fn nested_instances_with_geometry_effects_of_their_own_are_culled_in_bounded_time() {
    // Each instance's Transform effect reshapes the art it shows before the effects in that art run.
    let moved = vectorcraft_doc::Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 1}), visible: true };
    nested_instances_are_culled_in_bounded_time(Some(moved));
}

#[test]
fn a_scaled_instance_is_culled_by_the_effects_in_its_art_as_placed() {
    // The art, (-1, -1) to (1, 1), scaled 30 times and centered on (-50, 50): (-80, 20) to
    // (-20, 80), out of view. Its Transform effect doubles the placed art: (-110, -10) to
    // (10, 110).
    let mut d = Document::new(100.0, 100.0);
    let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let double = vectorcraft_doc::Effect { id: "distort.transform".into(), params: serde_json::json!({"scaleH": 200, "scaleV": 200}), visible: true };
    art.appearance.effects.push(double);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    let xf = Affine::translate((-50.0, 50.0)) * Affine::scale(30.0);
    d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf })).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(img.pixel(5, 50), [255, 0, 0, 255]);
}

#[test]
fn an_instance_is_culled_by_the_art_its_9_slice_guides_move() {
    // A 100 × 100 frame with guides a quarter in, holding an instance of a red bar from x 10 to
    // 70. Scaled 2 times across, the bar crosses the left guide and is placed by the middle
    // column's map, x → 3x − 50: moved 110 pt right, it draws from x 90 to 270 while the frame
    // starts at x 110, out of view.
    let mut d = Document::new(100.0, 100.0);
    let bar = rect_node(&mut d, Rect::new(10.0, 40.0, 70.0, 60.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Bar".into(), art: Arc::new(bar) });
    let frame = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::None, Paint::None, 0.0);
    let inner = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Bar".into(), xf: Affine::IDENTITY });
    let art = Node::group(d.alloc_id(), vec![Arc::new(frame), Arc::new(inner)]);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Panel".into(), art: Arc::new(art) });
    d.set_symbol_nine_slice("Panel", Some(vectorcraft_doc::SymbolNineSlice::default()));
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    let xf = Affine::translate((110.0, 0.0)) * Affine::scale_non_uniform(2.0, 1.0);
    d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Panel".into(), xf })).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(img.pixel(95, 50), [255, 0, 0, 255]);
}

#[test]
fn an_instance_is_culled_by_the_art_nested_9_slice_guides_move() {
    // The panel of the test above at its natural size, in a symbol without guides. The holder's
    // instance scales the panel 2 times across and moves it 110 pt right: the bar draws from x 90
    // to 270 while the frame starts at x 110, out of view.
    let mut d = Document::new(100.0, 100.0);
    let bar = rect_node(&mut d, Rect::new(10.0, 40.0, 70.0, 60.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Bar".into(), art: Arc::new(bar) });
    let frame = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::None, Paint::None, 0.0);
    let inner = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Bar".into(), xf: Affine::IDENTITY });
    let art = Node::group(d.alloc_id(), vec![Arc::new(frame), Arc::new(inner)]);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Panel".into(), art: Arc::new(art) });
    d.set_symbol_nine_slice("Panel", Some(vectorcraft_doc::SymbolNineSlice::default()));
    let panel = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Panel".into(), xf: Affine::IDENTITY });
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Holder".into(), art: Arc::new(panel) });
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    let xf = Affine::translate((110.0, 0.0)) * Affine::scale_non_uniform(2.0, 1.0);
    d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Holder".into(), xf })).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(img.pixel(95, 50), [255, 0, 0, 255]);
}

#[test]
fn an_instance_that_only_moves_is_culled_by_the_art_its_9_slice_guides_move() {
    // A symbol made in the app: art in the 20 × 20 square, a natural size of 2 × 2, and guides a
    // quarter in. An instance that only moves draws it at 20 × 20, ten times its natural size. The
    // bar from x −9.9 to 0.1 crosses the left guide and is placed by the middle column's map,
    // x → 1.9x: moved 115 pt right, it draws from x 96.19 to 115.19 while its art box starts at
    // x 105, out of view.
    let mut d = Document::new(100.0, 100.0);
    let bar = rect_node(&mut d, Rect::new(-9.9, -1.0, 0.1, 1.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Bar".into(), art: Arc::new(bar) });
    let frame = rect_node(&mut d, Rect::new(-10.0, -10.0, 10.0, 10.0), Paint::None, Paint::None, 0.0);
    let inner = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Bar".into(), xf: Affine::IDENTITY });
    let art = Node::group(d.alloc_id(), vec![Arc::new(frame), Arc::new(inner)]);
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Panel".into(), art: Arc::new(art) });
    d.unknown.insert(vectorcraft_doc::SYMBOL_SIZES.into(), serde_json::json!({ "Panel": [2.0, 2.0] }));
    d.set_symbol_nine_slice("Panel", Some(vectorcraft_doc::SymbolNineSlice::default()));
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    let xf = Affine::translate((115.0, 50.0));
    d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Panel".into(), xf })).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(img.pixel(98, 50), [255, 0, 0, 255]);
}

#[test]
fn an_instance_with_a_geometry_effect_is_culled_by_its_reshaped_art() {
    // The art, (0, 0) to (20, 20), drawn from (-100, 10) is out of view, and the Transform effect
    // moves it 150 pt right, into view.
    let mut d = far_instance(Rect::new(0.0, 0.0, 20.0, 20.0));
    let id = d.layers[0].children().unwrap()[0].id;
    let inst = d.node_mut(id).unwrap();
    inst.kind = NodeKind::SymbolInstance { symbol: "Far".into(), xf: Affine::translate((-100.0, 10.0)) };
    let effect = vectorcraft_doc::Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 150}), visible: true };
    inst.appearance.effects.push(effect);
    assert_eq!(Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default()).pixel(60, 20), [255, 0, 0, 255]);
}

#[test]
fn an_instance_with_a_geometry_effect_is_culled_by_the_effects_in_its_reshaped_art() {
    // The art, (-1, -1) to (1, 1), and the instance at (-11, 20) each have a Transform effect that
    // scales by 4. The instance's effect scales the placed art, (-12, 19) to (-10, 21), to
    // (-15, 16) to (-7, 24), and the art's effect then scales that to (-27, 4) to (5, 36), which
    // reaches into the view.
    let mut d = Document::new(100.0, 100.0);
    let scale = serde_json::json!({"scaleH": 400, "scaleV": 400});
    let effect = || vectorcraft_doc::Effect { id: "distort.transform".into(), params: scale.clone(), visible: true };
    let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    art.appearance.effects.push(effect());
    d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
    let (id, l) = (d.alloc_id(), d.layers[0].id);
    let mut instance = Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf: Affine::translate((-11.0, 20.0)) });
    instance.appearance.effects.push(effect());
    d.insert(Some(l), 0, instance).unwrap();
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(img.pixel(2, 20), [255, 0, 0, 255]);
}

#[test]
fn view_transform_zooms() {
    let mut d = Document::new(100.0, 100.0);
    let n = rect_node(&mut d, Rect::new(0.0, 0.0, 10.0, 10.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = Renderer::new().render(&d, 100, 100, Affine::scale(5.0), &RenderOptions::default());
    assert_eq!(r.pixel(45, 45)[3], 255);
    assert_eq!(r.pixel(55, 55)[3], 0);
}

#[test]
fn outline_mode_no_fill() {
    let mut d = Document::new(100.0, 100.0);
    let n = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r =
        Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { outline: true, background: Some([255; 4]), ..Default::default() });
    assert_eq!(r.pixel(50, 50), [255, 255, 255, 255]);
    assert!(r.pixel(10, 50)[0] < 200);
}

/// A 100 × 100 document with each kind of path Outline view draws, and the ids of its objects by
/// name: a stroked square, a compound path with a hole, a path and a compound path with a live
/// effect, a guide, a clip group, an Image Trace object showing its outlines, and a path in a layer
/// with Preview off. The clip group holds a path reaching past its clipping path, a compound path,
/// a group of two paths, a nested clip group, a path with a live effect, a hidden path and an
/// unstroked path a tenth of a point wide.
fn outline_doc() -> (Document, Vec<(&'static str, NodeId)>) {
    let mut d = Document::new(100.0, 100.0);
    let mut ids = vec![];
    let black = || Paint::solid(Color::BLACK);
    let offset = |by: f64| vectorcraft_doc::Effect { id: "path.offsetPath".into(), params: serde_json::json!({ "offset": by }), visible: true };
    let compound = |d: &mut Document, outer: Rect, hole: Rect| {
        let children = [outer, hole].map(|r| Arc::new(rect_node(d, r, Paint::None, Paint::None, 0.0))).to_vec();
        let mut n = Node::new(d.alloc_id(), NodeKind::Compound { children, rule: FillRule::EvenOdd });
        n.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0).into();
        n
    };
    let clipping = |mut n: Node| {
        if let NodeKind::Path { clipping, .. } = &mut n.kind {
            *clipping = true;
        }
        n
    };
    let mut named = |name: &'static str, n: Node| {
        ids.push((name, n.id));
        Arc::new(n)
    };
    let square = named("square", rect_node(&mut d, Rect::new(5.0, 5.0, 25.0, 25.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), black(), 2.0));
    let holed = named("holed", compound(&mut d, Rect::new(30.0, 5.0, 50.0, 25.0), Rect::new(35.0, 10.0, 45.0, 20.0)));
    let mut fx_path = rect_node(&mut d, Rect::new(55.0, 8.0, 70.0, 22.0), black(), Paint::None, 0.0);
    fx_path.appearance.effects.push(offset(2.0));
    let fx_path = named("fx path", fx_path);
    let mut fx_compound = compound(&mut d, Rect::new(75.0, 5.0, 95.0, 25.0), Rect::new(80.0, 10.0, 90.0, 20.0));
    fx_compound.appearance.effects.push(offset(-1.0));
    let fx_compound = named("fx compound", fx_compound);
    let mut guide = Node::path(d.alloc_id(), shapes::line(Point::new(5.0, 30.0), Point::new(95.0, 30.0)), Appearance::default());
    if let NodeKind::Path { guide, .. } = &mut guide.kind {
        *guide = true;
    }
    let guide = named("guide", guide);
    // The clip group.
    let ellipse = Node::path(d.alloc_id(), shapes::ellipse(Rect::new(5.0, 35.0, 55.0, 85.0)), Appearance::basic(Paint::None, Paint::None, 0.0));
    let clip_path = named("clipping path", clipping(ellipse));
    let past = named("past the clip", rect_node(&mut d, Rect::new(0.0, 50.0, 60.0, 60.0), Paint::None, black(), 1.0));
    let member_compound = named("member compound", compound(&mut d, Rect::new(10.0, 65.0, 20.0, 75.0), Rect::new(12.0, 67.0, 18.0, 73.0)));
    let pair_rect = named("pair rect", rect_node(&mut d, Rect::new(25.0, 65.0, 30.0, 75.0), black(), Paint::None, 0.0));
    let pair_ellipse = Node::path(d.alloc_id(), shapes::ellipse(Rect::new(32.0, 65.0, 40.0, 75.0)), Appearance::basic(black(), Paint::None, 0.0));
    let pair_ellipse = named("pair ellipse", pair_ellipse);
    let pair = named("pair", Node::group(d.alloc_id(), vec![pair_rect, pair_ellipse]));
    let nested_clip = named("nested clipping path", clipping(rect_node(&mut d, Rect::new(42.0, 62.0, 54.0, 80.0), Paint::None, Paint::None, 0.0)));
    let nested_path = named("nested path", rect_node(&mut d, Rect::new(44.0, 64.0, 70.0, 78.0), black(), Paint::None, 0.0));
    let nested = named("nested", Node::new(d.alloc_id(), NodeKind::Group { children: vec![nested_clip, nested_path], clip: true }));
    let mut member_fx = rect_node(&mut d, Rect::new(15.0, 40.0, 30.0, 47.0), black(), Paint::None, 0.0);
    member_fx.appearance.effects.push(offset(1.5));
    let member_fx = named("member fx", member_fx);
    let mut hidden = rect_node(&mut d, Rect::new(20.0, 80.0, 40.0, 84.0), Paint::None, black(), 1.0);
    hidden.visible = false;
    let hidden = named("hidden", hidden);
    let tiny = named("tiny", rect_node(&mut d, Rect::new(48.0, 56.0, 48.1, 56.1), black(), Paint::None, 0.0));
    let members = vec![clip_path, past, member_compound, pair, nested, member_fx, hidden, tiny];
    let clip = named("clip group", Node::new(d.alloc_id(), NodeKind::Group { children: members, clip: true }));
    // The Image Trace object: its hidden source, then the traced shapes.
    let mut source = rect_node(&mut d, Rect::new(60.0, 35.0, 95.0, 95.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)), Paint::None, 0.0);
    source.visible = false;
    let shape_a =
        named("trace shape a", rect_node(&mut d, Rect::new(62.0, 40.0, 80.0, 60.0), Paint::solid(Color::rgb(0.0, 0.5, 0.0)), Paint::None, 0.0));
    let shape_b = Node::path(
        d.alloc_id(),
        shapes::ellipse(Rect::new(70.0, 62.0, 92.0, 84.0)),
        Appearance::basic(Paint::solid(Color::rgb(1.0, 0.5, 0.0)), Paint::None, 0.0),
    );
    let shape_b = named("trace shape b", shape_b);
    let mut traced = Node::group(d.alloc_id(), vec![Arc::new(source), shape_a, shape_b]);
    traced.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "outlines" })));
    let traced = named("traced", traced);
    let unpreviewed =
        named("unpreviewed", rect_node(&mut d, Rect::new(5.0, 88.0, 40.0, 97.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0));
    let l = d.layers[0].id;
    for n in [square, holed, fx_path, fx_compound, guide, clip, traced] {
        d.insert(Some(l), usize::MAX, Arc::unwrap_or_clone(n)).unwrap();
    }
    let layer = Node::layer(d.alloc_id(), "Preview off", vectorcraft_doc::LayerColor::Preset(1));
    let off = d.insert(None, usize::MAX, layer).unwrap();
    if let Some(NodeKind::Layer { preview, .. }) = d.node_mut(off).map(|n| &mut n.kind) {
        *preview = false;
    }
    d.insert(Some(off), 0, Arc::unwrap_or_clone(unpreviewed)).unwrap();
    (d, ids)
}

/// The id `outline_doc` gave the object `name`.
fn id_of(ids: &[(&'static str, NodeId)], name: &str) -> NodeId {
    ids.iter().find(|(n, _)| *n == name).map(|(_, id)| *id).unwrap_or_else(|| panic!("no object {name}"))
}

/// Outline frames drawn from the geometry a renderer keeps between frames are the frames a new
/// renderer draws, at three views each drawn twice, and after a member of the clip group moves
/// (here with dotted guides). In Preview the Image Trace object's outlines and the layer with
/// Preview off are drawn in outline.
#[test]
fn outline_frames_from_kept_geometry_match_a_new_renderer() {
    let (d, ids) = outline_doc();
    let outline = RenderOptions { outline: true, guide: ([0, 128, 255], true), background: Some([255; 4]), ..Default::default() };
    let screen = RenderOptions { trace_views: true, background: Some([255; 4]), ..Default::default() };
    let views = [Affine::IDENTITY, Affine::scale(4.0) * Affine::translate((-40.0, -35.0)), Affine::translate((6.0, -4.0))];
    let mut moved = d.clone();
    if let Some(n) = moved.node_mut(id_of(&ids, "past the clip")) {
        n.transform(Affine::translate((3.0, 2.0)), false);
    }
    for opts in [&outline, &screen] {
        let mut kept = Renderer::new();
        for (d, view) in views.iter().chain(&views).map(|v| (&d, v)).chain([(&moved, &Affine::IDENTITY)]) {
            let new = Renderer::new().render(d, 100, 100, *view, opts);
            assert_eq!(kept.render(d, 100, 100, *view, opts).pixels, new.pixels, "outline: {}, view {view:?}", opts.outline);
        }
    }
    // Outline view draws the members of the clip group unclipped, the tenth-of-a-point path as a
    // dot, and no fills.
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &outline);
    assert!(img.pixel(58, 50)[0] < 200, "the member past the clipping path");
    assert!(img.pixel(48, 56)[0] < 255, "the tenth-of-a-point path");
    assert_eq!(img.pixel(15, 15), [255; 4], "the square's fill");
}

/// Outline view draws paths and compound paths from the geometry the renderer keeps for them,
/// the members of clip groups and the shapes of an Image Trace object showing its outlines
/// included: the next frame draws the same paths. A member of a clip group keeps its live-effect
/// geometry, and the members out of view are culled by bounds the renderer keeps.
#[test]
fn outline_view_draws_paths_from_kept_geometry() {
    let (d, ids) = outline_doc();
    let kept_paths = |r: &Renderer| -> HashMap<NodeId, Arc<BezPath>> { r.geom.values().filter_map(|e| Some((e.node.id, e.path.clone()?))).collect() };
    let outline = RenderOptions { outline: true, ..Default::default() };
    let screen = RenderOptions { trace_views: true, ..Default::default() };
    let in_outline = [
        "square",
        "holed",
        "fx path",
        "fx compound",
        "guide",
        "clipping path",
        "past the clip",
        "member compound",
        "pair rect",
        "pair ellipse",
        "nested clipping path",
        "nested path",
        "member fx",
        "tiny",
        "trace shape a",
        "trace shape b",
        "unpreviewed",
    ];
    for (opts, drawn) in [(&outline, &in_outline[..]), (&screen, &["trace shape a", "trace shape b", "unpreviewed"][..])] {
        let mut r = Renderer::new();
        r.render(&d, 100, 100, Affine::IDENTITY, opts);
        let first = kept_paths(&r);
        r.render(&d, 100, 100, Affine::IDENTITY, opts);
        let second = kept_paths(&r);
        for name in drawn {
            let id = id_of(&ids, name);
            let path = first.get(&id).unwrap_or_else(|| panic!("outline: {}: no path kept for {name}", opts.outline));
            assert!(second.get(&id).is_some_and(|p| Arc::ptr_eq(p, path)), "outline: {}: {name} drawn again from its kept path", opts.outline);
        }
        assert!(!first.contains_key(&id_of(&ids, "hidden")));
    }
    let mut r = Renderer::new();
    r.render(&d, 100, 100, Affine::IDENTITY, &outline);
    let member_fx = d.node(id_of(&ids, "member fx")).map(|n| n as *const Node as usize);
    assert!(member_fx.is_some_and(|key| r.fx_geom.contains_key(&key)), "the member's live-effect geometry is kept");
    // Zoomed in 4× on part of the clip group.
    let mut r = Renderer::new();
    r.render(&d, 100, 100, Affine::scale(4.0) * Affine::translate((-40.0, -35.0)), &outline);
    for name in ["member compound", "pair", "member fx"] {
        let id = id_of(&ids, name);
        assert!(r.geom.values().any(|e| e.node.id == id && e.bounds.is_some() && e.path.is_none()), "{name}: culled by kept bounds");
    }
}

/// An Image Trace object listed in `trace_source` draws its source image on screen whatever its
/// View, also inside a group whose raster effect a renderer keeps; without the list, and in
/// exports, it draws its result. A group that is not an Image Trace object draws its members.
#[test]
fn trace_source_draws_the_source_image_of_the_listed_objects() {
    use vectorcraft_doc::{ImageBlob, ImageObject};
    let img = image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]));
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let mut d = Document::new(100.0, 100.0);
    d.images.insert("red".into(), ImageBlob::new("image/png", png));
    // A group at `x`: the hidden red source image, then a blue traced square over it.
    let group = |d: &mut Document, x: f64, traced: bool| {
        let xf = Affine::translate((x, 10.0)) * Affine::scale(20.0);
        let im = ImageObject { key: "red".into(), width: 1, height: 1, xf, link: None, placement: Default::default(), pixels: None };
        let mut source = Node::new(d.alloc_id(), NodeKind::Image(Box::new(im)));
        source.visible = false;
        let shape = rect_node(d, Rect::new(x, 10.0, x + 20.0, 30.0), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
        let mut g = Node::group(d.alloc_id(), vec![Arc::new(source), Arc::new(shape)]);
        if traced {
            g.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "tracingResult" })));
        }
        g
    };
    let plain = group(&mut d, 10.0, true);
    let blurred = group(&mut d, 40.0, true);
    let other = group(&mut d, 70.0, false);
    let ids = Arc::new(vec![plain.id, blurred.id, other.id]);
    let mut holder = Node::group(d.alloc_id(), vec![Arc::new(blurred)]);
    holder.appearance.effects.push(vectorcraft_doc::Effect { id: "blur.gaussian".into(), params: serde_json::json!({ "radius": 1 }), visible: true });
    let l = d.layers[0].id;
    for n in [plain, holder, other] {
        d.insert(Some(l), usize::MAX, n).unwrap();
    }
    let screen = RenderOptions { trace_views: true, background: Some([255; 4]), ..Default::default() };
    let held = RenderOptions { trace_source: Some(ids.clone()), ..screen.clone() };
    let export = RenderOptions { trace_source: Some(ids), background: Some([255; 4]), ..Default::default() };
    let red = |p: [u8; 4]| p[0] > 200 && p[2] < 60;
    let blue = |p: [u8; 4]| p[2] > 200 && p[0] < 60;
    let mut kept = Renderer::new();
    for (opts, source) in [(&screen, false), (&held, true), (&screen, false), (&export, false)] {
        for r in [kept.render(&d, 100, 100, Affine::IDENTITY, opts), Renderer::new().render(&d, 100, 100, Affine::IDENTITY, opts)] {
            for (x, what) in [(20, "the Image Trace object"), (50, "the one in the blurred group")] {
                let p = r.pixel(x, 20);
                assert!(if source { red(p) } else { blue(p) }, "{what} with trace_source {source}: {p:?}");
            }
            assert!(blue(r.pixel(80, 20)), "the plain group draws its square: {:?}", r.pixel(80, 20));
        }
    }
}

#[test]
fn trace_source_applies_the_hidden_image_mask() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};

    let mut png = Vec::new();
    image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let mut d = Document::new(100.0, 100.0);
    d.images.insert("trace-source".into(), ImageBlob::new("image/png", png));
    let image = ImageObject {
        key: "trace-source".into(),
        width: 1,
        height: 1,
        xf: Affine::translate((10.0, 10.0)) * Affine::scale(80.0),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let mut source = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    source.visible = false;
    let mask = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    source.extras_mut().mask = Some(Box::new(OpacityMask::new(mask, true)));
    let result = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
    let mut trace = Node::group(d.alloc_id(), vec![Arc::new(source), Arc::new(result)]);
    trace.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "tracingResult" })));
    let trace_id = trace.id;
    d.insert(Some(d.layers[0].id), 0, trace).unwrap();

    let opts = RenderOptions { trace_views: true, trace_source: Some(Arc::new(vec![trace_id])), background: Some([255; 4]), ..Default::default() };
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(30, 50), [255, 0, 0, 255]);
    assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
}

/// The Split view draws the source image left of its divider and the traced shapes right of it,
/// along the image's own width (also when the image is turned), with traced art past any of the
/// image's edges drawn whatever the divider, in the same frames a renderer that keeps its caches
/// draws, and in a zoomed and scrolled view and a turned view; exports draw the tracing result.
#[test]
fn split_view_draws_the_source_on_one_side_of_the_divider_and_the_result_on_the_other() {
    use vectorcraft_doc::{ImageBlob, ImageObject};
    let mut png = vec![];
    image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let red = |p: [u8; 4]| p[0] > 200 && p[1] < 60 && p[2] < 60;
    let blue = |p: [u8; 4]| p[2] > 200 && p[0] < 60;
    // A red 80 × 80 source image at 10, 10, turned a half turn about its center when `turned`, with
    // a blue traced square over all of it.
    for turned in [false, true] {
        let mut d = Document::new(100.0, 100.0);
        d.images.insert("red".into(), ImageBlob::new("image/png", png.clone()));
        let turn = if turned { Affine::rotate_about(std::f64::consts::PI, kurbo::Point::new(50.0, 50.0)) } else { Affine::IDENTITY };
        let xf = turn * Affine::translate((10.0, 10.0)) * Affine::scale(80.0);
        let im = ImageObject { key: "red".into(), width: 1, height: 1, xf, link: None, placement: Default::default(), pixels: None };
        let mut source = Node::new(d.alloc_id(), NodeKind::Image(Box::new(im)));
        source.visible = false;
        let shape = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
        // Traced art past the image's right edge on the page (a stroke along it, say).
        let past = rect_node(&mut d, Rect::new(92.0, 46.0, 98.0, 54.0), Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0);
        // And above the image's top edge, near its left edge.
        let above = rect_node(&mut d, Rect::new(14.0, 2.0, 26.0, 8.0), Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0);
        let mut g = Node::group(d.alloc_id(), vec![Arc::new(source), Arc::new(shape), Arc::new(past), Arc::new(above)]);
        let id = g.id;
        g.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "split", "split": 0.25 })));
        d.insert(Some(d.layers[0].id), 0, g).unwrap();
        let screen = RenderOptions { trace_views: true, background: Some([255; 4]), ..Default::default() };
        let export = RenderOptions { background: Some([255; 4]), ..Default::default() };
        let mut kept = Renderer::new();
        for split in [0.25, 0.0, 1.0, 0.25] {
            if let Some(n) = d.node_mut(id) {
                n.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "split", "split": split })));
            }
            // The divider in page space: 30 across from the image's left edge, or from its right
            // edge on the page once the image is turned.
            let at = if turned { 90.0 - 80.0 * split } else { 10.0 + 80.0 * split };
            // The view: as is, zoomed in twice and scrolled, and turned a half turn about the page's
            // middle; `px` finds the pixel showing page point (x, y).
            let views: [(Affine, u32); 3] = [
                (Affine::IDENTITY, 100),
                (Affine::translate((5.0, 7.0)) * Affine::scale(2.0), 210),
                (Affine::rotate_about(std::f64::consts::PI, kurbo::Point::new(50.0, 50.0)), 100),
            ];
            for (view, size) in views {
                let px = |r: &Rendered, x: f64, y: f64| {
                    let q = view * kurbo::Point::new(x, y);
                    r.pixel(q.x.floor() as u32, q.y.floor() as u32)
                };
                for r in [kept.render(&d, size, size, view, &screen), Renderer::new().render(&d, size, size, view, &screen)] {
                    for x in [12.5, 30.5, 48.5, 52.5, 70.5, 88.5] {
                        let p = px(&r, x, 50.5);
                        let source = if turned { x > at } else { x < at };
                        assert!(if source { red(p) } else { blue(p) }, "turned {turned}, split {split}, view {view:?}, x = {x}: {p:?}");
                    }
                    assert_eq!(px(&r, 5.5, 50.5), [255; 4], "nothing outside the image");
                    // The art past the image's edges shows on either side of the divider.
                    let green = |p: [u8; 4]| p[1] > 200 && p[0] < 60 && p[2] < 60;
                    for (x, y) in [(95.5, 50.5), (20.5, 5.5)] {
                        let p = px(&r, x, y);
                        assert!(green(p), "turned {turned}, split {split}, view {view:?}, ({x}, {y}): {p:?}");
                    }
                }
            }
            let r = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &export);
            assert!([12u32, 50, 88].iter().all(|x| blue(r.pixel(*x, 50))), "exports draw the result");
        }
    }
}

#[test]
fn artboards_white_on_pasteboard() {
    let d = Document::new(50.0, 50.0);
    let r = Renderer::new().render(
        &d,
        100,
        100,
        Affine::IDENTITY,
        &RenderOptions { artboards: true, background: Some([30, 30, 30, 255]), ..Default::default() },
    );
    assert_eq!(r.pixel(25, 25), [255, 255, 255, 255]);
    assert_eq!(r.pixel(75, 75), [30, 30, 30, 255]);
}

#[test]
fn region_export_png() {
    let mut d = Document::new(100.0, 100.0);
    let n = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = Renderer::new().render_region(&d, d.artboards[0].rect, 2.0, true);
    assert_eq!((r.width, r.height), (200, 200));
    let png = r.to_png().unwrap();
    assert_eq!(&png[1..4], b"PNG");
}

#[test]
fn arrowheads_render() {
    let mut d = Document::new(100.0, 100.0);
    let id = d.alloc_id();
    let mut n =
        Node::path(id, shapes::line(Point::new(10.0, 50.0), Point::new(80.0, 50.0)), Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 2.0));
    n.appearance.stroke_mut().unwrap().end_arrow = Some(Arrowhead::Triangle);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let r = render(&d);
    // The arrowhead (past the end point by default: tip at 84) is wider than the 2 pt line.
    assert!(r.pixel(78, 48)[0] < 128);
    assert!(r.pixel(85, 50)[0] > 200, "nothing past the tip");
}

use vectorcraft_doc::Arrowhead;
use vectorcraft_geom::Point;

/// A red 10..90 square masked by a white square covering only its left half (10..50),
/// plus a mid-grey strip (50..70) — rendered both multi- and single-threaded.
fn masked_doc(clip: bool, invert: bool) -> Document {
    let mut d = Document::new(100.0, 100.0);
    let mut n = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let white = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let grey = rect_node(&mut d, Rect::new(50.0, 10.0, 70.0, 90.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)), Paint::None, 0.0);
    let gid = d.alloc_id();
    let art = Node::group(gid, vec![Arc::new(white), Arc::new(grey)]);
    let mut m = vectorcraft_doc::OpacityMask::new(art, clip);
    m.invert = invert;
    n.extras_mut().mask = Some(Box::new(m));
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    d
}

#[test]
fn opacity_mask_uses_luminance_and_clips() {
    for threads in [0, 2] {
        let mut r = Renderer::new();
        r.threads = threads;
        let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
        let img = r.render(&masked_doc(true, false), 100, 100, Affine::IDENTITY, &opts);
        assert_eq!(img.pixel(30, 50), [255, 0, 0, 255], "white mask → opaque ({threads} threads)");
        let mid = img.pixel(60, 50);
        assert!((100..160).contains(&mid[1]), "grey mask → about half opacity, got {mid:?}");
        assert_eq!(img.pixel(80, 50), [255, 255, 255, 255], "clip hides outside the mask art");
    }
}

#[test]
fn opacity_mask_without_clip_and_inverted() {
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let img = Renderer::new().render(&masked_doc(false, false), 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(80, 50), [255, 0, 0, 255], "no clip: outside the mask art stays visible");
    let img = Renderer::new().render(&masked_doc(true, true), 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(30, 50), [255, 255, 255, 255], "inverted white → hidden");
    assert_eq!(img.pixel(80, 50), [255, 0, 0, 255], "inverted clip background → visible");
}

#[test]
fn opacity_mask_art_root_applies_its_own_mask() {
    use vectorcraft_doc::OpacityMask;

    let mut d = Document::new(100.0, 100.0);
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let mut art = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let inner = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    art.extras_mut().mask = Some(Box::new(OpacityMask::new(inner, true)));
    art.visible = false;
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, true)));
    let object_id = object.id;
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let mut renderer = Renderer::new();
    let img = renderer.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(30, 50), [255, 0, 0, 255]);
    assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
    let view = renderer.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mask_view: Some(object_id), ..opts });
    assert_eq!(view.pixel(30, 50), [255, 255, 255, 255]);
    assert_eq!(view.pixel(70, 50), [0, 0, 0, 255]);
}

#[test]
fn undecodable_image_in_mask_art_root_mask_leaves_out_outer_object() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    d.images.insert("broken-root-mask-image".into(), ImageBlob::new("image/png", b"not a PNG".to_vec()));
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let mut art = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let image = ImageObject {
        key: "broken-root-mask-image".into(),
        width: 20,
        height: 20,
        xf: Affine::translate((30.0, 30.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    art.extras_mut().mask = Some(Box::new(OpacityMask::new(Node::new(d.alloc_id(), NodeKind::Image(Box::new(image))), false)));
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, true)));
    let object_id = object.id;
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255; 4]), ..Default::default() };
    let mut renderer = Renderer::new();
    let img = renderer.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
    let view = renderer.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mask_view: Some(object_id), ..opts });
    assert_eq!(view.pixel(70, 50), [0, 0, 0, 255]);
}

#[test]
fn placed_symbol_art_root_applies_its_own_mask() {
    use vectorcraft_doc::{NodeKind, OpacityMask};

    for grouped in [true, false] {
        let mut d = Document::new(100.0, 100.0);
        let red = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
        let mut art = if grouped { Node::group(d.alloc_id(), vec![Arc::new(red)]) } else { red };
        let inner = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
        art.extras_mut().mask = Some(Box::new(OpacityMask::new(inner, true)));
        d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Masked".into(), art: Arc::new(art) });
        let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Masked".into(), xf: Affine::IDENTITY });
        d.insert(Some(d.layers[0].id), 0, instance).unwrap();

        let img = render(&d);
        assert_eq!(img.pixel(30, 50), [255, 0, 0, 255], "grouped {grouped}");
        assert_eq!(img.pixel(70, 50), [255, 255, 255, 255], "grouped {grouped}");
    }
}

#[test]
fn node_thumbnail_applies_the_root_mask() {
    use vectorcraft_doc::OpacityMask;

    let mut d = Document::new(100.0, 100.0);
    let mut node = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let mask = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    node.extras_mut().mask = Some(Box::new(OpacityMask::new(mask, true)));

    let img = Renderer::new().render_node_thumbnail(&d, &node, 100, Some([255; 4])).unwrap();
    assert_eq!(img.pixel(25, 50), [255, 0, 0, 255]);
    assert_eq!(img.pixel(75, 50), [255, 255, 255, 255]);
}

/// #3602: a thumbnail reports the size it was drawn at, and one too small for its 1-pixel margin
/// still draws the art.
#[test]
fn node_thumbnail_reports_the_size_it_draws() {
    let mut d = Document::new(100.0, 100.0);
    let node = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let mut r = Renderer::new();
    for size in 0..4 {
        let img = r.render_node_thumbnail(&d, &node, size, None).unwrap();
        assert_eq!(img.pixels.len(), img.width as usize * img.height as usize * 4, "size {size}");
        assert_eq!(img.width, size.max(1), "size {size}");
        assert_eq!(img.height, size.max(1), "size {size}");
        assert!(img.pixels.chunks(4).any(|p| p[3] > 0), "size {size}: the art is drawn");
    }
}

/// A node rendered with `AntiAlias::None` has hard edges, as a page render with that option does.
#[test]
fn node_renders_follow_the_anti_alias_option() {
    let mut d = Document::new(100.0, 100.0);
    let id = d.alloc_id();
    let n = Node::path(id, shapes::ellipse(Rect::new(10.3, 10.3, 60.6, 60.6)), Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0));
    let node = Arc::new(n.clone());
    let hard = RenderOptions { anti_alias: AntiAlias::None, ..Default::default() };
    let partial = |img: &Rendered| img.pixels.chunks(4).any(|p| p[3] != 0 && p[3] != 255);
    let drawn = |img: &Rendered| img.pixels.chunks(4).any(|p| p[3] == 255);
    let mut r = Renderer::new();
    assert!(partial(&r.render_node(&d, &node, 100, 100, Affine::IDENTITY).unwrap()), "art anti-aliasing has partial edge pixels");
    assert!(partial(&r.render_node_thumbnail(&d, &n, 37, None).unwrap()), "art anti-aliasing has partial edge pixels");
    let img = r.render_node_with(&d, &node, 100, 100, Affine::IDENTITY, &hard).unwrap();
    assert!(drawn(&img) && !partial(&img), "render_node_with: every alpha is 0 or 255");
    let thumb = r.render_node_thumbnail_with(&d, &n, 37, None, &hard).unwrap();
    assert!(drawn(&thumb) && !partial(&thumb), "render_node_thumbnail_with: every alpha is 0 or 255");
}

/// Node renders follow `supersample` and `precise` as a page render does: two rectangles that meet
/// at x = 50.37 leave no seam when supersampled, and a node render gives the pixels of a page
/// render of the same art with the same options.
#[test]
fn node_renders_follow_the_supersample_and_precise_options() {
    let mut d = Document::new(100.0, 100.0);
    let left = rect_node(&mut d, Rect::new(10.0, 10.0, 50.37, 90.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let right = rect_node(&mut d, Rect::new(50.37, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(0.2, 0.6, 0.9)), Paint::None, 0.0);
    // Blended at half opacity over both, so the group composites offscreen.
    let mut over = rect_node(&mut d, Rect::new(30.0, 60.0, 70.0, 80.0), Paint::solid(Color::rgb(0.9, 0.7, 0.1)), Paint::None, 0.0);
    over.opacity = 0.5;
    over.blend = vectorcraft_color::BlendMode::Multiply;
    let group = Node::group(d.alloc_id(), vec![Arc::new(left), Arc::new(right), Arc::new(over)]);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, group.clone()).unwrap();
    let node = Arc::new(group.clone());
    let page = |opts: &RenderOptions| RenderOptions { background: None, artboards: false, ..opts.clone() };
    let mut r = Renderer::new();
    for (supersample, precise) in [(false, false), (true, false), (false, true), (true, true)] {
        let opts = RenderOptions { supersample, precise, ..Default::default() };
        let case = format!("supersample {supersample}, precise {precise}");
        let img = r.render_node_with(&d, &node, 100, 100, Affine::IDENTITY, &opts).unwrap();
        let whole = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &page(&opts));
        assert!(img.pixels == whole.pixels, "{case}: render_node_with draws as a page render does");
        // Pixel column 50 at row 50: the black rectangle covers 37% of it and the blue one the rest.
        let seam = img.pixels[(50 * 100 + 50) * 4 + 3];
        assert_eq!(seam == 255, supersample, "{case}: render_node_with seam alpha {seam}");
        // A 37-pixel thumbnail draws 0.4375 pixels a point, the seam at x = 18.66.
        let thumb = r.render_node_thumbnail_with(&d, &group, 37, None, &opts).unwrap();
        let view = Affine::translate((18.5, 18.5)) * Affine::scale(0.4375) * Affine::translate((-50.0, -50.0));
        let whole = Renderer::new().render(&d, 37, 37, view, &page(&opts));
        assert!(thumb.pixels == whole.pixels, "{case}: the thumbnail draws as a page render does");
        let seam = thumb.pixels[(18 * 37 + 18) * 4 + 3];
        assert_eq!(seam == 255, supersample, "{case}: thumbnail seam alpha {seam}");
    }
}

#[test]
fn opacity_mask_with_undecodable_image_leaves_out_object() {
    use vectorcraft_doc::{Effect, ImageBlob, ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    d.images.insert("broken-mask-image".into(), ImageBlob::new("image/png", b"not a PNG".to_vec()));
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let image = ImageObject {
        key: "broken-mask-image".into(),
        width: 20,
        height: 20,
        xf: Affine::translate((30.0, 30.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let mut image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    image.appearance.effects.push(Effect { id: "blur.gaussian".into(), params: serde_json::json!({"radius": 4.0}), visible: true });
    let white = rect_node(&mut d, Rect::new(10.0, 10.0, 20.0, 20.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let art = Node::group(d.alloc_id(), vec![Arc::new(white), Arc::new(image)]);
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    let object_id = object.id;
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let pending = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { kept_images_only: true, ..opts.clone() });
    assert_eq!(pending.pixel(70, 70), [255, 0, 0, 255], "an image awaiting decoding does not invalidate the mask");
    let mut renderer = Renderer::new();
    let img = renderer.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(70, 70), [255, 255, 255, 255], "missing mask image leaves out the object");
    let again = renderer.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(again.pixel(70, 70), [255, 255, 255, 255], "the cached raster keeps the mask incomplete");
    let mask_view = renderer.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mask_view: Some(object_id), ..opts.clone() });
    assert_eq!(mask_view.pixel(70, 70), [0, 0, 0, 255], "an incomplete mask view has no coverage");
    let marked = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mark_undecoded_images: true, ..opts.clone() });
    assert_eq!(marked.pixel(70, 70), [255, 0, 0, 255], "the undecoded-image mark remains part of the mask");
    let kept = RenderOptions { kept_images_only: true, ..opts.clone() };
    for frame in 0..2 {
        let img = renderer.render(&d, 100, 100, Affine::IDENTITY, &kept);
        assert_eq!(img.pixel(70, 70), [255, 255, 255, 255], "an image already known undecodable leaves out the object on frame {frame}");
    }

    let working = Renderer::new().render(&masked_doc(false, false), 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(working.pixel(80, 50), [255, 0, 0, 255], "a complete mask still draws the object");
}

#[test]
fn opacity_mask_with_missing_image_in_pattern_leaves_out_object() {
    use vectorcraft_doc::pattern::{PatternDef, pattern_paint};
    use vectorcraft_doc::{ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    let image = ImageObject {
        key: "missing-pattern-image".into(),
        width: 20,
        height: 20,
        xf: Affine::IDENTITY,
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    let mut pattern = PatternDef::new("Missing image", vec![Arc::new(image)]);
    pattern.tile = Rect::new(0.0, 0.0, 20.0, 20.0);
    d.patterns.push(pattern);
    let art = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), pattern_paint("Missing image"), Paint::None, 0.0);
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let mut r = Renderer::new();
    for frame in 0..2 {
        let img = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
        assert_eq!(img.pixel(70, 50), [255, 255, 255, 255], "frame {frame}: incomplete pattern mask leaves out the object");
    }
}

#[test]
fn opacity_mask_with_marked_image_in_pattern_draws_object() {
    use vectorcraft_doc::pattern::{PatternDef, pattern_paint};
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    d.images.insert("cut-mask".into(), ImageBlob::new("image/png", b"not a PNG: pattern mask".to_vec()));
    let image =
        ImageObject { key: "cut-mask".into(), width: 20, height: 20, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
    let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    let mut pattern = PatternDef::new("Cut mask", vec![Arc::new(image)]);
    pattern.tile = Rect::new(0.0, 0.0, 20.0, 20.0);
    d.patterns.push(pattern);
    let art = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), pattern_paint("Cut mask"), Paint::None, 0.0);
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let mut r = Renderer::new();
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let off = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(off.pixel(70, 50), [255, 255, 255, 255], "an unmarked missing image leaves out the object");
    let on = r.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mark_undecoded_images: true, ..opts });
    assert_eq!(on.pixel(70, 50), [255, 0, 0, 255], "the marked image keeps the mask complete");
    assert!(on.pixel(15, 15)[1] < 255, "the marked pattern contributes mask coverage");
}

#[test]
fn complete_image_mask_with_blur_reuses_cached_raster() {
    use vectorcraft_doc::{Effect, ImageBlob, ImageObject, NodeKind, OpacityMask};

    let mut png = Vec::new();
    image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(20, 20, image::Rgba([255, 255, 255, 255])))
        .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
        .unwrap();
    let mut d = Document::new(100.0, 100.0);
    d.images.insert("complete-mask-image".into(), ImageBlob::new("image/png", png));
    let image = ImageObject {
        key: "complete-mask-image".into(),
        width: 20,
        height: 20,
        xf: Affine::translate((30.0, 30.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let mut image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    image.appearance.effects.push(Effect { id: "blur.gaussian".into(), params: serde_json::json!({"radius": 4.0}), visible: true });
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let white = rect_node(&mut d, Rect::new(10.0, 10.0, 20.0, 20.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let art = Node::group(d.alloc_id(), vec![Arc::new(white), Arc::new(image)]);
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let mut r = Renderer::new();
    let first = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(first.pixel(70, 70), [255, 0, 0, 255]);
    let raster = r.pixel_fx.values().next().unwrap().image.clone();
    let second = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(second.pixels, first.pixels);
    assert!(Arc::ptr_eq(&raster, &r.pixel_fx.values().next().unwrap().image), "the second frame draws the image mask from its cached effect raster");
}

#[test]
fn missing_image_mask_with_blur_stays_incomplete_from_cached_raster() {
    use vectorcraft_doc::{Effect, ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    let image = ImageObject {
        key: "missing-blurred-mask-image".into(),
        width: 20,
        height: 20,
        xf: Affine::translate((30.0, 30.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let mut image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    image.appearance.effects.push(Effect { id: "blur.gaussian".into(), params: serde_json::json!({"radius": 4.0}), visible: true });
    let white = rect_node(&mut d, Rect::new(10.0, 10.0, 20.0, 20.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let art = Node::group(d.alloc_id(), vec![Arc::new(white), Arc::new(image)]);
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let mut r = Renderer::new();
    let first = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(first.pixel(70, 70), [255, 255, 255, 255]);
    let raster = r.pixel_fx.values().next().unwrap().image.clone();
    let second = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(second.pixel(70, 70), [255, 255, 255, 255]);
    assert!(Arc::ptr_eq(&raster, &r.pixel_fx.values().next().unwrap().image), "the incomplete raster is reused");
}

#[test]
fn missing_image_mask_with_rasterize_and_shadow_keeps_incomplete_content_raster() {
    use vectorcraft_doc::{Effect, ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    let image = ImageObject {
        key: "missing-rasterized-mask-image".into(),
        width: 20,
        height: 20,
        xf: Affine::translate((30.0, 30.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let mut image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    image.appearance.effects = [
        ("rasterize", serde_json::json!({"background": "white"})),
        ("stylize.dropShadow", serde_json::json!({"mode": "normal", "opacity": 100, "x": 2, "y": 2, "blur": 1, "color": "#0000ff"})),
        ("blur.gaussian", serde_json::json!({"radius": 4.0})),
    ]
    .into_iter()
    .map(|(id, params)| Effect { id: id.into(), params, visible: true })
    .collect();
    let white = rect_node(&mut d, Rect::new(10.0, 10.0, 20.0, 20.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let art = Node::group(d.alloc_id(), vec![Arc::new(white), Arc::new(image)]);
    let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    d.insert(Some(d.layers[0].id), 0, object).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let mut r = Renderer::new();
    let mut first_raster = None;
    for frame in 0..2 {
        let img = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
        assert_eq!(img.pixel(70, 70), [255, 255, 255, 255], "frame {frame}: the incomplete mask leaves out the object");
        let raster = r.pixel_fx.values().next().unwrap();
        assert!(raster.image_left_out, "frame {frame}: the content raster records the missing image");
        if let Some(first) = &first_raster {
            assert!(Arc::ptr_eq(first, &raster.image), "the second frame reuses the incomplete content raster");
        } else {
            first_raster = Some(raster.image.clone());
        }
    }
}

#[test]
fn undecodable_image_in_nested_opacity_mask_leaves_out_outer_object() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};

    let mut d = Document::new(100.0, 100.0);
    d.images.insert("broken-nested-mask-image".into(), ImageBlob::new("image/png", b"not a nested PNG".to_vec()));
    let mut outer = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let mut inner = rect_node(&mut d, Rect::new(20.0, 20.0, 80.0, 80.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    let image = ImageObject {
        key: "broken-nested-mask-image".into(),
        width: 20,
        height: 20,
        xf: Affine::translate((30.0, 30.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    inner.extras_mut().mask = Some(Box::new(OpacityMask::new(image, false)));
    let art = Node::group(d.alloc_id(), vec![Arc::new(inner)]);
    outer.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
    d.insert(Some(d.layers[0].id), 0, outer).unwrap();

    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(70, 70), [255, 255, 255, 255], "the incomplete inner mask invalidates the outer mask");
}

/// The drop shadows of a blend's steps draw under an opacity mask with Clip off, where the mask
/// art leaves them uncovered, as they draw without the mask; with Clip on they are hidden.
#[test]
fn blend_step_shadows_draw_under_an_unclipped_opacity_mask() {
    use vectorcraft_doc::live::{BlendSpacing, BlendSpec};
    use vectorcraft_doc::{NodeKind, OpacityMask};

    let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 15, "y": 15, "blur": 0}), visible: true };
    let blend = |d: &mut Document, mask: Option<bool>| {
        let end = |d: &mut Document, x: f64| {
            let mut n = rect_node(d, Rect::new(x, 10.0, x + 10.0, 20.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
            n.appearance.effects.push(shadow.clone());
            Arc::new(n)
        };
        let ends = vec![end(d, 10.0), end(d, 70.0)];
        let spec = BlendSpec { spacing: BlendSpacing::Steps(2), ..Default::default() };
        let mut n = Node::new(d.alloc_id(), NodeKind::Blend { children: ends, spec: Box::new(spec) });
        if let Some(clip) = mask {
            let white = rect_node(d, Rect::new(0.0, 0.0, 5.0, 5.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
            n.extras_mut().mask = Some(Box::new(OpacityMask::new(white, clip)));
        }
        n
    };
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let draw = |mask: Option<bool>| {
        let mut d = Document::new(120.0, 60.0);
        let n = blend(&mut d, mask);
        d.insert(Some(d.layers[0].id), 0, n).unwrap();
        Renderer::new().render(&d, 120, 60, Affine::IDENTITY, &opts)
    };
    let (plain, unclipped, clipped) = (draw(None), draw(Some(false)), draw(Some(true)));
    for (x, y) in [(30, 30), (90, 32)] {
        assert!(plain.pixel(x, y)[0] < 128, "the shadow draws at ({x}, {y}) without a mask: {:?}", plain.pixel(x, y));
        assert_eq!(unclipped.pixel(x, y), plain.pixel(x, y), "the shadow draws at ({x}, {y}) under the unclipped mask");
        assert_eq!(clipped.pixel(x, y), [255, 255, 255, 255], "the clipped mask hides the shadow at ({x}, {y})");
    }
    assert_eq!(unclipped.pixel(15, 15), plain.pixel(15, 15), "the first end draws under the unclipped mask");
}

/// #3580: each opacity mask is rendered over its object's pixels, not over the whole frame. 300
/// masked 4 × 4 pt squares on a 1000 × 1000 page draw offscreen in proportion to their bounds, and
/// still draw where their masks show them.
#[test]
fn small_masks_render_over_their_objects_only() {
    let mut d = Document::new(1000.0, 1000.0);
    let l = d.layers[0].id;
    for i in 0..300 {
        let (x, y) = (10.0 + (i % 30) as f64 * 30.0, 10.0 + (i / 30) as f64 * 30.0);
        let square = Rect::new(x, y, x + 4.0, y + 4.0);
        let mut n = rect_node(&mut d, square, Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
        let white = rect_node(&mut d, square, Paint::solid(Color::WHITE), Paint::None, 0.0);
        n.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(white, true)));
        d.insert(Some(l), usize::MAX, n).unwrap();
    }
    let mut r = Renderer::new();
    r.threads = 0;
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    OFFSCREEN_PIXELS.with(|n| n.set(0));
    let img = r.render(&d, 1000, 1000, Affine::IDENTITY, &opts);
    let px = OFFSCREEN_PIXELS.with(|n| n.get());
    // Each square's region is at most 4 + 2 × 2 margin + 1 rounding pixels a side, mask and content.
    assert!(px <= 300 * 2 * 9 * 9, "{px} offscreen pixels for 300 masked 4 × 4 squares");
    assert_eq!(img.pixel(12, 12), [255, 0, 0, 255]);
    assert_eq!(img.pixel(12 + 29 * 30, 12 + 9 * 30), [255, 0, 0, 255]);
    assert_eq!(img.pixel(20, 20), [255, 255, 255, 255]);
}

#[test]
fn disabled_or_outline_mask_draws_unmasked() {
    let mut d = masked_doc(true, false);
    let id = d.layers[0].children().unwrap()[0].id;
    let n = d.node_mut(id).unwrap();
    n.extras_mut().mask.as_mut().unwrap().disabled = true;
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(80, 50), [255, 0, 0, 255]);
}

#[test]
fn linked_mask_moves_with_object() {
    let mut d = masked_doc(true, false);
    let id = d.layers[0].children().unwrap()[0].id;
    d.node_mut(id).unwrap().transform(Affine::translate((5.0, 0.0)), false);
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(img.pixel(52, 50), [255, 0, 0, 255], "mask art moved by 5 too");
}

#[test]
fn trim_view_clips_to_artboards() {
    let mut d = Document::new(50.0, 50.0);
    let n = rect_node(&mut d, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), trim: true, ..Default::default() };
    let r = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
    assert_eq!(r.pixel(25, 25), [255, 0, 0, 255]);
    assert_eq!(r.pixel(75, 75), [255, 255, 255, 255]);
    assert_eq!(render(&d).pixel(75, 75), [255, 0, 0, 255]);
}

#[test]
fn drop_shadow_profile_and_cache() {
    let mut d = Document::new(100.0, 100.0);
    let mut n = rect_node(&mut d, Rect::new(20.0, 20.0, 60.0, 60.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
    n.appearance.effects.push(vectorcraft_doc::Effect {
        id: "stylize.dropShadow".into(),
        params: serde_json::json!({"x": 10.0, "y": 10.0, "blur": 4.0, "opacity": 75.0}),
        visible: true,
    });
    let l = d.layers[0].id;
    d.insert(Some(l), 0, n).unwrap();
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    for threads in [0, 4] {
        let mut r = Renderer::new();
        r.threads = threads;
        let a = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
        // Inside the shadow (not under the object): 75% black. At its blurred edge: about half.
        let inside = a.pixel(65, 50)[0] as i32;
        assert!((inside - 64).abs() <= 6, "inside {inside}");
        let edge = a.pixel(70, 50)[0] as i32;
        assert!((edge - 160).abs() <= 20, "edge {edge}");
        assert_eq!(a.pixel(85, 50), [255, 255, 255, 255]);
        // Second frame comes from the cache: identical. A pan moves it rigidly.
        let b = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
        assert_eq!(a.pixels, b.pixels);
        let c = r.render(&d, 100, 100, Affine::translate((-5.0, 0.0)), &opts);
        assert_eq!(c.pixel(60, 50), a.pixel(65, 50));
        assert_eq!(c.pixel(65, 50), a.pixel(70, 50));
    }
}

#[test]
fn blend_mode_on_a_single_fill_is_exact_without_a_layer() {
    let mut d = Document::new(100.0, 100.0);
    let red = rect_node(&mut d, Rect::new(0.0, 0.0, 50.0, 100.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    let mut over = rect_node(&mut d, Rect::new(25.0, 0.0, 75.0, 100.0), Paint::solid(Color::rgb(0.5, 0.5, 1.0)), Paint::None, 0.0);
    over.blend = vectorcraft_color::BlendMode::Multiply;
    let l = d.layers[0].id;
    d.insert(Some(l), 0, red).unwrap();
    d.insert(Some(l), 1, over).unwrap();
    for threads in [0, 4] {
        let mut r = Renderer::new();
        r.threads = threads;
        let img = r.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() });
        let [rr, g, b, _] = img.pixel(35, 50);
        assert!((rr as i32 - 128).abs() <= 2 && g <= 1 && b <= 1, "multiply over red: {:?}", img.pixel(35, 50));
        let [rr, g, b, _] = img.pixel(65, 50);
        assert!((rr as i32 - 128).abs() <= 2 && (g as i32 - 128).abs() <= 2 && b >= 253, "multiply over white");
        assert_eq!(img.pixel(10, 50), [255, 0, 0, 255]);
    }
}

#[test]
fn invalid_dash_pattern_strokes_solid() {
    // A negative dash value makes the pattern invalid (PDF `d`, SVG `stroke-dasharray`): the stroke
    // is drawn solid. [-5, 3] used to send the dasher into an endless loop.
    let line = |dash: Option<Vec<f64>>| {
        let mut d = Document::new(100.0, 100.0);
        let mut bp = BezPath::new();
        bp.move_to((10.0, 50.0));
        bp.line_to((90.0, 50.0));
        let id = d.alloc_id();
        let mut n = Node::path(id, vectorcraft_geom::PathData::from_bezpath(&bp), Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 6.0));
        n.appearance.stroke_mut().unwrap().dash = dash.map(|pattern| vectorcraft_doc::Dash { pattern, ..Default::default() });
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        d
    };
    let solid = render(&line(None));
    for pattern in [vec![-5.0, 3.0], vec![3.0, -5.0], vec![4.0, f64::NAN], vec![0.0, 0.0]] {
        let d = line(Some(pattern.clone()));
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || tx.send(render(&d)).ok());
        let img = rx.recv_timeout(std::time::Duration::from_secs(300)).unwrap_or_else(|_| panic!("rendering dash {pattern:?} did not finish"));
        assert_eq!(img.pixels, solid.pixels, "dash {pattern:?}");
    }
}

/// With `solid_dashes` a dashed stroke draws as the same stroke without dashes, on its own and as
/// the silhouette of a drop shadow; without it the dashes stay.
#[test]
fn solid_dashes_draw_dashed_strokes_as_solid_lines() {
    let line = |dash: bool, shadow: bool| {
        let mut d = Document::new(100.0, 100.0);
        let mut bp = BezPath::new();
        bp.move_to((10.0, 50.0));
        bp.line_to((90.0, 50.0));
        let id = d.alloc_id();
        let mut n = Node::path(id, vectorcraft_geom::PathData::from_bezpath(&bp), Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 6.0));
        let st = n.appearance.stroke_mut().unwrap();
        st.dash = dash.then(|| vectorcraft_doc::Dash { pattern: vec![8.0, 8.0], ..Default::default() });
        if shadow {
            let params = serde_json::json!({"x": 4, "y": 4, "blur": 1, "opacity": 100});
            st.effects.push(vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params, visible: true });
        }
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        d
    };
    let opts = |solid_dashes: bool| RenderOptions { background: Some([255, 255, 255, 255]), solid_dashes, ..Default::default() };
    let draw = |d: &Document, solid: bool| Renderer::new().render(d, 100, 100, Affine::IDENTITY, &opts(solid));
    for shadow in [false, true] {
        let solid = draw(&line(false, shadow), false);
        assert_ne!(draw(&line(true, shadow), false).pixels, solid.pixels, "the dashes show without the option (shadow: {shadow})");
        assert_eq!(draw(&line(true, shadow), true).pixels, solid.pixels, "the dashes go with the option (shadow: {shadow})");
        assert_eq!(draw(&line(false, shadow), true).pixels, solid.pixels, "a solid stroke is unchanged (shadow: {shadow})");
    }
    // One renderer that draws the shadow both ways makes its raster again for each setting.
    let (dashed, mut r) = (line(true, true), Renderer::new());
    let fresh = |solid: bool| draw(&dashed, solid).pixels;
    for solid in [false, true, false] {
        assert_eq!(r.render(&dashed, 100, 100, Affine::IDENTITY, &opts(solid)).pixels, fresh(solid), "reused renderer, solid: {solid}");
    }
}

/// An image that can't be decoded is counted on the thread that tried to decode it: an export
/// compares [`decode_failures`] before and after it draws, so another thread's failure meanwhile is
/// not its own.
#[test]
fn decode_failures_counts_this_threads_images_only() {
    let undecodable = || paint::decode_pixmap(b"\x89PNG\r\n\x1a\n").is_none();
    let before = decode_failures();
    let other = std::thread::spawn(move || {
        let start = decode_failures();
        undecodable() && decode_failures() == start + 1
    });
    assert!(other.join().unwrap(), "counted on the other thread");
    assert_eq!(decode_failures(), before, "the other thread's failure");
    assert!(undecodable());
    assert_eq!(decode_failures(), before + 1, "this thread's failure");
}

/// Pixels decoded elsewhere and kept with [`keep_decoded_image`] are the pixels a renderer decodes,
/// premultiplied alike, and a render that draws the image uses them without decoding it. A CMYK
/// image's pixels are not kept.
#[test]
fn a_kept_decoded_image_is_drawn_without_decoding_it() {
    use vectorcraft_doc::cmyk::Inks;
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
    // The image has pixels no other test uses, since every test shares the store, and half of
    // them are partly transparent.
    let img = image::RgbaImage::from_fn(37, 23, |x, y| image::Rgba([(x * 7) as u8, (y * 11) as u8, 99, if (x + y) % 2 == 0 { 255 } else { 77 }]));
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let blob = ImageBlob::new("image/png", png.clone());
    assert!(!image_cached(&blob));
    assert!(keep_decoded_image(&blob, image::load_from_memory(&png).unwrap()));
    assert!(image_cached(&blob));
    let key = blobs::blob_key(&blob).0;
    let kept = images::store().peek(&key).unwrap();
    assert!(kept.data() == paint::decode_pixmap(&png).unwrap().data(), "the pixels a renderer decodes");
    let mut d = Document::new(37.0, 23.0);
    d.images.insert("kept".into(), blob);
    let id = d.alloc_id();
    let im = ImageObject { key: "kept".into(), width: 37, height: 23, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
    d.insert(Some(d.layers[0].id), 0, Node::new(id, NodeKind::Image(Box::new(im)))).unwrap();
    let decodes = image_decodes();
    let r = Renderer::new().render(&d, 37, 23, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(image_decodes(), decodes, "drawn from the kept pixels");
    assert!(r.pixel(18, 11)[3] > 0, "the image is drawn");
    let cmyk = ImageBlob::cmyk_tiff(&Inks::new(2, 2, [10, 20, 30, 40].repeat(4)).unwrap()).unwrap();
    assert!(!keep_decoded_image(&cmyk, image::DynamicImage::new_rgba8(2, 2)));
    assert!(!image_cached(&cmyk));
}

/// A CMYK image's inks and the pixels shown from them are admitted against the decode allowance
/// before either is made: past it, the image is read as other images are (reduced), not whole.
#[test]
fn cmyk_images_past_the_allowance_are_not_decoded_whole() {
    use vectorcraft_doc::ImageBlob;
    use vectorcraft_doc::cmyk::Inks;
    let cmyk = ImageBlob::cmyk_tiff(&Inks::new(64, 48, [11, 22, 33, 44].repeat(64 * 48)).unwrap()).unwrap();
    let whole = with_allowance(1 << 20, || crate::blobs::blob_pixmap(&cmyk, false)).map(|(_, pm)| (pm.width(), pm.height()));
    assert_eq!(whole, Some((64, 48)), "within the allowance, whole");
    let other = ImageBlob::cmyk_tiff(&Inks::new(64, 48, [12, 22, 33, 44].repeat(64 * 48)).unwrap()).unwrap();
    let small = with_allowance(64 * 48 * 4 - 1, || crate::blobs::blob_pixmap(&other, false)).map(|(_, pm)| (pm.width(), pm.height()));
    assert_ne!(small, Some((64, 48)), "past it, not decoded whole");
}

/// An image drawn larger than its pixels samples smoothly, or with `smooth_images` off each screen
/// pixel takes its nearest image pixel (Pixel Preview with File Handling › Display Bitmaps as
/// Anti-aliased Images off, #394).
#[test]
fn images_take_their_nearest_pixel_without_smoothing() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
    let img = image::RgbaImage::from_fn(2, 1, |x, _| image::Rgba(if x == 0 { [0, 0, 0, 255] } else { [255, 255, 255, 255] }));
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let mut d = Document::new(40.0, 20.0);
    d.images.insert("bw".into(), ImageBlob::new("image/png", png));
    let id = d.alloc_id();
    let im = ImageObject { key: "bw".into(), width: 2, height: 1, xf: Affine::scale(20.0), link: None, placement: Default::default(), pixels: None };
    d.insert(Some(d.layers[0].id), 0, Node::new(id, NodeKind::Image(Box::new(im)))).unwrap();
    // How many pixels across the image are neither black nor white.
    let greys = |d: &Document, smooth_images| {
        let r = Renderer::new().render(d, 40, 20, Affine::IDENTITY, &RenderOptions { smooth_images, ..Default::default() });
        (0..40).filter(|x| (16..240).contains(&r.pixel(*x, 10)[0])).count()
    };
    assert!(greys(&d, true) >= 4, "smooth: a ramp from black to white");
    assert_eq!(greys(&d, false), 0, "nearest: black, then white");
    // A pixelated image (an envelope's with Anti-Alias off) takes its nearest pixel either way.
    d.node_mut(id).unwrap().extras_mut().pixelated = true;
    assert_eq!(greys(&d, true), 0, "pixelated: black, then white");
}

/// An image masked by one white shape with Clip on draws as an image clipped by that shape, with
/// nothing drawn offscreen; any other mask goes through the offscreen mask.
#[test]
fn an_image_masked_by_a_white_shape_draws_as_if_clipped() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};
    let img = image::RgbaImage::from_pixel(2, 2, image::Rgba([255, 0, 0, 255]));
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let doc_masked = |mask_color: Color, shape: Rect| {
        let mut d = Document::new(100.0, 100.0);
        d.images.insert("red".into(), ImageBlob::new("image/png", png.clone()));
        let id = d.alloc_id();
        let im = ImageObject {
            key: "red".into(),
            width: 2,
            height: 2,
            xf: Affine::translate((10.0, 10.0)) * Affine::scale(40.0),
            link: None,
            placement: Default::default(),
            pixels: None,
        };
        let shape = rect_node(&mut d, shape, Paint::solid(mask_color), Paint::None, 0.0);
        let n = Node::new(id, NodeKind::Image(Box::new(im))).with_mask(Some(Box::new(OpacityMask::new(shape, true))));
        d.insert(Some(d.layers[0].id), 0, n).unwrap();
        d
    };
    let doc = |mask_color: Color| doc_masked(mask_color, Rect::new(10.0, 10.0, 50.0, 90.0));
    let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
    let draw = |d: &Document| {
        let mut r = Renderer::new();
        r.threads = 0;
        OFFSCREEN_PIXELS.with(|n| n.set(0));
        let img = r.render(d, 100, 100, Affine::IDENTITY, &opts);
        (img, OFFSCREEN_PIXELS.with(|n| n.get()))
    };
    let (img, px) = draw(&doc(Color::WHITE));
    assert_eq!(px, 0, "no offscreen mask");
    assert_eq!(img.pixel(30, 50), [255, 0, 0, 255], "inside the shape");
    assert_eq!(img.pixel(70, 50), [255, 255, 255, 255], "outside it");
    let (img, px) = draw(&doc(Color::rgb(0.5, 0.5, 0.5)));
    assert!(px > 0, "a grey mask is drawn offscreen");
    assert!((100..160).contains(&img.pixel(30, 50)[1]), "at about half opacity: {:?}", img.pixel(30, 50));
    // A white mask reaching past the image's frame shows nothing outside the frame.
    let (img, _) = draw(&doc_masked(Color::WHITE, Rect::new(0.0, 10.0, 50.0, 90.0)));
    assert_eq!(img.pixel(5, 50), [255, 255, 255, 255], "left of the frame");
    assert_eq!(img.pixel(30, 50), [255, 0, 0, 255]);
    // In isolation mode, outside the isolated object, it is dimmed like the art around it.
    let mut d = doc(Color::WHITE);
    let other = rect_node(&mut d, Rect::new(60.0, 60.0, 90.0, 90.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
    let other = d.insert(Some(d.layers[0].id), usize::MAX, other).unwrap();
    let isolated = RenderOptions { isolated: Some(other), ..opts.clone() };
    let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &isolated);
    assert!(img.pixel(30, 50)[1] > 60, "dimmed: {:?}", img.pixel(30, 50));
}

/// A pixelated image drawn reduced with `image_mips` on takes its nearest full pixel, not a
/// mip's average.
#[test]
fn a_pixelated_image_draws_reduced_without_mips() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
    let img = image::RgbaImage::from_fn(64, 64, |x, y| image::Rgba(if (x + y) % 2 == 0 { [255, 0, 0, 255] } else { [0, 0, 255, 255] }));
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let mut d = Document::new(64.0, 64.0);
    d.images.insert("check".into(), ImageBlob::new("image/png", png));
    let id = d.alloc_id();
    let im =
        ImageObject { key: "check".into(), width: 64, height: 64, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
    let mut n = Node::new(id, NodeKind::Image(Box::new(im)));
    n.extras_mut().pixelated = true;
    d.insert(Some(d.layers[0].id), 0, n).unwrap();
    let on = RenderOptions { image_mips: true, ..Default::default() };
    let r = Renderer::new().render(&d, 16, 16, Affine::scale(0.25), &on);
    let mixed = (0..16).flat_map(|y| (0..16).map(move |x| (x, y))).filter(|(x, y)| r.pixel(*x, *y)[0] > 40 && r.pixel(*x, *y)[2] > 40).count();
    assert_eq!(mixed, 0, "each pixel red or blue");
}

/// A PNG that says it is `w` × `h` 1-bit gray pixels (its header and no rows).
fn png_header(w: u32, h: u32) -> Vec<u8> {
    let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
    let ihdr: Vec<u8> = [&b"IHDR"[..], &w.to_be_bytes(), &h.to_be_bytes(), &[1, 0, 0, 0, 0]].concat();
    for chunk in [&ihdr[..], b"IDAT\x78\x9c", b"IEND"] {
        out.extend((chunk.len() as u32 - 4).to_be_bytes());
        out.extend(chunk);
        let mut crc = flate2::Crc::new();
        crc.update(chunk);
        out.extend(crc.sum().to_be_bytes());
    }
    out
}

/// A little-endian TIFF that says it is `w` × `h` 8-bit gray pixels in one strip (its header and
/// no pixels).
fn tiff_header(w: u32, h: u32) -> Vec<u8> {
    let mut out = b"II*\0".to_vec();
    out.extend(8u32.to_le_bytes());
    // (tag, type: 3 SHORT or 4 LONG, value), one value each.
    let entries: [(u16, u16, u32); 9] =
        [(256, 4, w), (257, 4, h), (258, 3, 8), (259, 3, 1), (262, 3, 1), (273, 4, 8), (277, 3, 1), (278, 4, h), (279, 4, 0)];
    out.extend((entries.len() as u16).to_le_bytes());
    for (tag, kind, value) in entries {
        out.extend(tag.to_le_bytes());
        out.extend(kind.to_le_bytes());
        out.extend(1u32.to_le_bytes());
        out.extend(value.to_le_bytes());
    }
    out.extend(0u32.to_le_bytes());
    out
}

/// The decode allowance (2 GB) counts an image's pixels as RGBA, read from its header (#1717): a
/// 1-bit gray image whose 8-bit gray pixels are within it but whose RGBA is over it is not decoded
/// whole, and one a column narrower goes on to decoding. `decode_pixmap` reads the larger one at
/// reduced size; this header has no rows to read, so it draws nothing, counted for an export to
/// report.
#[test]
fn an_image_too_large_as_rgba_is_not_decoded() {
    let over = paint::decode_within_allowance(&png_header(23_171, 23_170));
    assert!(matches!(over, Err(paint::DecodeError::Limit(_))), "{over:?}");
    assert!(over.unwrap_err().to_string().starts_with("the image is too large to decode: "));
    let failed = decode_failures();
    assert!(paint::decode_pixmap(&png_header(23_171, 23_170)).is_none());
    assert!(decode_failures() > failed);
    // Within the allowance: decoded, and its missing rows can't be read.
    let within = paint::decode_within_allowance(&png_header(23_170, 23_170));
    assert!(matches!(within, Err(paint::DecodeError::Unreadable(_))), "{within:?}");
    assert!(within.unwrap_err().to_string().starts_with("the image's pixels can't be read: "));
    // A header whose size as RGBA does not fit in 64 bits gets the same limit error.
    let huge = paint::decode_within_allowance(&tiff_header(1 << 31, 1 << 31));
    assert!(matches!(huge, Err(paint::DecodeError::Limit(_))), "{huge:?}");
}

/// A PNG of `w` × `h` noisy pixels cut off halfway through its bytes: its header reads, and its
/// pixels can't be decoded.
fn truncated_png(w: u32, h: u32) -> Vec<u8> {
    let img = image::RgbaImage::from_fn(w, h, |x, y| {
        let v = (x * 7919 + y * 104_729).wrapping_mul(2_654_435_761).to_le_bytes();
        image::Rgba([v[0], v[1], v[2], 255])
    });
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    png.truncate(png.len() / 2);
    png
}

/// An image whose pixels can't be decoded is tried once: later renders find the store's note of it,
/// and each render counts it in `decode_failures` for an export to report. On screen
/// (`mark_undecoded_images`) it draws as a crossed gray box; an export draws nothing there.
#[test]
fn an_undecodable_image_is_tried_once_and_marked_on_screen() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
    let png = truncated_png(61, 47);
    assert_eq!(image::ImageReader::new(std::io::Cursor::new(&png)).with_guessed_format().unwrap().into_dimensions().unwrap(), (61, 47));
    let mut d = Document::new(80.0, 60.0);
    d.images.insert("cut".into(), ImageBlob::new("image/png", png));
    let id = d.alloc_id();
    let im = ImageObject {
        key: "cut".into(),
        width: 61,
        height: 47,
        xf: Affine::translate((10.0, 5.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    d.insert(Some(d.layers[0].id), 0, Node::new(id, NodeKind::Image(Box::new(im)))).unwrap();
    // The alpha summed over the image's frame.
    let alpha = |r: &Rendered| (10..71).flat_map(|x| (5..52).map(move |y| u64::from(r.pixel(x, y)[3]))).sum::<u64>();
    let attempts = || paint::DECODE_ATTEMPTS.with(std::cell::Cell::get);
    let (tried, failed) = (attempts(), decode_failures());
    let screen = RenderOptions { mark_undecoded_images: true, ..Default::default() };
    let mut r = Renderer::new();
    for render in 1..=2 {
        let shown = r.render(&d, 80, 60, Affine::IDENTITY, &screen);
        assert!(alpha(&shown) > 0, "render {render}: the crossed box shows");
        assert_eq!(decode_failures() - failed, render, "render {render}: counted");
    }
    assert_eq!(attempts() - tried, 1, "tried once");
    let export = Renderer::new().render(&d, 80, 60, Affine::IDENTITY, &RenderOptions::default());
    assert_eq!(alpha(&export), 0, "an export draws nothing there");
    assert_eq!((attempts() - tried, decode_failures() - failed), (1, 3), "and counts it without decoding it again");
}

#[test]
fn pattern_tile_follows_undecoded_image_mark_in_both_cache_orders() {
    use vectorcraft_doc::pattern::{PatternDef, pattern_paint};
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};

    let mut d = Document::new(80.0, 80.0);
    d.images.insert("cut-pattern".into(), ImageBlob::new("image/png", b"not a PNG: pattern fill".to_vec()));
    let image = ImageObject {
        key: "cut-pattern".into(),
        width: 20,
        height: 20,
        xf: Affine::IDENTITY,
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
    let mut pattern = PatternDef::new("Cut pattern", vec![Arc::new(image)]);
    pattern.tile = Rect::new(0.0, 0.0, 20.0, 20.0);
    d.patterns.push(pattern);
    let shape = rect_node(&mut d, Rect::new(10.0, 10.0, 70.0, 70.0), pattern_paint("Cut pattern"), Paint::None, 0.0);
    d.insert(Some(d.layers[0].id), 0, shape).unwrap();

    let mut r = Renderer::new();
    for order in [[false, true, false], [true, false, true]] {
        for mark in order {
            let opts = RenderOptions { mark_undecoded_images: mark, ..Default::default() };
            let pixel = r.render(&d, 80, 80, Affine::IDENTITY, &opts).pixel(15, 15);
            assert_eq!(pixel[3] > 0, mark, "mark_undecoded_images {mark}: {pixel:?}");
            if mark {
                assert_eq!(pixel[0], pixel[1], "the pattern draws a gray mark");
                assert_eq!(pixel[1], pixel[2], "the pattern draws a gray mark");
                assert!((120..=140).contains(&pixel[0]), "the pattern draws a gray mark: {pixel:?}");
            }
        }
    }
}

/// The rasters a renderer keeps of an object's raster effects are made again when
/// `mark_undecoded_images` changes: a blurred image whose pixels can't be decoded shows its blurred
/// mark only while the option is on.
#[test]
fn raster_effects_follow_the_undecoded_image_mark() {
    use vectorcraft_doc::{Effect, ImageBlob, ImageObject, NodeKind};
    let mut d = Document::new(80.0, 60.0);
    d.images.insert("cut".into(), ImageBlob::new("image/png", truncated_png(43, 31)));
    let id = d.alloc_id();
    let im = ImageObject {
        key: "cut".into(),
        width: 43,
        height: 31,
        xf: Affine::translate((15.0, 12.0)),
        link: None,
        placement: Default::default(),
        pixels: None,
    };
    let mut n = Node::new(id, NodeKind::Image(Box::new(im)));
    n.appearance.effects.push(Effect { id: "blur.gaussian".into(), params: serde_json::json!({"radius": 2.0}), visible: true });
    d.insert(Some(d.layers[0].id), 0, n).unwrap();
    let alpha = |r: &Rendered| r.pixels.chunks(4).map(|p| u64::from(p[3])).sum::<u64>();
    let mut r = Renderer::new();
    for mark in [true, false, true] {
        let opts = RenderOptions { mark_undecoded_images: mark, ..Default::default() };
        let a = alpha(&r.render(&d, 80, 60, Affine::IDENTITY, &opts));
        assert_eq!(a > 0, mark, "mark_undecoded_images {mark}: alpha {a}");
    }
}

/// A gray image within the allowance draws its gray levels.
#[test]
fn a_gray_image_draws_its_gray_levels() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
    let img = image::GrayImage::from_fn(2, 1, |x, _| image::Luma([if x == 0 { 40 } else { 200 }]));
    let mut png = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
    let mut d = Document::new(40.0, 20.0);
    d.images.insert("gray".into(), ImageBlob::new("image/png", png));
    let id = d.alloc_id();
    let im =
        ImageObject { key: "gray".into(), width: 2, height: 1, xf: Affine::scale(20.0), link: None, placement: Default::default(), pixels: None };
    d.insert(Some(d.layers[0].id), 0, Node::new(id, NodeKind::Image(Box::new(im)))).unwrap();
    let r = Renderer::new().render(&d, 40, 20, Affine::IDENTITY, &RenderOptions { smooth_images: false, ..Default::default() });
    assert_eq!((r.pixel(5, 10), r.pixel(35, 10)), ([40, 40, 40, 255], [200, 200, 200, 255]));
}

/// Runs `f` with the decode allowance of one image on this thread set to `bytes`.
fn with_allowance<T>(bytes: u64, f: impl FnOnce() -> T) -> T {
    paint::TEST_ALLOWANCE.with(|c| c.set(Some(bytes)));
    let out = f();
    paint::TEST_ALLOWANCE.with(|c| c.set(None));
    out
}

/// `img` encoded as `format`.
fn encoded(img: image::DynamicImage, format: image::ImageFormat) -> Vec<u8> {
    let mut out = vec![];
    img.write_to(&mut std::io::Cursor::new(&mut out), format).unwrap();
    out
}

/// A 16-bit PNG past the decode allowance is read a row at a time at the largest size within the
/// allowance and draws its colors; the file's own size is kept.
#[test]
fn a_png_past_the_allowance_draws_at_reduced_size() {
    use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
    let img = image::ImageBuffer::<image::Rgba<u16>, _>::from_fn(400, 400, |x, _| {
        if x < 200 { image::Rgba([51400, 10280, 10280, 65535]) } else { image::Rgba([10280, 10280, 51400, 65535]) }
    });
    let png = encoded(image::DynamicImage::ImageRgba16(img), image::ImageFormat::Png);
    // 400 × 400 is 640,000 bytes as RGBA; 387 × 387 is the largest within 600,000.
    assert_eq!(with_allowance(600_000, || paint::reduced_size(&png)), Some((387, 387)));
    assert_eq!(paint::reduced_size(&png), None, "within the 2 GB allowance");
    let mut d = Document::new(400.0, 400.0);
    d.images.insert("over".into(), ImageBlob::new("image/png", png));
    let id = d.alloc_id();
    let im =
        ImageObject { key: "over".into(), width: 400, height: 400, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
    d.insert(Some(d.layers[0].id), 0, Node::new(id, NodeKind::Image(Box::new(im)))).unwrap();
    let failed = decode_failures();
    // As on screen, where an image that can't be decoded draws as a crossed box.
    let screen = RenderOptions { smooth_images: false, mark_undecoded_images: true, ..Default::default() };
    let r = with_allowance(600_000, || Renderer::new().render(&d, 400, 400, Affine::IDENTITY, &screen));
    assert_eq!(decode_failures(), failed);
    assert_eq!((r.pixel(50, 200), r.pixel(350, 200)), ([200, 40, 40, 255], [40, 40, 200, 255]));
    // 300 × 300 at 16 bits a sample is 720,000 bytes decoded and 360,000 as RGBA: past 600,000 as
    // decoded and within it as RGBA, it is read a row at a time at its own size.
    let small = image::ImageBuffer::<image::Rgba<u16>, _>::from_pixel(300, 300, image::Rgba([51400, 10280, 10280, 65535]));
    let small = encoded(image::DynamicImage::ImageRgba16(small), image::ImageFormat::Png);
    assert_eq!(with_allowance(600_000, || paint::reduced_size(&small)), Some((300, 300)));
    let pm = with_allowance(600_000, || paint::decode_pixmap(&small)).expect("read a row at a time");
    let c = pm.sample(3, 3);
    assert_eq!((pm.width(), pm.height(), [c.r, c.g, c.b, c.a]), (300, 300, [200, 40, 40, 255]));
    // A row is read only while the reader's buffers fit the allowance, counting the stream buffer
    // three times for the moment it grows. Below 4,096 columns of 16-bit RGBA they take 516,103
    // bytes plus 64 bytes a column: 1,310 columns take 599,943 bytes, within 600,000, and 1,311
    // columns take 600,007 and are not decoded (their samples, 8 bytes a pixel, are past it too).
    let wide = |w: u32| {
        let img = image::ImageBuffer::<image::Rgba<u16>, _>::from_pixel(w, 60, image::Rgba([51400, 10280, 10280, 65535]));
        encoded(image::DynamicImage::ImageRgba16(img), image::ImageFormat::Png)
    };
    assert_eq!(with_allowance(600_000, || paint::reduced_size(&wide(1310))), Some((1310, 60)));
    assert_eq!(with_allowance(600_000, || paint::reduced_size(&wide(1311))), None);
    // An image of more pixels than the allowance has bytes is not read: 1,000 × 400 8-bit gray
    // pixels are drawn at 500 × 200 within 400,000 bytes, and 1,000 × 401 are not.
    let gray = |h: u32| encoded(image::DynamicImage::ImageLuma8(image::GrayImage::from_pixel(1000, h, image::Luma([90]))), image::ImageFormat::Png);
    assert_eq!(with_allowance(400_000, || paint::reduced_size(&gray(400))), Some((500, 200)));
    assert_eq!(with_allowance(400_000, || paint::reduced_size(&gray(401))), None);
}

/// A JPEG whose RGB samples are within the allowance but whose RGBA is past it is decoded whole
/// and shrunk to the largest size within the allowance; an image whose decoded samples are past it
/// and can't be read a row at a time (a GIF, 4 bytes a pixel) is still not decoded, counted, and
/// noted as undecodable.
#[test]
fn a_jpeg_past_the_allowance_as_rgba_is_shrunk_and_a_gif_past_it_is_not_decoded() {
    let jpeg = encoded(image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(80, 60, image::Rgb([30, 120, 200]))), image::ImageFormat::Jpeg);
    // 14,400 bytes of RGB, 19,200 as RGBA: 73 × 54 is the largest within 16,000.
    let pm = with_allowance(16_000, || paint::decode_pixmap(&jpeg)).expect("decoded at reduced size");
    assert_eq!((pm.width(), pm.height()), (73, 54));
    let c = pm.sample(36, 27);
    assert!(c.r.abs_diff(30) <= 4 && c.g.abs_diff(120) <= 4 && c.b.abs_diff(200) <= 4 && c.a == 255, "{c:?}");
    // A placed document's preview asks for 20 × 15: decoded straight at that size.
    let pm = with_allowance(16_000, || paint::decode_pixmap_fit(&jpeg, 20, 15)).expect("decoded at the size asked for");
    assert_eq!((pm.width(), pm.height()), (20, 15));
    let gif =
        encoded(image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(80, 60, image::Rgba([30, 120, 200, 255]))), image::ImageFormat::Gif);
    assert_eq!(with_allowance(16_000, || paint::reduced_size(&gif)), None);
    let failed = decode_failures();
    let why = with_allowance(16_000, || paint::decode_pixmap_or_why(&gif)).err();
    assert_eq!(why, Some(paint::Undecoded::Unreadable), "noted as undecodable");
    assert_eq!(decode_failures(), failed + 1);
}

/// An image past the allowance is decoded at reduced size only while the result and what reading
/// it takes (the image decoded whole, or the PNG reader's row buffers) fit beside the images
/// paints hold; when they don't, it isn't decoded and is not noted as undecodable, and it is
/// decoded once there is room.
#[test]
fn a_reduced_decode_counts_what_reading_takes_beside_the_images_paints_hold() {
    /// Runs `f` with room for `bytes` beside the images paints hold.
    fn with_room<T>(bytes: u64, f: impl FnOnce() -> T) -> T {
        paint::TEST_ROOM.with(|c| c.set(Some(bytes)));
        let out = f();
        paint::TEST_ROOM.with(|c| c.set(None));
        out
    }
    // Decoded whole: 14,400 bytes of RGB and a 16-row band of 8,960 bytes, beside a 73 × 54 result
    // of 15,768 bytes.
    let jpeg = encoded(image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(80, 60, image::Rgb([30, 120, 200]))), image::ImageFormat::Jpeg);
    // Read a row at a time: 541,703 bytes in the reader's buffers for 400 columns of 16-bit RGBA,
    // beside a 387 × 387 result of 599,076 bytes.
    let png = encoded(
        image::DynamicImage::ImageRgba16(image::ImageBuffer::from_pixel(400, 400, image::Rgba([51400, 10280, 10280, 65535]))),
        image::ImageFormat::Png,
    );
    for (bytes, allowance, room, size) in [(&jpeg, 16_000, 15_768 + 14_400 + 8_960, (73, 54)), (&png, 600_000, 599_076 + 541_703, (387, 387))] {
        let failed = decode_failures();
        let none = with_allowance(allowance, || with_room(room - 1, || paint::decode_pixmap_or_why(bytes)));
        assert_eq!(none.err(), Some(paint::Undecoded::NoRoom), "not decoded with a byte too few, and tried again later");
        assert_eq!(decode_failures(), failed + 1);
        let pm = with_allowance(allowance, || with_room(room, || paint::decode_pixmap(bytes))).expect("decoded once there is room");
        assert_eq!((pm.width(), pm.height()), size);
    }
}

/// Art Optimized exports are supersampled (#983): two red shapes meeting along a diagonal leave no
/// seam of the white background between them, where shape-by-shape smoothing shows one; the outer
/// edges stay smooth.
#[test]
fn supersampled_art_leaves_no_seam_between_shapes_that_meet() {
    let red = || Paint::solid(Color::rgb(1.0, 0.0, 0.0));
    let mut d = Document::new(100.0, 100.0);
    let layer = d.layers[0].id;
    // Two triangles sharing the diagonal of the square (10, 10)–(90, 90).
    for pts in [[(10.0, 10.0), (90.0, 10.0), (90.0, 90.0)], [(10.0, 10.0), (90.0, 90.0), (10.0, 90.0)]] {
        let mut b = vectorcraft_geom::BezPath::new();
        b.move_to(pts[0]);
        b.line_to(pts[1]);
        b.line_to(pts[2]);
        b.close_path();
        let id = d.alloc_id();
        d.insert(Some(layer), 0, Node::path(id, vectorcraft_geom::PathData::from_bezpath(&b), Appearance::basic(red(), Paint::None, 0.0))).unwrap();
    }
    let draw = |supersample: bool| {
        let opts = RenderOptions { background: Some([255, 255, 255, 255]), precise: true, supersample, ..Default::default() };
        Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts)
    };
    // The palest pixel on the shared diagonal, away from the corners.
    let palest = |r: &Rendered| (20..80).map(|i| r.pixel(i, i)[1].max(r.pixel(i + 1, i)[1])).max().unwrap();
    assert!(palest(&draw(false)) > 20, "shape-by-shape smoothing shows the seam");
    assert_eq!(palest(&draw(true)), 0, "supersampled: no seam");
    // The outer edge is still smoothed: a pixel half covered is between white and red.
    let ss = draw(true);
    let edge = ss.pixel(50, 9);
    assert!(edge == [255, 255, 255, 255] || (edge[1] > 0 && edge[1] < 255) || ss.pixel(50, 10)[1] < 255, "{edge:?}");
    // The factor fits the size and the renderer's limits.
    assert_eq!(supersample_factor(100, 100), Some(4));
    assert_eq!(supersample_factor(2000, 2000), Some(3));
    assert_eq!(supersample_factor(3000, 3000), Some(2));
    assert_eq!(supersample_factor(5000, 5000), None);
    assert_eq!(supersample_factor(40_000, 10), None);
}

/// A supersampled render draws Outline view's lines one output pixel wide, as an unsupersampled
/// one does, instead of a quarter of a pixel: the line down x = 50.5 draws darker than 64 of 255
/// either way, and no wider than one pixel. So do the outlines of type and a dotted guide along
/// y = 90.5.
#[test]
fn supersampled_outlines_stay_one_pixel_wide() {
    let _fonts = crate::tests_fontchange::fonts_steady();
    let mut d = Document::new(100.0, 100.0);
    let style = vectorcraft_doc::text::CharStyle { size: 30.0, ..Default::default() };
    let text = vectorcraft_doc::text::TextObject::point(vectorcraft_geom::Point::new(60.0, 80.0), "H", style);
    let t = Node::new(d.alloc_id(), NodeKind::Text(Box::new(text)));
    d.insert(Some(d.layers[0].id), 0, t).unwrap();
    let n = rect_node(&mut d, Rect::new(20.0, 20.0, 50.5, 80.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
    d.insert(Some(d.layers[0].id), 0, n).unwrap();
    let mut guide = Node::path(d.alloc_id(), shapes::line(Point::new(5.0, 90.5), Point::new(95.0, 90.5)), Appearance::default());
    if let NodeKind::Path { guide, .. } = &mut guide.kind {
        *guide = true;
    }
    d.insert(Some(d.layers[0].id), 1, guide).unwrap();
    let draw = |supersample: bool| {
        let opts = RenderOptions {
            background: Some([255, 255, 255, 255]),
            outline: true,
            guide: ([0, 0, 0], true),
            precise: true,
            supersample,
            ..Default::default()
        };
        Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts)
    };
    let darkest = |r: &Rendered| (48..=52).map(|x| r.pixel(x, 50)[0]).min().unwrap();
    assert!(darkest(&draw(false)) < 64, "unsupersampled: {}", darkest(&draw(false)));
    assert!(darkest(&draw(true)) < 64, "supersampled: {}", darkest(&draw(true)));
    let type_darkest = |r: &Rendered| (56..=95).flat_map(|x| (45..=85).map(move |y| (x, y))).map(|(x, y)| r.pixel(x, y)[0]).min().unwrap();
    assert!(type_darkest(&draw(false)) < 64, "unsupersampled type: {}", type_darkest(&draw(false)));
    assert!(type_darkest(&draw(true)) < 64, "supersampled type: {}", type_darkest(&draw(true)));
    let guide_darkest = |r: &Rendered| (10..=90).flat_map(|x| (89..=92).map(move |y| (x, y))).map(|(x, y)| r.pixel(x, y)[0]).min().unwrap();
    assert!(guide_darkest(&draw(false)) < 64, "unsupersampled guide: {}", guide_darkest(&draw(false)));
    assert!(guide_darkest(&draw(true)) < 64, "supersampled guide: {}", guide_darkest(&draw(true)));
    // One pixel wide, not wider: across each line, the pixels darker than `dark` are one pixel. The
    // path lies on pixel centers; the type's stems do not, and the guide's dots are 1.5 pixels
    // wide, so their edges are softer.
    let widest = |values: Vec<u8>, dark: u8| {
        let mut runs = vec![0usize];
        for v in values {
            if v < dark {
                *runs.last_mut().unwrap() += 1;
            } else if runs.last() != Some(&0) {
                runs.push(0);
            }
        }
        runs.into_iter().max().unwrap()
    };
    for supersample in [false, true] {
        let r = draw(supersample);
        let row = |y: u32, xs: std::ops::RangeInclusive<u32>| xs.map(|x| r.pixel(x, y)[0]).collect::<Vec<_>>();
        assert_eq!(widest(row(50, 44..=56), 200), 1, "path, supersample {supersample}: {:?}", row(50, 44..=56));
        // Below the H's crossbar, across its two stems.
        assert_eq!(widest(row(76, 56..=95), 128), 1, "type, supersample {supersample}: {:?}", row(76, 56..=95));
        let guide = (10..=90).map(|x| widest((86..=95).map(|y| r.pixel(x, y)[0]).collect(), 128)).max().unwrap();
        assert_eq!(guide, 1, "guide, supersample {supersample}");
    }
}

/// #1613: a zero-length subpath (`m` then `L` to the same point, as `.ai` files write dots) is a
/// dot as wide as the stroke: a disc under a round cap, a square under a projecting one, nothing
/// under a butt cap (PDF 32000-1 8.5.3.2, SVG 2 "Path data" zero-length segments).
#[test]
fn zero_length_subpaths_draw_dots_with_their_cap() {
    let dots = |cap: vectorcraft_doc::LineCap| {
        let mut d = Document::new(100.0, 100.0);
        let mut bp = BezPath::new();
        for at in [(30.0, 50.0), (70.0, 50.0)] {
            bp.move_to(at);
            bp.line_to(at);
        }
        let id = d.alloc_id();
        let mut n = Node::path(id, vectorcraft_geom::PathData::from_bezpath(&bp), Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 10.0));
        n.appearance.stroke_mut().unwrap().cap = cap;
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        render(&d)
    };
    let (black, white) = ([0, 0, 0, 255], [255, 255, 255, 255]);
    let round = dots(vectorcraft_doc::LineCap::Round);
    for x in [30, 70] {
        assert_eq!(round.pixel(x, 50), black, "round dot at {x}");
        assert_eq!(round.pixel(x + 2, 52), black, "inside the round dot at {x}");
        assert_eq!(round.pixel(x + 4, 54), white, "the round dot's corner at {x}");
    }
    let square = dots(vectorcraft_doc::LineCap::Square);
    for x in [30, 70] {
        assert_eq!(square.pixel(x, 50), black, "square dot at {x}");
        assert_eq!(square.pixel(x + 4, 54), black, "the square dot's corner at {x}");
        assert_eq!(square.pixel(x + 6, 50), white, "past the square dot at {x}");
    }
    let butt = dots(vectorcraft_doc::LineCap::Butt);
    assert_eq!(butt.pixels.iter().filter(|&&p| p != 255).count(), 0, "a butt cap draws no dot");
}

/// #2788: a dashed solid stroke is one paint, so its object opacity folds into its colour instead
/// of compositing a viewport-wide layer; the result matches the layered one, overlapping dashes
/// included.
#[test]
fn dashed_stroke_opacity_folds_into_its_colour() {
    let dashed = |d: &mut Document, object: f32, stroke: f32| {
        let mut n = rect_node(d, Rect::new(20.0, 20.0, 80.0, 60.0), Paint::None, Paint::solid(Color::rgb(0.2, 0.4, 0.9)), 6.0);
        let st = n.appearance.stroke_mut().unwrap();
        st.dash = Some(vectorcraft_doc::Dash { pattern: vec![4.0, 1.0], ..Default::default() });
        st.cap = vectorcraft_doc::LineCap::Round;
        st.opacity = stroke;
        n.opacity = object;
        n
    };
    let doc = |object: f32, stroke: f32| {
        let mut d = Document::new(100.0, 100.0);
        let n = dashed(&mut d, object, stroke);
        let l = d.layers[0].id;
        d.insert(Some(l), 0, n).unwrap();
        d
    };
    let mut d = Document::new(100.0, 100.0);
    assert_eq!(painted_items(&dashed(&mut d, 0.6, 1.0)), 1, "a dashed stroke takes the folded fast path");
    let mut brushed = dashed(&mut d, 0.6, 1.0);
    brushed.appearance.stroke_mut().unwrap().brush = Some("Calligraphic".into());
    assert_ne!(painted_items(&brushed), 1, "a dashed brush stroke draws its pieces one by one: no folding");
    let folded = render(&doc(0.6, 1.0));
    let layered = render(&doc(1.0, 0.6));
    assert_ne!(folded.pixel(20, 40), [255, 255, 255, 255], "the stroke is drawn");
    for (a, b) in folded.pixels.chunks(4).zip(layered.pixels.chunks(4)) {
        assert!(a.iter().zip(b).all(|(x, y)| (*x as i32 - *y as i32).abs() <= 1), "folded {a:?} vs layered {b:?}");
    }
}