lichtung-plot 0.3.0

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

#![deny(missing_docs)]

#[cfg(test)]
use crate::theme::color_from_rgb;
use crate::Error;
use crate::{style::LinePattern, Color};

use super::primitives::{
    CanonicalFillStyle, CanonicalGradientFill, CanonicalLineStyle, CanonicalPlannedBracket,
    CanonicalPlannedText, CanonicalScatter,
};
use crate::layout::{
    DeferredAnnotationTextPlan, PhysicalTextAlignment, PixelRect, ResolvedTextPlan,
};
use crate::render::coordinates::{
    self, CanonicalBracket, CanonicalGradient, CanonicalPoint, CanonicalRect, CanonicalRoundedRect,
    CanonicalText,
};
use crate::style::PointShape;

/// A 2D point in math-style canvas coordinates after data projection.
///
/// X increases rightward and Y increases upward from the canvas bottom. A
/// backend converts this canonical canvas space to its native coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point2D {
    /// X coordinate.
    pub x: f64,
    /// Y coordinate.
    pub y: f64,
}

impl Point2D {
    /// Construct a point.
    #[must_use]
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

/// Visual style for a scatter point.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PointStyle {
    /// Complete point fill paint.
    pub(crate) color: Color,
    /// Radius in pixels.
    pub(crate) radius: u32,
    /// Optional outline stroke.
    pub(crate) outline: Option<PointOutline>,
    /// Marker geometry.
    pub(crate) shape: PointShape,
}

impl PointStyle {
    /// Construct a backend fixture with the default publication point outline.
    #[cfg(test)]
    #[must_use]
    pub(crate) fn new(color: (u8, u8, u8), radius: u32) -> Self {
        Self {
            color: color_from_rgb(color).with_alpha(209.0 / 255.0),
            radius,
            outline: Some(PointOutline::publication().unwrap()),
            shape: PointShape::Circle,
        }
    }

    /// Set the marker geometry.
    #[must_use]
    pub const fn with_shape(mut self, shape: PointShape) -> Self {
        self.shape = shape;
        self
    }
}

/// Outline stroke for scatter-like point marks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PointOutline {
    /// Independent complete outline paint.
    pub(crate) color: Color,
    /// Stroke width as a proportion of the point radius.
    pub(crate) width_ratio: crate::theme::RelativeSpacing,
}

impl PointOutline {
    /// Construct the validated publication outline.
    #[cfg(test)]
    pub(crate) fn publication() -> Result<Self, Error> {
        Ok(crate::theme::PointOutlineTheme::publication()?.backend_style())
    }

    /// Resolve the outline stroke width for the given point radius.
    pub(crate) fn width_for_radius(
        &self,
        radius: u32,
    ) -> Result<coordinates::CanonicalCoordinate, Error> {
        let width = self
            .width_ratio
            .product(crate::theme::LocalMetric::css_pixels(f64::from(radius))?)
            .round(crate::layout::kernel::SpatialRounding::NearestEven)?
            .quanta();
        if width > (1_i64 << 53) - 1 || (width == 0 && !self.width_ratio.is_zero()) {
            return Err(Error::Render("point outline width exceeds canonical coordinates or its positive extent collapses".into()));
        }
        Ok(coordinates::CanonicalCoordinate::from_quanta(width))
    }
}

/// Visual style for a line (used by regression fits, axis lines).
#[derive(Debug, Clone, PartialEq)]
pub struct LineStyle {
    /// Complete stroke paint.
    pub(crate) color: Color,
    /// Stroke width in pixels (supports sub-pixel, e.g. 0.8).
    pub width: f32,
    /// Closed stroke pattern.
    pub pattern: LinePattern,
}

impl LineStyle {
    /// Create a solid line style.
    #[must_use]
    #[cfg(test)]
    pub(crate) fn solid(color: Color, width: f32) -> Self {
        Self {
            color,
            width,
            pattern: LinePattern::Solid,
        }
    }
}

/// Independent complete fill and stroke paints for a polygon or rectangle.
#[derive(Debug, Clone)]
pub(crate) struct FillStyle {
    pub(crate) color: Color,
    pub(crate) stroke: Color,
    /// Stroke width in pixels (0.0 for no outline).
    pub(crate) stroke_width: f32,
}

