lichtung-plot 0.1.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
//! Admitted final primitive payloads. All positions are retained top-down quanta.

use super::backend::{
    self, FillStyle, GradientStop, LineStyle, Point2D, PointStyle, StrokePattern,
};
use super::backend::{
    PlannedSignificanceCall, PlannedTextOrigin, PlannedTextPlacement, TextAnchor,
};
use super::coordinates::{
    self, CanonicalBox, CanonicalCoordinate, CanonicalGradientStop, CanonicalPoint,
    CoordinatePrecision,
};
use crate::layout::ResolvedTextPlan;
use crate::layout::{kernel::FinalStage, PixelRect};
use crate::theme::Color;
use crate::{Error, PointShape};
use std::sync::Arc;

#[derive(Debug, PartialEq)]
pub(crate) struct CanonicalTextRun {
    pub(crate) origin: Option<CanonicalPoint>,
    pub(crate) glyphs: Vec<CanonicalPoint>,
}

#[derive(Debug, PartialEq)]
pub(crate) struct CanonicalTextLine {
    pub(crate) origin: CanonicalPoint,
    pub(crate) runs: Vec<CanonicalTextRun>,
    pub(crate) order: Vec<usize>,
    pub(crate) isolate_runs: bool,
}

#[derive(Debug, PartialEq)]
pub(crate) struct CanonicalTextPayload {
    pub(crate) text: crate::layout::text::FrozenText,
    pub(crate) center: CanonicalPoint,
    pub(crate) rotation: coordinates::CanonicalAngle,
    pub(crate) rotation_sin_cos: (f64, f64),
    pub(crate) lines: Vec<CanonicalTextLine>,
    pub(crate) clip: Option<CanonicalBox>,
    pub(crate) suppressed: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CanonicalPlannedText(pub(crate) Arc<CanonicalTextPayload>);

impl CanonicalPlannedText {
    pub(crate) fn new(
        placement: PlannedTextPlacement,
        plan: &ResolvedTextPlan,
        height: u32,
        stage: Option<&mut FinalStage<'_>>,
    ) -> Result<Self, Error> {
        let canonical = backend::validate_planned_text(placement, plan)?;
        Self::from_validated(canonical, placement, plan, height, stage)
    }

    #[expect(
        clippy::too_many_lines,
        reason = "one admitted pass freezes linked line, run, and glyph placement"
    )]
    pub(crate) fn from_validated(
        canonical: coordinates::CanonicalText,
        placement: PlannedTextPlacement,
        plan: &ResolvedTextPlan,
        height: u32,
        mut stage: Option<&mut FinalStage<'_>>,
    ) -> Result<Self, Error> {
        let half = |value| {
            backend::nearest_even_text_half(value)
                .ok_or_else(|| Error::Render("planned text extent cannot be negative".into()))
        };
        let overflow = || Error::Render("planned text absolute placement overflowed".into());
        let anchor = match placement.anchor {
            TextAnchor::Start => 0,
            TextAnchor::Middle => half(plan.width_quanta)?,
            TextAnchor::End => plan.width_quanta,
        };
        let left = canonical
            .position
            .x
            .units()
            .checked_sub(anchor)
            .ok_or_else(overflow)?;
        let reference = match placement.origin {
            PlannedTextOrigin::Middle => half(plan.height_quanta)?,
            PlannedTextOrigin::Baseline => plan.first_baseline_quanta.unwrap_or(0),
        };
        let mut lines = storage(
            plan.lines.len(),
            stage.as_deref_mut(),
            "prepared text lines",
        )?;
        for line in &plan.lines {
            let x = left
                .checked_add(line.translation_x_quanta)
                .ok_or_else(overflow)?;
            let baseline = canonical
                .position
                .y
                .units()
                .checked_add(reference)
                .and_then(|y| {
                    y.checked_sub(placement.line_y(plan, line.translation_y_quanta).ok()?)
                })
                .ok_or_else(overflow)?;
            let origin = CanonicalPoint {
                x: CanonicalCoordinate::from_quanta(x),
                y: CanonicalCoordinate::from_quanta(baseline),
            }
            .inverted(height)?;
            let mut runs = storage(line.runs.len(), stage.as_deref_mut(), "prepared text runs")?;
            let mut order = storage(
                line.runs.len(),
                stage.as_deref_mut(),
                "prepared text run order",
            )?;
            order.extend(0..line.runs.len());
            order.sort_unstable_by_key(|index| line.runs[*index].source_span.start_byte);
            for run in &line.runs {
                let run_origin = run
                    .glyphs
                    .first()
                    .map(|first| {
                        CanonicalPoint {
                            x: CanonicalCoordinate::from_quanta(
                                first
                                    .glyph_origin_x_quanta
                                    .checked_sub(first.x_offset_quanta)
                                    .and_then(|v| x.checked_add(v))
                                    .ok_or_else(overflow)?,
                            ),
                            y: CanonicalCoordinate::from_quanta(
                                first
                                    .glyph_origin_y_quanta
                                    .checked_sub(first.y_offset_quanta)
                                    .and_then(|v| baseline.checked_add(v))
                                    .ok_or_else(overflow)?,
                            ),
                        }
                        .inverted(height)
                    })
                    .transpose()?;
                let mut glyphs = storage(
                    run.glyphs.len(),
                    stage.as_deref_mut(),
                    "prepared text glyph origins",
                )?;
                for glyph in &run.glyphs {
                    glyphs.push(
                        CanonicalPoint {
                            x: CanonicalCoordinate::from_quanta(
                                x.checked_add(glyph.glyph_origin_x_quanta)
                                    .ok_or_else(overflow)?,
                            ),
                            y: CanonicalCoordinate::from_quanta(
                                baseline
                                    .checked_add(glyph.glyph_origin_y_quanta)
                                    .ok_or_else(overflow)?,
                            ),
                        }
                        .inverted(height)?,
                    );
                }
                runs.push(CanonicalTextRun {
                    origin: run_origin,
                    glyphs,
                });
            }
            lines.push(CanonicalTextLine {
                origin,
                runs,
                order,
                isolate_runs: line.selected_soft_hyphen.is_some()
                    || line.runs.len() > 1
                    || line
                        .runs
                        .iter()
                        .any(|run| run.bidi_level > 1 || run.direction != line.base_direction),
            });
        }

        let clip = crate::layout::text::placement::paint_clip(placement, plan)?;
        let suppressed = clip.is_some_and(|region| region.width() <= 0.0 || region.height() <= 0.0);
        let clip = clip
            .filter(|_| !suppressed)
            .map(|region| {
                coordinates::CanonicalBox::from_bottom_up(
                    backend::validate_clip_region(region)?,
                    height,
                )
            })
            .transpose()?;
        Ok(Self(Arc::new(CanonicalTextPayload {
            text: plan.freeze_selected(),
            center: canonical.position.inverted(height)?,
            rotation: canonical.rotation,
            rotation_sin_cos: canonical.rotation.value().to_radians().sin_cos(),
            lines,
            clip,
            suppressed,
        })))
    }
}

