lini 0.2.0

A small, human-readable language for plain-text diagrams that compiles to clean SVG
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
mod anchors;
mod flex;
mod grid;
mod ir;
mod primitives;
mod text;
mod titles;
mod values;
mod wires;

pub use ir::*;
pub(crate) use text::{approx_height, approx_width};
pub(crate) use wires::cross;
pub use wires::{Rule, Severity, Violation, node_rect};

use crate::error::Error;
use crate::resolve::{Program, ResolvedInst, ResolvedValue, ShapeKind, VarTable};
use crate::span::Span;

use flex::Axis;

/// Extra gap per container (dot-path → `(Δy, Δx)` px), accumulated by gap
/// growth. Growth only ever adds to the gap the user set — `gap` stays
/// their density dial.
type GapGrowth = std::collections::BTreeMap<String, (f64, f64)>;

pub fn layout(program: &Program) -> Result<LaidOut, Error> {
    layout_mode(program, true)
}

/// The testing hook: growth disabled, so the clearance sweep
/// measures the raw router rather than the escape hatch.
pub fn layout_raw(program: &Program) -> Result<LaidOut, Error> {
    layout_mode(program, false)
}

/// Lay out and route; when wires are impossible for lack of corridor lanes
/// (WIRING §Impossible layouts), grow the named containers' gaps by exactly
/// the deficit and rerun — at most 2 rounds, keeping the best result (most
/// drawn, then fewest crossings). Airwires cover whatever still fails.
fn layout_mode(program: &Program, growth_on: bool) -> Result<LaidOut, Error> {
    let mut growth = GapGrowth::new();
    let mut best = attempt(program, &growth)?;
    if growth_on && !best.routing.starved.is_empty() {
        let mut starved = best.routing.starved.clone();
        for _ in 0..2 {
            if !grow(&mut growth, &starved, program) {
                break;
            }
            let next = attempt(program, &growth)?;
            starved = next.routing.starved.clone();
            if better(&next, &best) {
                best = next;
            }
            if starved.is_empty() {
                break;
            }
        }
    }
    Ok(finish(program, best))
}

/// One placed-and-routed scene under a given growth map.
struct Attempt {
    nodes: Vec<PlacedNode>,
    bbox: Bbox,
    routing: wires::Routing,
}

fn attempt(program: &Program, growth: &GapGrowth) -> Result<Attempt, Error> {
    // Lay out top-level scene children.
    let mut top_nodes = Vec::with_capacity(program.scene.nodes.len());
    for inst in &program.scene.nodes {
        top_nodes.push(layout_inst(
            inst,
            &program.vars,
            growth,
            &child_path("", inst),
        )?);
    }

    // Apply scene-level layout to top-level children (scene itself is a
    // container; its attrs drive how its children are positioned). The scene
    // is never a table, so its grid rules — if any — are discarded.
    let (bbox, _) = lay_out_container_children(
        &mut top_nodes,
        &program.scene.attrs,
        &program.vars,
        Span::empty(),
        gap_bump(growth, ""),
    )?;

    // The scene has no border or padding, so a scene-level `place:out` band has
    // no frame to sit outside of — it coincides with `place:in`, reserved just
    // beyond the content extent. Place it before routing so wires see it.
    let (scene_gap_y, _) = primitives::gap(&program.scene.attrs, &program.vars, Span::empty())?;
    let (bbox, _) =
        titles::place_out_bands(&mut top_nodes, bbox, scene_gap_y + gap_bump(growth, "").0)?;

    // Route wires once the nodes are placed.
    let routing = wires::route_wires(program, &top_nodes)?;
    Ok(Attempt {
        nodes: top_nodes,
        bbox,
        routing,
    })
}

/// Strictly better routing outcome: more wires drawn, then fewer crossings.
fn better(a: &Attempt, b: &Attempt) -> bool {
    let key = |t: &Attempt| {
        let crossings = t
            .routing
            .report
            .iter()
            .filter(|v| v.rule == Rule::Crossing)
            .count();
        (t.routing.wires.len(), std::cmp::Reverse(crossings))
    };
    key(a) > key(b)
}

/// Fold one routing's corridor deficits into the growth map. A container
/// pinned by an explicit `size` cannot honestly widen and is skipped.
/// Returns whether anything grew — `false` ends the growth loop.
fn grow(growth: &mut GapGrowth, starved: &GapGrowth, program: &Program) -> bool {
    let mut grew = false;
    for (path, &(dy, dx)) in starved {
        if !growable(program, path) {
            continue;
        }
        let (gy, gx) = growth.entry(path.clone()).or_insert((0.0, 0.0));
        *gy += dy;
        *gx += dx;
        grew |= dy > 0.0 || dx > 0.0;
    }
    grew
}