impl FillStyle {
    pub(crate) const fn new(color: Color, stroke: Color, stroke_width: f32) -> Self {
        // Width zero is the operational encoding of structural absence. Its
        // unused paint cannot become guide/cache/feedback identity.
        if stroke_width == 0.0 {
            return Self {
                color,
                stroke: Color::TRANSPARENT,
                stroke_width: 0.0,
            };
        }
        Self {
            color,
            stroke,
            stroke_width,
        }
    }
}

/// One color in a linear gradient at a normalized offset.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GradientStop {
    /// Position along the gradient vector, in the inclusive range `[0, 1]`.
    pub offset: f64,
    /// Color at this position (R, G, B).
    pub color: (u8, u8, u8),
}

impl GradientStop {
    /// Construct a gradient stop.
    #[must_use]
    pub const fn new(offset: f64, color: (u8, u8, u8)) -> Self {
        Self { offset, color }
    }
}

/// Text anchor alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextAnchor {
    /// Left-aligned (SVG default).
    #[default]
    Start,
    /// Center-aligned.
    Middle,
    /// Right-aligned.
    End,
}

/// Visual style for a text label.
#[derive(Debug, Clone, PartialEq)]
pub struct TextStyle {
    /// Complete text paint.
    pub(crate) color: Color,
    /// Font family.
    pub(crate) font_family: crate::font::canonical::FontStack,
    /// Font size in actual CSS pixels.
    pub font_size: f64,
    /// Closed font-weight policy.
    pub weight: crate::text::TextWeight,
    /// Horizontal alignment anchor. Default: `Start`.
    pub anchor: TextAnchor,
    /// Rotation angle in degrees (clockwise). Default: `0.0`.
    pub rotation: f64,
}

/// A significance bracket drawn inside the plot panel: a horizontal
/// line spanning two x-positions, with a vertical tick at each end
/// and a glyph (e.g. "***") above the midpoint.
#[derive(Debug, Clone)]
pub struct SignificanceBracket {
    /// X position of the left tick.
    pub x_left: f64,
    /// X position of the right tick.
    pub x_right: f64,
    /// Y position of the bracket.
    pub y: f64,
    /// Glyph string ("ns", "*", "**", "***", "****").
    pub label: String,
    /// Whether the span runs along x above marks or along y to their right.
    pub orientation: BracketOrientation,
}

/// Semantic role of a named backend layer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LayerRole {
    /// Ordinary layer emitted in call order.
    #[default]
    Generic,
    /// Grid layer that must render behind data marks.
    Grid,
}

/// Ownership role for an ordered painted surface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SurfaceRole {
    /// Canvas-owned root surface.
    CanvasRoot,
    /// Plot-owned panel surface.
    PlotPanel,
}

/// Orientation of a pairwise significance bracket.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BracketOrientation {
    /// Span two x anchors at a fixed y position.
    Vertical,
    /// Span two y anchors at a fixed x position. In this orientation,
    /// `x_left`/`x_right` store bottom/top y anchors and `y` stores x.
    Horizontal,
}

pub(crate) const POLYGON_MITER_LIMIT: f64 = 4.0;

