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/// Where the caret sits inside an empty slot, which has no item to stand beside.
48#[derive(Debug, Clone, Copy, Default, PartialEq)]
49pub enum EmptySlotCaret {
50    /// At the placeholder's left edge.
51    #[default]
52    Start,
53    /// At the placeholder's horizontal center.
54    Center,
55    /// This many points in from the placeholder's left edge, never past its right edge.
56    Inset(f64),
57}
58
59/// Everything the host paints on top of the typeset fragment.
60#[derive(Debug, Clone, PartialEq)]
61pub struct RenderOutput {
62    /// The caret, a zero width rectangle.
63    pub caret: Rect,
64    /// Selection highlight rectangles.
65    pub selection: Vec<Rect>,
66    /// Empty slot placeholder rectangles.
67    pub placeholders: Vec<Rect>,
68    /// The fragment's size.
69    pub metrics: Metrics,
70    /// Where the open swap or delete menu anchors, see `Editor::menu` for its rows.
71    pub menu: Option<Rect>,
72    /// Placements of host boxes so the host can overlay their content.
73    pub host_objects: Vec<HostObject>,
74}
75
76/// A [`crate::Source`] exported at another revision or by another editor.
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub struct StaleSource {
79    /// Revision the source was exported at.
80    pub source: u64,
81    /// The editor's current revision.
82    pub editor: u64,
83}
84
85impl fmt::Display for StaleSource {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "source from revision {} does not match editor revision {}", self.source, self.editor)
88    }
89}
90
91impl std::error::Error for StaleSource {}