hephaestus 0.1.0

Backend-agnostic 2D scene renderer for data visualization.
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
//! Space reservation for legends.
//!
//! [`LegendMeasure`] pre-shapes one legend — labels, title, and the
//! body's own geometry — and reports the block's primary (column width
//! / row height) and cross extents to the layout solver.
//! [`LegendStackMeasure`] composes several of them for one side.
//!
//! Building a measure does the work the draw pass would otherwise
//! repeat: it shapes every label and, for a discrete stack, solves the
//! whole inner grid. The draw pass takes the finished measure, so
//! reserved space and drawn space come from one computation.

use crate::layout::{Measure, WidthHint};
use crate::plot::chrome::linear_axis::pt_to_px;
use crate::plot::scale::ScaleRegistry;
use crate::plot::theme::HAlign;
use crate::scales::breaks::DEFAULT_BREAK_COUNT;
use crate::scales::chrome::LegendSide;
use crate::scales::value::Value;
use crate::shape::ShapeRegistry;
use crate::text::TextRun;

use super::colorbar::{bin_edges, bin_midpoints};
use super::layout::{
    build_discrete_stack_layout, DiscreteStackLayout, LabelMeasure, SwatchCellMeasure,
};
use super::render_keys::swatch_dim_for;
use super::spec::{resolve_key, Legend, LegendBody};
use super::{cardinal_side, legend_text_elements};

/// Everything the layout solver needs to reserve a slot for one
/// legend, pre-shaped against the theme it will be drawn with.
pub(crate) struct LegendMeasure {
    side: LegendSide,
    /// Pre-solved geometry for whichever body the legend carries —
    /// the draw pass reads its own variant back out.
    pub(super) body: BodyMeasure,
    /// Shaped label dims, max across breaks.
    max_label_w_px: f64,
    max_label_h_px: f64,
    pub(super) title_w_px: f64,
    pub(super) title_h_px: f64,
    /// Number of non-null breaks the domain scale produces.
    entry_count: usize,
    // ── Layout sizes resolved from the LegendTheme at construction
    // so the `Measure` trait impl can use them without needing
    // `&LegendTheme` at width_hint/height_at call time.
    /// Inner padding in px (uniform — uses `lt.padding.left`).
    pub(super) padding_px: f64,
    /// Gap between adjacent keys in a single legend, px.
    pub(super) row_gap_px: f64,
    /// Gap between a key swatch and its label, px. Same value the
    /// legend's axis renders for tick → label gap — pre-resolved
    /// here only because [`build_discrete_stack_layout`] needs it as
    /// a `Fixed` track gap during construction.
    swatch_label_gap_px: f64,
    /// The legend's axis chrome — kept here so the binned + colorbar
    /// primary dim can ask the axis for its own thickness instead of
    /// re-summing tick / gap arms by hand. Cloned at construction
    /// because the `Measure` trait impl outlives `&LegendTheme`.
    axis: crate::plot::theme::AxisTheme,
    /// Gap between the panel-facing slot edge and the legend's outer
    /// block, px. Inflates the primary dim reservation; the renderer
    /// shrinks the slot rect on the panel-facing side by the same
    /// amount before drawing.
    legend_gap_px: f64,
}