/// Visual style for a significance bracket.
#[derive(Debug, Clone)]
pub struct BracketStyle {
    /// Complete bracket-path stroke paint.
    pub(crate) color: Color,
    /// Complete bracket-label text paint.
    pub(crate) label_color: Color,
    /// Stroke width in pixels.
    pub width: f32,
    /// Label font family.
    pub(crate) font_family: crate::font::canonical::FontStack,
    /// Label font size in pixels.
    pub font_size: f64,
    /// Label line height in pixels.
    pub(crate) line_height: f64,
    /// Label font weight.
    pub weight: crate::text::TextWeight,
    /// Gap between the bracket line and its label.
    pub label_gap: f64,
    /// Retained selected tick height; canonical validation has no default.
    pub(crate) tick_height: f64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PlannedTextOrigin {
    Middle,
    Baseline,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PlannedTextPlacement {
    pub(crate) position: Point2D,
    pub(crate) anchor: TextAnchor,
    pub(crate) rotation: f64,
    pub(crate) origin: PlannedTextOrigin,
    /// Physical column order for existing outward-stacked rotated axis titles.
    pub(crate) reverse_lines: bool,
}

#[derive(Debug, Clone)]
pub(crate) struct PlacedText {
    pub(crate) plan: ResolvedTextPlan,
    pub(crate) placement: PlannedTextPlacement,
}

pub(crate) fn reserve_text_records<T>(
    records: &mut Vec<T>,
    additional: usize,
) -> Result<(), Error> {
    if additional > records.capacity() - records.len() {
        let capacity = records
            .len()
            .checked_add(additional)
            .zip(records.capacity().checked_mul(2))
            .map(|(required, doubled)| required.max(doubled).max(4))
            .ok_or_else(|| Error::Render("producer text capacity overflow".into()))?;

        crate::operation::check()?;
        records
            .try_reserve_exact(capacity - records.len())
            .map_err(|_| Error::Render("producer text allocation failed".into()))?;
    }
    Ok(())
}

impl PlacedText {
    pub(crate) fn middle(
        plan: ResolvedTextPlan,
        position: Point2D,
        anchor: TextAnchor,
        rotation: f64,
    ) -> Result<Self, Error> {
        Ok(Self {
            placement: crate::layout::text::placement::resolve(
                PlannedTextPlacement::middle(position, anchor, rotation),
                &plan,
                None,
            )?,
            plan,
        })
    }
    pub(crate) fn draw(&self, backend: &mut dyn Backend) -> Result<(), Error> {
        backend.draw_planned_text(self.placement, &self.plan)
    }

    pub(crate) fn with_reversed_lines(mut self) -> Self {
        self.placement.reverse_lines = true;
        self
    }
}

impl PlannedTextPlacement {
    pub(crate) fn line_y(self, plan: &ResolvedTextPlan, y: i64) -> Result<i64, Error> {
        if !self.reverse_lines {
            return Ok(y);
        }
        plan.last_baseline_quanta
            .unwrap_or(0)
            .checked_sub(y)
            .and_then(|offset| plan.first_baseline_quanta.unwrap_or(0).checked_add(offset))
            .ok_or_else(|| Error::Render("reversed text line placement overflowed".into()))
    }
    pub(crate) const fn middle(position: Point2D, anchor: TextAnchor, rotation: f64) -> Self {
        Self {
            position,
            anchor,
            rotation,
            origin: PlannedTextOrigin::Middle,
            reverse_lines: false,
        }
    }

    pub(crate) const fn baseline(position: Point2D, anchor: TextAnchor, rotation: f64) -> Self {
        Self {
            position,
            anchor,
            rotation,
            origin: PlannedTextOrigin::Baseline,
            reverse_lines: false,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct PlannedSignificanceCall<'a> {
    pub(crate) bracket: &'a SignificanceBracket,
    pub(crate) style: &'a BracketStyle,
    pub(crate) label: &'a DeferredAnnotationTextPlan,
}

/// The `Backend` trait. Every concrete renderer (Plotters,
/// future Skia, future Vello) implements this.
///
/// All draw methods take a mutable `&mut self` and return
/// `Result<(), Error>`. Backends may buffer internally and
/// only emit output on `end()` (or `finalize()`).
///
/// Geometry passed to raw draw methods is already projected into math-style
/// canvas coordinates: X increases rightward and Y increases upward. Data
/// scaling and panel placement are renderer responsibilities, not backend
/// responsibilities. Canonical methods consume retained top-down operands for
/// the selected canvas and must not delegate back through raw draw methods.
pub trait Backend {
    /// Draw a fixed image or frozen scene in its admitted top-down frame.
    fn draw_fixed_content(
        &mut self,
        _content: &crate::composition::PlacedContent,
    ) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support fixed image content".into(),
        ))
    }
    /// Begin a semantic batch in the actual native capture.
    #[doc(hidden)]
    fn begin_paint_batch(
        &mut self,
        _reference: &crate::layout::PaintBatchRef,
    ) -> Result<(), Error> {
        Ok(())
    }

    /// Finish the semantic batch after its drawing scopes have closed.
    #[doc(hidden)]
    fn end_paint_batch(&mut self) -> Result<(), Error> {
        Ok(())
    }
    /// Draw retained line, run and glyph placement without layout or conversion.
    fn draw_canonical_text(&mut self, _text: &CanonicalPlannedText) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical text".into(),
        ))
    }

    /// Draw a retained bracket and its canonical label placement.
    fn draw_canonical_bracket(&mut self, _bracket: &CanonicalPlannedBracket) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical bracket".into(),
        ))
    }

    /// Draw retained top-down markers without raw geometry conversion.
    fn draw_canonical_scatter(&mut self, _scatter: &CanonicalScatter) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical scatter".into(),
        ))
    }

    /// Draw retained top-down line points and stroke operands.
    fn draw_canonical_line(
        &mut self,
        _points: &[CanonicalPoint],
        _style: &CanonicalLineStyle,
    ) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical lines".into(),
        ))
    }

    /// Draw retained top-down polygon points and paint operands.
    fn draw_canonical_polygon(
        &mut self,
        _points: &[CanonicalPoint],
        _style: &CanonicalFillStyle,
    ) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical polygons".into(),
        ))
    }

    /// Draw retained top-down gradient geometry and normalized stops.
    fn draw_canonical_gradient(&mut self, _gradient: &CanonicalGradientFill) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical gradients".into(),
        ))
    }

    /// Begin a clip from retained, checked top-down spatial operands.
    fn begin_canonical_clip(&mut self, _region: coordinates::CanonicalBox) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical clips".into(),
        ))
    }

    /// Paint a surface from retained, checked top-down spatial operands.
    fn draw_canonical_surface(
        &mut self,
        _bounds: coordinates::CanonicalBox,
        _paint: Color,
        _role: SurfaceRole,
    ) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical surfaces".into(),
        ))
    }

    /// Draw a rectangle from retained top-down geometry, stroke, and paint.
    fn draw_canonical_rounded_rect(
        &mut self,
        _rounded: coordinates::CanonicalRoundedBox,
    ) -> Result<(), Error> {
        Err(Error::Render(
            "backend does not support canonical rectangles".into(),
        ))
    }

    /// Begin a chart of the given pixel size. Must be called before
    /// any draw method.
    fn begin(&mut self, width: f64, height: f64) -> Result<(), Error>;

    /// Begin a named SVG layer (`<g id="...">`). Draw calls between
    /// `begin_layer` and `end_layer` are grouped. Layers control
    /// z-order: earlier layers render behind later ones.
    fn begin_layer(&mut self, _name: &str) -> Result<(), Error> {
        Ok(())
    }

    /// Begin a named layer with an explicit semantic role.
    ///
    /// Backends that do not reorder layers may use the default delegation to
    /// [`Backend::begin_layer`].
    fn begin_layer_with_role(&mut self, name: &str, _role: LayerRole) -> Result<(), Error> {
        self.begin_layer(name)
    }

    /// End the current SVG layer.
    fn end_layer(&mut self) -> Result<(), Error> {
        Ok(())
    }

    /// Begin clipping subsequent draw calls to a math-style canvas rectangle.
    /// Clip scopes may nest with layers and must be closed in last-in-first-out
    /// order with [`Backend::end_clip`].
    fn begin_clip(&mut self, region: PixelRect) -> Result<(), Error>;

    /// End the current clip scope.
    fn end_clip(&mut self) -> Result<(), Error>;

    /// Paint a validated math-style rectangular surface in semantic surface
    /// order rather than ordinary draw-call order.
    fn draw_surface(
        &mut self,
        bounds: PixelRect,
        paint: Color,
        role: SurfaceRole,
    ) -> Result<(), Error>;

    /// Draw a scatter cloud.
    fn draw_scatter(&mut self, points: &[Point2D], style: &PointStyle) -> Result<(), Error>;

    /// Draw a line strip (e.g. a regression fit).
    fn draw_line(&mut self, points: &[Point2D], style: &LineStyle) -> Result<(), Error>;

    /// Draw a filled polygon (bar body, violin body).
    fn draw_polygon(&mut self, points: &[Point2D], style: &FillStyle) -> Result<(), Error>;

    /// Fill a rectangle with a native linear gradient.
    ///
    /// `bounds` and the gradient vector use math-style canvas coordinates.
    /// Stops must begin at 0, end at 1, and have strictly increasing offsets.
    fn draw_linear_gradient(
        &mut self,
        bounds: PixelRect,
        start: Point2D,
        end: Point2D,
        stops: &[GradientStop],
    ) -> Result<(), Error>;

    /// Draw a text label at finite signed math-style canvas coordinates with
    /// alignment and optional rotation.
    fn draw_text(&mut self, position: Point2D, text: &str, style: &TextStyle) -> Result<(), Error>;

    /// Draw one immutable operation-owned text plan.
    #[doc(hidden)]
    #[allow(
        private_interfaces,
        reason = "immutable text plans are a crate-private renderer transition"
    )]
    fn draw_planned_text(
        &mut self,
        placement: PlannedTextPlacement,
        plan: &ResolvedTextPlan,
    ) -> Result<(), Error> {
        self.draw_text(
            placement.position,
            &plan.source,
            &planned_text_style(placement, plan)?,
        )
    }

    /// Draw a rounded rectangle. Coordinates are in math-coordinate space
    /// (y increases upward). `x` and `y` are the bottom-left corner,
    /// `w` and `h` are width and height, `r` is the corner radius.
    ///
    fn draw_rounded_rect(
        &mut self,
        x: f64,
        y: f64,
        w: f64,
        h: f64,
        r: f64,
        style: &FillStyle,
    ) -> Result<(), Error>;

    /// Draw a significance bracket.
    fn draw_bracket(
        &mut self,
        bracket: &SignificanceBracket,
        style: &BracketStyle,
    ) -> Result<(), Error>;

    /// Draw one significance bracket with its immutable planned label.
    #[doc(hidden)]
    #[allow(
        private_interfaces,
        reason = "immutable significance plans are a crate-private renderer transition"
    )]
    fn draw_planned_bracket(&mut self, call: PlannedSignificanceCall<'_>) -> Result<(), Error> {
        self.draw_bracket(call.bracket, call.style)
    }

    /// End the chart. Flushes any buffered output. After `end()`,
    /// no more draw methods may be called until the next `begin()`.
    fn end(&mut self) -> Result<(), Error>;
}

