Skip to main content

mathtex_editor_core/
geometry.rs

1//! Geometry handed to the host: points, y down, origin at the fragment surface's top left corner.
2
3use std::fmt;
4
5/// A point in points (scaled points divided by 65536), y grows down from the surface's top left.
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Point {
8    /// Distance from the surface's left edge.
9    pub x: f64,
10    /// Distance from the surface's top edge.
11    pub y: f64,
12}
13
14/// A rectangle in points, `(x, y)` is its top left corner with y growing down.
15#[derive(Debug, Clone, Copy, PartialEq)]
16pub struct Rect {
17    /// Left edge.
18    pub x: f64,
19    /// Top edge.
20    pub y: f64,
21    /// Width.
22    pub width: f64,
23    /// Height.
24    pub height: f64,
25}
26
27/// Size of the whole typeset fragment in points.
28#[derive(Debug, Clone, Copy, PartialEq)]
29pub struct Metrics {
30    /// Surface width.
31    pub width: f64,
32    /// Surface height.
33    pub height: f64,
34    /// Distance from the surface's top edge down to the baseline.
35    pub baseline: f64,
36}
37
38/// Placement of one host box in the rendered fragment.
39#[derive(Debug, Clone, Copy, PartialEq)]
40pub struct HostObject {
41    /// The host minted token identifying the object.
42    pub token: u32,
43    /// The box the object should cover.
44    pub rect: Rect,
45}
46
47/// Everything the host paints on top of the typeset fragment.
48#[derive(Debug, Clone, PartialEq)]
49pub struct RenderOutput {
50    /// The caret, a zero width rectangle.
51    pub caret: Rect,
52    /// Selection highlight rectangles.
53    pub selection: Vec<Rect>,
54    /// Empty slot placeholder rectangles.
55    pub placeholders: Vec<Rect>,
56    /// The fragment's size.
57    pub metrics: Metrics,
58    /// Where the open swap or delete menu anchors, see `Editor::menu` for its rows.
59    pub menu: Option<Rect>,
60    /// Placements of host boxes so the host can overlay their content.
61    pub host_objects: Vec<HostObject>,
62}
63
64/// A [`crate::Source`] exported at another revision or by another editor.
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct StaleSource {
67    /// Revision the source was exported at.
68    pub source: u64,
69    /// The editor's current revision.
70    pub editor: u64,
71}
72
73impl fmt::Display for StaleSource {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(f, "source from revision {} does not match editor revision {}", self.source, self.editor)
76    }
77}
78
79impl std::error::Error for StaleSource {}