Skip to main content

ggplot_rs/
compose.rs

1//! Plot composition for the native SVG path — a patchwork-style [`PlotGrid`].
2//!
3//! ```
4//! use ggplot_rs::prelude::*;
5//! use ggplot_rs::data::Value;
6//! let data = || vec![
7//!     ("x".to_string(), (0..10).map(|i| Value::Float(i as f64)).collect::<Vec<_>>()),
8//!     ("y".to_string(), (0..10).map(|i| Value::Float((i * i) as f64)).collect::<Vec<_>>()),
9//! ];
10//! let p = || GGPlot::new(data()).aes(Aes::new().x("x").y("y"));
11//! // a | b puts plots side by side, a / b stacks them; `/` binds tighter.
12//! let svg = ((p().geom_point() | p().geom_line()) / p().geom_col())
13//!     .title("Diagnostics")
14//!     .tag_levels(TagLevels::Lower)
15//!     .render_svg_native_with_size(900, 700)
16//!     .unwrap();
17//! assert!(svg.contains("data-panel=\"a\"") && svg.contains("data-panel=\"c\""));
18//! ```
19//!
20//! Every sub-plot is rendered as a positioned nested `<svg>` fragment (see
21//! [`GGPlot::render_svg_native_at`]) that keeps all of its host attributes
22//! (`data-plot`, `data-domain`, marks' `data-x`/`data-series`/`data-value`)
23//! and additionally carries `data-panel="<tag or index>"`. A host maps a
24//! pointer to data coordinates by first finding the enclosing sub-`<svg>`.
25
26use std::ops::{BitOr, Div};
27
28use crate::aes::Aesthetic;
29use crate::guide::legend;
30use crate::plot::{GGError, GGPlot, RenderMeta};
31use crate::render::backend::{
32    DrawBackend, LineStyle, PointStyle, RectStyle, TextAnchor, TextStyle,
33};
34use crate::render::renderer::PlotRenderer;
35use crate::render::svg_backend::{root_data_attrs, validate_root_attrs, SvgBackend};
36use crate::render::{Rect, RenderError};
37use crate::theme::elements::{ElementLine, ElementText};
38use crate::theme::{AxisLabelFit, LegendPosition, Theme};
39
40/// Panel-tag sequence (patchwork's `tag_levels`).
41#[derive(Clone, Debug, PartialEq)]
42pub enum TagLevels {
43    /// `a`, `b`, … `z`, `aa`, …
44    Lower,
45    /// `A`, `B`, …
46    Upper,
47    /// `1`, `2`, …
48    Numeric,
49    /// `i`, `ii`, `iii`, …
50    LowerRoman,
51    /// `I`, `II`, …
52    UpperRoman,
53    /// Explicit tags; panels beyond the list are numbered.
54    Custom(Vec<String>),
55}
56
57impl TagLevels {
58    /// The tag of the `i`-th (0-based) panel.
59    pub fn tag(&self, i: usize) -> String {
60        match self {
61            TagLevels::Lower => alpha(i, b'a'),
62            TagLevels::Upper => alpha(i, b'A'),
63            TagLevels::Numeric => (i + 1).to_string(),
64            TagLevels::LowerRoman => roman(i + 1).to_lowercase(),
65            TagLevels::UpperRoman => roman(i + 1),
66            TagLevels::Custom(v) => v.get(i).cloned().unwrap_or_else(|| (i + 1).to_string()),
67        }
68    }
69}
70
71fn alpha(mut i: usize, base: u8) -> String {
72    // Bijective base-26: a..z, aa..az, ba..
73    let mut out = Vec::new();
74    loop {
75        out.push(base + (i % 26) as u8);
76        if i < 26 {
77            break;
78        }
79        i = i / 26 - 1;
80    }
81    out.reverse();
82    String::from_utf8(out).unwrap_or_default()
83}
84
85fn roman(mut n: usize) -> String {
86    if n >= 4000 {
87        return n.to_string();
88    }
89    const T: [(usize, &str); 13] = [
90        (1000, "M"),
91        (900, "CM"),
92        (500, "D"),
93        (400, "CD"),
94        (100, "C"),
95        (90, "XC"),
96        (50, "L"),
97        (40, "XL"),
98        (10, "X"),
99        (9, "IX"),
100        (5, "V"),
101        (4, "IV"),
102        (1, "I"),
103    ];
104    let mut s = String::new();
105    for (v, r) in T {
106        while n >= v {
107            s.push_str(r);
108            n -= v;
109        }
110    }
111    s
112}
113
114/// How a [`PlotGrid`] aligns its own direct cells' plot areas and x scale
115/// within each column (patchwork's `plot_layout(axes = ...)`, applied to
116/// the x axis and plot-area alignment specifically — sipemu/
117/// ggplot-rs#175, tracked from DataZooDE/anofox-visualization#318). Only a
118/// column of [`GridCell::Plot`] cells participates; a [`GridCell::Spacer`]
119/// or a nested [`PlotGrid`] cell keeps its own layout, and a column with
120/// fewer than two plot cells is a no-op. Set via [`PlotGrid::axis_collect`].
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
122pub enum AxisCollect {
123    /// Each cell keeps its own margins, x axis and x domain (default).
124    #[default]
125    Keep,
126    /// Equal left/right plot-area insets within each column, from the
127    /// widest y-axis label requirement in that column — the same idea as
128    /// anofox-visualization's `track.rs` `left = max(...)` fallback, done
129    /// here as one layout pass (`GGPlot::layout_built`'s own arithmetic)
130    /// rather than a render-measure-render loop. Every cell in the column
131    /// also switches to [`AxisLabelFit::Shift`](crate::theme::AxisLabelFit::Shift)
132    /// so an overflowing outer tick label can never perturb the alignment.
133    /// A cell set thinner than the plotting engine's 50px panel-height
134    /// floor via [`PlotGrid::heights`](Self::heights) (an
135    /// anofox-visualization `::TRACK` strip, typically 16-30px) gets a
136    /// centred `aspect_ratio` instead, so its panel comes out exactly as
137    /// tall as the cell rather than overflowing it — `track.rs`'s own
138    /// `Fit::correct` `aspect` trick, computed from the same layout pass.
139    /// Domains and axis text are otherwise untouched.
140    Align,
141    /// [`Align`](Self::Align), plus one x domain shared across the column
142    /// — the union of every cell's own trained x domain — with x-axis
143    /// ticks/title/labels drawn only on the bottom cell. Skipped (with a
144    /// warning), for a column whose x scale is discrete or untrained on
145    /// any cell, in which case the column still gets [`Align`](Self::Align)'s
146    /// plot-area alignment.
147    CollectX,
148}
149
150/// Where a [`PlotGrid`] puts its collected legends.
151#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
152pub enum GridLegendPosition {
153    /// A column to the right of the grid (default).
154    #[default]
155    Right,
156    /// A row below the grid.
157    Bottom,
158}
159
160/// How a grid was built by the `|` / `/` operators, so chains flatten
161/// (`a | b | c` is one row) while mixed operators nest.
162#[derive(Clone, Copy, Debug, PartialEq, Eq)]
163enum Op {
164    Row,
165    Col,
166}
167
168/// One grid cell.
169pub enum GridCell {
170    Plot(Box<GGPlot>),
171    Grid(Box<PlotGrid>),
172    /// An empty cell (patchwork's `plot_spacer()`).
173    Spacer,
174}
175
176impl From<GGPlot> for GridCell {
177    fn from(p: GGPlot) -> Self {
178        GridCell::Plot(Box::new(p))
179    }
180}
181
182impl From<PlotGrid> for GridCell {
183    fn from(g: PlotGrid) -> Self {
184        GridCell::Grid(Box::new(g))
185    }
186}
187
188/// A patchwork-style arrangement of plots rendered into **one** SVG on the
189/// plotters-free native path.
190///
191/// - cells fill row-major (or column-major with [`byrow(false)`](Self::byrow))
192///   across [`ncol`](Self::ncol) / [`nrow`](Self::nrow) (default: a near-square
193///   grid; `a | b` is one row, `a / b` one column);
194/// - [`widths`](Self::widths) / [`heights`](Self::heights) set relative
195///   column widths / row heights;
196/// - [`title`](Self::title) / [`subtitle`](Self::subtitle) /
197///   [`caption`](Self::caption) annotate the whole grid;
198/// - [`tag_levels`](Self::tag_levels) tags panels `a, b, …` (`A`, `1`, roman,
199///   custom) in reading order, through nested grids;
200/// - [`collect_legends`](Self::collect_legends) removes the sub-plots'
201///   legends and draws each distinct legend once beside the grid (identical
202///   legends are de-duplicated).
203///
204/// Nested grids keep their own layout and annotations; tagging and legend
205/// collection are controlled by the outermost grid.
206pub struct PlotGrid {
207    cells: Vec<GridCell>,
208    ncol: Option<usize>,
209    nrow: Option<usize>,
210    byrow: bool,
211    widths: Vec<f64>,
212    heights: Vec<f64>,
213    title: Option<String>,
214    subtitle: Option<String>,
215    caption: Option<String>,
216    tags: Option<TagLevels>,
217    tag_prefix: String,
218    tag_suffix: String,
219    collect_legends: bool,
220    legend_position: GridLegendPosition,
221    /// [`PlotGrid::legend_bottom_when_narrow`]'s share, if set (#363).
222    legend_bottom_share: Option<f64>,
223    spacing: f64,
224    theme: Theme,
225    axis_collect: AxisCollect,
226    op: Option<Op>,
227    /// Extra root `<svg>` attributes requested via [`PlotGrid::svg_attr`]/
228    /// [`PlotGrid::svg_attrs`], validated and applied at render time.
229    root_attrs: Vec<(String, String)>,
230    /// Whether to emit a `data-warnings` JSON array of the panels'
231    /// combined build warnings on the root `<svg>`
232    /// ([`PlotGrid::svg_warnings_attr`]). Default off; byte-identical
233    /// output when off.
234    svg_warnings_attr: bool,
235}
236
237impl Default for PlotGrid {
238    fn default() -> Self {
239        PlotGrid {
240            cells: Vec::new(),
241            ncol: None,
242            nrow: None,
243            byrow: true,
244            widths: Vec::new(),
245            heights: Vec::new(),
246            title: None,
247            subtitle: None,
248            caption: None,
249            tags: None,
250            tag_prefix: String::new(),
251            tag_suffix: String::new(),
252            collect_legends: false,
253            legend_position: GridLegendPosition::Right,
254            legend_bottom_share: None,
255            spacing: 0.0,
256            theme: Theme::default(),
257            axis_collect: AxisCollect::Keep,
258            op: None,
259            root_attrs: Vec::new(),
260            svg_warnings_attr: false,
261        }
262    }
263}
264
265/// Upper bound on grid columns/rows — a guard against absurd user input.
266const MAX_TRACKS: usize = 10_000;
267
268impl PlotGrid {
269    pub fn new() -> Self {
270        Self::default()
271    }
272
273    /// Append a plot (or a nested grid) as the next cell.
274    #[allow(clippy::should_implement_trait)]
275    pub fn add(mut self, cell: impl Into<GridCell>) -> Self {
276        self.cells.push(cell.into());
277        self
278    }
279
280    /// Append several plots.
281    pub fn add_all(mut self, plots: impl IntoIterator<Item = GGPlot>) -> Self {
282        self.cells.extend(plots.into_iter().map(GridCell::from));
283        self
284    }
285
286    /// Append an empty cell (patchwork's `plot_spacer()`).
287    pub fn add_spacer(mut self) -> Self {
288        self.cells.push(GridCell::Spacer);
289        self
290    }
291
292    /// Number of columns.
293    pub fn ncol(mut self, n: usize) -> Self {
294        self.ncol = (n > 0).then_some(n.min(MAX_TRACKS));
295        self
296    }
297
298    /// Number of rows.
299    pub fn nrow(mut self, n: usize) -> Self {
300        self.nrow = (n > 0).then_some(n.min(MAX_TRACKS));
301        self
302    }
303
304    /// Fill cells row by row (default) or column by column.
305    pub fn byrow(mut self, byrow: bool) -> Self {
306        self.byrow = byrow;
307        self
308    }
309
310    /// Relative column widths (recycled; non-positive / non-finite = 1).
311    pub fn widths(mut self, widths: &[f64]) -> Self {
312        self.widths = widths.to_vec();
313        self
314    }
315
316    /// Relative row heights (recycled; non-positive / non-finite = 1).
317    pub fn heights(mut self, heights: &[f64]) -> Self {
318        self.heights = heights.to_vec();
319        self
320    }
321
322    /// Title over the whole grid.
323    pub fn title(mut self, title: &str) -> Self {
324        self.title = Some(title.to_string());
325        self
326    }
327
328    /// Subtitle over the whole grid.
329    pub fn subtitle(mut self, subtitle: &str) -> Self {
330        self.subtitle = Some(subtitle.to_string());
331        self
332    }
333
334    /// Caption under the whole grid.
335    pub fn caption(mut self, caption: &str) -> Self {
336        self.caption = Some(caption.to_string());
337        self
338    }
339
340    /// Tag the panels (`labs(tag)` of each sub-plot is replaced).
341    pub fn tag_levels(mut self, levels: TagLevels) -> Self {
342        self.tags = Some(levels);
343        self
344    }
345
346    /// Text around each tag, e.g. `("(", ")")` for `(a)`.
347    pub fn tag_affixes(mut self, prefix: &str, suffix: &str) -> Self {
348        self.tag_prefix = prefix.to_string();
349        self.tag_suffix = suffix.to_string();
350        self
351    }
352
353    /// Collect the sub-plots' legends into one strip (identical legends are
354    /// drawn once). Sub-plots with an inside or hidden legend keep it.
355    pub fn collect_legends(mut self, collect: bool) -> Self {
356        self.collect_legends = collect;
357        self
358    }
359
360    /// Where collected legends go.
361    pub fn legend_position(mut self, pos: GridLegendPosition) -> Self {
362        self.legend_position = pos;
363        self
364    }
365
366    /// Move a [`Right`](GridLegendPosition::Right) collected-legend column
367    /// to a row below the grid when it would take more than `share` (0..1)
368    /// of the grid's width at render time — the grid counterpart of
369    /// [`GGPlot::legend_bottom_when_narrow`] (#363). The column is roughly
370    /// fixed in size, so on a phone-width render it squeezes the rightmost
371    /// cells; a render wide enough keeps `Right`, unchanged. Off by
372    /// default; no effect on [`Bottom`](GridLegendPosition::Bottom).
373    pub fn legend_bottom_when_narrow(mut self, share: f64) -> Self {
374        self.legend_bottom_share = Some(share);
375        self
376    }
377
378    /// Align this grid's own direct cells' plot areas and/or x scale,
379    /// one column at a time — see [`AxisCollect`] (sipemu/ggplot-rs#175).
380    /// Default [`AxisCollect::Keep`] (no change, byte-identical output).
381    pub fn axis_collect(mut self, mode: AxisCollect) -> Self {
382        self.axis_collect = mode;
383        self
384    }
385
386    /// Gap between cells, in px (default 0).
387    pub fn spacing(mut self, px: f64) -> Self {
388        self.spacing = if px.is_finite() { px.max(0.0) } else { 0.0 };
389        self
390    }
391
392    /// Theme for the grid's own title/subtitle/caption and the collected
393    /// legends' text.
394    pub fn theme(mut self, theme: Theme) -> Self {
395        self.theme = theme;
396        self
397    }
398
399    /// Add one extra attribute to the root `<svg>` element — see
400    /// [`GGPlot::svg_attr`](crate::plot::GGPlot::svg_attr), same allow-list
401    /// (`data-*`, `aria-*`, `role`, `id`, `class`, `lang`) and the same
402    /// "ignored with a warning" rule for anything else or a collision with
403    /// what ggplot-rs already writes (`data-grid`, or `data-warnings` when
404    /// [`svg_warnings_attr`](Self::svg_warnings_attr) is also on).
405    pub fn svg_attr(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
406        self.root_attrs.push((name.into(), value.into()));
407        self
408    }
409
410    /// [`svg_attr`](Self::svg_attr) for several attributes at once.
411    pub fn svg_attrs(mut self, attrs: impl IntoIterator<Item = (String, String)>) -> Self {
412        self.root_attrs.extend(attrs);
413        self
414    }
415
416    /// Emit a `data-warnings` attribute on the root `<svg>` — a JSON array
417    /// of every panel's combined build warnings, `[]` when there are none.
418    /// Default off, and omitted entirely when off.
419    pub fn svg_warnings_attr(mut self, on: bool) -> Self {
420        self.svg_warnings_attr = on;
421        self
422    }
423
424    /// Number of cells (plots, nested grids and spacers).
425    pub fn len(&self) -> usize {
426        self.cells.len()
427    }
428
429    /// Whether the grid has no cells.
430    pub fn is_empty(&self) -> bool {
431        self.cells.is_empty()
432    }
433
434    /// Render at 800×600.
435    pub fn render_svg_native(self) -> Result<String, GGError> {
436        self.render_svg_native_with_size(800, 600)
437    }
438
439    /// Render the grid to one standalone SVG document of `w`×`h` px. The root
440    /// carries `data-grid="<rows> <cols>"` (and no `data-plot`: each sub-`<svg>`
441    /// has its own).
442    pub fn render_svg_native_with_size(self, w: u32, h: u32) -> Result<String, GGError> {
443        Ok(self.render_page(w, h)?.0.finish())
444    }
445
446    /// Like [`render_svg_native_with_size`](Self::render_svg_native_with_size),
447    /// also returning every sub-plot's warnings, prefixed `panel <tag>: `.
448    pub fn render_svg_native_with_warnings(
449        self,
450        w: u32,
451        h: u32,
452    ) -> Result<(String, Vec<String>), GGError> {
453        let (page, warnings) = self.render_page(w, h)?;
454        Ok((page.finish(), warnings))
455    }
456
457    /// Render as a nested `<svg>` fragment positioned at `(x, y)` — for
458    /// composing the grid into a larger SVG (cf. [`GGPlot::render_svg_native_at`]).
459    pub fn render_svg_native_at(self, x: f64, y: f64, w: u32, h: u32) -> Result<String, GGError> {
460        Ok(self.render_page(w, h)?.0.finish_fragment(x, y))
461    }
462
463    fn render_page(self, w: u32, h: u32) -> Result<(SvgBackend, Vec<String>), GGError> {
464        let (w, h) = (w.max(1), h.max(1));
465        let mut theme = self.theme.clone();
466        theme.resolve_inheritance();
467        let collect = self.collect_legends;
468        let mut legend_pos = self.legend_position;
469        // #363: also capture each legend's Bottom layout, to switch to it
470        // once the page width is known.
471        let alt_pos = self
472            .legend_bottom_share
473            .filter(|_| legend_pos == GridLegendPosition::Right)
474            .map(|share| (share, GridLegendPosition::Bottom));
475        let root_attrs = self.root_attrs.clone();
476        let svg_warnings_attr = self.svg_warnings_attr;
477
478        // Phase 1 (size-independent): build every plot, assign tags and
479        // capture legends to collect.
480        let mut ctx = PrepCtx {
481            tags: self
482                .tags
483                .clone()
484                .map(|t| (t, self.tag_prefix.clone(), self.tag_suffix.clone())),
485            next_tag: 0,
486            next_index: 0,
487            collect: collect.then_some(legend_pos),
488            collect_alt: alt_pos.map(|(_, p)| p),
489            legends: Vec::new(),
490            page_w: w as f64,
491            page_h: h as f64,
492            legend_warnings: Vec::new(),
493        };
494        let prepared = prepare_grid(self, &mut ctx)?;
495        let (mut legends, alt_legends): (Vec<Legend>, Vec<Option<Legend>>) =
496            ctx.legends.into_iter().unzip();
497        if let Some((share, alt)) = alt_pos {
498            let strip_w = legends.iter().map(|l| l.w).fold(0.0, f64::max) + 2.0 * LEGEND_PAD;
499            if !legends.is_empty() && strip_w > share * w as f64 {
500                legend_pos = alt;
501                legends = alt_legends.into_iter().flatten().collect();
502            }
503        }
504        let legend_warnings = ctx.legend_warnings;
505
506        // Phase 2: page layout.
507        let full = Rect {
508            x: 0.0,
509            y: 0.0,
510            width: w as f64,
511            height: h as f64,
512        };
513        let mut page = SvgBackend::new(w, h, full.clone());
514        page.without_plot_attr();
515        let (rows, cols) = prepared.dims();
516        let data_grid_attr = ("data-grid".to_string(), format!("{rows} {cols}"));
517        page.set_root_attrs(vec![data_grid_attr.clone()]);
518        if let Some(fill) = theme
519            .plot_background
520            .fill
521            .filter(|_| theme.plot_background.visible)
522        {
523            page.draw_rect(
524                (0.0, 0.0),
525                (w as f64, h as f64),
526                &RectStyle {
527                    fill: Some(fill),
528                    stroke: None,
529                    stroke_width: 0.0,
530                    alpha: 1.0,
531                    clip: false,
532                    corner_radius: 0.0,
533                },
534            )
535            .map_err(GGError::Render)?;
536        }
537
538        let mut body = full;
539        if !legends.is_empty() {
540            let gap = theme.legend_spacing.max(0.0) * 2.0;
541            let pad = LEGEND_PAD;
542            match legend_pos {
543                GridLegendPosition::Right => {
544                    let strip_w = legends.iter().map(|l| l.w).fold(0.0, f64::max) + 2.0 * pad;
545                    let strip_w = strip_w.min(body.width * 0.4);
546                    body.width -= strip_w;
547                    let total_h =
548                        legends.iter().map(|l| l.h).sum::<f64>() + gap * (legends.len() - 1) as f64;
549                    let mut y = (body.y + (body.height - total_h) / 2.0).max(body.y + pad);
550                    for l in &legends {
551                        place_legend(&mut page, l, body.x + body.width + pad, y);
552                        y += l.h + gap;
553                    }
554                }
555                GridLegendPosition::Bottom => {
556                    let strip_h = legends.iter().map(|l| l.h).fold(0.0, f64::max) + 2.0 * pad;
557                    let strip_h = strip_h.min(body.height * 0.4);
558                    body.height -= strip_h;
559                    let total_w =
560                        legends.iter().map(|l| l.w).sum::<f64>() + gap * (legends.len() - 1) as f64;
561                    let mut x = (body.x + (body.width - total_w) / 2.0).max(body.x + pad);
562                    for l in &legends {
563                        place_legend(&mut page, l, x, body.y + body.height + pad);
564                        x += l.w + gap;
565                    }
566                }
567            }
568        }
569
570        // Phase 3: render cells into their rects.
571        let mut warnings = legend_warnings;
572        render_prepared(prepared, &body, &mut page, &mut warnings)?;
573
574        let mut reserved = vec![data_grid_attr];
575        if svg_warnings_attr {
576            reserved.push(("data-warnings".to_string(), String::new()));
577        }
578        let extra = crate::render::svg_backend::validate_root_attrs(
579            &root_attrs,
580            &reserved,
581            "grid",
582            &mut warnings,
583        );
584        page.extend_root_attrs(extra);
585        if svg_warnings_attr {
586            page.extend_root_attrs(vec![(
587                "data-warnings".to_string(),
588                crate::render::svg_backend::warnings_json(&warnings),
589            )]);
590        }
591        Ok((page, warnings))
592    }
593}
594
595/// Padding around the collected-legend strip.
596const LEGEND_PAD: f64 = 8.0;
597
598/// A collected legend: rendered markup (also its identity) and extent.
599struct Legend {
600    body: String,
601    x0: f64,
602    y0: f64,
603    w: f64,
604    h: f64,
605}
606
607fn place_legend(page: &mut SvgBackend, l: &Legend, x: f64, y: f64) {
608    let (tx, ty) = (x - l.x0, y - l.y0);
609    if tx.is_finite() && ty.is_finite() {
610        page.push_raw(&format!(
611            "<g class=\"legend\" transform=\"translate({tx:.2} {ty:.2})\">{}</g>",
612            l.body
613        ));
614    }
615}
616
617struct PrepCtx {
618    tags: Option<(TagLevels, String, String)>,
619    next_tag: usize,
620    next_index: usize,
621    collect: Option<GridLegendPosition>,
622    /// A second position to also capture each legend for (#363's
623    /// narrow-page fallback), chosen in `render_page`.
624    collect_alt: Option<GridLegendPosition>,
625    /// Collected legends, each with its `collect_alt` layout.
626    legends: Vec<(Legend, Option<Legend>)>,
627    /// The grid's overall page size (sipemu/ggplot-rs#155): a collected
628    /// legend's available width/height for its own B3-2 wrap/grid plan,
629    /// since a `PlotGrid`'s legend strip has no other natural bound —
630    /// it's sized by whatever the legend needs, so the legend instead
631    /// wraps to fit within the page itself.
632    page_w: f64,
633    page_h: f64,
634    /// Warnings `capture_legend`'s `plan_all_legends` call produces for a
635    /// collected legend (#170 / review #167 finding 6) — previously
636    /// discarded; merged into `render_page`'s own `warnings` so they
637    /// reach `render_svg_native_with_warnings` / `data-warnings` like
638    /// every other warning.
639    legend_warnings: Vec<String>,
640}
641
642/// A built sub-plot awaiting its cell size.
643struct PreparedPlot {
644    built: crate::build::BuiltPlot,
645    meta: RenderMeta,
646    panel: String,
647    /// The cell's own `GGPlot::svg_formatted_values` setting (review
648    /// PR#141 finding 6, LOW): `build_for_render` consumes the `GGPlot`
649    /// and discards this field (it only matters at render time, and
650    /// `PlotGrid` builds its cells independently of
651    /// `GGPlot::render_native`, the only place that previously read it),
652    /// so it has to be captured here and carried to the cell's own
653    /// backend in `render_prepared`.
654    svg_formatted_values: bool,
655    /// The cell's own `GGPlot::svg_attr`/`svg_attrs` (#208): same reason
656    /// as `svg_formatted_values` above — `build_for_render` discards
657    /// `GGPlot::root_attrs`, so it's captured here and applied in
658    /// `render_prepared`, on a `<g>` wrapping the cell's own content (the
659    /// cell's root `<svg>` carries `root_data_attrs` and `data-panel`
660    /// already; the grid's root carries the *grid's* own `svg_attr`s —
661    /// neither is this cell's to share).
662    root_attrs: Vec<(String, String)>,
663}
664
665enum Prepared {
666    Plot(Box<PreparedPlot>),
667    Grid(Box<PreparedGrid>),
668    Spacer,
669}
670
671struct PreparedGrid {
672    cells: Vec<Prepared>,
673    ncol: usize,
674    nrow: usize,
675    byrow: bool,
676    widths: Vec<f64>,
677    heights: Vec<f64>,
678    title: Option<String>,
679    subtitle: Option<String>,
680    caption: Option<String>,
681    spacing: f64,
682    theme: Theme,
683    axis_collect: AxisCollect,
684}
685
686impl Prepared {
687    fn dims(&self) -> (usize, usize) {
688        match self {
689            Prepared::Grid(g) => (g.nrow, g.ncol),
690            _ => (1, 1),
691        }
692    }
693}
694
695fn prepare_grid(grid: PlotGrid, ctx: &mut PrepCtx) -> Result<Prepared, GGError> {
696    let n = grid.cells.len();
697    let (ncol, nrow) = match (grid.ncol, grid.nrow, grid.op) {
698        (Some(c), Some(r), _) => {
699            // Grow rows if the cells don't fit.
700            (c, r.max(n.div_ceil(c)))
701        }
702        (Some(c), None, _) => (c, n.div_ceil(c).max(1)),
703        (None, Some(r), _) => (n.div_ceil(r).max(1), r),
704        (None, None, Some(Op::Row)) => (n.max(1), 1),
705        (None, None, Some(Op::Col)) => (1, n.max(1)),
706        (None, None, None) => {
707            let c = ((n as f64).sqrt().ceil() as usize).max(1);
708            (c, n.div_ceil(c).max(1))
709        }
710    };
711    let mut theme = grid.theme;
712    theme.resolve_inheritance();
713    let mut cells = Vec::with_capacity(n);
714    for cell in grid.cells {
715        cells.push(match cell {
716            GridCell::Spacer => Prepared::Spacer,
717            GridCell::Grid(g) => prepare_grid(*g, ctx)?,
718            GridCell::Plot(p) => Prepared::Plot(Box::new(prepare_plot(*p, ctx)?)),
719        });
720    }
721    Ok(Prepared::Grid(Box::new(PreparedGrid {
722        cells,
723        ncol,
724        nrow,
725        byrow: grid.byrow,
726        widths: grid.widths,
727        heights: grid.heights,
728        title: grid.title,
729        subtitle: grid.subtitle,
730        caption: grid.caption,
731        spacing: grid.spacing,
732        theme,
733        axis_collect: grid.axis_collect,
734    })))
735}
736
737fn prepare_plot(mut plot: GGPlot, ctx: &mut PrepCtx) -> Result<PreparedPlot, GGError> {
738    ctx.next_index += 1;
739    if let Some((levels, pre, suf)) = &ctx.tags {
740        plot.labels.tag = Some(format!("{pre}{}{suf}", levels.tag(ctx.next_tag)));
741        ctx.next_tag += 1;
742    }
743    let panel = plot
744        .labels
745        .tag
746        .clone()
747        .unwrap_or_else(|| ctx.next_index.to_string());
748
749    // Collect an outside legend: suppress it in the plot, draw it separately.
750    let original = plot.theme.legend_position.clone();
751    let collect = ctx.collect.filter(|_| {
752        matches!(
753            original,
754            LegendPosition::Right
755                | LegendPosition::Left
756                | LegendPosition::Top
757                | LegendPosition::Bottom
758        )
759    });
760    if collect.is_some() {
761        plot.theme.legend_position = LegendPosition::None;
762    }
763    let svg_formatted_values = plot.svg_formatted_values;
764    let root_attrs = plot.root_attrs.clone();
765    let (built, mut meta) = plot.build_for_render()?;
766    if let Some(pos) = collect {
767        meta.has_legend = false;
768        if let Some(l) = capture_legend(
769            &built,
770            pos,
771            ctx.page_w,
772            ctx.page_h,
773            &mut ctx.legend_warnings,
774        )
775        .map_err(GGError::Render)?
776        {
777            if !ctx.legends.iter().any(|(o, _)| o.body == l.body) {
778                let alt = match ctx.collect_alt {
779                    Some(alt) => {
780                        capture_legend(&built, alt, ctx.page_w, ctx.page_h, &mut Vec::new())
781                            .map_err(GGError::Render)?
782                    }
783                    None => None,
784                };
785                ctx.legends.push((l, alt));
786            }
787        }
788    }
789    Ok(PreparedPlot {
790        built,
791        meta,
792        panel,
793        svg_formatted_values,
794        root_attrs,
795    })
796}
797
798/// Draw a built plot's legend in isolation, measuring its extent.
799///
800/// `page_w`/`page_h` are the grid's overall page size (sipemu/
801/// ggplot-rs#155): a collected legend has no natural width/height bound
802/// of its own — the strip that holds it is sized by whatever the legend
803/// needs — so a discrete guide wraps/grids here against the page itself,
804/// the same way a single plot's legend wraps against its own canvas.
805fn capture_legend(
806    built: &crate::build::BuiltPlot,
807    pos: GridLegendPosition,
808    page_w: f64,
809    page_h: f64,
810    // Review #167 finding 6 (#170): the plan's own `guide_legend:
811    // nrow(0)/ncol(0) is invalid`-style warnings used to go into a
812    // `legend_warnings` local that nothing ever read — this path dropped
813    // them silently. The caller threads its own warnings vec through so
814    // they land in the grid's `render_svg_native_with_warnings` /
815    // `data-warnings`, the same place a single plot's own legend
816    // warnings go (`GGPlot::layout_built` → `built.warnings`).
817    warnings: &mut Vec<String>,
818) -> Result<Option<Legend>, RenderError> {
819    let mut theme = built.theme.clone();
820    let m = theme.legend_margin.clone();
821    // Fake a zero-size panel so the legend's origin lands at (0, 0).
822    let (pa, lp, is_horizontal) = match pos {
823        GridLegendPosition::Right => (
824            Rect {
825                x: -m.left,
826                y: -m.top,
827                width: 0.0,
828                height: 0.0,
829            },
830            LegendPosition::Right,
831            false,
832        ),
833        GridLegendPosition::Bottom => (
834            Rect {
835                x: -m.left,
836                y: -(m.top + 30.0),
837                width: 0.0,
838                height: 0.0,
839            },
840            LegendPosition::Bottom,
841            true,
842        ),
843    };
844    theme.legend_position = lp;
845    let mut svg = SvgBackend::new(1, 1, pa.clone());
846    let mut meas = Measure::new(&mut svg, pa.clone());
847    let avail_width = (page_w + m.left).max(0.0);
848    let avail_height = (page_h + m.top).max(0.0);
849    let legend_plans = legend::plan_all_legends(
850        &built.scales,
851        &built.guide_legend,
852        &theme,
853        &built.suppressed_aes,
854        avail_width,
855        avail_height,
856        is_horizontal,
857        warnings,
858    );
859    legend::draw_legend_planned(
860        &built.scales,
861        &theme,
862        &pa,
863        // Review #167 finding 1: step by the same `avail_width` share
864        // `plan_all_legends` just planned against, not `pa.width` (always
865        // 0.0 — `pa` is a fake zero-size panel so the legend's origin
866        // lands at (0, 0)). Using `pa.width` made every discrete guide
867        // after the first advance by zero, so two collected guides (e.g.
868        // `colour` and `shape`) were drawn on top of each other.
869        avail_width,
870        &mut meas,
871        &built.guide_legend,
872        &built.suppressed_aes,
873        &legend_plans,
874    )?;
875    let ext = meas.extent();
876    if svg.body().is_empty() {
877        return Ok(None);
878    }
879    let (x0, y0, x1, y1) = match ext {
880        Some(e) => e,
881        None => return Ok(None),
882    };
883    Ok(Some(Legend {
884        body: svg.body().to_string(),
885        x0,
886        y0,
887        w: (x1 - x0).max(0.0),
888        h: (y1 - y0).max(0.0),
889    }))
890}
891
892/// Normalised track sizes: `n` relative weights (recycled, invalid → 1).
893fn tracks(weights: &[f64], n: usize, total: f64, gap: f64) -> Vec<(f64, f64)> {
894    let ws: Vec<f64> = (0..n)
895        .map(|i| {
896            if weights.is_empty() {
897                1.0
898            } else {
899                let w = weights[i % weights.len()];
900                if w.is_finite() && w > 0.0 {
901                    w
902                } else {
903                    1.0
904                }
905            }
906        })
907        .collect();
908    let sum: f64 = ws.iter().sum();
909    let avail = (total - gap * n.saturating_sub(1) as f64).max(0.0);
910    let mut pos = 0.0;
911    ws.iter()
912        .map(|w| {
913            let size = avail * w / sum;
914            let t = (pos, size);
915            pos += size + gap;
916            t
917        })
918        .collect()
919}
920
921fn text_style(el: &crate::theme::elements::ElementText, anchor: TextAnchor) -> TextStyle {
922    TextStyle {
923        color: el.color,
924        size: el.size,
925        anchor,
926        angle: 0.0,
927        family: (!el.family.is_empty()).then(|| el.family.clone()),
928        face: el.face,
929        bg_colour: None,
930        bg_r: 0.0,
931    }
932}
933
934fn hjust_x(el: &crate::theme::elements::ElementText, x0: f64, x1: f64) -> (f64, TextAnchor) {
935    let hj = if el.hjust.is_finite() {
936        el.hjust.clamp(0.0, 1.0)
937    } else {
938        0.0
939    };
940    let anchor = if hj <= 0.02 {
941        TextAnchor::Start
942    } else if hj >= 0.98 {
943        TextAnchor::End
944    } else {
945        TextAnchor::Middle
946    };
947    (x0 + hj * (x1 - x0), anchor)
948}
949
950fn render_prepared(
951    prepared: Prepared,
952    area: &Rect,
953    page: &mut SvgBackend,
954    warnings: &mut Vec<String>,
955) -> Result<(), GGError> {
956    match prepared {
957        Prepared::Spacer => Ok(()),
958        Prepared::Plot(p) => {
959            let PreparedPlot {
960                mut built,
961                meta,
962                panel,
963                svg_formatted_values,
964                root_attrs,
965            } = *p;
966            let (cw, ch) = (
967                area.width.floor().max(1.0) as u32,
968                area.height.floor().max(1.0) as u32,
969            );
970            let layout = GGPlot::layout_built(&mut built, &meta, cw, ch);
971            let mut backend = SvgBackend::new(cw, ch, layout.plot_area.clone());
972            backend.set_formatted_values(svg_formatted_values);
973            let mut attrs = root_data_attrs(&built);
974            attrs.push(("data-panel".into(), panel.clone()));
975            backend.set_root_attrs(attrs);
976            PlotRenderer::render(&built, &mut backend).map_err(GGError::Render)?;
977            // #208: this cell's own `svg_attr`s go on a `<g>` wrapping its
978            // content, not the cell's root `<svg>` (that one is
979            // `root_data_attrs` + `data-panel`, ggplot-rs's own) nor the
980            // grid's root (that one is the *grid's* own `svg_attr`s) — so
981            // `existing` is empty: nothing built-in to collide with here,
982            // only a name reused across two `svg_attr` calls.
983            let mut attr_warnings = Vec::new();
984            let group_attrs = validate_root_attrs(&root_attrs, &[], "plot", &mut attr_warnings);
985            backend.wrap_body_in_group(&group_attrs);
986            warnings.extend(
987                built
988                    .warnings
989                    .iter()
990                    .cloned()
991                    .chain(backend.take_warnings())
992                    .chain(attr_warnings)
993                    .map(|w| format!("panel {panel}: {w}")),
994            );
995            page.push_raw(&backend.finish_fragment(area.x, area.y));
996            Ok(())
997        }
998        Prepared::Grid(g) => {
999            let mut g = *g;
1000            let theme = &g.theme;
1001            let m = &theme.plot_margin;
1002            // Header / footer bands.
1003            let mut top = area.y;
1004            let (x0, x1) = (area.x + m.left, area.x + area.width - m.right);
1005            if let Some(t) = &g.title {
1006                top += m.top;
1007                let (tx, anchor) = hjust_x(&theme.title, x0, x1);
1008                page.draw_text(
1009                    t,
1010                    (tx, top + theme.title.size * 0.8),
1011                    &text_style(&theme.title, anchor),
1012                )
1013                .map_err(GGError::Render)?;
1014                top += theme.title.size * 1.6;
1015            }
1016            if let Some(s) = &g.subtitle {
1017                if g.title.is_none() {
1018                    top += m.top;
1019                }
1020                let (sx, anchor) = hjust_x(&theme.subtitle, x0, x1);
1021                page.draw_text(
1022                    s,
1023                    (sx, top + theme.subtitle.size * 0.7),
1024                    &text_style(&theme.subtitle, anchor),
1025                )
1026                .map_err(GGError::Render)?;
1027                top += theme.subtitle.size * 1.5;
1028            }
1029            let mut bottom = area.y + area.height;
1030            if let Some(c) = &g.caption {
1031                let (cx, anchor) = hjust_x(&theme.caption, x0, x1);
1032                bottom -= theme.caption.size * 1.8;
1033                page.draw_text(
1034                    c,
1035                    (cx, bottom + theme.caption.size * 0.9),
1036                    &text_style(&theme.caption, anchor),
1037                )
1038                .map_err(GGError::Render)?;
1039            }
1040            let body_h = (bottom - top).max(1.0);
1041            let cols = tracks(&g.widths, g.ncol, area.width, g.spacing);
1042            let rows = tracks(&g.heights, g.nrow, body_h, g.spacing);
1043            if g.axis_collect != AxisCollect::Keep {
1044                align_axes(
1045                    &mut g.cells,
1046                    (g.ncol, g.nrow, g.byrow),
1047                    &cols,
1048                    &rows,
1049                    g.axis_collect,
1050                    warnings,
1051                );
1052            }
1053            for (k, cell) in g.cells.into_iter().enumerate() {
1054                let (r, c) = if g.byrow {
1055                    (k / g.ncol, k % g.ncol)
1056                } else {
1057                    (k % g.nrow, k / g.nrow)
1058                };
1059                if r >= g.nrow || c >= g.ncol {
1060                    // More cells than nrow × ncol: the rest do not fit.
1061                    warnings.push(format!(
1062                        "plot_grid: cell {} does not fit a {}×{} grid; skipped",
1063                        k + 1,
1064                        g.nrow,
1065                        g.ncol
1066                    ));
1067                    continue;
1068                }
1069                let rect = Rect {
1070                    x: area.x + cols[c].0,
1071                    y: top + rows[r].0,
1072                    width: cols[c].1,
1073                    height: rows[r].1,
1074                };
1075                render_prepared(cell, &rect, page, warnings)?;
1076            }
1077            Ok(())
1078        }
1079    }
1080}
1081
1082/// sipemu/ggplot-rs#175: align a grid's own direct cells' plot areas and/or
1083/// x domain, column by column ([`AxisCollect::Align`]/[`AxisCollect::CollectX`]).
1084/// `cols`/`rows` are this grid's own track sizes (same for every cell in a
1085/// column/row), already computed by the caller. Only `Prepared::Plot` cells
1086/// participate; a column with fewer than two of them is left untouched.
1087fn align_axes(
1088    cells: &mut [Prepared],
1089    // (ncol, nrow, byrow) — bundled so the function stays under clippy's
1090    // argument-count limit.
1091    (ncol, nrow, byrow): (usize, usize, bool),
1092    cols: &[(f64, f64)],
1093    rows: &[(f64, f64)],
1094    mode: AxisCollect,
1095    warnings: &mut Vec<String>,
1096) {
1097    let idx_of = |r: usize, c: usize| -> usize {
1098        if byrow {
1099            r * ncol + c
1100        } else {
1101            c * nrow + r
1102        }
1103    };
1104    for c in 0..ncol {
1105        let cw = cols.get(c).map_or(0.0, |t| t.1);
1106        if !cw.is_finite() || cw <= 0.0 {
1107            continue;
1108        }
1109        // (row, cell index) of every plot cell in this column, top to
1110        // bottom. A spacer or a nested grid is skipped — it keeps its own
1111        // layout and doesn't count toward "the bottom cell" either.
1112        let plot_cells: Vec<(usize, usize)> = (0..nrow)
1113            .filter_map(|r| {
1114                let k = idx_of(r, c);
1115                (k < cells.len() && matches!(cells[k], Prepared::Plot(_))).then_some((r, k))
1116            })
1117            .collect();
1118        if plot_cells.len() < 2 {
1119            continue; // nothing to align against in this column
1120        }
1121        let bottom_row = plot_cells.iter().map(|&(r, _)| r).max().unwrap();
1122
1123        // Switch every cell to `Shift` (so an overflowing outer label can
1124        // never grow its margins behind our back) and, under `CollectX`,
1125        // blank every non-bottom cell's x axis *before* measuring, so the
1126        // measurement already reflects what's actually drawn.
1127        for &(r, k) in &plot_cells {
1128            if let Prepared::Plot(p) = &mut cells[k] {
1129                p.built.theme.axis_text_x_fit = AxisLabelFit::Shift;
1130                if mode == AxisCollect::CollectX && r != bottom_row {
1131                    p.built.theme.axis_text_x = ElementText::blank();
1132                    p.built.theme.axis_title_x = ElementText::blank();
1133                    p.built.theme.axis_ticks_x = Some(ElementLine::blank());
1134                }
1135            }
1136        }
1137
1138        // Each cell's own plot-area rect at this column's width — pure
1139        // layout arithmetic (`GGPlot::layout_built`, the same computation
1140        // the real render pass makes), not a drawn-and-measured SVG, so
1141        // this whole pass costs one extra layout per cell, not a redraw.
1142        // `y`/`height` are kept too, for the thin-cell correction below —
1143        // changing a cell's left/right margin never moves them (only
1144        // top/bottom margins and the vertical bands do), so one layout
1145        // call per cell covers both passes.
1146        let mut natural: Vec<(f64, f64, f64, f64, f64)> = Vec::with_capacity(plot_cells.len());
1147        for &(r, k) in &plot_cells {
1148            let ch = rows.get(r).map_or(0.0, |t| t.1);
1149            match &mut cells[k] {
1150                Prepared::Plot(p) => {
1151                    let layout = GGPlot::layout_built(
1152                        &mut p.built,
1153                        &p.meta,
1154                        cw.floor().max(1.0) as u32,
1155                        ch.floor().max(1.0) as u32,
1156                    );
1157                    natural.push((
1158                        layout.plot_area.x,
1159                        layout.plot_area.x + layout.plot_area.width,
1160                        layout.plot_area.y,
1161                        layout.plot_area.height,
1162                        ch,
1163                    ));
1164                }
1165                _ => unreachable!("plot_cells only holds Prepared::Plot indices"),
1166            }
1167        }
1168
1169        // Equal left/right insets: every cell's left margin grows to the
1170        // widest left requirement in the column, and likewise for the
1171        // right (anofox-visualization's `track.rs` `left = max(...)`
1172        // fallback, generalised to both edges and computed once).
1173        let target_left = natural.iter().fold(f64::MIN, |m, &(l, ..)| m.max(l));
1174        let target_right_inset = natural
1175            .iter()
1176            .fold(f64::MIN, |m, &(_, r, ..)| m.max(cw - r));
1177        let width = cw - target_right_inset - target_left;
1178        if target_left.is_finite() && target_right_inset.is_finite() {
1179            for (&(_, k), &(nl, nr, ..)) in plot_cells.iter().zip(&natural) {
1180                if let Prepared::Plot(p) = &mut cells[k] {
1181                    p.built.theme.plot_margin.left += (target_left - nl).max(0.0);
1182                    p.built.theme.plot_margin.right += (target_right_inset - (cw - nr)).max(0.0);
1183                }
1184            }
1185        }
1186
1187        // A cell thinner than the plotting engine's 50px panel-height
1188        // floor (anofox-visualization's `::TRACK` strips, ~16-30px) would
1189        // otherwise overflow its own canvas: `layout_built` never shrinks
1190        // a panel below 50px on its own. Mirror `track.rs`'s `Fit::correct`
1191        // `aspect` trick — a centred `aspect_ratio` set so the panel comes
1192        // out exactly as tall as what's actually left after the margins —
1193        // but computed once from this same `layout_built` measurement
1194        // instead of track.rs's render-measure-render loop. A cell that
1195        // already fits (the common case — a normal chart cell) is
1196        // untouched.
1197        if width.is_finite() && width > 0.0 {
1198            for (&(_, k), &(_, _, y, h, ch)) in plot_cells.iter().zip(&natural) {
1199                if let Prepared::Plot(p) = &mut cells[k] {
1200                    let bottom_budget = ch - p.built.theme.plot_margin.bottom;
1201                    if y + h > bottom_budget + 0.5 {
1202                        let fits = (ch - y - p.built.theme.plot_margin.bottom).max(8.0);
1203                        p.built.theme.aspect_ratio = Some(fits / width);
1204                        p.built.theme.plot_margin.top -= (h - fits) / 2.0;
1205                    }
1206                }
1207            }
1208        }
1209
1210        if mode == AxisCollect::CollectX {
1211            collect_x_domain(cells, &plot_cells, warnings);
1212        }
1213    }
1214}
1215
1216/// Train one x domain across a column's cells — the union of every cell's
1217/// own trained domain — then pin every cell's x scale to it
1218/// ([`ScaleSet::set_limits`](crate::scale::ScaleSet::set_limits), the same
1219/// mechanism `coord_cartesian_zoom` uses). Each scale keeps its own
1220/// `expand`; since every cell now trains the same raw domain, identical
1221/// `expand` settings produce an identical expanded (drawn) domain too.
1222/// Skipped, with a warning, when any cell's x scale is discrete or
1223/// untrained — there's no well-defined union of levels or of nothing.
1224fn collect_x_domain(
1225    cells: &mut [Prepared],
1226    plot_cells: &[(usize, usize)],
1227    warnings: &mut Vec<String>,
1228) {
1229    let mut domains = Vec::with_capacity(plot_cells.len());
1230    for &(_, k) in plot_cells {
1231        let d = match &cells[k] {
1232            Prepared::Plot(p) => match p.built.scales.get(&Aesthetic::X) {
1233                Some(s) if !s.is_discrete() => s.domain(),
1234                _ => None,
1235            },
1236            _ => None,
1237        };
1238        match d {
1239            Some(d) => domains.push(d),
1240            None => {
1241                warnings.push(
1242                    "plot_grid: a column's x scale is discrete or untrained; x domain not \
1243                     collected"
1244                        .to_string(),
1245                );
1246                return;
1247            }
1248        }
1249    }
1250    let lo = domains.iter().map(|d| d.0).fold(f64::INFINITY, f64::min);
1251    let hi = domains
1252        .iter()
1253        .map(|d| d.1)
1254        .fold(f64::NEG_INFINITY, f64::max);
1255    if lo.is_finite() && hi.is_finite() && hi >= lo {
1256        for &(_, k) in plot_cells {
1257            if let Prepared::Plot(p) = &mut cells[k] {
1258                p.built.scales.set_limits(&Aesthetic::X, lo, hi);
1259            }
1260        }
1261    }
1262}
1263
1264/// A backend wrapper that records the bounding box of everything drawn.
1265struct Measure<'a> {
1266    inner: &'a mut SvgBackend,
1267    pa: Rect,
1268    bbox: Option<(f64, f64, f64, f64)>,
1269}
1270
1271impl<'a> Measure<'a> {
1272    fn new(inner: &'a mut SvgBackend, pa: Rect) -> Self {
1273        Measure {
1274            inner,
1275            pa,
1276            bbox: None,
1277        }
1278    }
1279
1280    fn grow(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) {
1281        if !(x0.is_finite() && y0.is_finite() && x1.is_finite() && y1.is_finite()) {
1282            return;
1283        }
1284        self.bbox = Some(match self.bbox {
1285            None => (x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)),
1286            Some((a, b, c, d)) => (
1287                a.min(x0.min(x1)),
1288                b.min(y0.min(y1)),
1289                c.max(x0.max(x1)),
1290                d.max(y0.max(y1)),
1291            ),
1292        });
1293    }
1294
1295    fn extent(&self) -> Option<(f64, f64, f64, f64)> {
1296        self.bbox
1297    }
1298}
1299
1300impl DrawBackend for Measure<'_> {
1301    fn draw_circle(
1302        &mut self,
1303        c: (f64, f64),
1304        r: f64,
1305        style: &PointStyle,
1306    ) -> Result<(), RenderError> {
1307        self.grow(c.0 - r, c.1 - r, c.0 + r, c.1 + r);
1308        self.inner.draw_circle(c, r, style)
1309    }
1310    fn draw_line(&mut self, pts: &[(f64, f64)], style: &LineStyle) -> Result<(), RenderError> {
1311        for p in pts {
1312            self.grow(p.0, p.1, p.0, p.1);
1313        }
1314        self.inner.draw_line(pts, style)
1315    }
1316    fn draw_quadratic(
1317        &mut self,
1318        p0: (f64, f64),
1319        ctrl: (f64, f64),
1320        p1: (f64, f64),
1321        style: &LineStyle,
1322    ) -> Result<(), RenderError> {
1323        self.grow(p0.0, p0.1, p0.0, p0.1);
1324        self.grow(ctrl.0, ctrl.1, ctrl.0, ctrl.1);
1325        self.grow(p1.0, p1.1, p1.0, p1.1);
1326        self.inner.draw_quadratic(p0, ctrl, p1, style)
1327    }
1328    fn draw_rect(
1329        &mut self,
1330        a: (f64, f64),
1331        b: (f64, f64),
1332        style: &RectStyle,
1333    ) -> Result<(), RenderError> {
1334        self.grow(a.0, a.1, b.0, b.1);
1335        self.inner.draw_rect(a, b, style)
1336    }
1337    fn draw_text(
1338        &mut self,
1339        text: &str,
1340        pos: (f64, f64),
1341        style: &TextStyle,
1342    ) -> Result<(), RenderError> {
1343        self.draw_text_with_title(text, pos, style, None)
1344    }
1345    fn draw_text_with_title(
1346        &mut self,
1347        text: &str,
1348        pos: (f64, f64),
1349        style: &TextStyle,
1350        title: Option<&str>,
1351    ) -> Result<(), RenderError> {
1352        // Review #167 finding 4 (#170): this wrapper must forward to
1353        // `inner.draw_text_with_title`, not `inner.draw_text` — the
1354        // latter is what `DrawBackend::draw_text_with_title`'s own
1355        // default impl calls, and relying on that default here would
1356        // silently drop a truncated legend label's `<title>` tooltip
1357        // (#159) whenever the label is measured through `Measure`
1358        // (`compose::capture_legend`'s collected-legend path).
1359        let w = text.chars().count() as f64 * style.size * 0.6;
1360        let (x0, x1) = match style.anchor {
1361            TextAnchor::Start => (pos.0, pos.0 + w),
1362            TextAnchor::Middle => (pos.0 - w / 2.0, pos.0 + w / 2.0),
1363            TextAnchor::End => (pos.0 - w, pos.0),
1364        };
1365        self.grow(x0, pos.1 - style.size / 2.0, x1, pos.1 + style.size / 2.0);
1366        self.inner.draw_text_with_title(text, pos, style, title)
1367    }
1368    fn draw_polygon(&mut self, pts: &[(f64, f64)], style: &RectStyle) -> Result<(), RenderError> {
1369        for p in pts {
1370            self.grow(p.0, p.1, p.0, p.1);
1371        }
1372        self.inner.draw_polygon(pts, style)
1373    }
1374    fn draw_shape(&mut self, c: (f64, f64), r: f64, style: &PointStyle) -> Result<(), RenderError> {
1375        self.grow(c.0 - r, c.1 - r, c.0 + r, c.1 + r);
1376        self.inner.draw_shape(c, r, style)
1377    }
1378    fn plot_area(&self) -> Rect {
1379        self.pa.clone()
1380    }
1381    fn total_area(&self) -> Rect {
1382        self.inner.total_area()
1383    }
1384    fn set_tooltip(&mut self, t: Option<String>) {
1385        self.inner.set_tooltip(t)
1386    }
1387    fn set_mark_axis(&mut self, k: Option<String>) {
1388        self.inner.set_mark_axis(k)
1389    }
1390    fn set_mark_series(&mut self, s: Option<String>) {
1391        self.inner.set_mark_series(s)
1392    }
1393    fn set_mark_value(&mut self, v: Option<String>) {
1394        self.inner.set_mark_value(v)
1395    }
1396    fn warn(&mut self, m: String) {
1397        self.inner.warn(m)
1398    }
1399    fn set_mark_geom(&mut self, geom: Option<&str>) {
1400        self.inner.set_mark_geom(geom)
1401    }
1402    fn set_mark_part(&mut self, part: Option<&str>) {
1403        self.inner.set_mark_part(part)
1404    }
1405    fn set_mark_attrs(&mut self, attrs: Vec<(String, String)>) {
1406        self.inner.set_mark_attrs(attrs)
1407    }
1408
1409    fn set_mark_class(&mut self, class: Option<String>) {
1410        self.inner.set_mark_class(class)
1411    }
1412
1413    fn set_mark_group(&mut self, group: Option<(String, String)>) {
1414        self.inner.set_mark_group(group)
1415    }
1416
1417    fn set_layer_attrs(&mut self, attrs: Vec<(String, String)>) {
1418        self.inner.set_layer_attrs(attrs)
1419    }
1420    fn wants_formatted_values(&self) -> bool {
1421        self.inner.wants_formatted_values()
1422    }
1423    fn set_mark_fvalue(&mut self, value: Option<String>) {
1424        self.inner.set_mark_fvalue(value)
1425    }
1426    fn set_mark_fx(&mut self, value: Option<String>) {
1427        self.inner.set_mark_fx(value)
1428    }
1429}
1430
1431// ─── Operator sugar ─────────────────────────────────────────────────
1432
1433fn join(op: Op, a: GridCell, b: GridCell) -> PlotGrid {
1434    let flatten = |c: GridCell| -> Vec<GridCell> {
1435        match c {
1436            GridCell::Grid(g) if g.op == Some(op) && g.is_plain() => g.cells,
1437            other => vec![other],
1438        }
1439    };
1440    let mut cells = flatten(a);
1441    cells.extend(flatten(b));
1442    PlotGrid {
1443        cells,
1444        op: Some(op),
1445        ..PlotGrid::default()
1446    }
1447}
1448
1449impl PlotGrid {
1450    /// Built purely by an operator chain (no explicit layout/annotations), so
1451    /// it can be flattened into a longer chain of the same operator.
1452    fn is_plain(&self) -> bool {
1453        self.ncol.is_none()
1454            && self.nrow.is_none()
1455            && self.widths.is_empty()
1456            && self.heights.is_empty()
1457            && self.title.is_none()
1458            && self.subtitle.is_none()
1459            && self.caption.is_none()
1460            && self.tags.is_none()
1461            && !self.collect_legends
1462            && self.legend_bottom_share.is_none()
1463            && self.axis_collect == AxisCollect::Keep
1464    }
1465}
1466
1467macro_rules! grid_ops {
1468    ($lhs:ty, $rhs:ty) => {
1469        /// `a | b`: place side by side (patchwork).
1470        impl BitOr<$rhs> for $lhs {
1471            type Output = PlotGrid;
1472            fn bitor(self, rhs: $rhs) -> PlotGrid {
1473                join(Op::Row, self.into(), rhs.into())
1474            }
1475        }
1476        /// `a / b`: stack vertically (patchwork).
1477        impl Div<$rhs> for $lhs {
1478            type Output = PlotGrid;
1479            fn div(self, rhs: $rhs) -> PlotGrid {
1480                join(Op::Col, self.into(), rhs.into())
1481            }
1482        }
1483    };
1484}
1485
1486grid_ops!(GGPlot, GGPlot);
1487grid_ops!(GGPlot, PlotGrid);
1488grid_ops!(PlotGrid, GGPlot);
1489grid_ops!(PlotGrid, PlotGrid);
1490
1491#[cfg(test)]
1492mod tests {
1493    use super::*;
1494
1495    #[test]
1496    fn tag_sequences() {
1497        let l: Vec<String> = (0..28).map(|i| TagLevels::Lower.tag(i)).collect();
1498        assert_eq!(&l[..3], &["a", "b", "c"]);
1499        assert_eq!(l[25], "z");
1500        assert_eq!(l[26], "aa");
1501        assert_eq!(l[27], "ab");
1502        assert_eq!(TagLevels::Upper.tag(1), "B");
1503        assert_eq!(TagLevels::Numeric.tag(9), "10");
1504        assert_eq!(TagLevels::LowerRoman.tag(3), "iv");
1505        assert_eq!(TagLevels::UpperRoman.tag(13), "XIV");
1506        assert_eq!(TagLevels::Custom(vec!["x".into()]).tag(1), "2");
1507    }
1508
1509    #[test]
1510    fn tracks_are_relative_and_robust() {
1511        let t = tracks(&[2.0, 1.0], 2, 300.0, 0.0);
1512        assert_eq!(t, vec![(0.0, 200.0), (200.0, 100.0)]);
1513        let t = tracks(&[f64::NAN, -1.0], 2, 100.0, 10.0);
1514        assert_eq!(t, vec![(0.0, 45.0), (55.0, 45.0)]);
1515        let t = tracks(&[], 3, 30.0, 100.0);
1516        assert!(t.iter().all(|(p, s)| p.is_finite() && *s == 0.0));
1517    }
1518
1519    /// `Measure` (the legend bbox pre-pass) forwards every `DrawBackend`
1520    /// method to its inner backend and grows its tracked extent to include
1521    /// a quadratic curve's control points — not just straight lines/shapes.
1522    #[test]
1523    fn measure_forwards_draw_quadratic_and_mark_metadata_and_grows_its_extent() {
1524        let pa = Rect {
1525            x: 0.0,
1526            y: 0.0,
1527            width: 100.0,
1528            height: 100.0,
1529        };
1530        let mut backend = SvgBackend::new(100, 100, pa.clone());
1531        backend.set_formatted_values(true);
1532        let mut meas = Measure::new(&mut backend, pa);
1533
1534        assert!(meas.extent().is_none());
1535        // B2-4: Measure forwards the formatted-values query/setters too.
1536        assert!(meas.wants_formatted_values());
1537        meas.set_mark_geom(Some("line"));
1538        meas.set_mark_part(Some("arrow"));
1539        meas.set_mark_attrs(vec![("data-x".to_string(), "v".to_string())]);
1540        meas.set_mark_fvalue(Some("1,234.5".to_string()));
1541        meas.set_mark_fx(Some("2026-10-12".to_string()));
1542        meas.draw_quadratic(
1543            (10.0, 20.0),
1544            (90.0, 5.0),
1545            (50.0, 80.0),
1546            &LineStyle::default(),
1547        )
1548        .unwrap();
1549        meas.set_mark_geom(None);
1550        meas.set_mark_part(None);
1551        meas.set_mark_attrs(Vec::new());
1552        meas.set_mark_fvalue(None);
1553        meas.set_mark_fx(None);
1554
1555        let (x0, y0, x1, y1) = meas.extent().expect("extent after drawing");
1556        assert_eq!((x0, y0, x1, y1), (10.0, 5.0, 90.0, 80.0));
1557
1558        // The marks actually reached the inner SvgBackend (data-part, the
1559        // data_attr, the mark class and the formatted values all appear in
1560        // its output).
1561        let svg = backend.finish();
1562        assert!(svg.contains("data-part=\"arrow\""));
1563        assert!(svg.contains("data-x=\"v\""));
1564        assert!(svg.contains("gg-arrow"));
1565        assert!(svg.contains("data-fvalue=\"1,234.5\""));
1566        assert!(svg.contains("data-fx=\"2026-10-12\""));
1567    }
1568
1569    /// #170 / review #167 finding 4: `Measure::draw_text_with_title` must
1570    /// forward the `<title>` to its inner `SvgBackend`, not fall through
1571    /// to `DrawBackend::draw_text_with_title`'s default impl (which calls
1572    /// `self.draw_text`, and `Measure::draw_text` only ever calls
1573    /// `inner.draw_text` — dropping the title). This only matters once a
1574    /// `<title>` is actually passed through a `Measure`-wrapped backend
1575    /// (today that's `compose::capture_legend`'s collected-legend path, a
1576    /// `PlotGrid`'s B3-2 label truncation doesn't run yet, so the bug is
1577    /// latent rather than reachable from `PlotGrid`'s own public API —
1578    /// hence this unit test against `Measure` directly).
1579    #[test]
1580    fn measure_forwards_the_title_to_its_inner_backend() {
1581        let pa = Rect {
1582            x: 0.0,
1583            y: 0.0,
1584            width: 100.0,
1585            height: 100.0,
1586        };
1587        let mut backend = SvgBackend::new(100, 100, pa.clone());
1588        let mut meas = Measure::new(&mut backend, pa);
1589        meas.draw_text_with_title(
1590            "Grp…",
1591            (10.0, 20.0),
1592            &TextStyle {
1593                size: 12.0,
1594                anchor: TextAnchor::Start,
1595                ..Default::default()
1596            },
1597            Some("Group 01"),
1598        )
1599        .unwrap();
1600        let svg = backend.finish();
1601        assert!(
1602            svg.contains("<title>Group 01</title>"),
1603            "the full label should reach the inner backend's <title>: {svg}"
1604        );
1605    }
1606}