pub(crate) fn validate_clip_region(region: PixelRect) -> Result<CanonicalRect, Error> {
    coordinates::canonical_clip(region)
}

pub(crate) fn validate_surface_bounds(bounds: PixelRect) -> Result<CanonicalRect, Error> {
    coordinates::canonical_clip(bounds)
}

pub(crate) fn validate_points(
    points: &[Point2D],
    context: &str,
) -> Result<Vec<CanonicalPoint>, Error> {
    coordinates::canonical_points(points, context)
}

pub(crate) fn points_share_canonical_position(
    first: Point2D,
    second: Point2D,
    context: &str,
) -> Result<bool, Error> {
    let points = coordinates::canonical_points(&[first, second], context)?;
    Ok(points[0].same_position(points[1]))
}

pub(crate) fn validate_line_points(points: &[Point2D]) -> Result<Vec<CanonicalPoint>, Error> {
    coordinates::canonical_line_points(points)
}

pub(crate) fn validate_polygon_points(points: &[Point2D]) -> Result<Vec<CanonicalPoint>, Error> {
    coordinates::canonical_polygon_points(points)
}

pub(crate) fn validate_triangle_points(
    points: &[Point2D],
    radius: u32,
) -> Result<Vec<[CanonicalPoint; 3]>, Error> {
    coordinates::canonical_triangles(points, radius)
}

