lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
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
//! Per-call dimensions, distinct from the root's completed output size.

use std::cell::Cell;

use crate::decorations::legend::discrete::scoped::{GuideAllocation, GuideConsumption};
use crate::decorations::plan::children::MeasurementPhase;
use crate::layout::fit::FitScope;
use crate::layout::kernel::{FeedbackKind, FeedbackRound};
use crate::layout::{PixelRect, RangeProvenance, RootRequest, RootSize};
use crate::orchestrator::registration::{
    BaseAxisRef, CandidateId, DisplayExtent, PlotOccurrenceId, PlotRegistration, PreparedAxesOwner,
    RootIdentity,
};
use crate::orchestrator::root::{PlotIntrinsics, PreparedComposition};
use crate::spec::{AxisScale, FacetScales, LegendPosition, PlotKind, PlotMeasureContext};
use crate::Error;

/// A selected domain owns scale coordinates, never authored raw endpoints.
#[derive(Debug, Clone)]
pub(crate) struct AxisDomainAssignment {
    pub(crate) extent: DisplayExtent,
    pub(crate) provenance: crate::layout::RangeProvenance,
}

#[derive(Clone, Copy)]
pub(crate) struct AssignedAxisRef<'v> {
    pub(crate) base: BaseAxisRef<'v>,
    pub(crate) domain: &'v AxisDomainAssignment,
}

#[derive(Clone, Copy)]
pub(crate) struct AxisPairRef<'v> {
    pub(crate) axes: [AssignedAxisRef<'v>; 2],
}

pub(crate) enum AxisPairScope {
    Pair,
    MatrixCell { row: usize, column: usize },
    MatrixDiagonalDensity { variable: usize },
    MarginalCentral,
    MarginalTop,
    MarginalRight,
}

#[derive(Debug, Clone)]
pub(crate) enum AxisDomainAssignments {
    Pair([AxisDomainAssignment; 2]),
    Matrix {
        variables: Vec<AxisDomainAssignment>,
        diagonal_density: Vec<AxisDomainAssignment>,
    },
    Marginal {
        central: [AxisDomainAssignment; 2],
        strip_density: [AxisDomainAssignment; 2],
    },
}

pub(crate) struct RootAssignment {
    pub(crate) candidate: CandidateId,
    pub(crate) request: RootRequest,
    pub(crate) measurement_size: RootSize,
}

#[derive(Clone, Copy)]
pub(crate) struct RootPlotAssignmentRef<'a> {
    #[expect(
        dead_code,
        reason = "retains root lineage for the plot view; drawing dimensions belong to the local assignment"
    )]
    pub(crate) root: &'a RootAssignment,
    pub(crate) local: &'a PlotMeasureAssignment,
}

/// The selected allocation remains distinct from its completed root frame.
#[derive(Debug, Clone)]
pub(crate) struct PlotMeasureAssignment {
    pub(crate) plot_id: PlotOccurrenceId,
    #[expect(
        dead_code,
        reason = "retains a pure candidate label on the selected allocation, not a rendering input"
    )]
    pub(crate) candidate: CandidateId,
    pub(crate) dimensions: PlotMeasureDimensions,
    pub(crate) cell: PixelRect,
    pub(crate) frame: PixelRect,
    pub(crate) body: PixelRect,
    pub(crate) axis_label_width: f64,
    pub(crate) content_height: bool,
    pub(crate) fill_content_height: bool,
    pub(crate) content_preference: Option<f64>,
    pub(crate) insets: ParentInsets,
    pub(crate) domains: AxisDomainAssignments,
    pub(crate) guide: GuideTrialAssignment,
}