pub(super) enum BodyMeasure {
    /// Discrete stack — entries area pre-solved by the layout grid.
    /// The grid handles per-row auto sizing for free: vertical legend
    /// rows pick the widest swatch as the uniform column width while
    /// each row's height tracks its own marker, horizontal legends
    /// transpose. `no_keys` short-circuits the measure to zero when
    /// the stack is empty.
    Stack {
        layout: DiscreteStackLayout,
        no_keys: bool,
    },
    /// Binned stack — N-bins-from-N+1-breaks layout with a between-row
    /// tick rail. `row_cells_px` holds one entry per **bin**, sized at
    /// the bin's midpoint so it matches what the renderer paints
    /// there; binned rows touch, so the row gap doesn't apply. Bin
    /// count and per-bin dimensions are independent of
    /// [`BinSpacing`](super::BinSpacing) — spacing only redistributes
    /// bins along the finished bar. An empty `row_cells_px` means the
    /// domain yields no bins and the body draws nothing. `no_keys`
    /// short-circuits the measure to zero when the stack is empty.
    BinnedStack {
        row_cells_px: Vec<(f64, f64)>,
        no_keys: bool,
    },
    /// Colorbar — `bar_thickness_px` is the bar's perpendicular
    /// extent; `samples` is forwarded to the renderer.
    Colorbar {
        bar_thickness_px: f64,
        samples: usize,
    },
}