pub(crate) fn validate_point_style(style: &PointStyle) -> Result<(), Error> {
    if style.outline.is_some() && style.radius == 0 {
        return Err(Error::Render(
            "outlined point radius must be positive".into(),
        ));
    }
    Ok(())
}

pub(crate) fn validate_line_style(style: &LineStyle) -> Result<(), Error> {
    if style.width.is_finite() && style.width >= 0.0 {
        Ok(())
    } else {
        Err(Error::Render(
            format!(
                "line width must be finite and non-negative; got {}",
                style.width
            )
            .into(),
        ))
    }
}

pub(crate) fn validate_fill_style(style: &FillStyle) -> Result<(), Error> {
    if !style.stroke_width.is_finite() || style.stroke_width < 0.0 {
        return Err(Error::Render(
            format!(
                "fill stroke width must be finite and non-negative; got {}",
                style.stroke_width
            )
            .into(),
        ));
    }
    Ok(())
}

pub(crate) fn validate_linear_gradient(
    bounds: PixelRect,
    start: Point2D,
    end: Point2D,
    stops: &[GradientStop],
) -> Result<CanonicalGradient, Error> {
    coordinates::canonical_gradient(bounds, start, end, stops)
}

pub(crate) fn validate_text_style(style: &TextStyle) -> Result<(), Error> {
    if style.rotation.is_finite() {
        Ok(())
    } else {
        Err(Error::Render(
            format!("text rotation must be finite; got {}", style.rotation).into(),
        ))
    }
}