fn growable(program: &Program, path: &str) -> bool {
    if path.is_empty() {
        return true;
    }
    let mut nodes = &program.scene.nodes;
    let mut found: Option<&ResolvedInst> = None;
    for seg in path.split('.') {
        match nodes.iter().find(|n| n.id.as_deref() == Some(seg)) {
            Some(inst) => {
                nodes = &inst.children;
                found = Some(inst);
            }
            None => return false,
        }
    }
    found
        .is_some_and(|inst| inst.attrs.get("width").is_none() && inst.attrs.get("height").is_none())
}

fn gap_bump(growth: &GapGrowth, path: &str) -> (f64, f64) {
    growth.get(path).copied().unwrap_or((0.0, 0.0))
}

/// A child's dot-path under `parent`. Anonymous children get a `#` segment —
/// never a wire endpoint's ancestor, so never a growth target.
fn child_path(parent: &str, inst: &ResolvedInst) -> String {
    let id = inst.id.as_deref().unwrap_or("#");
    if parent.is_empty() {
        id.to_owned()
    } else {
        format!("{parent}.{id}")
    }
}

/// Union every node's drawn extent into `bbox`, in world coords — so the
/// canvas includes absolute overlays that don't grow their parent's bbox.
fn accumulate_extent(n: &PlacedNode, ox: f64, oy: f64, bbox: &mut Bbox) {
    let (wx, wy) = (ox + n.cx, oy + n.cy);
    *bbox = bbox.union(n.bbox.shifted(wx, wy));
    for c in &n.children {
        accumulate_extent(c, wx, wy, bbox);
    }
}

fn finish(program: &Program, attempt: Attempt) -> LaidOut {
    // Viewbox = scene bbox + wire paths, labels, airwires + canvas-pad on
    // every side.
    let pad = values::layout_var(&program.vars, "canvas-pad").unwrap_or(20.0);
    // Absolute overlays don't grow their parent's bbox, so the scene bbox can
    // miss one that overflows; the canvas must still include every drawn node,
    // so take the true visual extent of the whole tree.
    let mut bbox = attempt.bbox;
    for n in &attempt.nodes {
        accumulate_extent(n, 0.0, 0.0, &mut bbox);
    }
    let routing = attempt.routing;
    let wire_points = routing.wires.iter().flat_map(|w| &w.path);
    let air_points = routing.airwires.iter().flat_map(|a| [&a.from, &a.to]);
    for &(x, y) in wire_points.chain(air_points) {
        bbox.min_x = bbox.min_x.min(x);
        bbox.min_y = bbox.min_y.min(y);
        bbox.max_x = bbox.max_x.max(x);
        bbox.max_y = bbox.max_y.max(y);
    }
    for t in routing.wires.iter().flat_map(|w| &w.texts) {
        let size = t.attrs.number("font-size").unwrap_or(12.0);
        let (hw, hh) = (
            text::approx_width(&t.content, size) / 2.0,
            text::approx_height(&t.content, size) / 2.0,
        );
        bbox.min_x = bbox.min_x.min(t.position.0 - hw);
        bbox.min_y = bbox.min_y.min(t.position.1 - hh);
        bbox.max_x = bbox.max_x.max(t.position.0 + hw);
        bbox.max_y = bbox.max_y.max(t.position.1 + hh);
    }
    let vb = ViewBox {
        x: bbox.min_x - pad,
        y: bbox.min_y - pad,
        w: bbox.w() + 2.0 * pad,
        h: bbox.h() + 2.0 * pad,
    };

    // A root `fill:` is the canvas colour (SPEC §13); `none` stays transparent.
    let canvas_fill = program
        .scene
        .attrs
        .get("fill")
        .filter(|v| !matches!(v, ResolvedValue::Ident(s) if s == "none"))
        .cloned();

    LaidOut {
        viewbox: vb,
        nodes: attempt.nodes,
        wires: routing.wires,
        wire_report: routing.report,
        airwires: routing.airwires,
        vars: program.vars.clone(),
        sheet: program.sheet.clone(),
        canvas_fill,
    }
}

/// Validate a laid-out scene's wires against the routing contract (WIRING.md):
/// the router's own report (kept crossings, impossible wires), then the
/// independent four-law check. Used by `lini::validate_str`.
pub fn validate_routing(laid: &LaidOut) -> Vec<Violation> {
    let mut out = laid.wire_report.clone();
    out.extend(wires::validate_routing(
        &laid.nodes,
        &laid.wires,
        &laid.wire_report,
        &laid.vars,
    ));
    out
}

