mathtex-editor-core 0.3.0

Headless core of the mathtex structural math editor: model, operations, navigation, selection, IR matching
Documentation
//! Geometry handed to the host: points, y down, origin at the fragment surface's top left corner.

use std::fmt;

/// A point in points (scaled points divided by 65536), y grows down from the surface's top left.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
    /// Distance from the surface's left edge.
    pub x: f64,
    /// Distance from the surface's top edge.
    pub y: f64,
}

/// A rectangle in points, `(x, y)` is its top left corner with y growing down.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
    /// Left edge.
    pub x: f64,
    /// Top edge.
    pub y: f64,
    /// Width.
    pub width: f64,
    /// Height.
    pub height: f64,
}

/// Size of the whole typeset fragment in points.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Metrics {
    /// Surface width.
    pub width: f64,
    /// Surface height.
    pub height: f64,
    /// Distance from the surface's top edge down to the baseline.
    pub baseline: f64,
}

/// Placement of one host box in the rendered fragment.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HostObject {
    /// The host minted token identifying the object.
    pub token: u32,
    /// The box the object should cover.
    pub rect: Rect,
}

/// Everything the host paints on top of the typeset fragment.
#[derive(Debug, Clone, PartialEq)]
pub struct RenderOutput {
    /// The caret, a zero width rectangle.
    pub caret: Rect,
    /// Selection highlight rectangles.
    pub selection: Vec<Rect>,
    /// Empty slot placeholder rectangles.
    pub placeholders: Vec<Rect>,
    /// The fragment's size.
    pub metrics: Metrics,
    /// Where the open swap or delete menu anchors, see `Editor::menu` for its rows.
    pub menu: Option<Rect>,
    /// Placements of host boxes so the host can overlay their content.
    pub host_objects: Vec<HostObject>,
}

/// A [`crate::Source`] exported at another revision or by another editor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StaleSource {
    /// Revision the source was exported at.
    pub source: u64,
    /// The editor's current revision.
    pub editor: u64,
}

impl fmt::Display for StaleSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "source from revision {} does not match editor revision {}", self.source, self.editor)
    }
}

impl std::error::Error for StaleSource {}