pub(crate) fn validate_text(position: Point2D, style: &TextStyle) -> Result<CanonicalText, Error> {
    validate_text_style(style)?;
    coordinates::canonical_text(
        position,
        style.rotation,
        coordinates::CoordinatePrecision::CssQuantum,
    )
}

pub(crate) fn planned_text_style(
    placement: PlannedTextPlacement,
    plan: &ResolvedTextPlan,
) -> Result<TextStyle, Error> {
    Ok(TextStyle {
        color: plan.style.paint,
        font_family: plan
            .copy_stack()
            .map_err(|error| Error::Render(error.to_string().into()))?,
        font_size: plan.style.font_size,
        weight: plan.style.weight,
        anchor: placement.anchor,
        rotation: placement.rotation,
    })
}

pub(crate) const fn nearest_even_text_half(value: i64) -> Option<i64> {
    if value < 0 {
        return None;
    }
    let quotient = value / 2;
    if value % 2 == 0 || quotient % 2 == 0 {
        Some(quotient)
    } else {
        quotient.checked_add(1)
    }
}

pub(crate) fn validate_planned_text(
    placement: PlannedTextPlacement,
    plan: &ResolvedTextPlan,
) -> Result<CanonicalText, Error> {
    let canonical = coordinates::canonical_text(
        placement.position,
        placement.rotation,
        coordinates::CoordinatePrecision::CssQuantum,
    )?;
    validate_text_plan(placement, plan)?;
    Ok(canonical)
}