/// Recursively lay out a single instance into a PlacedNode.
///
/// Bottom-up: lay out children first, then size this node around them. For
/// leaf primitives (no children), the shape's dimensions drive the bbox.
/// `path` is the inst's dot-path — the key gap growth bumps it under.
fn layout_inst(
    inst: &ResolvedInst,
    vars: &VarTable,
    growth: &GapGrowth,
    path: &str,
) -> Result<PlacedNode, Error> {
    // Recurse into children first.
    let mut children: Vec<PlacedNode> = Vec::with_capacity(inst.children.len());
    for c in &inst.children {
        children.push(layout_inst(c, vars, growth, &child_path(path, c))?);
    }

    // Determine this node's bbox + arrange children inside.
    let mut dividers: Vec<GridRule> = Vec::new();
    // The drawn frame, set only when a `place:out` band grows the footprint
    // past it; otherwise the footprint is the drawn box (`frame` stays None).
    let mut frame: Option<Bbox> = None;
    let bbox = if children.is_empty() {
        // Leaf primitive.
        primitives::leaf_bbox(inst, vars)?
    } else {
        // Container or closed shape with content.
        let (content_bbox, rules) = lay_out_container_children(
            &mut children,
            &inst.attrs,
            vars,
            inst.span,
            gap_bump(growth, path),
        )?;

        // Interior dividers (grid or 1-D) the container draws, per `divider:`.
        // A table is just a group with `divider: all` — no special-casing; its
        // border is the group rect, its inner lines these dividers.
        dividers = rules;

        // The closed shape sizes border-box: explicit width/height, else
        // content + padding per axis (SPEC §6).
        let b = primitives::closed_bbox(inst, content_bbox, vars)?;
        let text_only = children.iter().all(|c| c.shape == ShapeKind::Text);

        // Some closed shapes carry decoration at the top — a cloud's lobes, a
        // cylinder's rim — so the optical body-center sits below the bbox center
        // and a centered label reads too high. Drop a text-only label into the
        // body by a shape-specific fraction of the height (the outlines are
        // scale-invariant, so a fraction holds at any size).
        const CLOUD_LABEL_DROP: f64 = 0.1;
        const CYL_LABEL_DROP: f64 = 0.03;
        let label_drop = match inst.shape {
            ShapeKind::Cloud => CLOUD_LABEL_DROP,
            ShapeKind::Cyl => CYL_LABEL_DROP,
            _ => 0.0,
        };
        if label_drop > 0.0 && text_only {
            let dy = b.h() * label_drop;
            for c in &mut children {
                c.cy += dy;
            }
        }

        // `place:out` bands sit a container `gap` outside the drawn frame `b`.
        // The footprint grows to reserve them — so siblings clear the caption —
        // while the shape keeps drawing at `b`.
        let (gap_y, _) = primitives::gap(&inst.attrs, vars, inst.span)?;
        let (footprint, has_out) =
            titles::place_out_bands(&mut children, b, gap_y + gap_bump(growth, path).0)?;
        if has_out {
            frame = Some(b);
        }
        footprint
    };

    let rotation = inst.attrs.number("rotate").unwrap_or(0.0);

    Ok(PlacedNode {
        id: inst.id.clone(),
        shape: inst.shape,
        type_chain: inst.type_chain.clone(),
        applied_styles: inst.applied_styles.clone(),
        label: inst.label.clone(),
        attrs: inst.attrs.clone(),
        markers: inst.markers.clone(),
        cx: 0.0,
        cy: 0.0,
        bbox,
        frame,
        rotation,
        children,
        dividers,
        span: inst.span,
    })
}

/// Interior separators between adjacent flow children — perpendicular to the
/// flow at each gap's midpoint, spanning the flow's cross extent (SPEC §5,
/// 1-D `divider`).
fn one_d_dividers(
    children: &[PlacedNode],
    flow: &[usize],
    mode: LayoutMode,
    flow_bbox: Bbox,
) -> Vec<GridRule> {
    let row = matches!(mode, LayoutMode::Row);
    let main = |i: usize| if row { children[i].cx } else { children[i].cy };
    let half = |i: usize| {
        if row {
            children[i].bbox.w() / 2.0
        } else {
            children[i].bbox.h() / 2.0
        }
    };
    let mut order: Vec<usize> = flow.to_vec();
    order.sort_by(|&a, &b| main(a).total_cmp(&main(b)));
    let mut segs = Vec::new();
    for pair in order.windows(2) {
        let mid = (main(pair[0]) + half(pair[0]) + main(pair[1]) - half(pair[1])) / 2.0;
        if row {
            segs.push((mid, flow_bbox.min_y, mid, flow_bbox.max_y));
        } else {
            segs.push((flow_bbox.min_x, mid, flow_bbox.max_x, mid));
        }
    }
    segs
}