#[derive(Debug)]
pub(crate) struct CanonicalPlannedBracket {
    pub(crate) points: [CanonicalPoint; 4],
    pub(crate) orientation: backend::BracketOrientation,
    pub(crate) width: CanonicalCoordinate,
    pub(crate) color: Color,
    pub(crate) text: CanonicalPlannedText,
}

impl CanonicalPlannedBracket {
    pub(crate) fn new(
        call: PlannedSignificanceCall<'_>,
        height: u32,
        stage: Option<&mut FinalStage<'_>>,
    ) -> Result<Self, Error> {
        let canonical = backend::validate_bracket(call.bracket, call.style)?;
        Self::from_geometry(canonical, call, height, stage)
    }

    pub(crate) fn from_geometry(
        canonical: coordinates::CanonicalBracket,
        call: PlannedSignificanceCall<'_>,
        height: u32,
        stage: Option<&mut FinalStage<'_>>,
    ) -> Result<Self, Error> {
        backend::validate_bracket_label(canonical, call)?;
        let width = CanonicalCoordinate::nonnegative_length(
            f64::from(call.style.width),
            "bracket stroke width",
        )?;
        let points = [
            canonical.line_start.inverted(height)?,
            canonical.line_end.inverted(height)?,
            canonical.first_tick_end.inverted(height)?,
            canonical.second_tick_end.inverted(height)?,
        ];
        let text = CanonicalPlannedText::from_validated(
            coordinates::CanonicalText {
                position: canonical.label_position,
                rotation: coordinates::CanonicalAngle::ZERO,
            },
            backend::planned_bracket_placement(canonical, &call.label.plan)?,
            &call.label.plan,
            height,
            stage,
        )?;
        Ok(Self {
            points,
            orientation: canonical.orientation,
            width,
            color: call.style.color,
            text,
        })
    }
}