#[expect(
    clippy::too_many_lines,
    reason = "one protocol validator checks linked immutable style, line, discretionary-break, and glyph spans"
)]
pub(crate) fn validate_text_plan(
    placement: PlannedTextPlacement,
    plan: &ResolvedTextPlan,
) -> Result<(), Error> {
    let expected_alignment = match placement.anchor {
        TextAnchor::Start => PhysicalTextAlignment::Start,
        TextAnchor::Middle => PhysicalTextAlignment::Center,
        TextAnchor::End => PhysicalTextAlignment::End,
    };
    if plan.style.alignment != expected_alignment {
        return Err(Error::Render(
            "planned text alignment does not match its backend placement".into(),
        ));
    }
    if !plan.style.font_size.is_finite()
        || plan.style.font_size <= 0.0
        || plan.style.stack_source.is_empty()
        || plan.style.stack.is_empty()
        || plan.style.line_height_quanta <= 0
        || plan.line_pitch_quanta <= 0
        || plan.width_quanta < 0
        || plan.height_quanta < 0
    {
        return Err(Error::Render(
            "planned text has invalid resolved style or dimensions".into(),
        ));
    }
    let valid_span = |start: u32, end: u32| {
        let start = start as usize;
        let end = end as usize;
        start <= end
            && end <= plan.source.len()
            && plan.source.is_char_boundary(start)
            && plan.source.is_char_boundary(end)
    };
    let valid_ink = |left: i64, bottom: i64, right: i64, top: i64| left <= right && bottom <= top;
    match (plan.lines.first(), plan.lines.last()) {
        (Some(first), Some(last)) => {
            if plan.first_baseline_quanta != Some(first.translation_y_quanta)
                || plan.last_baseline_quanta != Some(last.translation_y_quanta)
                || plan.height_quanta == 0
            {
                return Err(Error::Render(
                    "planned text baselines do not match its stored lines".into(),
                ));
            }
        }
        (None, None) => {
            if plan.first_baseline_quanta.is_some()
                || plan.last_baseline_quanta.is_some()
                || plan.width_quanta != 0
                || plan.height_quanta != 0
            {
                return Err(Error::Render(
                    "empty planned text has nonempty metrics".into(),
                ));
            }
        }
        _ => unreachable!("a slice cannot have only one of first/last"),
    }
    for (index, line) in plan.lines.iter().enumerate() {
        placement.line_y(plan, line.translation_y_quanta)?;
        if let Some(soft) = line.selected_soft_hyphen {
            if !valid_span(soft.start_byte, soft.end_byte)
                || soft.start_byte < line.source_span.start_byte
                || soft.end_byte != line.source_span.end_byte
                || &plan.source[soft.start_byte as usize..soft.end_byte as usize] != "\u{ad}"
                || !line.runs.iter().any(|run| {
                    run.source_span.start_byte <= soft.start_byte
                        && run.source_span.end_byte >= soft.end_byte
                })
            {
                return Err(Error::Render(
                    "planned text has an invalid selected soft-hyphen span or run".into(),
                ));
            }
        }
        let expected_y = plan
            .first_baseline_quanta
            .and_then(|first| {
                i64::try_from(index)
                    .ok()
                    .and_then(|index| plan.line_pitch_quanta.checked_mul(index))
                    .and_then(|offset| first.checked_add(offset))
            })
            .ok_or_else(|| Error::Render("planned text line offset overflow".into()))?;
        if line.translation_y_quanta != expected_y
            || !valid_span(line.source_span.start_byte, line.source_span.end_byte)
            || line
                .ink_quanta
                .is_some_and(|ink| !valid_ink(ink.left, ink.bottom, ink.right, ink.top))
        {
            return Err(Error::Render(
                "planned text has an invalid line span, translation, or ink box".into(),
            ));
        }
        for run in &line.runs {
            if !valid_span(run.source_span.start_byte, run.source_span.end_byte)
                || run.source_span.start_byte < line.source_span.start_byte
                || run.source_span.end_byte > line.source_span.end_byte
                || run.units_per_em == 0
                || run
                    .ink_quanta
                    .is_some_and(|ink| !valid_ink(ink.left, ink.bottom, ink.right, ink.top))
                || run.glyphs.iter().any(|glyph| {
                    glyph
                        .ink_quanta
                        .is_some_and(|ink| !valid_ink(ink.left, ink.bottom, ink.right, ink.top))
                })
            {
                return Err(Error::Render(
                    "planned text has an invalid shaped run or glyph ink box".into(),
                ));
            }
        }
    }
    Ok(())
}

pub(crate) fn validate_rounded_rect(
    x: f64,
    y: f64,
    width: f64,
    height: f64,
    radius: f64,
) -> Result<CanonicalRoundedRect, Error> {
    coordinates::canonical_rounded_rect(x, y, width, height, radius)
}

pub(crate) fn validate_bracket(
    bracket: &SignificanceBracket,
    style: &BracketStyle,
) -> Result<CanonicalBracket, Error> {
    if !style.width.is_finite() || style.width < 0.0 {
        return Err(Error::Render(
            format!(
                "bracket width must be finite and non-negative; got {}",
                style.width
            )
            .into(),
        ));
    }
    if !style.font_size.is_finite()
        || style.font_size <= 0.0
        || !style.line_height.is_finite()
        || style.line_height <= 0.0
        || !style.label_gap.is_finite()
        || style.label_gap < 0.0
        || !style.tick_height.is_finite()
        || style.tick_height < 0.0
    {
        return Err(Error::Render(
            "bracket style requires positive font size and line height and finite nonnegative gap and tick height"
                .into(),
        ));
    }
    coordinates::canonical_bracket(bracket, style.tick_height, style.label_gap)
}