impl LegendMeasure {
    /// Shape a legend against the theme and registries it will be
    /// drawn with. `shapes` has to be the registry the draw pass
    /// resolves markers through for the reserved cells to match the
    /// markers' bounds.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn new(
        legend: &Legend,
        registry: &ScaleRegistry,
        shapes: &ShapeRegistry,
        dpi: f64,
        lt: &crate::plot::theme::LegendTheme,
        geom: &crate::plot::theme::GeomTheme,
        legend_gap_px: f64,
        locale: &crate::scales::Locale,
        root_pt: f64,
    ) -> Self {
        // Label + title styles come from the LegendTheme so measure
        // and draw size identical glyph runs. `Blank` short-circuits
        // to `None` — the corresponding cell reserves zero space
        // because the renderer won't draw it.
        let (title_el, label_el) = legend_text_elements(lt);
        let label_style =
            label_el.map(|el| crate::plot::chrome::text::text_style_from(&el, root_pt));
        let title_style =
            title_el.map(|el| crate::plot::chrome::text::text_style_from(&el, root_pt));
        let domain = registry.get(&legend.domain_scale);
        let breaks = domain
            .map(|s| s.breaks(DEFAULT_BREAK_COUNT))
            .unwrap_or_default();

        let mut entry_count = 0usize;
        let mut max_label_w: f64 = 0.0;
        let mut max_label_h: f64 = 0.0;
        for v in &breaks {
            if matches!(v, Value::Null) {
                continue;
            }
            entry_count += 1;
            // Blank labels render nothing → no reserved label box.
            let Some(label_style) = label_style.as_ref() else {
                continue;
            };
            let label = domain.map(|s| s.format(v, locale)).unwrap_or_default();
            let run = TextRun::new(&label, label_style, dpi);
            let h = run.set_max_width(f32::INFINITY, HAlign::Start) as f64;
            // Labels render unwrapped, so the slot needs the full
            // single-line width — `width_hint` returns the
            // longest-unbreakable-cluster bound (one word), which
            // undershoots multi-word labels and clips them at draw.
            let w = run.natural_width();
            max_label_w = max_label_w.max(w);
            max_label_h = max_label_h.max(h);
        }

        // Resolve LegendTheme layout sizes the construction step needs
        // as px. `swatch_label_gap_px` is pulled forward of the body
        // match so the grid build below sees a `Fixed` gap track.
        let padding_px = pt_to_px(lt.padding.left.resolve(0.0), dpi);
        let row_gap_px = pt_to_px(lt.key.spacing.resolve(0.0), dpi);
        let swatch_label_gap_px = pt_to_px(lt.axis.resolved().tick_gap.resolve(0.0), dpi);

        let body = match &legend.body {
            LegendBody::Stack(stack) if stack.binned => {
                let key_w_floor = pt_to_px(lt.key.width.resolve(0.0), dpi);
                let key_h_floor = pt_to_px(lt.key.height.resolve(0.0), dpi);
                // One row per bin, sized from the bin midpoint — the
                // value `render_binned_stack_body` resolves its keys
                // at — so reserved bar length and thickness match the
                // draw. Sampling the edges instead would yield one
                // row too many and size them off values no bin uses.
                let row_cells_px: Vec<(f64, f64)> = bin_midpoints(&bin_edges(&breaks))
                    .iter()
                    .map(|v| {
                        let (mut row_w, mut row_h) = (0.0_f64, 0.0_f64);
                        for key in &stack.keys {
                            let resolved = resolve_key(key, registry, v);
                            let (w, h) = swatch_dim_for(key.kind, &resolved, dpi, geom, shapes);
                            row_w = row_w.max(w);
                            row_h = row_h.max(h);
                        }
                        (row_w.max(key_w_floor), row_h.max(key_h_floor))
                    })
                    .collect();
                BodyMeasure::BinnedStack {
                    row_cells_px,
                    no_keys: stack.keys.is_empty(),
                }
            }
            LegendBody::Stack(stack) => {
                // Build the discrete-stack layout via the grid solver.
                // Each row contributes a SwatchCellMeasure (max of its
                // keys' intrinsic dims, floored at KeyTheme) and a
                // LabelMeasure (natural single-line width / height).
                // The grid's Auto tracks resolve to content, giving
                // per-row auto sizing with cross-axis uniformity.
                let key_w_floor = pt_to_px(lt.key.width.resolve(0.0), dpi);
                let key_h_floor = pt_to_px(lt.key.height.resolve(0.0), dpi);
                let horizontal = matches!(
                    cardinal_side(legend.side),
                    LegendSide::Top | LegendSide::Bottom
                );
                let mut rows: Vec<(SwatchCellMeasure, LabelMeasure)> = Vec::new();
                for v in breaks.iter().filter(|v| !matches!(v, Value::Null)) {
                    let (mut row_w, mut row_h) = (0.0_f64, 0.0_f64);
                    for key in &stack.keys {
                        let resolved = resolve_key(key, registry, v);
                        let (w, h) = swatch_dim_for(key.kind, &resolved, dpi, geom, shapes);
                        row_w = row_w.max(w);
                        row_h = row_h.max(h);
                    }
                    let swatch = SwatchCellMeasure {
                        intrinsic_w_px: row_w,
                        intrinsic_h_px: row_h,
                        floor_w_px: key_w_floor,
                        floor_h_px: key_h_floor,
                    };
                    // Blank labels → zero-extent LabelMeasure (no
                    // draw, no reservation).
                    let label = match label_style.as_ref() {
                        Some(style) => {
                            let label_text =
                                domain.map(|s| s.format(v, locale)).unwrap_or_default();
                            let run = TextRun::new(&label_text, style, dpi);
                            let nat_h = run.set_max_width(f32::INFINITY, HAlign::Start) as f64;
                            let nat_w = run.natural_width();
                            LabelMeasure {
                                natural_w_px: nat_w,
                                natural_h_px: nat_h,
                            }
                        }
                        None => LabelMeasure {
                            natural_w_px: 0.0,
                            natural_h_px: 0.0,
                        },
                    };
                    rows.push((swatch, label));
                }
                let layout = build_discrete_stack_layout(
                    horizontal,
                    rows,
                    swatch_label_gap_px,
                    row_gap_px,
                    dpi,
                );
                BodyMeasure::Stack {
                    layout,
                    no_keys: stack.keys.is_empty(),
                }
            }
            LegendBody::Colorbar(spec) => BodyMeasure::Colorbar {
                bar_thickness_px: pt_to_px(lt.bar.width.resolve(0.0), dpi),
                samples: spec.samples.max(2),
            },
        };

        let (title_w_px, title_h_px) = match (&legend.title, title_style.as_ref()) {
            (Some(text), Some(style)) if !text.is_empty() => {
                let run = TextRun::new(text, style, dpi);
                let h = run.set_max_width(f32::INFINITY, HAlign::Start) as f64;
                // Titles render unwrapped — `natural_width` is the
                // actual draw width; `width_hint` would undershoot
                // for multi-word titles like "Category (hero)".
                let w = run.natural_width();
                (w, h)
            }
            _ => (0.0, 0.0),
        };

        LegendMeasure {
            // Store the cardinal layout direction so primary/cross
            // dim matches can use a 4-arm pattern. In-panel legends
            // size themselves the same as a Right legend.
            side: cardinal_side(legend.side),
            body,
            entry_count,
            max_label_w_px: max_label_w,
            max_label_h_px: max_label_h,
            title_w_px,
            title_h_px,
            padding_px,
            row_gap_px,
            swatch_label_gap_px,
            axis: lt.axis.clone(),
            legend_gap_px,
        }
    }

    /// True when the legend draws nothing, so the layout reserves no
    /// slot and the renderer returns early.
    pub(super) fn is_empty(&self) -> bool {
        if self.entry_count == 0 {
            return true;
        }
        // A binned body with no bins reserves nothing: fewer than two
        // finite numeric breaks (a discrete domain, a collapsed span)
        // leaves `render_binned_stack_body` with nothing to draw, so
        // reserving space would leave a blank band.
        if matches!(&self.body, BodyMeasure::BinnedStack { row_cells_px, .. } if row_cells_px.is_empty())
        {
            return true;
        }
        matches!(
            self.body,
            BodyMeasure::Stack { no_keys: true, .. }
                | BodyMeasure::BinnedStack { no_keys: true, .. }
        )
    }

    /// Block dimension along the side's primary axis: column width
    /// for Right/Left, row height for Top/Bottom. Includes
    /// [`Self::legend_gap_px`] — the renderer shrinks the slot by the
    /// same amount on the panel-facing edge before drawing.
    pub(super) fn primary_dim_px(&self, dpi: f64) -> f64 {
        let padding = self.padding_px;
        let title_gap = if self.title_h_px > 0.0 {
            self.row_gap_px
        } else {
            0.0
        };
        // Bar-style bodies (binned + colorbar) carry an axis arm:
        // bar thickness + tick + gap + label. Tick + gap come from
        // the legend's own AxisTheme — same source the legend's axis
        // renderer reads, so the slot reservation matches the draw.
        // Reservation uses the tick magnitude — sign flips draw
        // direction but the slot fits the tick either way.
        let axis_resolved = self.axis.resolved();
        let tick_px = pt_to_px(axis_resolved.tick_length.resolve(0.0), dpi).abs();
        let label_gap_axis = pt_to_px(axis_resolved.tick_gap.resolve(0.0), dpi);

        let body_dim = match (&self.body, self.side) {
            (BodyMeasure::Stack { layout, .. }, LegendSide::Right | LegendSide::Left) => {
                // Vertical legend column width comes straight from
                // the inner grid's resolved entries width.
                layout.entries_w_px.max(self.title_w_px) + 2.0 * padding
            }
            (BodyMeasure::Stack { layout, .. }, LegendSide::Top | LegendSide::Bottom) => {
                // Horizontal legend row height: title above + entries
                // block height.
                self.title_h_px + title_gap + layout.entries_h_px + 2.0 * padding
            }
            (
                BodyMeasure::BinnedStack { row_cells_px, .. },
                LegendSide::Right | LegendSide::Left,
            ) => {
                // Binned vertical: bar thickness = max of row widths.
                let thickness = row_cells_px.iter().map(|(w, _)| *w).fold(0.0_f64, f64::max);
                let axis_arm = thickness + tick_px + label_gap_axis + self.max_label_w_px;
                axis_arm.max(self.title_w_px) + 2.0 * padding
            }
            (
                BodyMeasure::BinnedStack { row_cells_px, .. },
                LegendSide::Top | LegendSide::Bottom,
            ) => {
                // Binned horizontal: bar thickness = max of row heights.
                let thickness = row_cells_px.iter().map(|(_, h)| *h).fold(0.0_f64, f64::max);
                self.title_h_px
                    + title_gap
                    + thickness
                    + tick_px
                    + label_gap_axis
                    + self.max_label_h_px
                    + 2.0 * padding
            }
            (
                BodyMeasure::Colorbar {
                    bar_thickness_px: thickness,
                    ..
                },
                LegendSide::Right | LegendSide::Left,
            ) => {
                let axis_arm = thickness + tick_px + label_gap_axis + self.max_label_w_px;
                axis_arm.max(self.title_w_px) + 2.0 * padding
            }
            (
                BodyMeasure::Colorbar {
                    bar_thickness_px: thickness,
                    ..
                },
                LegendSide::Top | LegendSide::Bottom,
            ) => {
                // Row height = title + gap + bar thickness + tick + gap + label_h.
                self.title_h_px
                    + title_gap
                    + thickness
                    + tick_px
                    + label_gap_axis
                    + self.max_label_h_px
                    + 2.0 * padding
            }
            (_, LegendSide::InPanel { .. }) => {
                unreachable!("LegendMeasure stores cardinal side, never InPanel")
            }
        };
        body_dim + self.legend_gap_px
    }

    /// Cross-axis dim: height for Right/Left, width for Top/Bottom.
    /// Used by [`LegendStackMeasure`] to split the slot rect among
    /// stacked legends.
    pub(super) fn cross_dim_px(&self, dpi: f64) -> f64 {
        let _ = dpi;
        let padding = self.padding_px;
        let row_gap = self.row_gap_px;
        let gap = self.swatch_label_gap_px;
        let title_gap = if self.title_h_px > 0.0 {
            self.row_gap_px
        } else {
            0.0
        };
        let n = self.entry_count as f64;

        match (&self.body, self.side) {
            (BodyMeasure::Stack { layout, .. }, LegendSide::Right | LegendSide::Left) => {
                // Vertical legend along-axis: entries block height
                // already accounts for per-row heights + inter-row
                // gaps via the grid's Fixed gap rows.
                self.title_h_px + title_gap + layout.entries_h_px + 2.0 * padding
            }
            (BodyMeasure::Stack { layout, .. }, LegendSide::Top | LegendSide::Bottom) => {
                // Horizontal legend along-axis: entries block width.
                layout.entries_w_px.max(self.title_w_px) + 2.0 * padding
            }
            (
                BodyMeasure::BinnedStack { row_cells_px, .. },
                LegendSide::Right | LegendSide::Left,
            ) => {
                // Binned vertical: rows touch, bar length = sum of
                // each bin's own h.
                let bar_len: f64 = row_cells_px.iter().map(|(_, h)| *h).sum();
                self.title_h_px + title_gap + bar_len + 2.0 * padding
            }
            (
                BodyMeasure::BinnedStack { row_cells_px, .. },
                LegendSide::Top | LegendSide::Bottom,
            ) => {
                let bar_len: f64 = row_cells_px.iter().map(|(w, _)| *w).sum();
                bar_len.max(self.title_w_px) + 2.0 * padding
            }
            (BodyMeasure::Colorbar { .. }, LegendSide::Right | LegendSide::Left) => {
                // Vertical bar length defaults to (n−1) × label-pitch
                // + label height — enough to space the major ticks
                // legibly. The actual rendered length scales to the
                // available slot height at draw time.
                let pitch = self.max_label_h_px + row_gap;
                let bar_len = (n - 1.0).max(1.0) * pitch + self.max_label_h_px;
                self.title_h_px + title_gap + bar_len + 2.0 * padding
            }
            (BodyMeasure::Colorbar { .. }, LegendSide::Top | LegendSide::Bottom) => {
                // Horizontal bar length: (n−1) × label-pitch +
                // label_w to leave clear gaps between tick labels.
                let pitch = self.max_label_w_px + gap * 3.0;
                let bar_len = (n - 1.0).max(1.0) * pitch + self.max_label_w_px;
                bar_len.max(self.title_w_px) + 2.0 * padding
            }
            (_, LegendSide::InPanel { .. }) => {
                unreachable!("LegendMeasure stores cardinal side, never InPanel")
            }
        }
    }
}