/// Position children within their container per its `layout=` attr.
/// Returns the bounding bbox of all placed children, in container-local
/// coords. A non-zero `grow` is gap growth's `(Δy, Δx)` for this container,
/// added to whatever gap the user set.
fn lay_out_container_children(
    children: &mut [PlacedNode],
    container_attrs: &crate::resolve::AttrMap,
    vars: &VarTable,
    span: Span,
    grow: (f64, f64),
) -> Result<(Bbox, Vec<GridRule>), Error> {
    if children.is_empty() {
        return Ok((Bbox::empty(), Vec::new()));
    }
    let grown;
    let container_attrs = if grow == (0.0, 0.0) {
        container_attrs
    } else {
        let (gy, gx) = primitives::gap(container_attrs, vars, span)?;
        let mut attrs = container_attrs.clone();
        attrs.insert(
            "gap",
            ResolvedValue::Tuple(vec![
                ResolvedValue::Number(gy + grow.0),
                ResolvedValue::Number(gx + grow.1),
            ]),
        );
        grown = attrs;
        &grown
    };

    // `margin:` is signed outer spacing on a child. Inflate each child's bbox
    // into its layout footprint up front; every layout routine below then
    // spaces and sizes against the footprint (so margin grows/shrinks the
    // parent and the gaps), and we deflate back to the drawn box at the end.
    // Negative margins simply make the footprint smaller than the box — they
    // tighten, and may overlap, which is the intent.
    let margins: Vec<(f64, f64, f64, f64)> = children
        .iter()
        .map(|c| primitives::margin(&c.attrs, c.span))
        .collect::<Result<_, _>>()?;
    for (c, &(t, r, b, l)) in children.iter_mut().zip(&margins) {
        c.bbox = c.bbox.expand(t, r, b, l);
    }

    // Sort children into three roles (SPEC §7). `side:` with `place:in`/`out`
    // reserves an edge band (the parent grows); `at:(x,y)` or `place:on` is an
    // absolute overlay (the parent does not grow); everything else flows.
    let mut flow_indices: Vec<usize> = Vec::new();
    let mut abs_indices: Vec<usize> = Vec::new();
    let mut reserve_indices: Vec<usize> = Vec::new();
    for (i, c) in children.iter().enumerate() {
        match anchors::child_role(&c.attrs, c.span)? {
            anchors::Role::Flow => flow_indices.push(i),
            anchors::Role::Reserve => reserve_indices.push(i),
            anchors::Role::Absolute => abs_indices.push(i),
        }
    }

    // Lay out the flow children per the container's `layout=` attr.
    let mode = read_layout_mode(container_attrs, span)?;
    // Slack for align/justify/stretch comes only from an explicit container
    // size: the content area is the declared dimension minus padding (SPEC §5).
    let pad = primitives::padding(container_attrs, vars, span)?;
    let avail = (
        container_attrs
            .number("width")
            .map(|w| (w - pad.left - pad.right).max(0.0)),
        container_attrs
            .number("height")
            .map(|h| (h - pad.top - pad.bottom).max(0.0)),
    );

    let mut grid_rules: Vec<GridRule> = Vec::new();
    let flow_bbox = if !flow_indices.is_empty() {
        let mut flow_children: Vec<PlacedNode> =
            flow_indices.iter().map(|i| children[*i].clone()).collect();
        let bbox = match mode {
            LayoutMode::Row => flex::lay_out_flex(
                Axis::Row,
                &mut flow_children,
                container_attrs,
                vars,
                span,
                avail,
            )?,
            LayoutMode::Column => flex::lay_out_flex(
                Axis::Column,
                &mut flow_children,
                container_attrs,
                vars,
                span,
                avail,
            )?,
            LayoutMode::Grid => {
                // A table (a grid with dividers) reads `padding` as the per-cell
                // inset (SPEC §8): inflate each cell so auto tracks size to
                // content + inset and the text centres with that breathing room.
                if grid::is_inset_grid(container_attrs) {
                    for c in &mut flow_children {
                        c.bbox = c.bbox.expand(pad.top, pad.right, pad.bottom, pad.left);
                    }
                }
                let (bbox, rules) =
                    grid::lay_out_grid(&mut flow_children, container_attrs, vars, span)?;
                grid_rules = rules;
                bbox
            }
        };
        for (slot, placed) in flow_indices.iter().zip(flow_children) {
            children[*slot] = placed;
        }
        bbox
    } else {
        Bbox::empty()
    };

    // 1-D dividers between flow children (a grid produced its own above),
    // painted by the container's own stroke (SPEC §5).
    if matches!(mode, LayoutMode::Row | LayoutMode::Column)
        && grid::read_divider(container_attrs) != grid::Divider::None
        && flow_indices.len() > 1
    {
        grid_rules = one_d_dividers(children, &flow_indices, mode, flow_bbox);
    }

    // Reserve children carve a band on the top/bottom edge. `place:in` bands
    // sit inside the frame, so the box grows around them here — before padding,
    // so the border wraps them. `place:out` bands sit outside the drawn frame
    // and are placed by the caller (`titles::place_out_bands`) once padding has
    // fixed the border. The result here — content + inside bands — is the body
    // the anchors and the parent bbox resolve against.
    let in_indices: Vec<usize> = reserve_indices
        .iter()
        .copied()
        .filter(|&i| !anchors::is_out_band(&children[i].attrs))
        .collect();
    let body_bbox = if in_indices.is_empty() {
        flow_bbox
    } else {
        // A title is separated from the content by the container's vertical gap
        // — the same gap that spaces flow siblings (SPEC §7).
        let (gap_y, _) = primitives::gap(container_attrs, vars, span)?;
        titles::reserve_bands(
            children,
            &flow_indices,
            &in_indices,
            flow_bbox,
            &mut grid_rules,
            gap_y,
        )
    };

    // Resolution bbox for edge anchors. If the container has explicit
    // dimensions (e.g. `size:(200, 120)`), anchors snap to those edges;
    // otherwise we fall back to the body extent.
    let anchor_parent_bbox = container_anchor_bbox(container_attrs).unwrap_or(body_bbox);

    // Absolutely positioned children.
    for i in &abs_indices {
        let pos = anchors::read_pos(&children[*i].attrs, children[*i].span)?
            .expect("abs child carries at: or side:");
        let offset = match children[*i].attrs.get("offset") {
            Some(v) => anchors::parse_offset(v, children[*i].span)?,
            None => (0.0, 0.0),
        };
        let (target_cx, target_cy) = anchors::resolve(pos, anchor_parent_bbox, children[*i].bbox);
        // `at:(x,y)` puts the bbox CENTER at (x,y) per SPEC §7 rule 1.
        let cb = children[*i].bbox;
        let local_off_x = (cb.min_x + cb.max_x) / 2.0;
        let local_off_y = (cb.min_y + cb.max_y) / 2.0;
        children[*i].cx = target_cx + offset.0 - local_off_x;
        children[*i].cy = target_cy + offset.1 - local_off_y;
    }

    // Absolute overlays (`at:`, `place:on`) are positioned above, but they do
    // NOT grow the parent (SPEC §7): the parent sizes to its flow + reserved
    // bands only. An absolutes-only container with no explicit `size:` collapses
    // — that's the deal. The canvas viewBox still includes them (see `finish`),
    // so an overlay is never clipped.

    // Deflate footprints back to drawn boxes. Each routine placed the footprint
    // centre at its target, so the box — sitting margin-inset within the
    // footprint — is already in the right spot; only its bbox shrinks back.
    for (c, &(t, r, b, l)) in children.iter_mut().zip(&margins) {
        c.bbox = c.bbox.expand(-t, -r, -b, -l);
    }

    Ok((body_bbox, grid_rules))
}