pub(crate) fn planned_bracket_placement(
    canonical: CanonicalBracket,
    plan: &ResolvedTextPlan,
) -> Result<PlannedTextPlacement, Error> {
    let position = canonical.label_position.value();
    let default = match canonical.orientation {
        BracketOrientation::Vertical => {
            PlannedTextPlacement::baseline(position, TextAnchor::Middle, 0.0)
        }
        BracketOrientation::Horizontal => {
            PlannedTextPlacement::middle(position, TextAnchor::Start, 0.0)
        }
    };
    crate::layout::text::placement::resolve(default, plan, None)
}

pub(crate) fn validate_planned_bracket(
    call: PlannedSignificanceCall<'_>,
) -> Result<CanonicalBracket, Error> {
    let canonical = validate_bracket(call.bracket, call.style)?;
    validate_bracket_label(canonical, call)?;
    Ok(canonical)
}

pub(crate) fn validate_bracket_label(
    canonical: CanonicalBracket,
    call: PlannedSignificanceCall<'_>,
) -> Result<(), Error> {
    let plan = &call.label.plan;
    validate_text_plan(planned_bracket_placement(canonical, plan)?, plan)?;
    if call.label.exact_override != plan.style.exact_override
        || call.bracket.label != plan.original_source()
        || call.style.label_color != plan.style.paint
        || !plan.same_stack(&call.style.font_family)
        || call.style.font_size.to_bits() != plan.style.font_size.to_bits()
        || call.style.weight != plan.style.weight
    {
        return Err(Error::Render(
            "planned significance label does not match its frozen source and style".into(),
        ));
    }
    Ok(())
}

pub(crate) fn with_clip<T>(
    backend: &mut dyn Backend,
    region: PixelRect,
    draw: impl FnOnce(&mut dyn Backend) -> Result<T, Error>,
) -> Result<T, Error> {
    backend.begin_clip(region)?;
    finish_scope(draw(backend), backend.end_clip(), "clip scope")
}

pub(crate) fn with_paint<T>(
    backend: &mut dyn Backend,
    owner: &crate::layout::PanelPath,
    role: crate::layout::PaintRole,
    layer: Option<crate::elements::LayerSlot>,
    draw: impl FnOnce(&mut dyn Backend) -> Result<T, Error>,
) -> Result<T, Error> {
    backend.begin_paint_batch(&crate::layout::PaintBatchRef::new(owner, role, layer))?;
    finish_scope(
        draw(backend),
        backend.end_paint_batch(),
        "semantic paint batch",
    )
}

pub(crate) fn with_layer<T>(
    backend: &mut dyn Backend,
    name: &str,
    draw: impl FnOnce(&mut dyn Backend) -> Result<T, Error>,
) -> Result<T, Error> {
    backend.begin_layer(name)?;
    finish_scope(
        draw(backend),
        backend.end_layer(),
        &format!("layer {name:?}"),
    )
}

pub(crate) fn with_layer_role<T>(
    backend: &mut dyn Backend,
    name: &str,
    role: LayerRole,
    draw: impl FnOnce(&mut dyn Backend) -> Result<T, Error>,
) -> Result<T, Error> {
    backend.begin_layer_with_role(name, role)?;
    finish_scope(
        draw(backend),
        backend.end_layer(),
        &format!("layer {name:?}"),
    )
}

pub(crate) fn finish_scope<T>(
    primary: Result<T, Error>,
    cleanup: Result<(), Error>,
    scope: &str,
) -> Result<T, Error> {
    match (primary, cleanup) {
        (Ok(value), Ok(())) => Ok(value),
        (Err(primary), Ok(())) => Err(primary),
        (Ok(_), Err(cleanup)) => Err(Error::Render(
            format!("failed to close {scope}: {cleanup}").into(),
        )),
        (Err(primary), Err(cleanup)) => match primary {
            Error::Render(diagnostic) => Err(Error::Render(diagnostic.map_message(|message| {
                format!("{message}; additionally failed to close {scope}: {cleanup}")
            }))),
            Error::Data(diagnostic) => Err(Error::Data(diagnostic.map_message(|message| {
                format!("{message}; additionally failed to close {scope}: {cleanup}")
            }))),
            error => Err(error),
        },
    }
}