impl PlotMeasureAssignment {
    pub(crate) fn ranges(&self) -> Option<[(f64, f64); 2]> {
        match &self.domains {
            AxisDomainAssignments::Pair(pair) => {
                Some(pair.each_ref().map(|axis| axis.extent.pair()))
            }
            AxisDomainAssignments::Marginal { central, .. } => {
                Some(central.each_ref().map(|axis| axis.extent.pair()))
            }
            AxisDomainAssignments::Matrix { .. } => None,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct ParentInsets {
    pub(crate) axis_gutter: [i64; 4],
    pub(crate) owned_ink: [i64; 4],
    pub(crate) shared_alignment: [i64; 4],
}

impl ParentInsets {
    pub(crate) fn consumed(
        round: &crate::layout::kernel::FeedbackRound,
        address: &crate::layout::ComponentAddress,
    ) -> Self {
        use crate::layout::kernel::FeedbackKind;
        Self {
            axis_gutter: round.assigned_axis_gutters_quanta(address),
            owned_ink: round.insets_quanta(address, FeedbackKind::OwnedInk),
            shared_alignment: round.insets_quanta(address, FeedbackKind::SharedAlignment),
        }
    }

    #[expect(
        clippy::cast_precision_loss,
        reason = "consumed insets retain the original separate exact-root-bounded pixel conversions before addition"
    )]
    pub(crate) fn assigned_gutters(&self) -> [f64; 4] {
        let ink = self.owned_ink_pixels();
        std::array::from_fn(|side| self.axis_gutter[side] as f64 / 1024.0 + ink[side])
    }

    #[expect(
        clippy::cast_precision_loss,
        reason = "consumed owned ink is exact-root-bounded layout quanta"
    )]
    pub(crate) fn owned_ink_pixels(&self) -> [f64; 4] {
        self.owned_ink.map(|value| value as f64 / 1024.0)
    }
}

#[derive(Debug, Clone)]
pub(crate) struct GuideTrialAssignment {
    pub(crate) edge: LegendPosition,
    pub(crate) compact: bool,
    pub(crate) allocation: Option<GuideAllocation>,
}

pub(crate) struct AssignmentFactory<'r> {
    pub(crate) identity: &'r RootIdentity,
    pub(crate) next_candidate: &'r Cell<usize>,
    pub(crate) original_request: RootRequest,
}

impl AssignmentFactory<'_> {
    pub(crate) fn assign(&self, measurement_size: RootSize) -> RootAssignment {
        let serial = self.next_candidate.get();
        self.next_candidate.set(serial + 1);
        RootAssignment {
            candidate: CandidateId {
                root: self.identity.clone(),
                serial,
            },
            request: self.original_request,
            measurement_size,
        }
    }
}

pub(crate) struct PlotMeasureAssignmentFactory<'r, 'src> {
    pub(crate) issuer: AssignmentFactory<'r>,
    pub(crate) root: &'r RootAssignment,
    pub(crate) registered: &'r PlotRegistration<'src>,
    pub(crate) text: &'r crate::text::TextService,
    pub(crate) canvas: Option<&'src crate::Canvas>,
    pub(crate) operation: &'r crate::font::canonical::OperationContext,
    pub(crate) intrinsic: &'r PlotIntrinsics,
    pub(crate) registrations: &'r [PlotRegistration<'src>],
    pub(crate) composition: &'r PreparedComposition,
}

pub(crate) struct PlotAllocationInputs {
    pub(crate) dimensions: PlotMeasureDimensions,
    pub(crate) cell: PixelRect,
    pub(crate) frame: PixelRect,
    pub(crate) body: PixelRect,
    pub(crate) axis_label_width: f64,
    pub(crate) content_height: bool,
    pub(crate) fill_content_height: bool,
    pub(crate) content_preference: Option<f64>,
    pub(crate) insets: ParentInsets,
    pub(crate) guide: GuideTrialAssignment,
}

#[cfg(test)]
impl PlotAllocationInputs {
    pub(crate) fn for_test(
        size: RootSize,
        panel: PixelRect,
        round: &FeedbackRound,
        owner: &crate::layout::ComponentAddress,
    ) -> Self {
        Self {
            dimensions: PlotMeasureDimensions {
                canvas: size,
                output_override: None,
            },
            cell: panel,
            frame: panel,
            body: panel,
            axis_label_width: panel.width(),
            content_height: false,
            fill_content_height: false,
            content_preference: None,
            insets: ParentInsets::consumed(round, owner),
            guide: GuideTrialAssignment {
                edge: LegendPosition::Auto,
                compact: false,
                allocation: None,
            },
        }
    }
}

pub(crate) struct PlotMeasureBranch {
    pub(crate) root: RootAssignment,
    pub(crate) entry: PlotMeasureAssignment,
}

pub(crate) struct GuideTrialChoice {
    pub(crate) edge: LegendPosition,
    pub(crate) compact: bool,
}

