nightshade-text 0.57.0

Text shaping, styling, and glyph geometry shared by the nightshade renderer and UI.
Documentation
//! Generated text geometry owned by the render layer.

use nalgebra_glm::{Vec2, Vec3};

/// Triangulated glyph geometry for a laid-out string.
#[derive(Clone, Debug, Default)]
pub struct TextMesh {
    /// Glyph quad vertices.
    pub vertices: Vec<TextVertex>,
    /// Triangle indices into `vertices`.
    pub indices: Vec<u32>,
    /// Width and height of the laid-out text in local units.
    pub bounds: Vec2,
}

/// A single text glyph vertex in the layout for GPU upload.
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct TextVertex {
    /// Local-space position.
    pub position: Vec3,
    /// Atlas texture coordinates.
    pub tex_coords: Vec2,
    /// Index of the source character within the string.
    pub character_index: u32,
    /// Padding to a 16-byte stride.
    pub _padding: u32,
}

/// Font family a run of text is rendered with.
#[derive(
    Clone,
    Copy,
    Debug,
    Default,
    PartialEq,
    Eq,
    Hash,
    serde::Serialize,
    serde::Deserialize,
    enum2schema::Schema,
)]
#[schema(string_enum)]
pub enum FontKind {
    /// Default proportional font.
    #[default]
    Default,
    /// Monospaced font.
    Mono,
    /// Material Design icon glyphs.
    IconsMaterial,
    /// Lucide icon glyphs.
    IconsLucide,
}

/// Horizontal alignment of text within its bounds.
#[derive(
    Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize, enum2schema::Schema,
)]
#[schema(string_enum)]
pub enum TextAlignment {
    /// Align to the left edge.
    Left,
    /// Center horizontally.
    Center,
    /// Align to the right edge.
    Right,
}

/// Vertical alignment of text within its bounds.
#[derive(
    Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize, enum2schema::Schema,
)]
#[schema(string_enum)]
pub enum VerticalAlignment {
    /// Align to the top edge.
    Top,
    /// Center vertically.
    Middle,
    /// Align to the bottom edge.
    Bottom,
    /// Align the first line's baseline to the origin.
    Baseline,
}