impl Measure for LegendMeasure {
    fn width_hint(&self, dpi: f64) -> WidthHint {
        if self.is_empty() {
            return WidthHint::Min(0.0);
        }
        match self.side {
            LegendSide::Right | LegendSide::Left => WidthHint::Min(self.primary_dim_px(dpi)),
            LegendSide::Top | LegendSide::Bottom => WidthHint::Min(0.0),
            LegendSide::InPanel { .. } => {
                unreachable!("LegendMeasure stores cardinal side, never InPanel")
            }
        }
    }

    fn height_at(&self, _width: f64, dpi: f64) -> f64 {
        if self.is_empty() {
            return 0.0;
        }
        match self.side {
            LegendSide::Top | LegendSide::Bottom => self.primary_dim_px(dpi),
            LegendSide::Right | LegendSide::Left => 0.0,
            LegendSide::InPanel { .. } => {
                unreachable!("LegendMeasure stores cardinal side, never InPanel")
            }
        }
    }
}

/// Composite measure for multiple legends stacked on the same side.
/// The primary extent is reserved for the *widest* child (so all
/// children get the same column width / row height); the cross
/// extent is the sum of children plus inter-legend gaps.
pub(super) struct LegendStackMeasure {
    pub(super) side: LegendSide,
    pub(super) children: Vec<LegendMeasure>,
    /// Resolved inter-legend gap in pixels — sourced from
    /// `Theme.legend_spacing` at construction; used by
    /// `cross_dim_for_layout` to size the slot for the stack +
    /// gaps. (Not currently consumed for the cross dim sizing
    /// because the layout reserves max(primary) and the renderer
    /// stacks children using this gap; the field exists so the
    /// renderer and measure share the same gap value.)
    #[allow(dead_code)]
    pub(super) gap_px: f64,
}