pub(crate) enum PlotAssignmentChange {
    ContentPreference {
        frame: PixelRect,
        preferred: f64,
    },
    OutsideGuide {
        body: PixelRect,
        axis_label_width: f64,
        allocation: GuideAllocation,
    },
    AxisInsets {
        body: PixelRect,
        axis_label_width: f64,
    },
    NumericExplanation {
        body: PixelRect,
    },
}

impl<'r, 'src: 'r> PlotMeasureAssignmentFactory<'r, 'src> {
    #[expect(
        clippy::unnecessary_wraps,
        reason = "the fixed factory interface returns Result while context creation only borrows prepared operands"
    )]
    pub(crate) fn context<'c>(
        &'c self,
        local: &'c PlotMeasureAssignment,
        guides: GuideConsumption<'c>,
        fit_scope: &'c FitScope,
    ) -> Result<PlotMeasureContext<'c, 'src>, Error> {
        Ok(PlotMeasureContext {
            registered: self.registered,
            assignment: RootPlotAssignmentRef {
                root: self.root,
                local,
            },
            canvas: self.canvas,
            text: self.text,
            operation: self.operation,
            intrinsic: self.intrinsic,
            guides,
            fit_scope,
        })
    }

    pub(crate) fn assign(
        &self,
        inputs: PlotAllocationInputs,
        consumed: &FeedbackRound,
        measurement_phase: Option<MeasurementPhase>,
    ) -> Result<PlotMeasureAssignment, Error> {
        let domains = self.entry_domains(consumed, measurement_phase)?;
        Ok(PlotMeasureAssignment {
            plot_id: self.registered.scope.id.clone(),
            candidate: self.root.candidate.clone(),
            dimensions: inputs.dimensions,
            cell: inputs.cell,
            frame: inputs.frame,
            body: inputs.body,
            axis_label_width: inputs.axis_label_width,
            content_height: inputs.content_height,
            fill_content_height: inputs.fill_content_height,
            content_preference: inputs.content_preference,
            domains,
            insets: inputs.insets,
            guide: inputs.guide,
        })
    }

    #[expect(
        clippy::needless_pass_by_value,
        reason = "a guide trial owns its choice input alongside the returned root and local assignment"
    )]
    pub(crate) fn fork(
        &self,
        entry: &PlotMeasureAssignment,
        choice: GuideTrialChoice,
        consumed: &FeedbackRound,
    ) -> Result<PlotMeasureBranch, Error> {
        let root = self.issuer.assign(self.root.measurement_size);
        let mut domains = entry.domains.clone();
        // A trial starts with the caller's selected domains, not another
        // unconditional reduction of the prepared bases.
        self.attempt_domains(&mut domains, consumed)?;
        let local = PlotMeasureAssignment {
            plot_id: entry.plot_id.clone(),
            candidate: root.candidate.clone(),
            dimensions: entry.dimensions,
            cell: entry.cell,
            frame: entry.frame,
            body: entry.body,
            axis_label_width: entry.axis_label_width,
            content_height: entry.content_height,
            fill_content_height: entry.fill_content_height,
            content_preference: entry.content_preference,
            domains,
            insets: ParentInsets {
                axis_gutter: entry.insets.axis_gutter,
                owned_ink: entry.insets.owned_ink,
                shared_alignment: entry.insets.shared_alignment,
            },
            guide: GuideTrialAssignment {
                edge: choice.edge,
                compact: choice.compact,
                allocation: entry.guide.allocation,
            },
        };
        Ok(PlotMeasureBranch { root, entry: local })
    }

    #[expect(
        clippy::unnecessary_wraps,
        reason = "the branch factory keeps the fixed fallible interface while borrowing an existing root"
    )]
    pub(crate) fn for_branch<'b>(
        &'b self,
        root: &'b RootAssignment,
    ) -> Result<PlotMeasureAssignmentFactory<'b, 'src>, Error> {
        Ok(PlotMeasureAssignmentFactory {
            issuer: AssignmentFactory {
                identity: self.issuer.identity,
                next_candidate: self.issuer.next_candidate,
                original_request: self.issuer.original_request,
            },
            root,
            registered: self.registered,
            text: self.text,
            canvas: self.canvas,
            operation: self.operation,
            intrinsic: self.intrinsic,
            registrations: self.registrations,
            composition: self.composition,
        })
    }

    #[expect(
        clippy::needless_pass_by_value,
        clippy::unnecessary_wraps,
        clippy::unused_self,
        reason = "residual changes use the factory's fixed fallible by-value API even when installing only copied geometry"
    )]
    pub(crate) fn apply_change(
        &self,
        mut from: PlotMeasureAssignment,
        change: PlotAssignmentChange,
    ) -> Result<PlotMeasureAssignment, Error> {
        match change {
            PlotAssignmentChange::ContentPreference { frame, preferred } => {
                from.frame = frame;
                from.body = frame;
                from.content_preference = Some(preferred);
            }
            PlotAssignmentChange::OutsideGuide {
                body,
                axis_label_width,
                allocation,
            } => {
                from.body = body;
                from.axis_label_width = axis_label_width;
                from.guide.allocation = Some(allocation);
            }
            PlotAssignmentChange::AxisInsets {
                body,
                axis_label_width,
            } => {
                from.body = body;
                from.axis_label_width = axis_label_width;
            }
            PlotAssignmentChange::NumericExplanation { body } => from.body = body,
        }
        Ok(from)
    }

    fn entry_domains(
        &self,
        consumed: &FeedbackRound,
        phase: Option<MeasurementPhase>,
    ) -> Result<AxisDomainAssignments, Error> {
        let preflight = matches!(
            (self.composition, phase),
            (
                PreparedComposition::Facets(_),
                Some(MeasurementPhase::Probe | MeasurementPhase::Hue)
            )
        );
        let coordinate = match (self.composition, phase) {
            (PreparedComposition::Single { .. }, _)
            | (PreparedComposition::Canvas(_), Some(MeasurementPhase::Legend)) => false,
            _ => !preflight,
        };
        let pair = |sources: &[crate::orchestrator::registration::PreparedAxisSource; 2]| {
            let mut domains = [
                self.initial_axis(&sources[0], 0, preflight)?,
                self.initial_axis(&sources[1], 1, preflight)?,
            ];
            if coordinate {
                self.coordinate_pair(&mut domains, consumed)?;
            }
            Ok::<_, Error>(domains)
        };
        match &self.registered.axes {
            PreparedAxesOwner::Pair(axes) => Ok(AxisDomainAssignments::Pair(pair(&axes.sources)?)),
            PreparedAxesOwner::Marginal {
                central,
                strip_density,
            } => Ok(AxisDomainAssignments::Marginal {
                central: pair(central)?,
                strip_density: [
                    seed_domain(&strip_density[0]),
                    seed_domain(&strip_density[1]),
                ],
            }),
            PreparedAxesOwner::Matrix {
                variables,
                diagonal_density,
            } => Ok(AxisDomainAssignments::Matrix {
                variables: variables.iter().map(seed_domain).collect(),
                diagonal_density: diagonal_density.iter().map(seed_domain).collect(),
            }),
        }
    }

    fn initial_axis(
        &self,
        source: &crate::orchestrator::registration::PreparedAxisSource,
        side: usize,
        preflight: bool,
    ) -> Result<AxisDomainAssignment, Error> {
        let mut domain = seed_domain(source);
        let authored = if side == 0 {
            self.registered.source.x_axis_spec()
        } else {
            self.registered.source.y_axis_spec()
        };
        if preflight {
            domain.provenance = if authored.window.is_some() {
                RangeProvenance::Configured
            } else {
                RangeProvenance::Automatic
            };
        }
        if let Some(window) = authored.window {
            domain.extent = self.window_extent(side, window.pair())?;
        }
        Ok(domain)
    }

    fn window_extent(&self, side: usize, range: (f64, f64)) -> Result<DisplayExtent, Error> {
        let (lower, upper) = if side == 0
            && self.registered.source.kind() == PlotKind::Forest
            && self.registered.source.axis_scale_spec() == AxisScale::Log10
        {
            (
                crate::numeric::log10(range.0)?,
                crate::numeric::log10(range.1)?,
            )
        } else {
            range
        };
        Ok(DisplayExtent { lower, upper })
    }

    fn coordinate_pair(
        &self,
        domains: &mut [AxisDomainAssignment; 2],
        consumed: &FeedbackRound,
    ) -> Result<(), Error> {
        match self.composition {
            PreparedComposition::Single { .. } => {}
            PreparedComposition::Facets(topology) => {
                for (side, kind) in [
                    FeedbackKind::AutomaticDomainX,
                    FeedbackKind::AutomaticDomainY,
                ]
                .into_iter()
                .enumerate()
                {
                    let fixed = match self.registered.policy.facet_scales {
                        Some(FacetScales::Fixed) => true,
                        Some(FacetScales::FreeY) => side == 0,
                        Some(FacetScales::FreeX) => side == 1,
                        _ => false,
                    };
                    let mut shared = None;
                    if fixed {
                        for id in &topology.shared_axis_members[side] {
                            let registered = self.registration(id);
                            if let Some(range) =
                                consumed.feedback_domain(&registered.scope.address, kind)
                            {
                                shared = Some(union(shared, range));
                            }
                        }
                    } else {
                        shared = consumed.feedback_domain(&self.registered.scope.address, kind);
                    }
                    if let Some(range) = shared {
                        domains[side].extent = self.window_extent(side, range)?;
                    }
                    // The original Forest resolved-x override is applied after
                    // conversion of its working raw window, including feedback.
                    if fixed && self.resolved_forest_x(side) {
                        domains[side].extent = self.registered.axes.sources()[side]
                            .base_seed
                            .expect("prepared axis display seed");
                    }
                }
            }
            PreparedComposition::Canvas(topology) => {
                let mut grouped = false;
                for group in &topology.range_groups {
                    if !group.contains(&self.registered.scope.id) {
                        continue;
                    }
                    let mut shared = None;
                    for id in group {
                        let registered = self.registration(id);
                        let base = registered.axes.sources()[0]
                            .base_seed
                            .expect("prepared shared x member")
                            .pair();
                        let range = consumed
                            .feedback_domain(
                                &registered.scope.address,
                                FeedbackKind::AutomaticDomainX,
                            )
                            .map_or(base, |range| union(Some(base), range));
                        shared = Some(union(shared, range));
                    }
                    let range = shared.expect("validated nonempty x-range group");
                    if !range.0.is_finite() || !range.1.is_finite() || range.0 >= range.1 {
                        return Err(Error::Render(
                            "Canvas shared x domain must be finite and ascending".into(),
                        ));
                    }
                    domains[0].extent = DisplayExtent {
                        lower: range.0,
                        upper: range.1,
                    };
                    grouped = true;
                }
                if !grouped {
                    if let Some(range) = consumed.feedback_domain(
                        &self.registered.scope.address,
                        FeedbackKind::AutomaticDomainX,
                    ) {
                        domains[0].extent = self.window_extent(0, range)?;
                    }
                }
                if let Some(range) = consumed.feedback_domain(
                    &self.registered.scope.address,
                    FeedbackKind::AutomaticDomainY,
                ) {
                    domains[1].extent = self.window_extent(1, range)?;
                }
            }
        }
        Ok(())
    }

    fn registration(&self, id: &PlotOccurrenceId) -> &PlotRegistration<'src> {
        self.registrations
            .iter()
            .find(|registered| registered.scope.id == *id)
            .expect("prepared sharing member")
    }

    fn resolved_forest_x(&self, side: usize) -> bool {
        side == 0
            && self.registered.source.kind() == PlotKind::Forest
            && self.registered.source.axis_scale_spec() == AxisScale::Log10
    }

    fn attempt_domains(
        &self,
        domains: &mut AxisDomainAssignments,
        consumed: &FeedbackRound,
    ) -> Result<(), Error> {
        let pair = match domains {
            AxisDomainAssignments::Pair(pair) => pair,
            AxisDomainAssignments::Marginal { central, .. } => central,
            AxisDomainAssignments::Matrix { .. } => return Ok(()),
        };
        for (side, kind) in [
            FeedbackKind::AutomaticDomainX,
            FeedbackKind::AutomaticDomainY,
        ]
        .into_iter()
        .enumerate()
        {
            let Some(range) = consumed.feedback_domain(&self.registered.scope.address, kind) else {
                continue;
            };
            let forest = self.resolved_forest_x(side);
            let resolved = forest
                && match self.composition {
                    PreparedComposition::Single { .. } => false,
                    PreparedComposition::Facets(_) => matches!(
                        self.registered.policy.facet_scales,
                        Some(FacetScales::Fixed | FacetScales::FreeY)
                    ),
                    PreparedComposition::Canvas(topology) => topology
                        .range_groups
                        .iter()
                        .any(|group| group.contains(&self.registered.scope.id)),
                };
            let working = match self.composition {
                PreparedComposition::Single { .. } => {
                    let source = self.registered.source;
                    if side == 0 {
                        source.x_axis_spec()
                    } else {
                        source.y_axis_spec()
                    }
                    .window
                    .map(crate::layout::RawWindow::pair)
                }
                PreparedComposition::Facets(topology) if resolved => {
                    let mut shared = None;
                    for id in &topology.shared_axis_members[side] {
                        if let Some(range) =
                            consumed.feedback_domain(&self.registration(id).scope.address, kind)
                        {
                            shared = Some(union(shared, range));
                        }
                    }
                    shared
                }
                // A free Forest caller has already installed this raw feedback
                // window. Its assigned extent is logarithmic, not that window.
                _ if forest => None,
                _ => Some(pair[side].extent.pair()),
            };
            let converted = self.window_extent(side, union(working, range))?;
            // The original helper converts the raw working window first, even
            // when the independently resolved Forest x extent then overrides it.
            if !resolved {
                pair[side].extent = converted;
            }
        }
        Ok(())
    }

    /// The original height prepass installs feedback directly. Its numeric
    /// native readers are Linear; coordinate intrinsic readers still use their
    /// prepared seeds and must not eagerly convert a Forest working window.
    pub(crate) fn content_ranges(
        &self,
        entry: &PlotMeasureAssignment,
        consumed: &FeedbackRound,
    ) -> Option<[(f64, f64); 2]> {
        let pair = match &entry.domains {
            AxisDomainAssignments::Pair(pair) => pair,
            AxisDomainAssignments::Marginal { central, .. } => central,
            AxisDomainAssignments::Matrix { .. } => return None,
        };
        Some(std::array::from_fn(|side| {
            consumed
                .feedback_domain(
                    &self.registered.scope.address,
                    if side == 0 {
                        FeedbackKind::AutomaticDomainX
                    } else {
                        FeedbackKind::AutomaticDomainY
                    },
                )
                .unwrap_or(pair[side].extent.pair())
        }))
    }
}

