Skip to main content

ggplot_rs/render/
svg_backend.rs

1//! A self-contained SVG `DrawBackend` — a *second* backend (no plotters),
2//! proving the `DrawBackend` abstraction: the same `PlotRenderer` drives it.
3//!
4//! It emits SVG elements directly, so SVG output needs no glyph rasterization
5//! (text is `<text>` with font attributes the viewer renders).
6
7use super::backend::{
8    DrawBackend, FillPattern, FontFace, LineEnd, LineStyle, Linetype, PointShape, PointStyle,
9    RectStyle, TextAnchor, TextStyle,
10};
11use super::{Rect, RenderError};
12
13/// Accumulates SVG markup for a plot rendered via [`DrawBackend`].
14///
15/// Every number written to an attribute is finite: marks whose geometry is
16/// `NaN`/`±inf` are dropped (a polyline keeps its finite vertices), and
17/// non-finite style values fall back to safe defaults. All data-derived
18/// attribute values and text go through [`escape`].
19pub struct SvgBackend {
20    plot_area: Rect,
21    total_area: Rect,
22    body: String,
23    tooltip: Option<String>,
24    axis_key: Option<String>,
25    series_key: Option<String>,
26    value_key: Option<String>,
27    mark_part: Option<String>,
28    mark_attrs: Vec<(String, String)>,
29    /// Geom of the layer being drawn (see [`DrawBackend::set_mark_geom`]).
30    mark_geom: Option<String>,
31    root_attrs: Vec<(String, String)>,
32    warnings: Vec<String>,
33    /// Emit the root `data-plot` panel rect (off for composite pages, whose
34    /// root is not a single panel).
35    plot_attr: bool,
36    /// B2-4: whether `GGPlot::svg_formatted_values` is on — see
37    /// [`DrawBackend::wants_formatted_values`].
38    formatted_values: bool,
39    /// `data-fvalue` for the next drawn mark(s) — see
40    /// [`DrawBackend::set_mark_fvalue`].
41    mark_fvalue: Option<String>,
42    /// `data-fx` for the next drawn mark(s) — see
43    /// [`DrawBackend::set_mark_fx`].
44    mark_fx: Option<String>,
45    /// Patterns registered so far via [`Self::pattern_url`] (#206),
46    /// deduped by equality: `(pattern, "<id_prefix>-pat-<n>")`. Drained
47    /// into a `<defs>` block by [`Self::pattern_defs`] on `finish`/
48    /// `finish_fragment`.
49    patterns: Vec<(FillPattern, String)>,
50    /// This backend instance's id prefix for `<defs>` — process-unique
51    /// (a monotonic counter), so inlining several rendered SVGs into one
52    /// HTML page never lets one's `url(#…)` resolve into another's
53    /// `<pattern>` (SVG ids share one DOM-wide namespace once inlined).
54    id_prefix: String,
55    /// #221: `Aes::class` tokens for the next drawn mark(s) — see
56    /// [`DrawBackend::set_mark_class`].
57    mark_user_class: Option<String>,
58    /// #221: the caller-named group `(attr, value)` the next drawn mark(s)
59    /// belong to — see [`DrawBackend::set_mark_group`].
60    mark_group: Option<(String, String)>,
61    /// #221: the current layer's `GGPlot::layer_svg_attr`s — see
62    /// [`DrawBackend::set_layer_attrs`].
63    layer_attrs: Vec<(String, String)>,
64    /// #221: each group's `(attr, value, accumulated markup)`, in order of
65    /// first appearance.
66    groups: Vec<(String, String, String)>,
67    /// #221: `(byte offset into body, index into groups)` — where each
68    /// group's `<g>` is spliced in by [`Self::flush_groups`]. Offsets are
69    /// non-decreasing (a group's slot is the body length when its first
70    /// mark arrives, and the body only ever grows at the end).
71    group_slots: Vec<(usize, usize)>,
72    /// #255: accessible name text ([`GGPlot::svg_title`](crate::plot::GGPlot::svg_title)),
73    /// written as a `<title id="{id_prefix}-title">` first child of the
74    /// root `<svg>` plus `role="img"`/`aria-labelledby`. Default `None`;
75    /// byte-identical output when not set.
76    svg_title: Option<String>,
77    /// #255: accessible description text ([`GGPlot::svg_desc`](crate::plot::GGPlot::svg_desc)),
78    /// written as a `<desc id="{id_prefix}-desc">` first child of the root
79    /// `<svg>` (after `<title>`, if any) plus `role="img"`/`aria-describedby`.
80    /// Default `None`; byte-identical output when not set.
81    svg_desc: Option<String>,
82}
83
84/// Source for [`SvgBackend::id_prefix`] — process-wide, so every backend
85/// instance gets a distinct prefix regardless of thread.
86static NEXT_ID_PREFIX: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
87
88impl SvgBackend {
89    pub fn new(width: u32, height: u32, plot_area: Rect) -> Self {
90        SvgBackend {
91            plot_area,
92            total_area: Rect {
93                x: 0.0,
94                y: 0.0,
95                width: width as f64,
96                height: height as f64,
97            },
98            body: String::new(),
99            tooltip: None,
100            axis_key: None,
101            series_key: None,
102            value_key: None,
103            mark_part: None,
104            mark_attrs: Vec::new(),
105            mark_geom: None,
106            root_attrs: Vec::new(),
107            warnings: Vec::new(),
108            plot_attr: true,
109            formatted_values: false,
110            mark_fvalue: None,
111            mark_fx: None,
112            patterns: Vec::new(),
113            id_prefix: format!(
114                "gg{:x}",
115                NEXT_ID_PREFIX.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
116            ),
117            mark_user_class: None,
118            mark_group: None,
119            layer_attrs: Vec::new(),
120            groups: Vec::new(),
121            group_slots: Vec::new(),
122            svg_title: None,
123            svg_desc: None,
124        }
125    }
126
127    /// The `url(#…)` fill value for `pattern`, registering its `<defs>
128    /// <pattern>` the first time this exact pattern is seen on this
129    /// backend (later calls with an equal pattern reuse the same id).
130    fn pattern_url(&mut self, pattern: &FillPattern) -> String {
131        if let Some((_, id)) = self.patterns.iter().find(|(p, _)| p == pattern) {
132            return format!("url(#{id})");
133        }
134        let id = format!("{}-pat-{}", self.id_prefix, self.patterns.len());
135        self.patterns.push((*pattern, id.clone()));
136        format!("url(#{id})")
137    }
138
139    /// Shared by [`DrawBackend::draw_rect`]/[`DrawBackend::draw_rect_pattern`]
140    /// — `fill` is already resolved (a hex colour or a pattern's `url(#…)`).
141    fn rect_markup(
142        &mut self,
143        (x0, y0): (f64, f64),
144        (x1, y1): (f64, f64),
145        fill: &str,
146        style: &RectStyle,
147    ) -> Result<(), RenderError> {
148        if !(x0.is_finite() && y0.is_finite() && x1.is_finite() && y1.is_finite()) {
149            return Ok(());
150        }
151        let (x, y) = (x0.min(x1), y0.min(y1));
152        let (w, h) = ((x1 - x0).abs(), (y1 - y0).abs());
153        let stroke = style.stroke.map(rgb).unwrap_or_else(|| "none".into());
154        // SVG `<rect>` takes rx/ry natively — clamp to half the shorter side
155        // so an oversized radius draws a "stadium" instead of a malformed box.
156        let r = style.corner_radius.max(0.0).min(w / 2.0).min(h / 2.0);
157        let radius = if r > 0.0 {
158            format!(" rx=\"{r:.2}\" ry=\"{r:.2}\"")
159        } else {
160            String::new()
161        };
162        let attrs = format!(
163            "x=\"{x:.2}\" y=\"{y:.2}\" width=\"{w:.2}\" height=\"{h:.2}\"{radius} fill=\"{fill}\"{} \
164             stroke=\"{stroke}\" stroke-width=\"{:.2}\"",
165            opacity_attr("fill", style.alpha),
166            finite_or(style.stroke_width, 0.0)
167        );
168        self.push_mark("rect", "bar", &attrs);
169        Ok(())
170    }
171
172    /// Shared by [`DrawBackend::draw_polygon`]/[`DrawBackend::draw_polygon_pattern`].
173    fn polygon_markup(
174        &mut self,
175        pts: &[(f64, f64)],
176        fill: &str,
177        style: &RectStyle,
178    ) -> Result<(), RenderError> {
179        let pts = finite_pts(pts);
180        if pts.len() < 3 {
181            return Ok(());
182        }
183        let stroke = style.stroke.map(rgb).unwrap_or_else(|| "none".into());
184        let attrs = format!(
185            "points=\"{}\" fill=\"{fill}\"{} stroke=\"{stroke}\" stroke-width=\"{:.2}\"",
186            points(&pts),
187            opacity_attr("fill", style.alpha),
188            finite_or(style.stroke_width, 0.0)
189        );
190        self.push_mark("polygon", "band", &attrs);
191        Ok(())
192    }
193
194    /// Shared by [`DrawBackend::draw_polygon_rings`]/
195    /// [`DrawBackend::draw_polygon_rings_pattern`].
196    fn polygon_rings_markup(
197        &mut self,
198        rings: &[Vec<(f64, f64)>],
199        fill: &str,
200        style: &RectStyle,
201    ) -> Result<(), RenderError> {
202        let mut d = String::new();
203        for ring in rings {
204            let pts = finite_pts(ring);
205            if pts.len() < 3 {
206                continue;
207            }
208            d.push('M');
209            d.push_str(&format!("{},{}", num(pts[0].0), num(pts[0].1)));
210            for p in &pts[1..] {
211                d.push('L');
212                d.push_str(&format!("{},{}", num(p.0), num(p.1)));
213            }
214            d.push('Z');
215        }
216        if d.is_empty() {
217            return Ok(());
218        }
219        let stroke = style.stroke.map(rgb).unwrap_or_else(|| "none".into());
220        let attrs = format!(
221            "d=\"{d}\" fill-rule=\"evenodd\" fill=\"{fill}\"{} stroke=\"{stroke}\" stroke-width=\"{:.2}\"",
222            opacity_attr("fill", style.alpha),
223            finite_or(style.stroke_width, 0.0)
224        );
225        self.push_mark("path", "band", &attrs);
226        Ok(())
227    }
228
229    /// The `<defs>` block for every pattern [`Self::pattern_url`]
230    /// registered (empty when none were) — a `<pattern>` tile per the
231    /// stripe's own cycle, rotated by `patternTransform`, a `bg`-filled
232    /// tile with a narrower `fg` stripe at its edge (#206).
233    fn pattern_defs(&self) -> String {
234        if self.patterns.is_empty() {
235            return String::new();
236        }
237        let mut defs = String::from("<defs>");
238        for (p, id) in &self.patterns {
239            let FillPattern::Stripe {
240                fg,
241                bg,
242                size_px,
243                angle_deg,
244            } = p;
245            let size = if *size_px > 0.0 { *size_px } else { 1.0 };
246            defs.push_str(&format!(
247                "<pattern id=\"{id}\" width=\"{size}\" height=\"{size}\" patternUnits=\"userSpaceOnUse\" patternTransform=\"rotate({angle_deg})\"><rect width=\"{size}\" height=\"{size}\" fill=\"{}\"/><rect width=\"{:.3}\" height=\"{size}\" fill=\"{}\"/></pattern>",
248                rgb(*bg),
249                size * 0.3,
250                rgb(*fg),
251            ));
252        }
253        defs.push_str("</defs>");
254        defs
255    }
256
257    /// Turn `data-fvalue`/`data-fx` on for the whole render
258    /// ([`GGPlot::svg_formatted_values`](crate::plot::GGPlot::svg_formatted_values)).
259    /// Default off (set once, before drawing).
260    pub(crate) fn set_formatted_values(&mut self, on: bool) {
261        self.formatted_values = on;
262    }
263
264    /// Add extra attributes to the root `<svg>` element (e.g. the
265    /// `data-domain` family from [`root_data_attrs`]). Names must be plain
266    /// attribute names; values are escaped on output.
267    pub fn set_root_attrs(&mut self, attrs: Vec<(String, String)>) {
268        self.root_attrs = attrs;
269    }
270
271    /// Append extra attributes to the root `<svg>` element, after any
272    /// already set by [`set_root_attrs`](Self::set_root_attrs) (e.g. the
273    /// `data-domain` family from [`root_data_attrs`]). Unlike
274    /// `set_root_attrs`, this does not replace what is already there.
275    /// Callers (`GGPlot::svg_attr`/`PlotGrid::svg_attr`, via
276    /// [`validate_root_attrs`]) are responsible for name validation and
277    /// collision checks; this just appends. Values are escaped on output.
278    pub fn extend_root_attrs(&mut self, attrs: Vec<(String, String)>) {
279        self.root_attrs.extend(attrs);
280    }
281
282    /// Omit the root `data-plot` attribute (composite pages).
283    pub(crate) fn without_plot_attr(&mut self) {
284        self.plot_attr = false;
285    }
286
287    /// #255: set the root `<svg>`'s accessible name text
288    /// ([`GGPlot::svg_title`](crate::plot::GGPlot::svg_title)). `None`
289    /// (the default) omits the `<title>` child and the `role`/
290    /// `aria-labelledby` attributes entirely.
291    pub(crate) fn set_svg_title(&mut self, title: Option<String>) {
292        self.svg_title = title;
293    }
294
295    /// #255: set the root `<svg>`'s accessible description text
296    /// ([`GGPlot::svg_desc`](crate::plot::GGPlot::svg_desc)). `None` (the
297    /// default) omits the `<desc>` child and the `role`/
298    /// `aria-describedby` attributes entirely.
299    pub(crate) fn set_svg_desc(&mut self, desc: Option<String>) {
300        self.svg_desc = desc;
301    }
302
303    /// Append pre-rendered, already-escaped SVG markup (e.g. a nested plot
304    /// fragment) to the body. Crate-internal: callers guarantee well-formedness.
305    pub(crate) fn push_raw(&mut self, markup: &str) {
306        self.body.push_str(markup);
307    }
308
309    /// The accumulated body markup (without the root element), with every
310    /// pending `group_wrap` group (#221) spliced in.
311    pub(crate) fn body(&mut self) -> &str {
312        self.flush_groups();
313        &self.body
314    }
315
316    /// Append one drawn mark: into its `group_wrap` group's buffer while a
317    /// layer's mark has one (#221), else straight onto the body.
318    fn emit(&mut self, markup: &str) {
319        let key = match (&self.mark_group, &self.mark_geom) {
320            (Some(k), Some(_)) => k,
321            _ => {
322                self.body.push_str(markup);
323                return;
324            }
325        };
326        match self
327            .groups
328            .iter_mut()
329            .find(|(n, v, _)| n == &key.0 && v == &key.1)
330        {
331            Some((_, _, content)) => content.push_str(markup),
332            None => {
333                self.group_slots.push((self.body.len(), self.groups.len()));
334                self.groups
335                    .push((key.0.clone(), key.1.clone(), markup.to_string()));
336            }
337        }
338    }
339
340    /// Splice every pending group (#221) into the body as
341    /// `<g data-<name>="<value>">…</g>` at its first mark's position. The
342    /// attribute name was validated by `Aes::group_wrap`/`resolve_mappings`
343    /// and is re-checked here (defence in depth, as for `mark_attrs`); a
344    /// group with an invalid name is spliced in unwrapped.
345    fn flush_groups(&mut self) {
346        if self.group_slots.is_empty() {
347            return;
348        }
349        let groups = std::mem::take(&mut self.groups);
350        let slots = std::mem::take(&mut self.group_slots);
351        let extra: usize = groups
352            .iter()
353            .map(|(n, v, c)| n.len() + v.len() + c.len() + 12)
354            .sum();
355        let mut out = String::with_capacity(self.body.len() + extra);
356        let mut last = 0;
357        for (off, gi) in slots {
358            out.push_str(&self.body[last..off]);
359            last = off;
360            let (name, value, content) = &groups[gi];
361            if is_valid_mark_attr_name(name) {
362                out.push_str(&format!(
363                    "<g {name}=\"{}\">{content}</g>",
364                    escape_attr(value)
365                ));
366            } else {
367                out.push_str(content);
368            }
369        }
370        out.push_str(&self.body[last..]);
371        self.body = out;
372    }
373
374    /// Wrap the accumulated body in a `<g>` carrying `attrs` (escaped) — a
375    /// `PlotGrid` cell's own `svg_attr`s (#208), which belong on that
376    /// cell's content, not the cell's own root `<svg>` (ggplot-rs's own
377    /// `root_data_attrs`/`data-panel`) or the grid's root (the grid's own
378    /// `svg_attr`s). A no-op for an empty `attrs` — unaffected output,
379    /// same as every other panel without a `svg_attr` call.
380    pub(crate) fn wrap_body_in_group(&mut self, attrs: &[(String, String)]) {
381        self.flush_groups();
382        if attrs.is_empty() {
383            return;
384        }
385        let mut open = String::from("<g");
386        for (k, v) in attrs {
387            open.push_str(&format!(" {k}=\"{}\"", escape_attr(v)));
388        }
389        open.push('>');
390        self.body = format!("{open}{}</g>", self.body);
391    }
392
393    /// Take the warnings reported while drawing (see [`DrawBackend::warn`]).
394    pub fn take_warnings(&mut self) -> Vec<String> {
395        std::mem::take(&mut self.warnings)
396    }
397
398    /// ` class="gg-mark gg-<kind>" data-geom="<geom>"` while a layer's geom is
399    /// drawing; empty for axes, grid lines, legends and backgrounds.
400    ///
401    /// #221: the layer's `layer_svg_attr("class", …)` tokens, then the
402    /// row's `Aes::class` tokens, are appended (each re-validated, defence
403    /// in depth; duplicates written once) — still one `class` attribute.
404    fn mark_class(&self, kind: &str) -> String {
405        match &self.mark_geom {
406            Some(g) => {
407                let mut class = format!("gg-mark gg-{kind}");
408                let layer = self
409                    .layer_attrs
410                    .iter()
411                    .filter(|(n, _)| n == "class")
412                    .map(|(_, v)| v.as_str());
413                let mut seen: Vec<&str> = Vec::new();
414                for t in layer
415                    .chain(self.mark_user_class.as_deref())
416                    .flat_map(str::split_whitespace)
417                {
418                    if crate::aes::is_valid_class_token(t) && !seen.contains(&t) {
419                        seen.push(t);
420                        class.push(' ');
421                        class.push_str(t);
422                    }
423                }
424                format!(" class=\"{class}\" data-geom=\"{}\"", escape_attr(g))
425            }
426            None => String::new(),
427        }
428    }
429
430    /// #221: ` aria-hidden="…" pointer-events="…"` from the current layer's
431    /// `layer_svg_attr`s (only while a layer draws). `aria_hidden_written`
432    /// skips the layer's `aria-hidden` when the mark already carries one
433    /// (a mark part — arrowhead/decorative — always writes `"true"`).
434    fn layer_flag_attrs(&self, aria_hidden_written: bool) -> String {
435        let mut out = String::new();
436        if self.mark_geom.is_none() {
437            return out;
438        }
439        for (name, value) in &self.layer_attrs {
440            let ok = match name.as_str() {
441                "aria-hidden" => !aria_hidden_written && matches!(value.as_str(), "true" | "false"),
442                "pointer-events" => POINTER_EVENTS_VALUES.contains(&value.as_str()),
443                _ => false,
444            };
445            if ok {
446                out.push_str(&format!(" {name}=\"{value}\""));
447            }
448        }
449        out
450    }
451
452    /// #221: the `data-*` attributes a layer's text mark carries — the
453    /// layer's `layer_svg_attr("data-…")`s, then the row's `Aes::data_attr`
454    /// ones (a row attr replaces a layer attr of the same name). Text marks
455    /// don't go through [`Self::push_mark`] (no `<title>`, no built-ins).
456    fn text_data_attrs(&self) -> String {
457        let mut pairs: Vec<(&str, &str)> = Vec::new();
458        if self.mark_geom.is_none() {
459            return String::new();
460        }
461        for (name, val) in self.layer_attrs.iter().chain(self.mark_attrs.iter()) {
462            if !is_valid_mark_attr_name(name) {
463                continue;
464            }
465            match pairs.iter_mut().find(|(n, _)| n == name) {
466                Some(existing) => existing.1 = val,
467                None => pairs.push((name, val)),
468            }
469        }
470        let mut out = String::new();
471        for (name, val) in pairs {
472            out.push_str(&format!(" {name}=\"{}\"", escape_attr(val)));
473        }
474        out
475    }
476
477    /// Emit `<tag attrs/>`, or `<tag attrs data-…><title>tip</title></tag>`
478    /// when a tooltip / mark metadata is set (native SVG hover + host grouping).
479    ///
480    /// `kind` is the data-mark class suffix (see [`Self::mark_class`]) used
481    /// when no mark part is set. While a mark part *is* set (e.g. an
482    /// arrowhead), the part name is the effective kind instead — an
483    /// arrowhead gets `gg-mark gg-arrow`, not its parent's `gg-band`/`gg-line`.
484    fn push_mark(&mut self, tag: &str, kind: &str, attrs: &str) {
485        // Built-ins first, then user `data-*` attrs — re-validated here as
486        // defence in depth even though `geom::set_row_extras` already
487        // derives them only from validated `data_attr` mappings, because
488        // `set_mark_attrs` is a public `DrawBackend` method any caller could
489        // invoke directly. A later entry with the same name replaces an
490        // earlier one rather than duplicating it — including `mark_part`'s
491        // own `data-part`, added last, so it always wins over anything a
492        // caller supplied under that name (an element never carries the
493        // same attribute twice, which is an XML parse error).
494        let mut pairs: Vec<(String, String)> = Vec::new();
495        for (name, val) in [
496            ("data-x", &self.axis_key),
497            ("data-series", &self.series_key),
498            ("data-value", &self.value_key),
499        ] {
500            if let Some(v) = val {
501                pairs.push((name.to_string(), v.clone()));
502            }
503        }
504        // B2-4: `data-fx` only when it says something `data-x` doesn't —
505        // most marks have a plain numeric x, where the "formatted" value is
506        // the same string, and a redundant duplicate attribute is noise.
507        if let Some(fx) = &self.mark_fx {
508            if self.axis_key.as_deref() != Some(fx.as_str()) {
509                pairs.push(("data-fx".to_string(), fx.clone()));
510            }
511        }
512        if let Some(fvalue) = &self.mark_fvalue {
513            pairs.push(("data-fvalue".to_string(), fvalue.clone()));
514        }
515        // #221: the layer's own `data-*` (`layer_svg_attr`) first, so a
516        // row's `data_attr` of the same name replaces it.
517        let layer_data = self.layer_attrs.iter().filter(|_| self.mark_geom.is_some());
518        for (name, val) in layer_data.chain(self.mark_attrs.iter()) {
519            if !is_valid_mark_attr_name(name) {
520                continue;
521            }
522            match pairs.iter_mut().find(|(n, _)| n == name) {
523                Some(existing) => existing.1 = val.clone(),
524                None => pairs.push((name.clone(), val.clone())),
525            }
526        }
527        // A mark part (e.g. an arrowhead) is decorative: no `<title>`, but it
528        // still carries `data-part` plus `aria-hidden` so assistive tech
529        // skips it (the parent mark already carries the semantic tooltip).
530        // Routed through the same dedup as the other attrs.
531        if let Some(part) = &self.mark_part {
532            match pairs.iter_mut().find(|(n, _)| n == "data-part") {
533                Some(existing) => existing.1 = part.clone(),
534                None => pairs.push(("data-part".to_string(), part.clone())),
535            }
536        }
537        let mut data = String::new();
538        for (name, val) in &pairs {
539            data.push_str(&format!(" {name}=\"{}\"", escape_attr(val)));
540        }
541        if self.mark_part.is_some() {
542            data.push_str(" aria-hidden=\"true\"");
543        }
544        data.push_str(&self.layer_flag_attrs(self.mark_part.is_some()));
545        // The class goes last so existing attribute sequences stay
546        // byte-identical. Exactly one `class` attribute is ever written here
547        // (no `data_attr` mapping can be named bare `class` — see
548        // `is_valid_mark_attr_name` — so nothing in `pairs` can collide with it).
549        let effective_kind: &str = self.mark_part.as_deref().unwrap_or(kind);
550        data.push_str(&self.mark_class(effective_kind));
551        let mark = if self.mark_part.is_some() {
552            format!("<{tag} {attrs}{data}/>")
553        } else {
554            match &self.tooltip {
555                Some(t) => format!("<{tag} {attrs}{data}><title>{}</title></{tag}>", escape(t)),
556                None => format!("<{tag} {attrs}{data}/>"),
557            }
558        };
559        self.emit(&mark);
560    }
561
562    fn root_open(&self, prefix: &str) -> String {
563        let p = &self.plot_area;
564        let (w, h) = (self.total_area.width as i64, self.total_area.height as i64);
565        let mut extra = String::new();
566        for (k, v) in &self.root_attrs {
567            extra.push_str(&format!(" {k}=\"{}\"", escape_attr(v)));
568        }
569        let plot = if self.plot_attr {
570            format!(
571                " data-plot=\"{} {} {} {}\"",
572                num(p.x),
573                num(p.y),
574                num(p.width),
575                num(p.height),
576            )
577        } else {
578            String::new()
579        };
580        // #255: `role="img"` plus `aria-labelledby`/`aria-describedby`
581        // pointing at the `<title>`/`<desc>` children `access_children`
582        // writes as the root's first children — only when a title/desc
583        // was actually set, so default output stays byte-identical.
584        let mut access = String::new();
585        if self.svg_title.is_some() || self.svg_desc.is_some() {
586            access.push_str(" role=\"img\"");
587        }
588        if self.svg_title.is_some() {
589            access.push_str(&format!(" aria-labelledby=\"{}-title\"", self.id_prefix));
590        }
591        if self.svg_desc.is_some() {
592            access.push_str(&format!(" aria-describedby=\"{}-desc\"", self.id_prefix));
593        }
594        format!(
595            "<svg {prefix}width=\"{w}\" height=\"{h}\" viewBox=\"0 0 {w} {h}\"{plot}{access}{extra}>"
596        )
597    }
598
599    /// #255: `<title>`/`<desc>` markup for the root `<svg>`'s first
600    /// children (SVG accessibility practice puts them before any other
601    /// content), ids matching [`root_open`]'s `aria-labelledby`/
602    /// `aria-describedby`. Empty when neither was set.
603    fn access_children(&self) -> String {
604        let mut out = String::new();
605        if let Some(t) = &self.svg_title {
606            out.push_str(&format!(
607                "<title id=\"{}-title\">{}</title>",
608                self.id_prefix,
609                escape(t)
610            ));
611        }
612        if let Some(d) = &self.svg_desc {
613            out.push_str(&format!(
614                "<desc id=\"{}-desc\">{}</desc>",
615                self.id_prefix,
616                escape(d)
617            ));
618        }
619        out
620    }
621
622    /// Wrap the accumulated elements in a complete `<svg>` document. The
623    /// `data-plot` attribute records the panel (data) area in viewBox units so a
624    /// host can map screen coordinates back to data (e.g. for zoom/crosshair).
625    pub fn finish(mut self) -> String {
626        self.flush_groups();
627        let open = self.root_open("xmlns=\"http://www.w3.org/2000/svg\" ");
628        let access = self.access_children();
629        let defs = self.pattern_defs();
630        format!("{open}{access}{defs}{}</svg>", self.body)
631    }
632
633    /// Wrap the accumulated elements in a *nested* `<svg>` positioned at
634    /// `(x, y)` in a parent SVG's user space — no `xmlns` (it inherits the
635    /// parent's namespace), so the result can be pasted into a larger SVG as-is.
636    /// `data-plot` stays in this fragment's own viewBox units.
637    pub fn finish_fragment(mut self, x: f64, y: f64) -> String {
638        self.flush_groups();
639        let open = self.root_open(&format!("x=\"{}\" y=\"{}\" ", num(x), num(y)));
640        let access = self.access_children();
641        let defs = self.pattern_defs();
642        format!("{open}{access}{defs}{}</svg>", self.body)
643    }
644
645    /// Emit one `<polyline>` for an already-finite run of points — see
646    /// [`DrawBackend::draw_line`]'s splitting at non-finite vertices.
647    fn draw_line_run(&mut self, pts: &[(f64, f64)], style: &LineStyle) {
648        let dash = dasharray_attr(&style.linetype);
649        // `Butt` is SVG's own default: omitting the attribute keeps default
650        // output byte-identical.
651        let linecap = match style.lineend {
652            LineEnd::Butt => "",
653            LineEnd::Round => " stroke-linecap=\"round\"",
654            LineEnd::Square => " stroke-linecap=\"square\"",
655        };
656        let attrs = format!(
657            "points=\"{}\" fill=\"none\" stroke=\"{}\" stroke-width=\"{:.2}\"{}{}{}",
658            points(pts),
659            rgb(style.color),
660            finite_or(style.width, 1.0),
661            opacity_attr("stroke", style.alpha),
662            dash,
663            linecap
664        );
665        self.push_mark("polyline", "line", &attrs);
666    }
667}
668
669/// Format a coordinate with two decimals; non-finite becomes `0` (callers
670/// drop marks with non-finite geometry before this, so this is a last resort).
671fn num(v: f64) -> String {
672    if v.is_finite() {
673        format!("{v:.2}")
674    } else {
675        "0".to_string()
676    }
677}
678
679/// ` stroke-dasharray="d,g,…"` for a linetype, or `""` for solid. Uses the
680/// sanitised [`Linetype::dash_pattern`], so a `Linetype::Custom` with NaN or
681/// negative lengths never reaches the attribute (#220).
682fn dasharray_attr(linetype: &Linetype) -> String {
683    let pattern = linetype.dash_pattern();
684    if pattern.is_empty() {
685        return String::new();
686    }
687    let s = pattern
688        .iter()
689        .flat_map(|(d, g)| [*d, *g])
690        .map(|v| format!("{v}"))
691        .collect::<Vec<_>>()
692        .join(",");
693    format!(" stroke-dasharray=\"{s}\"")
694}
695
696/// #214: `d` for [`SvgBackend::draw_polygon_rings_precise`] — one `M<x>,<y>`
697/// per ring (the first point, absolute) followed by one `l` command whose
698/// coordinate pairs are **deltas from the previous point**, SVG's implicit
699/// lineto-repeat (`l dx1,dy1 dx2,dy2 …`), each number rounded to
700/// `precision` decimals with trailing zeros trimmed. Deltas are typically
701/// far smaller than absolute screen coordinates, and the single `l` letter
702/// (vs. one `L` per point) is the other half of the size win; see
703/// [`push_num`] for the separator-omission this relies on. A ring under 3
704/// finite points is dropped, same as the absolute encoder.
705fn compact_rings_d(rings: &[Vec<(f64, f64)>], precision: u8) -> String {
706    let mut d = String::new();
707    for ring in rings {
708        let pts = finite_pts(ring);
709        if pts.len() < 3 {
710            continue;
711        }
712        d.push('M');
713        push_num(&mut d, &fmt_compact(pts[0].0, precision));
714        push_num(&mut d, &fmt_compact(pts[0].1, precision));
715        if pts.len() > 1 {
716            d.push('l');
717            let mut prev = pts[0];
718            for p in &pts[1..] {
719                push_num(&mut d, &fmt_compact(p.0 - prev.0, precision));
720                push_num(&mut d, &fmt_compact(p.1 - prev.1, precision));
721                prev = *p;
722            }
723        }
724        d.push('z');
725    }
726    d
727}
728
729/// Append `s` (a formatted number) to `d`, inserting a comma first only
730/// when one is actually needed to keep the path parseable: right after a
731/// digit, with the new number *not* itself starting with `-` (whose sign
732/// already disambiguates where the previous number ended, SVG's own
733/// "no separator needed before a negative number" rule — the "no
734/// redundant separators" #214 asks for). Right after a command letter
735/// (`M`/`l`/`z`) no separator is ever needed.
736fn push_num(d: &mut String, s: &str) {
737    let needs_sep =
738        matches!(d.chars().last(), Some(c) if c.is_ascii_digit()) && !s.starts_with('-');
739    if needs_sep {
740        d.push(',');
741    }
742    d.push_str(s);
743}
744
745/// Format `v` to `precision` decimals, then trim trailing zeros and a
746/// trailing `.` (`"5.0"` → `"5"`, `"1.20"` → `"1.2"`) — the SVG path
747/// grammar allows a bare integer anywhere a number is valid. Non-finite
748/// becomes `"0"`; `-0`-after-rounding (e.g. a tiny negative delta that
749/// rounds to zero) becomes `"0"` too, not `"-0"`.
750fn fmt_compact(v: f64, precision: u8) -> String {
751    if !v.is_finite() {
752        return "0".to_string();
753    }
754    let s = format!("{v:.*}", precision as usize);
755    let trimmed = if s.contains('.') {
756        s.trim_end_matches('0').trim_end_matches('.')
757    } else {
758        s.as_str()
759    };
760    match trimmed {
761        "" | "-" | "-0" => "0".to_string(),
762        other => other.to_string(),
763    }
764}
765
766fn finite_or(v: f64, fallback: f64) -> f64 {
767    if v.is_finite() {
768        v
769    } else {
770        fallback
771    }
772}
773
774/// ` {prefix}-opacity="{alpha:.3}"` (with its own leading space), or the
775/// empty string when `alpha` — after the usual non-finite fallback to fully
776/// opaque — formats to `"1.000"`. Full opacity is the SVG default, so
777/// omitting it keeps default output byte-identical; an explicit `alpha = 1`
778/// is indistinguishable from "not set" and also omits the no-op attribute.
779fn opacity_attr(prefix: &str, alpha: f64) -> String {
780    let a = finite_or(alpha, 1.0);
781    if format!("{a:.3}") == "1.000" {
782        String::new()
783    } else {
784        format!(" {prefix}-opacity=\"{a:.3}\"")
785    }
786}
787
788/// Root `<svg>` data attributes describing the trained position scales, for
789/// hosts that map screen coordinates back to data (zoom, crosshair, brushing):
790///
791/// - `data-xdomain="x0 x1"` / `data-ydomain="y0 y1"` — the data values at the
792///   panel's left/right (bottom/top) edges, i.e. the trained limits *after*
793///   expansion, for a continuous (numeric or date-time, epoch seconds) axis.
794///   Values are in the scale's transformed space (e.g. log10 units for
795///   `scale_x_log10`).
796/// - `data-domain="x0 x1 y0 y1"` — both of the above, emitted only when both
797///   axes are continuous.
798/// - `data-xlevels` / `data-ylevels` — a JSON array of the level labels for a
799///   discrete axis, in axis order.
800/// - `data-flip="true"` under `coord_flip` (x is then drawn vertically).
801///
802/// The `x`/`y` names refer to the x/y *aesthetics*. Omitted entirely for
803/// plots with free facet scales (each panel has its own domain).
804pub fn root_data_attrs(built: &crate::build::BuiltPlot) -> Vec<(String, String)> {
805    use crate::aes::Aesthetic;
806    let mut out = Vec::new();
807    if !built.panel_scales.is_empty() {
808        return out;
809    }
810    let axis = |aes: &Aesthetic| -> (Option<(f64, f64)>, Option<String>) {
811        match built.scales.get(aes) {
812            Some(s) if s.is_discrete() => {
813                let labels: Vec<String> = s
814                    .breaks()
815                    .into_iter()
816                    .map(|(_, l)| json_string(&l))
817                    .collect();
818                (None, Some(format!("[{}]", labels.join(","))))
819            }
820            Some(s) => (s.expanded_domain(), None),
821            None => (None, None),
822        }
823    };
824    let (xd, xl) = axis(&Aesthetic::X);
825    let (yd, yl) = axis(&Aesthetic::Y);
826    if let (Some((x0, x1)), Some((y0, y1))) = (xd, yd) {
827        out.push((
828            "data-domain".into(),
829            format!("{} {} {} {}", g(x0), g(x1), g(y0), g(y1)),
830        ));
831    }
832    if let Some((a, b)) = xd {
833        out.push(("data-xdomain".into(), format!("{} {}", g(a), g(b))));
834    }
835    if let Some((a, b)) = yd {
836        out.push(("data-ydomain".into(), format!("{} {}", g(a), g(b))));
837    }
838    if let Some(l) = xl {
839        out.push(("data-xlevels".into(), l));
840    }
841    if let Some(l) = yl {
842        out.push(("data-ylevels".into(), l));
843    }
844    if built.coord.is_flipped() {
845        out.push(("data-flip".into(), "true".into()));
846    }
847    out
848}
849
850/// Shortest round-trip formatting of a finite number (domains are filtered to
851/// finite values before this is called).
852fn g(v: f64) -> String {
853    format!("{v}")
854}
855
856/// Minimal JSON string literal (quotes, backslashes and control characters
857/// escaped); the result is attribute-escaped again on output.
858fn json_string(s: &str) -> String {
859    let mut out = String::with_capacity(s.len() + 2);
860    out.push('"');
861    for c in s.chars() {
862        match c {
863            '"' => out.push_str("\\\""),
864            '\\' => out.push_str("\\\\"),
865            '\n' => out.push_str("\\n"),
866            '\r' => out.push_str("\\r"),
867            '\t' => out.push_str("\\t"),
868            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
869            c => out.push(c),
870        }
871    }
872    out.push('"');
873    out
874}
875
876/// Allow-listed root-level attribute names for `GGPlot::svg_attr`/
877/// `PlotGrid::svg_attr`: the fixed names `role`, `id`, `class`, `lang`, or
878/// `data-*`/`aria-*` with the same charset-restricted suffix `data_attr`
879/// uses (`^[a-z0-9][a-z0-9-]*$`, excluding the reserved `part`/`geom`
880/// suffixes — irrelevant at the root, but no reason to special-case them
881/// out). Anything else is rejected.
882fn is_allowed_root_attr_name(name: &str) -> bool {
883    matches!(name, "role" | "id" | "class" | "lang")
884        || name
885            .strip_prefix("data-")
886            .is_some_and(crate::aes::is_valid_data_attr_name)
887        || name
888            .strip_prefix("aria-")
889            .is_some_and(crate::aes::is_valid_data_attr_name)
890}
891
892/// Validate and filter a caller's requested root `<svg>` attributes
893/// (`GGPlot::svg_attr`/`PlotGrid::svg_attr`), checked centrally here and
894/// used by both callers rather than re-implemented per type. A name outside
895/// [`is_allowed_root_attr_name`]'s allow-list, or one already present in
896/// `existing` (what ggplot-rs itself is about to write to the same root —
897/// e.g. `data-plot`/`data-domain`/`data-grid`, or `data-warnings` when
898/// `svg_warnings_attr` is also on), is dropped with a warning instead of
899/// silently overwriting or duplicating it. `label` names the caller
900/// (`"plot"`/`"grid"`) in the warning text.
901pub(crate) fn validate_root_attrs(
902    requested: &[(String, String)],
903    existing: &[(String, String)],
904    label: &str,
905    warnings: &mut Vec<String>,
906) -> Vec<(String, String)> {
907    let built_in: Vec<&str> = existing.iter().map(|(k, _)| k.as_str()).collect();
908    let mut used: Vec<&str> = built_in.clone();
909    let mut out = Vec::new();
910    for (name, value) in requested {
911        if !is_allowed_root_attr_name(name) {
912            warnings.push(format!(
913                "{label}: svg_attr(\"{name}\") is not on the allow-list (data-*, aria-*, role, id, class, lang) — ignored"
914            ));
915            continue;
916        }
917        if used.contains(&name.as_str()) {
918            // Review PR#141 nit #9: distinguish an actual collision with
919            // something ggplot-rs itself writes (`data-plot`, `data-grid`,
920            // …) from a *second* `svg_attr` call for the same name — the
921            // latter is the caller's own earlier value, not ggplot-rs's.
922            if built_in.contains(&name.as_str()) {
923                warnings.push(format!(
924                    "{label}: svg_attr(\"{name}\") collides with an attribute ggplot-rs already writes to the root — ignored"
925                ));
926            } else {
927                warnings.push(format!(
928                    "{label}: svg_attr(\"{name}\") was already set by an earlier svg_attr call — ignored"
929                ));
930            }
931            continue;
932        }
933        used.push(name.as_str());
934        out.push((name.clone(), value.clone()));
935    }
936    out
937}
938
939/// `["warning one", "warning two"]` — a minimal hand-rolled JSON array of
940/// strings for `svg_warnings_attr` (no serde dependency, kept out of the
941/// `sf`-feature dependency tree — see [`json_string`]).
942pub(crate) fn warnings_json(warnings: &[String]) -> String {
943    let items: Vec<String> = warnings.iter().map(|w| json_string(w)).collect();
944    format!("[{}]", items.join(","))
945}
946
947/// Defence-in-depth gate for `push_mark`'s `mark_attrs`: `^data-[a-z0-9][a-z0-9-]*$`.
948/// `geom::set_row_extras` only ever supplies already-validated names, but
949/// `set_mark_attrs` is a public `DrawBackend` method any caller (a custom
950/// `Geom` impl, or misuse from outside the crate) could invoke directly with
951/// an arbitrary string, so this is checked again right before it reaches
952/// the wire — never trust a single validation layer for attribute-name
953/// injection.
954fn is_valid_mark_attr_name(name: &str) -> bool {
955    match name.strip_prefix("data-") {
956        Some(suffix) => crate::aes::is_valid_data_attr_name(suffix),
957        None => false,
958    }
959}
960
961/// `pointer-events` keyword values accepted by
962/// [`validate_layer_svg_attr`] (#221) — SVG 1.1's full list.
963const POINTER_EVENTS_VALUES: &[&str] = &[
964    "auto",
965    "none",
966    "visiblePainted",
967    "visibleFill",
968    "visibleStroke",
969    "visible",
970    "painted",
971    "fill",
972    "stroke",
973    "all",
974    "bounding-box",
975];
976
977/// Validate one `GGPlot::layer_svg_attr(name, value)` (#221). The
978/// allow-list: `class` (space-separated tokens, each
979/// `[A-Za-z][A-Za-z0-9_-]*`; invalid tokens are dropped, the rest kept),
980/// `aria-hidden` (`"true"`/`"false"`), `pointer-events` (an SVG keyword,
981/// e.g. `"none"`), and `data-*` (same name rules as `Aes::data_attr`).
982/// Everything else — notably `style`, `on*` handlers, `id` — is rejected.
983/// Returns the attribute to keep (if any) and pushes a warning for anything
984/// dropped.
985pub(crate) fn validate_layer_svg_attr(
986    name: &str,
987    value: &str,
988    warnings: &mut Vec<String>,
989) -> Option<(String, String)> {
990    let keep = |v: &str| Some((name.to_string(), v.to_string()));
991    match name {
992        "class" => {
993            let (ok, bad) = crate::aes::split_class_tokens(value);
994            if !bad.is_empty() {
995                warnings.push(format!(
996                    "layer_svg_attr(\"class\"): invalid class token(s) {bad:?} (must match [A-Za-z][A-Za-z0-9_-]*) — dropped"
997                ));
998            }
999            if ok.is_empty() {
1000                None
1001            } else {
1002                keep(&ok.join(" "))
1003            }
1004        }
1005        "aria-hidden" if matches!(value, "true" | "false") => keep(value),
1006        "pointer-events" if POINTER_EVENTS_VALUES.contains(&value) => keep(value),
1007        "aria-hidden" | "pointer-events" => {
1008            warnings.push(format!(
1009                "layer_svg_attr(\"{name}\"): value {value:?} is not allowed — ignored"
1010            ));
1011            None
1012        }
1013        _ if is_valid_mark_attr_name(name) => keep(value),
1014        _ => {
1015            warnings.push(format!(
1016                "layer_svg_attr(\"{name}\") is not on the allow-list (class, aria-hidden, pointer-events, data-*) — ignored"
1017            ));
1018            None
1019        }
1020    }
1021}
1022
1023fn rgb((r, g, b): (u8, u8, u8)) -> String {
1024    format!("#{r:02X}{g:02X}{b:02X}")
1025}
1026
1027/// Escape text for use in SVG element *content* (`<text>`/`<title>`/
1028/// `<tspan>` text nodes). Quotes are escaped too: a `"` inside content is
1029/// harmless to SVG itself, but the document is routinely inlined into
1030/// HTML, where an unescaped `"` closing out of an attribute could matter
1031/// if this ever moved context — escaping it is free and one less thing to
1032/// reason about. `\t`/`\n`/`\r` are kept literal here (see `escape_attr`,
1033/// its sibling for *attribute* values, for why those three need different
1034/// treatment there). Characters that are not legal in XML 1.0 (C0
1035/// controls other than tab/LF/CR) are dropped so the document stays
1036/// well-formed.
1037fn escape(s: &str) -> String {
1038    escape_impl(s, false)
1039}
1040
1041/// Escape text for use in a *quoted SVG attribute value* (#226) — a
1042/// `data-*`/`href`/`class`/`font-family`/etc. attribute, as opposed to
1043/// `escape`'s element content. Everything `escape` does, plus `\t`/`\n`/
1044/// `\r` entity-escaped (`&#9;`/`&#10;`/`&#13;`) instead of left literal.
1045/// XML attribute-value normalisation collapses an unescaped tab/newline/CR
1046/// inside an attribute value to a plain space (the SVG spec defers to
1047/// XML 1.0 §3.3.3 here, and browsers follow it), so a caller reading a
1048/// multi-line `data-label` or similar back loses the original character
1049/// unless it's entity-escaped — entity-escaping round-trips it exactly
1050/// through any XML/DOM parser.
1051fn escape_attr(s: &str) -> String {
1052    escape_impl(s, true)
1053}
1054
1055fn escape_impl(s: &str, in_attr: bool) -> String {
1056    let mut out = String::with_capacity(s.len());
1057    for c in s.chars() {
1058        match c {
1059            '&' => out.push_str("&amp;"),
1060            '<' => out.push_str("&lt;"),
1061            '>' => out.push_str("&gt;"),
1062            '"' => out.push_str("&quot;"),
1063            '\'' => out.push_str("&#39;"),
1064            '\t' if in_attr => out.push_str("&#9;"),
1065            '\n' if in_attr => out.push_str("&#10;"),
1066            '\r' if in_attr => out.push_str("&#13;"),
1067            '\t' | '\n' | '\r' => out.push(c),
1068            c if (c as u32) < 0x20 || c == '\u{FFFE}' || c == '\u{FFFF}' => {}
1069            c => out.push(c),
1070        }
1071    }
1072    out
1073}
1074
1075fn points(pts: &[(f64, f64)]) -> String {
1076    pts.iter()
1077        .map(|(x, y)| format!("{x:.2},{y:.2}"))
1078        .collect::<Vec<_>>()
1079        .join(" ")
1080}
1081
1082fn finite_pts(pts: &[(f64, f64)]) -> Vec<(f64, f64)> {
1083    pts.iter()
1084        .copied()
1085        .filter(|(x, y)| x.is_finite() && y.is_finite())
1086        .collect()
1087}
1088
1089/// Split `pts` into maximal runs of consecutive finite vertices, dropping
1090/// any run shorter than 2 points. Matches ggplot2's NA-break handling for
1091/// lines: a non-finite vertex breaks the line instead of being silently
1092/// skipped, which would otherwise visually bridge across the gap — but a
1093/// single remaining observation isn't a line segment either (ggplot2 drops
1094/// a one-point path piece the same way), so it draws nothing rather than
1095/// an invisible 1-point `<polyline>` that would still carry a `<title>`
1096/// and hover attributes.
1097fn finite_runs(pts: &[(f64, f64)]) -> Vec<Vec<(f64, f64)>> {
1098    let mut runs = Vec::new();
1099    let mut cur: Vec<(f64, f64)> = Vec::new();
1100    for &(x, y) in pts {
1101        if x.is_finite() && y.is_finite() {
1102            cur.push((x, y));
1103        } else if !cur.is_empty() {
1104            if cur.len() >= 2 {
1105                runs.push(std::mem::take(&mut cur));
1106            } else {
1107                cur.clear();
1108            }
1109        }
1110    }
1111    if cur.len() >= 2 {
1112        runs.push(cur);
1113    }
1114    runs
1115}
1116
1117impl DrawBackend for SvgBackend {
1118    fn plot_area(&self) -> Rect {
1119        self.plot_area.clone()
1120    }
1121    fn total_area(&self) -> Rect {
1122        self.total_area.clone()
1123    }
1124
1125    fn set_tooltip(&mut self, tooltip: Option<String>) {
1126        self.tooltip = tooltip;
1127    }
1128
1129    fn set_mark_axis(&mut self, key: Option<String>) {
1130        self.axis_key = key;
1131    }
1132
1133    fn set_mark_series(&mut self, series: Option<String>) {
1134        self.series_key = series;
1135    }
1136
1137    fn set_mark_value(&mut self, value: Option<String>) {
1138        self.value_key = value;
1139    }
1140
1141    fn set_mark_geom(&mut self, geom: Option<&str>) {
1142        self.mark_geom = geom.map(str::to_string);
1143    }
1144
1145    fn warn(&mut self, message: String) {
1146        self.warnings.push(message);
1147    }
1148
1149    fn set_mark_part(&mut self, part: Option<&str>) {
1150        self.mark_part = part.map(|s| s.to_string());
1151    }
1152
1153    fn set_mark_attrs(&mut self, attrs: Vec<(String, String)>) {
1154        self.mark_attrs = attrs;
1155    }
1156
1157    fn wants_formatted_values(&self) -> bool {
1158        self.formatted_values
1159    }
1160
1161    fn set_mark_fvalue(&mut self, value: Option<String>) {
1162        self.mark_fvalue = value;
1163    }
1164
1165    fn set_mark_fx(&mut self, value: Option<String>) {
1166        self.mark_fx = value;
1167    }
1168
1169    fn set_mark_class(&mut self, class: Option<String>) {
1170        self.mark_user_class = class;
1171    }
1172
1173    fn set_mark_group(&mut self, group: Option<(String, String)>) {
1174        self.mark_group = group;
1175    }
1176
1177    fn set_layer_attrs(&mut self, attrs: Vec<(String, String)>) {
1178        self.layer_attrs = attrs;
1179    }
1180
1181    fn draw_circle(
1182        &mut self,
1183        (cx, cy): (f64, f64),
1184        radius: f64,
1185        style: &PointStyle,
1186    ) -> Result<(), RenderError> {
1187        if !(cx.is_finite() && cy.is_finite() && radius.is_finite()) {
1188            return Ok(());
1189        }
1190        let attrs = format!(
1191            "cx=\"{cx:.2}\" cy=\"{cy:.2}\" r=\"{:.2}\" fill=\"{}\"{}",
1192            radius.max(0.0),
1193            rgb(style.color),
1194            opacity_attr("fill", style.alpha)
1195        );
1196        self.push_mark("circle", "point", &attrs);
1197        Ok(())
1198    }
1199
1200    /// Native shapes: filled polygons for square/triangle/diamond, a single
1201    /// stroked `<path>` for the `+` / `×` glyphs (so each point stays one
1202    /// hoverable mark carrying its `data-*` attributes).
1203    fn draw_shape(
1204        &mut self,
1205        (cx, cy): (f64, f64),
1206        radius: f64,
1207        style: &PointStyle,
1208    ) -> Result<(), RenderError> {
1209        if !(cx.is_finite() && cy.is_finite() && radius.is_finite()) {
1210            return Ok(());
1211        }
1212        let r = radius.max(0.0);
1213        let color = rgb(style.color);
1214        let fill_opacity = opacity_attr("fill", style.alpha);
1215        let stroke_opacity = opacity_attr("stroke", style.alpha);
1216        let poly = |pts: &[(f64, f64)]| {
1217            if style.filled {
1218                format!("points=\"{}\" fill=\"{color}\"{fill_opacity}", points(pts))
1219            } else {
1220                format!(
1221                    "points=\"{}\" fill=\"none\" stroke=\"{color}\"{stroke_opacity}",
1222                    points(pts)
1223                )
1224            }
1225        };
1226        match style.shape {
1227            PointShape::Circle => return self.draw_circle((cx, cy), radius, style),
1228            PointShape::Square => {
1229                let a = poly(&[
1230                    (cx - r, cy - r),
1231                    (cx + r, cy - r),
1232                    (cx + r, cy + r),
1233                    (cx - r, cy + r),
1234                ]);
1235                self.push_mark("polygon", "point", &a);
1236            }
1237            PointShape::Triangle => {
1238                let a = poly(&[(cx, cy - r), (cx + r, cy + r), (cx - r, cy + r)]);
1239                self.push_mark("polygon", "point", &a);
1240            }
1241            PointShape::Diamond => {
1242                let a = poly(&[(cx, cy - r), (cx + r, cy), (cx, cy + r), (cx - r, cy)]);
1243                self.push_mark("polygon", "point", &a);
1244            }
1245            PointShape::Plus | PointShape::Cross => {
1246                let d = if style.shape == PointShape::Plus {
1247                    format!(
1248                        "M{:.2} {cy:.2}H{:.2}M{cx:.2} {:.2}V{:.2}",
1249                        cx - r,
1250                        cx + r,
1251                        cy - r,
1252                        cy + r
1253                    )
1254                } else {
1255                    format!(
1256                        "M{:.2} {:.2}L{:.2} {:.2}M{:.2} {:.2}L{:.2} {:.2}",
1257                        cx - r,
1258                        cy - r,
1259                        cx + r,
1260                        cy + r,
1261                        cx - r,
1262                        cy + r,
1263                        cx + r,
1264                        cy - r
1265                    )
1266                };
1267                let a = format!(
1268                    "d=\"{d}\" fill=\"none\" stroke=\"{color}\" stroke-width=\"{:.2}\"{stroke_opacity}",
1269                    (r / 2.5).clamp(1.0, 3.0)
1270                );
1271                self.push_mark("path", "point", &a);
1272            }
1273        }
1274        Ok(())
1275    }
1276
1277    fn draw_line(&mut self, pts: &[(f64, f64)], style: &LineStyle) -> Result<(), RenderError> {
1278        // Split at non-finite vertices instead of silently dropping them:
1279        // dropping-and-joining would visually bridge the gap, but ggplot2
1280        // treats a non-finite (NA) vertex as a break in the line (see
1281        // `finite_runs`). Each run becomes its own `<polyline>` mark, with
1282        // the same style and the same data-x/series/value/part attrs.
1283        for run in finite_runs(pts) {
1284            self.draw_line_run(&run, style);
1285        }
1286        Ok(())
1287    }
1288
1289    /// Emit a native `<path d="M.. Q..">` quadratic Bézier (one real curve
1290    /// instead of a flattened polyline). Non-finite input draws nothing.
1291    fn draw_quadratic(
1292        &mut self,
1293        p0: (f64, f64),
1294        ctrl: (f64, f64),
1295        p1: (f64, f64),
1296        style: &LineStyle,
1297    ) -> Result<(), RenderError> {
1298        if !(p0.0.is_finite()
1299            && p0.1.is_finite()
1300            && ctrl.0.is_finite()
1301            && ctrl.1.is_finite()
1302            && p1.0.is_finite()
1303            && p1.1.is_finite())
1304        {
1305            return Ok(());
1306        }
1307        let dash = dasharray_attr(&style.linetype);
1308        let linecap = match style.lineend {
1309            LineEnd::Butt => "",
1310            LineEnd::Round => " stroke-linecap=\"round\"",
1311            LineEnd::Square => " stroke-linecap=\"square\"",
1312        };
1313        let d = format!(
1314            "M{:.2} {:.2} Q{:.2} {:.2} {:.2} {:.2}",
1315            p0.0, p0.1, ctrl.0, ctrl.1, p1.0, p1.1
1316        );
1317        let attrs = format!(
1318            "d=\"{d}\" fill=\"none\" stroke=\"{}\" stroke-width=\"{:.2}\"{}{}{}",
1319            rgb(style.color),
1320            finite_or(style.width, 1.0),
1321            opacity_attr("stroke", style.alpha),
1322            dash,
1323            linecap
1324        );
1325        self.push_mark("path", "line", &attrs);
1326        Ok(())
1327    }
1328
1329    fn draw_rect(
1330        &mut self,
1331        top_left: (f64, f64),
1332        bottom_right: (f64, f64),
1333        style: &RectStyle,
1334    ) -> Result<(), RenderError> {
1335        let fill = style.fill.map(rgb).unwrap_or_else(|| "none".into());
1336        self.rect_markup(top_left, bottom_right, &fill, style)
1337    }
1338
1339    /// #206: same markup [`draw_rect`](Self::draw_rect) builds, filled
1340    /// with `pattern`'s registered `url(#…)` instead of `style.fill`.
1341    fn draw_rect_pattern(
1342        &mut self,
1343        top_left: (f64, f64),
1344        bottom_right: (f64, f64),
1345        pattern: &FillPattern,
1346        style: &RectStyle,
1347    ) -> Result<(), RenderError> {
1348        let fill = self.pattern_url(pattern);
1349        self.rect_markup(top_left, bottom_right, &fill, style)
1350    }
1351
1352    fn draw_polygon(&mut self, pts: &[(f64, f64)], style: &RectStyle) -> Result<(), RenderError> {
1353        let fill = style.fill.map(rgb).unwrap_or_else(|| "none".into());
1354        self.polygon_markup(pts, &fill, style)
1355    }
1356
1357    /// #206: same markup [`draw_polygon`](Self::draw_polygon) builds,
1358    /// filled with `pattern`'s registered `url(#…)` instead of `style.fill`.
1359    fn draw_polygon_pattern(
1360        &mut self,
1361        pts: &[(f64, f64)],
1362        pattern: &FillPattern,
1363        style: &RectStyle,
1364    ) -> Result<(), RenderError> {
1365        let fill = self.pattern_url(pattern);
1366        self.polygon_markup(pts, &fill, style)
1367    }
1368
1369    /// One `<path>`, every ring a subpath (`M…Z`), `fill-rule="evenodd"` so
1370    /// holes (rings after the first) are cut out of the fill — SVG's native
1371    /// equivalent of the even-odd rasteriser the pixel/plotters backends run
1372    /// (#178). A ring with fewer than 3 finite points is dropped; the whole
1373    /// shape draws nothing when every ring is.
1374    fn draw_polygon_rings(
1375        &mut self,
1376        rings: &[Vec<(f64, f64)>],
1377        style: &RectStyle,
1378    ) -> Result<(), RenderError> {
1379        let fill = style.fill.map(rgb).unwrap_or_else(|| "none".into());
1380        self.polygon_rings_markup(rings, &fill, style)
1381    }
1382
1383    /// #206: same markup [`draw_polygon_rings`](Self::draw_polygon_rings)
1384    /// builds (holes cut out under the same even-odd rule), filled with
1385    /// `pattern`'s registered `url(#…)` instead of `style.fill`.
1386    fn draw_polygon_rings_pattern(
1387        &mut self,
1388        rings: &[Vec<(f64, f64)>],
1389        pattern: &FillPattern,
1390        style: &RectStyle,
1391    ) -> Result<(), RenderError> {
1392        let fill = self.pattern_url(pattern);
1393        self.polygon_rings_markup(rings, &fill, style)
1394    }
1395
1396    /// #214: with `precision` set, writes a compact `d` (absolute `M` per
1397    /// ring, then one `l` with implicit-repeat relative coordinate pairs,
1398    /// rounded to `precision` decimals) instead of the one-`L`-per-point
1399    /// absolute encoding [`Self::polygon_rings_markup`] builds. `None`
1400    /// falls back to the trait default (today's encoding, unchanged).
1401    fn draw_polygon_rings_precise(
1402        &mut self,
1403        rings: &[Vec<(f64, f64)>],
1404        pattern: Option<&FillPattern>,
1405        precision: Option<u8>,
1406        style: &RectStyle,
1407    ) -> Result<(), RenderError> {
1408        let Some(precision) = precision else {
1409            return match pattern {
1410                Some(p) => self.draw_polygon_rings_pattern(rings, p, style),
1411                None => self.draw_polygon_rings(rings, style),
1412            };
1413        };
1414        let d = compact_rings_d(rings, precision);
1415        if d.is_empty() {
1416            return Ok(());
1417        }
1418        let fill = match pattern {
1419            Some(p) => self.pattern_url(p),
1420            None => style.fill.map(rgb).unwrap_or_else(|| "none".into()),
1421        };
1422        let stroke = style.stroke.map(rgb).unwrap_or_else(|| "none".into());
1423        let attrs = format!(
1424            "d=\"{d}\" fill-rule=\"evenodd\" fill=\"{fill}\"{} stroke=\"{stroke}\" stroke-width=\"{:.2}\"",
1425            opacity_attr("fill", style.alpha),
1426            finite_or(style.stroke_width, 0.0)
1427        );
1428        self.push_mark("path", "band", &attrs);
1429        Ok(())
1430    }
1431
1432    fn draw_text(
1433        &mut self,
1434        text: &str,
1435        pos: (f64, f64),
1436        style: &TextStyle,
1437    ) -> Result<(), RenderError> {
1438        self.draw_text_with_title(text, pos, style, None)
1439    }
1440
1441    /// #159: with `title`, wraps the `<text>` in a `<title>` child
1442    /// carrying the full (escaped) label — e.g. a legend key truncated
1443    /// with an ellipsis to fit a vertical multi-column grid.
1444    fn draw_text_with_title(
1445        &mut self,
1446        text: &str,
1447        (x, y): (f64, f64),
1448        style: &TextStyle,
1449        title: Option<&str>,
1450    ) -> Result<(), RenderError> {
1451        if !(x.is_finite() && y.is_finite()) {
1452            return Ok(());
1453        }
1454        let anchor = match style.anchor {
1455            TextAnchor::Start => "start",
1456            TextAnchor::Middle => "middle",
1457            TextAnchor::End => "end",
1458        };
1459        let family = escape_attr(style.family.as_deref().unwrap_or("sans-serif"));
1460        let weight = if style.face == FontFace::Bold {
1461            " font-weight=\"bold\""
1462        } else {
1463            ""
1464        };
1465        let fstyle = if style.face == FontFace::Italic {
1466            " font-style=\"italic\""
1467        } else {
1468            ""
1469        };
1470        let halo = text_halo_attrs(style);
1471        let angle = finite_or(style.angle, 0.0);
1472        let transform = if angle.abs() > 0.01 {
1473            format!(" transform=\"rotate({angle:.1} {x:.2} {y:.2})\"")
1474        } else {
1475            String::new()
1476        };
1477        let class = format!(
1478            "{}{}{}",
1479            self.text_data_attrs(),
1480            self.layer_flag_attrs(false),
1481            self.mark_class("text")
1482        );
1483        let title_tag = match title {
1484            Some(t) => format!("<title>{}</title>", escape(t)),
1485            None => String::new(),
1486        };
1487        let markup = format!(
1488            "<text x=\"{x:.2}\" y=\"{y:.2}\" font-size=\"{:.2}\" text-anchor=\"{anchor}\" \
1489             dominant-baseline=\"middle\" font-family=\"{family}\"{weight}{fstyle}{halo} fill=\"{}\"{transform}{class}>{title_tag}{}</text>",
1490            finite_or(style.size, 12.0),
1491            rgb(style.color),
1492            escape(text)
1493        );
1494        self.emit(&markup);
1495        Ok(())
1496    }
1497
1498    /// #196: each linked run becomes `<a href="…" target="_blank"
1499    /// rel="noopener"><tspan text-decoration="underline">…</tspan></a>`
1500    /// (`rel="noopener"` so the opened tab can't reach back via
1501    /// `window.opener`); a plain run is a bare `<tspan>`. All runs sit
1502    /// inside one `<text>`, so `style.anchor` aligns their combined width
1503    /// exactly as it would a single run's.
1504    fn draw_text_runs(
1505        &mut self,
1506        runs: &[(&str, Option<&str>)],
1507        (x, y): (f64, f64),
1508        style: &TextStyle,
1509    ) -> Result<(), RenderError> {
1510        if !(x.is_finite() && y.is_finite()) {
1511            return Ok(());
1512        }
1513        let anchor = match style.anchor {
1514            TextAnchor::Start => "start",
1515            TextAnchor::Middle => "middle",
1516            TextAnchor::End => "end",
1517        };
1518        let family = escape_attr(style.family.as_deref().unwrap_or("sans-serif"));
1519        let weight = if style.face == FontFace::Bold {
1520            " font-weight=\"bold\""
1521        } else {
1522            ""
1523        };
1524        let fstyle = if style.face == FontFace::Italic {
1525            " font-style=\"italic\""
1526        } else {
1527            ""
1528        };
1529        let halo = text_halo_attrs(style);
1530        let angle = finite_or(style.angle, 0.0);
1531        let transform = if angle.abs() > 0.01 {
1532            format!(" transform=\"rotate({angle:.1} {x:.2} {y:.2})\"")
1533        } else {
1534            String::new()
1535        };
1536        let class = format!(
1537            "{}{}{}",
1538            self.text_data_attrs(),
1539            self.layer_flag_attrs(false),
1540            self.mark_class("text")
1541        );
1542        let mut inner = String::new();
1543        for (text, href) in runs {
1544            match href {
1545                Some(u) => inner.push_str(&format!(
1546                    "<a href=\"{}\" target=\"_blank\" rel=\"noopener\"><tspan text-decoration=\"underline\">{}</tspan></a>",
1547                    escape_attr(u),
1548                    escape(text)
1549                )),
1550                None => inner.push_str(&format!("<tspan>{}</tspan>", escape(text))),
1551            }
1552        }
1553        let markup = format!(
1554            "<text x=\"{x:.2}\" y=\"{y:.2}\" font-size=\"{:.2}\" text-anchor=\"{anchor}\" \
1555             dominant-baseline=\"middle\" font-family=\"{family}\"{weight}{fstyle}{halo} fill=\"{}\"{transform}{class}>{inner}</text>",
1556            finite_or(style.size, 12.0),
1557            rgb(style.color),
1558        );
1559        self.emit(&markup);
1560        Ok(())
1561    }
1562}
1563
1564/// #229: the `stroke`/`stroke-width`/`stroke-linejoin`/`paint-order`
1565/// attributes for a text halo (ggrepel's `bg.colour`/`bg.r`;
1566/// `shadowtext`'s `bg.col`/`bg.r`), or `""` when `style.bg_colour` is
1567/// `None` or `style.bg_r` is non-finite or `<= 0.0` — so the default
1568/// (`bg_colour: None`) keeps existing output byte-identical. The stroke
1569/// paints *under* the fill (`paint-order="stroke"`), so the halo never
1570/// overlaps the glyph's own fill colour. `rgb()` already allow-lists its
1571/// output to `#RRGGBB` (#221's convention: every attribute value stays
1572/// escaped/allow-listed, never a raw caller string).
1573fn text_halo_attrs(style: &TextStyle) -> String {
1574    let Some(bg) = style.bg_colour else {
1575        return String::new();
1576    };
1577    let r = finite_or(style.bg_r, 0.0);
1578    if r <= 0.0 {
1579        return String::new();
1580    }
1581    format!(
1582        " stroke=\"{}\" stroke-width=\"{:.2}\" stroke-linejoin=\"round\" paint-order=\"stroke\"",
1583        rgb(bg),
1584        2.0 * r
1585    )
1586}
1587
1588#[cfg(test)]
1589mod tests {
1590    use super::*;
1591    use crate::render::Rect;
1592
1593    /// #221: the backend re-checks everything callers hand it directly
1594    /// through the public `DrawBackend` setters (defence in depth).
1595    #[test]
1596    fn issue221_backend_rechecks_class_group_and_layer_attrs() {
1597        let mut b = backend();
1598        b.set_mark_geom(Some("point"));
1599        b.set_mark_class(Some("ok \"bad 1x ok".into()));
1600        b.set_layer_attrs(vec![
1601            ("aria-hidden".into(), "maybe".into()),
1602            ("pointer-events".into(), "none\" onload=\"x".into()),
1603            ("style".into(), "fill:red".into()),
1604            ("class".into(), "lay".into()),
1605        ]);
1606        b.set_mark_group(Some(("onclick".into(), "x".into())));
1607        b.draw_circle((1.0, 1.0), 1.0, &PointStyle::default())
1608            .unwrap();
1609        b.set_mark_group(Some(("data-of".into(), "n".into())));
1610        let runs = [("a", None), ("b", Some("https://e.x"))];
1611        b.draw_text_runs(&runs, (5.0, 5.0), &TextStyle::default())
1612            .unwrap();
1613        b.set_mark_group(None);
1614        b.set_mark_geom(None);
1615        let body = b.body().to_string();
1616        assert!(
1617            body.contains(r#"class="gg-mark gg-point lay ok""#),
1618            "{body}"
1619        );
1620        assert!(!body.contains("bad") && !body.contains("1x"), "{body}");
1621        assert!(
1622            !body.contains("aria-hidden") && !body.contains("onload"),
1623            "{body}"
1624        );
1625        assert!(
1626            !body.contains("style=") && !body.contains("onclick"),
1627            "{body}"
1628        );
1629        assert!(body.contains(r#"<g data-of="n"><text"#), "{body}");
1630        assert!(body.contains("gg-text lay ok"), "{body}");
1631        // A later finish() has nothing left to splice.
1632        assert!(b.finish().contains(r#"<g data-of="n">"#));
1633    }
1634
1635    fn backend() -> SvgBackend {
1636        SvgBackend::new(
1637            100,
1638            100,
1639            Rect {
1640                x: 0.0,
1641                y: 0.0,
1642                width: 100.0,
1643                height: 100.0,
1644            },
1645        )
1646    }
1647
1648    #[test]
1649    fn draw_quadratic_emits_a_path_with_m_and_q() {
1650        let mut b = backend();
1651        b.draw_quadratic((0.0, 0.0), (10.0, 20.0), (20.0, 0.0), &LineStyle::default())
1652            .unwrap();
1653        let svg = b.finish();
1654        assert!(svg.contains("<path"));
1655        assert!(svg.contains("d=\"M0.00 0.00 Q10.00 20.00 20.00 0.00\""));
1656        assert!(!svg.contains("NaN"));
1657        assert!(!svg.contains("inf"));
1658    }
1659
1660    #[test]
1661    fn draw_quadratic_drops_non_finite_input() {
1662        let mut b = backend();
1663        b.draw_quadratic(
1664            (f64::NAN, 0.0),
1665            (10.0, 20.0),
1666            (20.0, 0.0),
1667            &LineStyle::default(),
1668        )
1669        .unwrap();
1670        b.draw_quadratic(
1671            (0.0, 0.0),
1672            (f64::INFINITY, 20.0),
1673            (20.0, 0.0),
1674            &LineStyle::default(),
1675        )
1676        .unwrap();
1677        let svg = b.finish();
1678        assert!(!svg.contains("<path"));
1679    }
1680
1681    #[test]
1682    fn draw_line_omits_linecap_by_default_but_writes_it_when_set() {
1683        let mut b = backend();
1684        b.draw_line(&[(0.0, 0.0), (10.0, 10.0)], &LineStyle::default())
1685            .unwrap();
1686        let svg = b.finish();
1687        assert!(!svg.contains("stroke-linecap"));
1688
1689        let mut b2 = backend();
1690        let style = LineStyle {
1691            lineend: LineEnd::Round,
1692            ..Default::default()
1693        };
1694        b2.draw_line(&[(0.0, 0.0), (10.0, 10.0)], &style).unwrap();
1695        let svg2 = b2.finish();
1696        assert!(svg2.contains("stroke-linecap=\"round\""));
1697    }
1698
1699    #[test]
1700    fn square_linecap_is_written_on_both_draw_line_and_draw_quadratic() {
1701        let style = LineStyle {
1702            lineend: LineEnd::Square,
1703            ..Default::default()
1704        };
1705        let mut b = backend();
1706        b.draw_line(&[(0.0, 0.0), (10.0, 10.0)], &style).unwrap();
1707        assert!(b.finish().contains("stroke-linecap=\"square\""));
1708
1709        let mut b2 = backend();
1710        b2.draw_quadratic((0.0, 0.0), (5.0, 10.0), (10.0, 0.0), &style)
1711            .unwrap();
1712        assert!(b2.finish().contains("stroke-linecap=\"square\""));
1713    }
1714
1715    /// `push_mark`'s defence-in-depth gate: even if an invalid name reaches
1716    /// `set_mark_attrs` directly (bypassing `Aes::data_attr`'s own
1717    /// validation — `set_mark_attrs` is a public `DrawBackend` method any
1718    /// caller could invoke), it's dropped before it reaches the wire.
1719    #[test]
1720    fn set_mark_attrs_defence_in_depth_drops_invalid_names() {
1721        let mut b = backend();
1722        b.set_mark_attrs(vec![
1723            ("not-data-prefixed".to_string(), "x".to_string()),
1724            ("data-".to_string(), "x".to_string()), // empty suffix
1725            ("data-Upper".to_string(), "x".to_string()), // uppercase
1726            ("data-ok".to_string(), "valid".to_string()), // the one legitimate entry
1727        ]);
1728        b.draw_circle((1.0, 1.0), 1.0, &PointStyle::default())
1729            .unwrap();
1730        let svg = b.finish();
1731        assert!(!svg.contains("not-data-prefixed"));
1732        assert!(!svg.contains("data-Upper"));
1733        assert!(!svg.contains("data-=\""));
1734        assert!(svg.contains("data-ok=\"valid\""));
1735    }
1736
1737    // #206: pattern fills.
1738
1739    fn stripe() -> FillPattern {
1740        FillPattern::Stripe {
1741            fg: (10, 20, 30),
1742            bg: (200, 210, 220),
1743            size_px: 4.0,
1744            angle_deg: 45.0,
1745        }
1746    }
1747
1748    fn pattern_id(svg: &str) -> String {
1749        let i = svg.find("<pattern id=\"").unwrap() + "<pattern id=\"".len();
1750        svg[i..i + svg[i..].find('"').unwrap()].to_string()
1751    }
1752
1753    #[test]
1754    fn a_pattern_fill_registers_one_defs_pattern_and_references_it() {
1755        let mut b = backend();
1756        b.draw_rect_pattern((0.0, 0.0), (10.0, 10.0), &stripe(), &RectStyle::default())
1757            .unwrap();
1758        let svg = b.finish();
1759        assert_eq!(svg.matches("<pattern ").count(), 1, "{svg}");
1760        let id = pattern_id(&svg);
1761        assert!(
1762            svg.contains(&format!("fill=\"url(#{id})\"")),
1763            "rect references the registered pattern id {id}: {svg}"
1764        );
1765    }
1766
1767    #[test]
1768    fn a_rounded_rect_pattern_still_rounds_its_corners() {
1769        let mut b = backend();
1770        b.draw_rect_pattern(
1771            (0.0, 0.0),
1772            (10.0, 10.0),
1773            &stripe(),
1774            &RectStyle {
1775                corner_radius: 3.0,
1776                ..Default::default()
1777            },
1778        )
1779        .unwrap();
1780        let svg = b.finish();
1781        assert!(svg.contains("rx=\"3.00\""), "{svg}");
1782        assert!(svg.contains("fill=\"url(#"), "{svg}");
1783    }
1784
1785    #[test]
1786    fn an_equal_pattern_reuses_the_same_defs_entry() {
1787        let mut b = backend();
1788        b.draw_rect_pattern((0.0, 0.0), (5.0, 5.0), &stripe(), &RectStyle::default())
1789            .unwrap();
1790        b.draw_polygon_pattern(
1791            &[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)],
1792            &stripe(),
1793            &RectStyle::default(),
1794        )
1795        .unwrap();
1796        let svg = b.finish();
1797        assert_eq!(
1798            svg.matches("<pattern ").count(),
1799            1,
1800            "two equal patterns share one <defs> entry: {svg}"
1801        );
1802    }
1803
1804    #[test]
1805    fn different_patterns_get_their_own_defs_entries() {
1806        let mut b = backend();
1807        b.draw_rect_pattern((0.0, 0.0), (5.0, 5.0), &stripe(), &RectStyle::default())
1808            .unwrap();
1809        let other = FillPattern::Stripe {
1810            fg: (1, 1, 1),
1811            bg: (2, 2, 2),
1812            size_px: 8.0,
1813            angle_deg: 0.0,
1814        };
1815        b.draw_rect_pattern((0.0, 0.0), (5.0, 5.0), &other, &RectStyle::default())
1816            .unwrap();
1817        let svg = b.finish();
1818        assert_eq!(svg.matches("<pattern ").count(), 2, "{svg}");
1819    }
1820
1821    #[test]
1822    fn no_pattern_used_means_no_defs_block_at_all() {
1823        let mut b = backend();
1824        b.draw_rect((0.0, 0.0), (5.0, 5.0), &RectStyle::default())
1825            .unwrap();
1826        let svg = b.finish();
1827        assert!(!svg.contains("<defs>"), "{svg}");
1828    }
1829
1830    #[test]
1831    fn pattern_fill_cuts_out_holes_like_a_flat_fill_does() {
1832        let mut b = backend();
1833        let outer = vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)];
1834        let hole = vec![(2.0, 2.0), (8.0, 2.0), (8.0, 8.0), (2.0, 8.0)];
1835        b.draw_polygon_rings_pattern(&[outer, hole], &stripe(), &RectStyle::default())
1836            .unwrap();
1837        let svg = b.finish();
1838        assert!(svg.contains("fill-rule=\"evenodd\""), "{svg}");
1839        assert_eq!(
1840            svg.matches("<path").count(),
1841            1,
1842            "one path, both rings: {svg}"
1843        );
1844    }
1845
1846    #[test]
1847    fn two_backends_on_one_page_never_collide_on_a_pattern_id() {
1848        let mut a = backend();
1849        a.draw_rect_pattern((0.0, 0.0), (5.0, 5.0), &stripe(), &RectStyle::default())
1850            .unwrap();
1851        let mut b = backend();
1852        b.draw_rect_pattern((0.0, 0.0), (5.0, 5.0), &stripe(), &RectStyle::default())
1853            .unwrap();
1854        let (sa, sb) = (a.finish(), b.finish());
1855        assert_ne!(pattern_id(&sa), pattern_id(&sb), "{sa}\n{sb}");
1856    }
1857
1858    /// #214: decode a [`compact_rings_d`]-style `d` back into absolute
1859    /// points, for the round-trip test below. Relies on the same
1860    /// separator rule [`push_num`] writes: a number starts right after a
1861    /// command letter, right after a comma, or right at a `-` sign (which
1862    /// needs no comma before it); every other digit boundary has an
1863    /// explicit comma.
1864    fn decode_compact_d(d: &str) -> Vec<Vec<(f64, f64)>> {
1865        let b = d.as_bytes();
1866        let mut i = 0;
1867        let read_num = |b: &[u8], i: &mut usize| -> f64 {
1868            let start = *i;
1869            if *i < b.len() && b[*i] == b'-' {
1870                *i += 1;
1871            }
1872            while *i < b.len() && b[*i].is_ascii_digit() {
1873                *i += 1;
1874            }
1875            if *i < b.len() && b[*i] == b'.' {
1876                *i += 1;
1877                while *i < b.len() && b[*i].is_ascii_digit() {
1878                    *i += 1;
1879                }
1880            }
1881            std::str::from_utf8(&b[start..*i]).unwrap().parse().unwrap()
1882        };
1883        let is_num_start = |c: u8| c.is_ascii_digit() || c == b'-' || c == b'.';
1884        let mut rings = Vec::new();
1885        let mut current = Vec::new();
1886        let mut cursor = (0.0, 0.0);
1887        while i < b.len() {
1888            match b[i] {
1889                b'M' => {
1890                    if !current.is_empty() {
1891                        rings.push(std::mem::take(&mut current));
1892                    }
1893                    i += 1;
1894                    let x = read_num(b, &mut i);
1895                    if i < b.len() && b[i] == b',' {
1896                        i += 1;
1897                    }
1898                    let y = read_num(b, &mut i);
1899                    cursor = (x, y);
1900                    current.push(cursor);
1901                }
1902                b'l' => {
1903                    i += 1;
1904                    while i < b.len() && is_num_start(b[i]) {
1905                        let dx = read_num(b, &mut i);
1906                        if i < b.len() && b[i] == b',' {
1907                            i += 1;
1908                        }
1909                        let dy = read_num(b, &mut i);
1910                        if i < b.len() && b[i] == b',' {
1911                            i += 1;
1912                        }
1913                        cursor = (cursor.0 + dx, cursor.1 + dy);
1914                        current.push(cursor);
1915                    }
1916                }
1917                b'z' => {
1918                    i += 1;
1919                    if !current.is_empty() {
1920                        rings.push(std::mem::take(&mut current));
1921                    }
1922                }
1923                _ => i += 1,
1924            }
1925        }
1926        if !current.is_empty() {
1927            rings.push(current);
1928        }
1929        rings
1930    }
1931
1932    /// A noisy, roughly-circular ring with `n` points, the shape of a
1933    /// simplified real boundary (not axis-aligned, no repeating deltas).
1934    fn noisy_ring(n: usize, cx: f64, cy: f64, r: f64) -> Vec<(f64, f64)> {
1935        (0..n)
1936            .map(|i| {
1937                let t = i as f64 / n as f64 * std::f64::consts::TAU;
1938                let wobble = 1.0 + 0.03 * ((i * 7) as f64).sin();
1939                (cx + r * wobble * t.cos(), cy + r * wobble * t.sin())
1940            })
1941            .collect()
1942    }
1943
1944    #[test]
1945    fn compact_encoding_is_well_under_the_absolute_encodings_size() {
1946        // Isolated from simplification entirely: the same 2,000-point
1947        // ring, encoded both ways directly (no GeomSf/simplify_px
1948        // involved) — the byte-size win is the encoding alone.
1949        let ring = noisy_ring(2000, 400.0, 300.0, 280.0);
1950        let mut b = backend();
1951        b.draw_polygon_rings(std::slice::from_ref(&ring), &RectStyle::default())
1952            .unwrap();
1953        let absolute_svg = b.finish();
1954        let absolute_d = absolute_svg
1955            .split("d=\"")
1956            .nth(1)
1957            .and_then(|s| s.split('"').next())
1958            .expect("absolute path d");
1959
1960        let compact_d = compact_rings_d(std::slice::from_ref(&ring), 1);
1961
1962        assert!(
1963            compact_d.len() * 3 < absolute_d.len() * 2,
1964            "compact {} bytes should be well under 2/3 of absolute {} bytes",
1965            compact_d.len(),
1966            absolute_d.len()
1967        );
1968    }
1969
1970    #[test]
1971    fn compact_encoding_round_trips_within_precision() {
1972        let ring = noisy_ring(40, 100.0, 80.0, 60.0);
1973        let precision: u8 = 1;
1974        let d = compact_rings_d(std::slice::from_ref(&ring), precision);
1975        let decoded = decode_compact_d(&d);
1976        assert_eq!(decoded.len(), 1, "{d}");
1977        assert_eq!(decoded[0].len(), ring.len(), "{d}");
1978        // Each step's rounding error is at most half a unit of precision;
1979        // reconstructed points accumulate it additively from M, so the
1980        // tolerance at point k scales with k (still a tight, provable
1981        // bound, not an arbitrary constant).
1982        let step_tol = 0.5 * 10f64.powi(-(precision as i32));
1983        for (k, (orig, got)) in ring.iter().zip(decoded[0].iter()).enumerate() {
1984            let tol = step_tol * (k as f64 + 1.0) + 1e-9;
1985            assert!(
1986                (orig.0 - got.0).abs() <= tol && (orig.1 - got.1).abs() <= tol,
1987                "point {k}: {orig:?} vs {got:?} (tol {tol}): {d}"
1988            );
1989        }
1990    }
1991
1992    #[test]
1993    fn compact_encoding_round_trips_a_hole_and_a_multipolygons_parts() {
1994        // Two parts (first a plain square, second a square with a hole) —
1995        // exactly the ring-set shape `GeomSf::draw_rings` now passes
1996        // through for a MultiPolygon row (#213) or a Polygon with holes.
1997        let part1 = vec![(0.0, 0.0), (20.0, 0.0), (20.0, 20.0), (0.0, 20.0)];
1998        let part2_outer = vec![(50.0, 0.0), (90.0, 0.0), (90.0, 40.0), (50.0, 40.0)];
1999        let part2_hole = vec![(60.0, 10.0), (80.0, 10.0), (80.0, 30.0), (60.0, 30.0)];
2000        let rings = vec![part1.clone(), part2_outer.clone(), part2_hole.clone()];
2001        let d = compact_rings_d(&rings, 1);
2002        let decoded = decode_compact_d(&d);
2003        assert_eq!(decoded.len(), 3, "{d}");
2004        for (orig, got) in [part1, part2_outer, part2_hole].iter().zip(decoded.iter()) {
2005            assert_eq!(orig.len(), got.len(), "{d}");
2006            for (o, g) in orig.iter().zip(got.iter()) {
2007                assert!((o.0 - g.0).abs() <= 0.1 && (o.1 - g.1).abs() <= 0.1, "{d}");
2008            }
2009        }
2010        assert!(d.contains("M0,0l20,0,0,20-20,0z"), "{d}");
2011    }
2012}