/// Container layout mode, parsed from the `layout=` attr.
#[derive(Clone, Copy, Debug)]
enum LayoutMode {
    Row,
    Column,
    /// 2D grid; sized by its `columns` / `rows` track lists (read in `grid`).
    Grid,
}

fn read_layout_mode(attrs: &crate::resolve::AttrMap, span: Span) -> Result<LayoutMode, Error> {
    match attrs.get("layout") {
        None => Ok(LayoutMode::Column),
        Some(ResolvedValue::Ident(s)) => match s.as_str() {
            "row" => Ok(LayoutMode::Row),
            "column" => Ok(LayoutMode::Column),
            "grid" => Ok(LayoutMode::Grid),
            other => Err(Error::at(
                span,
                format!("unknown layout '{}' — expected row, column, or grid", other),
            )),
        },
        Some(_) => Err(Error::at(span, "'layout' expects row, column, or grid")),
    }
}

/// If the container declared explicit `width` *and* `height`, the children's
/// anchors resolve against those edges (no stroke pad — anchors live on the
/// drawn shape); otherwise they fall back to the body extent.
fn container_anchor_bbox(attrs: &crate::resolve::AttrMap) -> Option<Bbox> {
    let w = attrs.number("width")?;
    let h = attrs.number("height")?;
    Some(Bbox::centered(w, h))
}