fn storage<T>(
    count: usize,
    stage: Option<&mut FinalStage<'_>>,
    payload: &str,
) -> Result<Vec<T>, Error> {
    let mut values = Vec::new();
    if let Some(stage) = stage {
        stage.reserve_capture(&mut values, count, payload)?;
    } else {
        values
            .try_reserve_exact(count)
            .map_err(|error| Error::Render(format!("{payload} reservation: {error}")))?;
    }
    Ok(values)
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct CanonicalLineStyle {
    pub(crate) color: Color,
    pub(crate) width: CanonicalCoordinate,
    pub(crate) pattern: StrokePattern,
}

impl CanonicalLineStyle {
    pub(crate) fn new(style: &LineStyle) -> Result<Self, Error> {
        backend::validate_line_style(style)?;
        Ok(Self {
            color: style.color,
            width: CanonicalCoordinate::nonnegative_length(
                f64::from(style.width),
                "line stroke width",
            )?,
            pattern: style.pattern,
        })
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct CanonicalFillStyle {
    pub(crate) color: Color,
    pub(crate) stroke: Color,
    pub(crate) stroke_width: CanonicalCoordinate,
}

impl CanonicalFillStyle {
    pub(crate) fn new(style: &FillStyle) -> Result<Self, Error> {
        backend::validate_fill_style(style)?;
        Ok(Self {
            color: style.color,
            stroke: style.stroke,
            stroke_width: CanonicalCoordinate::nonnegative_length(
                f64::from(style.stroke_width),
                "polygon stroke width",
            )?,
        })
    }
}

#[derive(Clone, Copy)]
pub(crate) enum PathKind {
    Line,
    Polygon,
}

pub(crate) fn path(
    raw: &[Point2D],
    kind: PathKind,
    height: u32,
    mut stage: Option<&mut FinalStage<'_>>,
) -> Result<Vec<CanonicalPoint>, Error> {
    let (name, payload) = match kind {
        PathKind::Line => ("line", "prepared line points"),
        PathKind::Polygon => ("polygon", "prepared polygon points"),
    };
    let mut points = storage(raw.len(), stage.as_deref_mut(), payload)?;
    let mut indices = match kind {
        PathKind::Line => storage(raw.len(), stage, "prepared line validation indices")?,
        PathKind::Polygon => Vec::new(),
    };
    for point in coordinates::canonical_points_iter(raw, name) {
        points.push(point?);
    }
    match kind {
        PathKind::Line => {
            indices.extend(0..raw.len());
            coordinates::validate_line_geometry(raw, &points, &mut indices)?;
        }
        PathKind::Polygon => coordinates::validate_polygon_geometry(raw, &points)?,
    }
    for point in &mut points {
        *point = point.inverted(height)?;
    }
    Ok(points)
}

#[derive(Debug)]
pub(crate) enum CanonicalMarkers {
    Circles {
        points: Vec<CanonicalPoint>,
        radius: CanonicalCoordinate,
    },
    Triangles(Vec<[CanonicalPoint; 3]>),
}

#[derive(Debug)]
pub(crate) struct CanonicalScatter {
    pub(crate) markers: CanonicalMarkers,
    pub(crate) color: Color,
    pub(crate) outline: Option<(Color, CanonicalCoordinate)>,
}

impl CanonicalScatter {
    pub(crate) fn new(
        raw: &[Point2D],
        style: &PointStyle,
        height: u32,

        stage: Option<&mut FinalStage<'_>>,
    ) -> Result<Self, Error> {
        let markers = match style.shape {
            PointShape::Circle => {
                let mut points = storage(raw.len(), stage, "prepared scatter points")?;
                for point in coordinates::canonical_points_iter(raw, "scatter") {
                    points.push(point?.inverted(height)?);
                }
                CanonicalMarkers::Circles {
                    points,
                    radius: CanonicalCoordinate::from_f64(
                        f64::from(style.radius),
                        CoordinatePrecision::CssQuantum,
                        "scatter radius",
                    )?,
                }
            }
            PointShape::TriangleUp => {
                let mut triangles = storage(raw.len(), stage, "prepared scatter triangles")?;
                for (index, point) in raw.iter().copied().enumerate() {
                    let mut triangle = coordinates::canonical_triangle(point, style.radius, index)?;
                    for vertex in &mut triangle {
                        *vertex = vertex.inverted(height)?;
                    }
                    triangles.push(triangle);
                }
                CanonicalMarkers::Triangles(triangles)
            }
        };
        backend::validate_point_style(style)?;
        let outline = style
            .outline
            .as_ref()
            .map(|outline| Ok::<_, Error>((outline.color, outline.width_for_radius(style.radius)?)))
            .transpose()?;
        Ok(Self {
            markers,
            color: style.color,
            outline,
        })
    }
}

#[derive(Debug)]
pub(crate) struct CanonicalGradientFill {
    pub(crate) bounds: CanonicalBox,
    pub(crate) start: CanonicalPoint,
    pub(crate) end: CanonicalPoint,
    pub(crate) stops: Vec<CanonicalGradientStop>,
}

impl CanonicalGradientFill {
    pub(crate) fn new(
        bounds: PixelRect,
        start: Point2D,
        end: Point2D,
        raw: &[GradientStop],
        height: u32,
        stage: Option<&mut FinalStage<'_>>,
    ) -> Result<Self, Error> {
        let mut stops = storage(raw.len(), stage, "prepared gradient stops")?;
        let (bounds, start, end) =
            coordinates::visit_canonical_gradient(bounds, start, end, raw, |stop| {
                stops.push(stop);
            })?;
        Ok(Self {
            bounds: CanonicalBox::from_bottom_up(bounds, height)?,
            start: start.inverted(height)?,
            end: end.inverted(height)?,
            stops,
        })
    }
}