impl LegendStackMeasure {
    fn non_empty(&self) -> impl Iterator<Item = &LegendMeasure> {
        self.children.iter().filter(|c| !c.is_empty())
    }
    fn primary_max(&self, dpi: f64) -> f64 {
        self.non_empty()
            .map(|c| c.primary_dim_px(dpi))
            .fold(0.0_f64, f64::max)
    }
}

impl Measure for LegendStackMeasure {
    fn width_hint(&self, dpi: f64) -> WidthHint {
        let any = self.non_empty().next().is_some();
        if !any {
            return WidthHint::Min(0.0);
        }
        match self.side {
            LegendSide::Right | LegendSide::Left => WidthHint::Min(self.primary_max(dpi)),
            LegendSide::Top | LegendSide::Bottom => WidthHint::Min(0.0),
            LegendSide::InPanel { .. } => {
                unreachable!("LegendStackMeasure is constructed with a cardinal side")
            }
        }
    }
    fn height_at(&self, _width: f64, dpi: f64) -> f64 {
        let any = self.non_empty().next().is_some();
        if !any {
            return 0.0;
        }
        match self.side {
            LegendSide::Top | LegendSide::Bottom => self.primary_max(dpi),
            LegendSide::Right | LegendSide::Left => 0.0,
            LegendSide::InPanel { .. } => {
                unreachable!("LegendStackMeasure is constructed with a cardinal side")
            }
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::color::rgb;
    use crate::plot::chrome::legend::{legend_measure, LegendKeySpec};
    use crate::plot::scale;
    use crate::plot::theme::Theme;
    use std::sync::Arc;

    fn dpi_96() -> f64 {
        96.0
    }

    fn default_theme() -> Theme {
        // Blank the key frame so tests count only emissions driven by
        // the key specs themselves, not the theme's swatch background.
        let mut t = Theme::default();
        t.legend.key.frame = crate::plot::theme::Element::Blank;
        t
    }

    fn shape_reg() -> ShapeRegistry {
        ShapeRegistry::with_builtins()
    }

    fn build_registry() -> ScaleRegistry {
        let mut reg = ScaleRegistry::new();
        reg.insert(
            "category_color",
            scale::discrete([
                Value::String(Arc::from("A")),
                Value::String(Arc::from("B")),
                Value::String(Arc::from("C")),
            ])
            .range_colors([
                rgb(1.0, 0.0, 0.0),
                rgb(0.0, 1.0, 0.0),
                rgb(0.0, 0.0, 1.0),
            ]),
        );
        reg.insert(
            "category_size",
            scale::discrete([
                Value::String(Arc::from("A")),
                Value::String(Arc::from("B")),
                Value::String(Arc::from("C")),
            ])
            .range_numbers([4.0, 8.0, 12.0]),
        );
        reg
    }
    #[test]
    fn empty_legend_reports_zero_size() {
        let legend = Legend::new("category_color");
        let reg = build_registry();
        let m = legend_measure(&legend, &reg, &shape_reg(), dpi_96(), &default_theme());
        assert_eq!(m.width_hint(dpi_96()), WidthHint::Min(0.0));
        assert_eq!(m.height_at(100.0, dpi_96()), 0.0);
    }

    #[test]
    fn point_legend_with_scaled_fill_reports_nonzero() {
        let legend = Legend::new("category_color")
            .title("Category")
            .key(LegendKeySpec::point().scaled("fill", "category_color"));
        let reg = build_registry();
        let m = legend_measure(&legend, &reg, &shape_reg(), dpi_96(), &default_theme());
        let w = match m.width_hint(dpi_96()) {
            WidthHint::Min(w) => w,
            WidthHint::NeedsHeight { seed } => seed,
        };
        assert!(w > 0.0);
    }
    #[test]
    fn point_swatch_dim_scales_with_size_channel() {
        let small = Legend::new("category_color").key(
            LegendKeySpec::point()
                .scaled("fill", "category_color")
                .fixed("size", 4.0_f64),
        );
        let large = Legend::new("category_color").key(
            LegendKeySpec::point()
                .scaled("fill", "category_color")
                .scaled("size", "category_size"),
        );
        let reg = build_registry();
        let s_w = match legend_measure(&small, &reg, &shape_reg(), dpi_96(), &default_theme())
            .width_hint(dpi_96())
        {
            WidthHint::Min(w) => w,
            _ => 0.0,
        };
        let l_w = match legend_measure(&large, &reg, &shape_reg(), dpi_96(), &default_theme())
            .width_hint(dpi_96())
        {
            WidthHint::Min(w) => w,
            _ => 0.0,
        };
        assert!(
            l_w > s_w,
            "legend with scaled size up to 12pt should be wider than fixed-4pt: {s_w} vs {l_w}"
        );
    }

    // ─── Binned-stack measure / draw symmetry ────────────────────────

    /// Four edges → three bins. `bin_fill` indexes a palette by bin;
    /// `bin_size` gives each bin a distinct pt size so per-bin row
    /// extents are strictly increasing and measurable.
    fn binned_registry() -> ScaleRegistry {
        let mut reg = ScaleRegistry::new();
        let edges = vec![0.0, 10.0, 20.0, 30.0];
        reg.insert(
            "bin_fill",
            scale::binned(0.0..=30.0, edges.clone()).range_colors([
                rgb(1.0, 0.0, 0.0),
                rgb(0.0, 1.0, 0.0),
                rgb(0.0, 0.0, 1.0),
            ]),
        );
        reg.insert(
            "bin_size",
            scale::binned(0.0..=30.0, edges).range_numbers([12.0, 18.0, 24.0]),
        );
        reg
    }

    fn binned_rows(legend: &Legend, reg: &ScaleRegistry, theme: &Theme) -> Vec<(f64, f64)> {
        let m = LegendMeasure::new(
            legend,
            reg,
            &shape_reg(),
            dpi_96(),
            theme.legend_for(legend.theme_variant.as_deref()),
            &theme.geom,
            0.0,
            &theme.locale,
            crate::plot::chrome::root_text_pt(theme),
        );
        match m.body {
            BodyMeasure::BinnedStack { row_cells_px, .. } => row_cells_px,
            _ => panic!("expected a binned stack body"),
        }
    }

    #[test]
    fn binned_stack_measures_one_row_per_bin() {
        let legend = Legend::new("bin_fill")
            .binned()
            .key(LegendKeySpec::rect().scaled("fill", "bin_fill"));
        let theme = default_theme();
        let rows = binned_rows(&legend, &binned_registry(), &theme);
        // Three bins from four edges — not four rows from four edges.
        assert_eq!(rows.len(), 3, "one row per bin, got {rows:?}");
    }

    #[test]
    fn binned_size_legend_measures_at_bin_midpoints() {
        let legend = Legend::new("bin_size")
            .binned()
            .key(LegendKeySpec::point().scaled("size", "bin_size"));
        let theme = default_theme();
        let rows = binned_rows(&legend, &binned_registry(), &theme);
        assert_eq!(rows.len(), 3, "one row per bin, got {rows:?}");
        // Bins 0/1/2 resolve to 12/18/24 pt, so row extents must be
        // strictly increasing. Edge sampling would repeat the last bin
        // (edges 0,10,20,30 land in bins 0,1,2,2) and give a flat tail.
        assert!(
            rows[0].1 < rows[1].1 && rows[1].1 < rows[2].1,
            "row heights should follow the per-bin size palette: {rows:?}"
        );
    }

    #[test]
    fn binned_stack_reserved_length_matches_the_bins_drawn() {
        let legend = Legend::new("bin_fill")
            .binned()
            .key(LegendKeySpec::rect().scaled("fill", "bin_fill"));
        let theme = default_theme();
        let reg = binned_registry();
        let m = LegendMeasure::new(
            &legend,
            &reg,
            &shape_reg(),
            dpi_96(),
            theme.legend_for(None),
            &theme.geom,
            0.0,
            &theme.locale,
            crate::plot::chrome::root_text_pt(&theme),
        );
        // Derive the expectation from the bin count independently of
        // the measure, so an extra reserved row can't hide inside a
        // self-consistent sum. Rect keys sit at the theme floor.
        let n_bins = bin_midpoints(&bin_edges(
            &reg.get("bin_fill").expect("bin_fill").breaks(5),
        ))
        .len();
        assert_eq!(n_bins, 3);
        let key_h_floor = pt_to_px(theme.legend.key.height.resolve(0.0), dpi_96());
        // No title, so the reserved cross extent is bar + padding only.
        let expected = (n_bins as f64) * key_h_floor + 2.0 * m.padding_px;
        assert!(
            (m.cross_dim_px(dpi_96()) - expected).abs() < 1e-9,
            "reserved {} should equal {n_bins} bins + padding {}",
            m.cross_dim_px(dpi_96()),
            expected
        );
    }

    #[test]
    fn binned_stack_over_a_non_numeric_domain_reserves_nothing() {
        // A discrete domain yields no numeric edges, so the renderer
        // draws no body — the measure must not reserve a band for it.
        let legend = Legend::new("category_color")
            .binned()
            .key(LegendKeySpec::rect().scaled("fill", "category_color"));
        let theme = default_theme();
        let reg = build_registry();
        assert!(binned_rows(&legend, &reg, &theme).is_empty());
        let measure = legend_measure(&legend, &reg, &shape_reg(), dpi_96(), &theme);
        assert_eq!(measure.width_hint(dpi_96()), WidthHint::Min(0.0));
        assert_eq!(measure.height_at(0.0, dpi_96()), 0.0);
    }
}