// ───────────────────────────── Tests ─────────────────────────────

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

    fn lay_out(src: &str) -> LaidOut {
        let tokens = crate::lexer::lex(src).expect("lex");
        let file = crate::syntax::parser::parse(&tokens).expect("parse");
        let program = crate::resolve::resolve_with_theme(&file, &[]).expect("resolve");
        layout(&program).expect("layout")
    }

    // ── Sizing (SPEC §6) ──

    #[test]
    fn empty_closed_shape_is_two_paddings() {
        // padding 16 each side → 32 drawn; + stroke 1 → 33 bbox.
        let n = &lay_out("|box|\n").nodes[0];
        assert!((n.bbox.w() - 33.0).abs() < 0.01, "w={}", n.bbox.w());
        assert!((n.bbox.h() - 33.0).abs() < 0.01, "h={}", n.bbox.h());
    }

    #[test]
    fn explicit_dims_are_border_box() {
        let n = &lay_out("|box| { width: 100; height: 50; }\n").nodes[0];
        assert!((n.bbox.w() - 101.0).abs() < 0.01, "w={}", n.bbox.w());
        assert!((n.bbox.h() - 51.0).abs() < 0.01, "h={}", n.bbox.h());
    }

    #[test]
    fn stroke_width_counts_toward_the_bbox() {
        // SPEC §6: width 100 height 50 stroke-width 4 → 104×54.
        let n = &lay_out("|box| { width: 100; height: 50; stroke-width: 4; }\n").nodes[0];
        assert!((n.bbox.w() - 104.0).abs() < 0.01, "w={}", n.bbox.w());
        assert!((n.bbox.h() - 54.0).abs() < 0.01, "h={}", n.bbox.h());
    }

    #[test]
    fn label_auto_sizes_to_content_plus_padding() {
        // text ~15.4 + 2×16 padding + stroke → ~48.
        let n = &lay_out("|box| { \"hi\" }\n").nodes[0];
        assert!(n.bbox.w() > 40.0 && n.bbox.w() < 60.0, "w={}", n.bbox.w());
    }

    #[test]
    fn dims_are_independent_per_axis() {
        let n = &lay_out("|box| { width: 200; \"hi\" }\n").nodes[0];
        assert!((n.bbox.w() - 201.0).abs() < 0.01, "w={}", n.bbox.w());
        // height auto = one text line (14) + 32 padding + 1 stroke = 47.
        assert!((n.bbox.h() - 47.0).abs() < 0.01, "h={}", n.bbox.h());
    }

    #[test]
    fn oval_uses_width_height() {
        let n = &lay_out("|oval| { width: 100; height: 50; }\n").nodes[0];
        assert!((n.bbox.w() - 101.0).abs() < 0.01, "w={}", n.bbox.w());
        assert!((n.bbox.h() - 51.0).abs() < 0.01, "h={}", n.bbox.h());
    }

    #[test]
    fn text_sizes_to_its_glyphs_without_padding() {
        let n = &lay_out("\"hi\"\n").nodes[0];
        assert!((n.bbox.w() - 15.4).abs() < 0.5, "w={}", n.bbox.w()); // 2 × 14 × 0.55
        assert!((n.bbox.h() - 14.0).abs() < 0.5, "h={}", n.bbox.h());
    }

    // ── Basic flow (full align/justify/stretch/evenly land in the flex chunk) ──

    #[test]
    fn row_layout_stacks_horizontally() {
        let l = lay_out(
            "layout: row; gap: 10;\n\
             |box| { width: 100; height: 40; }\n\
             |box| { width: 60; height: 40; }\n",
        );
        assert_eq!(l.nodes.len(), 2);
        // half (50.5) + gap (10) + half (30.5) = 91.
        let dx = l.nodes[1].cx - l.nodes[0].cx;
        assert!((dx - 91.0).abs() < 0.5, "dx={}", dx);
        assert!((l.nodes[0].cy - l.nodes[1].cy).abs() < 0.01);
    }

    #[test]
    fn column_layout_stacks_vertically() {
        let l = lay_out(
            "layout: column; gap: 20;\n\
             |box| { width: 100; height: 40; }\n\
             |box| { width: 100; height: 60; }\n",
        );
        // half (20.5) + gap (20) + half (30.5) = 71.
        let dy = l.nodes[1].cy - l.nodes[0].cy;
        assert!((dy - 71.0).abs() < 0.5, "dy={}", dy);
        assert!((l.nodes[0].cx - l.nodes[1].cx).abs() < 0.01);
    }

    #[test]
    fn viewbox_wraps_content_with_canvas_pad() {
        // bbox 101×41, + 20 canvas-pad each side → 141×81.
        let l = lay_out("|box| { width: 100; height: 40; }\n");
        assert!((l.viewbox.w - 141.0).abs() < 0.01, "w={}", l.viewbox.w);
        assert!((l.viewbox.h - 81.0).abs() < 0.01, "h={}", l.viewbox.h);
    }

    // ── Caption bands (mount: in, SPEC §6/§8) ──

    #[test]
    fn group_caption_reserves_a_band_above_the_content() {
        let h = |src: &str| lay_out(src).nodes[0].bbox.h();
        let plain = h("g |group| {\n  a |box| { width: 80; height: 30; }\n}\n");
        let capped =
            h("g |group| {\n  |caption| { \"Cap\" }\n  a |box| { width: 80; height: 30; }\n}\n");
        assert!(
            capped > plain + 10.0,
            "caption adds a band: plain={plain} capped={capped}"
        );
    }

    #[test]
    fn caption_sits_above_the_content() {
        let l = lay_out(
            "g |group| {\n  |caption| { \"Cap\" }\n  a |box| { width: 80; height: 30; }\n}\n",
        );
        let g = &l.nodes[0];
        let cap = g
            .children
            .iter()
            .find(|c| c.type_chain.iter().any(|t| t == "caption"))
            .expect("caption child");
        let a = g
            .children
            .iter()
            .find(|c| c.id.as_deref() == Some("a"))
            .expect("box child");
        assert!(cap.cy < a.cy, "cap.cy={} a.cy={}", cap.cy, a.cy);
    }

    // ── Flex distribution with slack (SPEC §5) ──

    #[test]
    fn justify_orders_children_start_center_end() {
        let first_cx = |j: &str| {
            let src = format!(
                "g |row| {{ width: 300; justify: {j};\n  a |box| {{ width: 40; height: 20; }}\n  b |box| {{ width: 40; height: 20; }}\n}}\n"
            );
            lay_out(&src).nodes[0].children[0].cx
        };
        let (start, center, end) = (first_cx("start"), first_cx("center"), first_cx("end"));
        assert!(
            start < center && center < end,
            "start={start} center={center} end={end}"
        );
    }

    #[test]
    fn justify_evenly_spaces_children_equally() {
        let l = lay_out(
            "g |row| { width: 300; justify: evenly;\n  a |box| { width: 20; height: 20; }\n  b |box| { width: 20; height: 20; }\n  c |box| { width: 20; height: 20; }\n}\n",
        );
        let cx: Vec<f64> = l.nodes[0].children.iter().map(|c| c.cx).collect();
        assert!(
            ((cx[1] - cx[0]) - (cx[2] - cx[1])).abs() < 0.01,
            "centers {cx:?}"
        );
    }

    #[test]
    fn align_stretch_fills_the_cross_axis() {
        // An unsized child grows to the row's content height (row pads 0).
        let l = lay_out("g |row| { height: 80; align: stretch;\n  a |box| { width: 40; }\n}\n");
        let a = &l.nodes[0].children[0];
        assert!((a.bbox.h() - 80.0).abs() < 1.0, "a.h={}", a.bbox.h());
    }

    #[test]
    fn no_slack_means_no_distribution() {
        // An auto-width row ignores justify — children stay packed at the gap.
        let span = |j: &str| {
            let src = format!(
                "g |row| {{ justify: {j};\n  a |box| {{ width: 40; height: 20; }}\n  b |box| {{ width: 40; height: 20; }}\n}}\n"
            );
            let l = lay_out(&src);
            l.nodes[0].children[1].cx - l.nodes[0].children[0].cx
        };
        assert!(
            (span("start") - span("end")).abs() < 0.01,
            "auto row: justify is a no-op"
        );
    }

    // ── Grid (SPEC §5) ──

    #[test]
    fn grid_fixed_columns_place_children_in_order() {
        let l = lay_out(
            "layout: grid; columns: 80 80 80; gap: 0;\n\
             a |box| { width: 40; height: 40; }\n\
             b |box| { width: 40; height: 40; }\n\
             c |box| { width: 40; height: 40; }\n",
        );
        let cx: Vec<f64> = l.nodes.iter().map(|n| n.cx).collect();
        assert!((cx[1] - cx[0] - 80.0).abs() < 0.5, "dx={}", cx[1] - cx[0]);
        assert!((cx[2] - cx[1] - 80.0).abs() < 0.5);
        assert!((l.nodes[0].cy - l.nodes[1].cy).abs() < 0.01);
    }

    #[test]
    fn grid_repeat_makes_auto_columns_and_wraps() {
        let l = lay_out(
            "layout: grid; columns: repeat(2);\n\
             a |box| { width: 30; height: 30; }\n\
             b |box| { width: 30; height: 30; }\n\
             c |box| { width: 30; height: 30; }\n",
        );
        // 2 columns, 3 children → c wraps to the second row.
        assert!(l.nodes[2].cy > l.nodes[0].cy, "c below a");
    }

    #[test]
    fn grid_cell_pins_placement() {
        let l = lay_out(
            "layout: grid; columns: repeat(3);\n\
             a |box| { cell: 3 1; }\n\
             b |box|\n",
        );
        // a pins to column 3; b auto-flows to the first free cell (column 1).
        assert!(
            l.nodes[0].cx > l.nodes[1].cx,
            "a (col 3) right of b (col 1)"
        );
    }

    #[test]
    fn grid_cell_fills_its_track_under_stretch() {
        let l = lay_out(
            "layout: grid; columns: 120 120; gap: 0;\n\
             a |box| { justify: stretch; align: stretch; }\n\
             b |box|\n",
        );
        assert!(
            (l.nodes[0].bbox.w() - 120.0).abs() < 1.0,
            "a.w={}",
            l.nodes[0].bbox.w()
        );
    }

    #[test]
    fn grid_rows_track_list_is_a_floor_implicit_rows_overflow() {
        // SPEC §5/§20: a declared `rows` track list sizes the first rows; extra
        // children flow into implicit auto rows (CSS grid) rather than erroring.
        // Here 2 cols × 1 declared row track, 4 children → a second, implicit row.
        let l = lay_out(
            "layout: grid; columns: 40 40; rows: auto;\n\
             a |box| { width: 30; height: 30; }\n\
             b |box| { width: 30; height: 30; }\n\
             c |box| { width: 30; height: 30; }\n\
             d |box| { width: 30; height: 30; }\n",
        );
        assert!(l.nodes[2].cy > l.nodes[0].cy, "c (row 2) below a (row 1)");
        assert!(
            (l.nodes[2].cy - l.nodes[3].cy).abs() < 0.01,
            "c, d share row 2"
        );
    }

    #[test]
    fn grid_without_columns_is_an_error() {
        let tokens = crate::lexer::lex("layout: grid;\na |box|\nb |box|\n").expect("lex");
        let file = crate::syntax::parser::parse(&tokens).expect("parse");
        let program = crate::resolve::resolve_with_theme(&file, &[]).expect("resolve");
        assert!(layout(&program).is_err());
    }

    // ── Dividers (SPEC §5) ──

    #[test]
    fn table_draws_interior_dividers_no_frame() {
        let l = lay_out("t |table| { columns: 40 40;\n  \"a\" \"b\" \"c\" \"d\"\n}\n");
        // 2×2 grid with the table's divider: all → interior separators.
        assert!(
            !l.nodes[0].dividers.is_empty(),
            "table has interior dividers"
        );
        // A plain group draws none.
        assert!(
            lay_out("g |group| { x |box| }\n").nodes[0]
                .dividers
                .is_empty()
        );
    }

    #[test]
    fn grid_dividers_stay_within_the_content_box() {
        // Interior dividers must not overshoot the frame: every endpoint sits
        // inside the grid's own content box (a gap-sized overshoot at the far
        // edge once leaked past the group's border).
        let l = lay_out(
            "t |table| { columns: 40 40; gap: 20;\n  \"a\"\n  \"b\"\n  \"c\"\n  \"d\"\n}\n",
        );
        let t = &l.nodes[0];
        let (hw, hh) = (t.draw_box().w() / 2.0 + 0.01, t.draw_box().h() / 2.0 + 0.01);
        for (x1, y1, x2, y2) in &t.dividers {
            for (x, y) in [(x1, y1), (x2, y2)] {
                assert!(x.abs() <= hw, "divider x {x} exceeds half-width {hw}");
                assert!(y.abs() <= hh, "divider y {y} exceeds half-height {hh}");
            }
        }
    }

    #[test]
    fn one_d_divider_falls_between_flow_children() {
        let l = lay_out(
            "g |row| { divider: all;\n  a |box| { width: 30; height: 30; }\n  b |box| { width: 30; height: 30; }\n  c |box| { width: 30; height: 30; }\n}\n",
        );
        assert_eq!(
            l.nodes[0].dividers.len(),
            2,
            "two separators between three children"
        );
    }
}