fn seed_domain(
    source: &crate::orchestrator::registration::PreparedAxisSource,
) -> AxisDomainAssignment {
    AxisDomainAssignment {
        extent: source.base_seed.expect("prepared axis display seed"),
        provenance: source.base_provenance,
    }
}

fn union(prior: Option<(f64, f64)>, range: (f64, f64)) -> (f64, f64) {
    prior.map_or(range, |prior| (prior.0.min(range.0), prior.1.max(range.1)))
}

pub(crate) struct PlotFrameHeightPreference {
    pub(crate) height: f64,
}

pub(crate) fn completion_frame(
    kind: crate::plots::PlotKind,
    selected: &PlotMeasureAssignment,
    result: &crate::spec::PlotRenderResult,
) -> PixelRect {
    let frame = selected.frame;
    if selected.fill_content_height {
        PixelRect::new(
            frame.x0,
            frame.x1,
            frame.y0,
            frame.y0 + selected.content_preference.unwrap_or(frame.height()),
        )
    } else if selected.content_height
        && matches!(
            kind,
            crate::plots::PlotKind::Heatmap
                | crate::plots::PlotKind::Beeswarm
                | crate::plots::PlotKind::Matrix
        )
    {
        PixelRect::new(
            frame.x0,
            frame.x1,
            frame.y0,
            result.layout.projection.panel_box().y1 + selected.insets.assigned_gutters()[3],
        )
    } else {
        frame
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct PlotMeasureDimensions {
    pub(crate) canvas: RootSize,
    pub(crate) output_override: Option<RootSize>,
}

impl PlotMeasureDimensions {
    pub(crate) const fn canvas_size(self) -> RootSize {
        self.canvas
    }

    pub(crate) fn figure_size(self) -> RootSize {
        self.output_override.unwrap_or(self.canvas_size())
    }
}